From b5ccb1636fc0efb0ea9d87c38995fee14cfd45d3 Mon Sep 17 00:00:00 2001
From: AlphaWu <wp@zafu.edu.cn>
Date: 星期日, 20 八月 2023 15:50:24 +0800
Subject: [PATCH] 修正Database拼写错误。

---
 src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/KeepaliveNotifyMessageHandler.java |   95 ++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 76 insertions(+), 19 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/KeepaliveNotifyMessageHandler.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/KeepaliveNotifyMessageHandler.java
old mode 100644
new mode 100755
index 05e8bd7..7d94787
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/KeepaliveNotifyMessageHandler.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/KeepaliveNotifyMessageHandler.java
@@ -1,12 +1,19 @@
 package com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.notify.cmd;
 
 import com.genersoft.iot.vmp.common.VideoManagerConstants;
+import com.genersoft.iot.vmp.conf.DynamicTask;
+import com.genersoft.iot.vmp.conf.UserSetting;
 import com.genersoft.iot.vmp.gb28181.bean.Device;
 import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform;
-import com.genersoft.iot.vmp.gb28181.event.EventPublisher;
+import com.genersoft.iot.vmp.gb28181.bean.RemoteAddressInfo;
 import com.genersoft.iot.vmp.gb28181.transmit.event.request.SIPRequestProcessorParent;
 import com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.IMessageHandler;
 import com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.notify.NotifyMessageHandler;
+import com.genersoft.iot.vmp.gb28181.utils.SipUtils;
+import com.genersoft.iot.vmp.service.IDeviceService;
+import com.genersoft.iot.vmp.utils.DateUtil;
+import gov.nist.javax.sip.message.SIPRequest;
+import org.apache.commons.lang3.ObjectUtils;
 import org.dom4j.Element;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -20,17 +27,27 @@
 import javax.sip.message.Response;
 import java.text.ParseException;
 
+/**
+ * 鐘舵�佷俊鎭�(蹇冭烦)鎶ラ��
+ */
 @Component
 public class KeepaliveNotifyMessageHandler extends SIPRequestProcessorParent implements InitializingBean, IMessageHandler {
 
-    private Logger logger = LoggerFactory.getLogger(KeepaliveNotifyMessageHandler.class);
-    private final String cmdType = "Keepalive";
+
+    private final Logger logger = LoggerFactory.getLogger(KeepaliveNotifyMessageHandler.class);
+    private final static String cmdType = "Keepalive";
 
     @Autowired
     private NotifyMessageHandler notifyMessageHandler;
 
     @Autowired
-    private EventPublisher publisher;
+    private IDeviceService deviceService;
+
+    @Autowired
+    private UserSetting userSetting;
+
+    @Autowired
+    private DynamicTask dynamicTask;
 
     @Override
     public void afterPropertiesSet() throws Exception {
@@ -39,25 +56,65 @@
 
     @Override
     public void handForDevice(RequestEvent evt, Device device, Element element) {
-        // 妫�鏌ヨ澶囨槸鍚﹀瓨鍦ㄥ苟鍦ㄧ嚎锛� 涓嶅湪绾垮垯璁剧疆涓哄湪绾�
-        try {
-            if (device != null ) {
-                // 鍥炲200 OK
-                responseAck(evt, Response.OK);
-                publisher.onlineEventPublish(device, VideoManagerConstants.EVENT_ONLINE_KEEPLIVE);
-            }
-        } catch (SipException e) {
-            e.printStackTrace();
-        } catch (InvalidArgumentException e) {
-            e.printStackTrace();
-        } catch (ParseException e) {
-            e.printStackTrace();
+        if (device == null) {
+            // 鏈敞鍐岀殑璁惧涓嶅仛澶勭悊
+            return;
         }
+        SIPRequest request = (SIPRequest) evt.getRequest();
+        logger.info("[鏀跺埌蹇冭烦]锛� device: {}, callId: {}", device.getDeviceId(), request.getCallIdHeader().getCallId());
+
+        // 鍥炲200 OK
+        try {
+            responseAck(request, Response.OK);
+        } catch (SipException | InvalidArgumentException | ParseException e) {
+            logger.error("[鍛戒护鍙戦�佸け璐 蹇冭烦鍥炲: {}", e.getMessage());
+        }
+        if (!ObjectUtils.isEmpty(device.getKeepaliveTime()) && DateUtil.getDifferenceForNow(device.getKeepaliveTime()) <= 3000L) {
+            logger.info("[鏀跺埌蹇冭烦] 蹇冭烦鍙戦�佽繃浜庨绻侊紝宸插拷鐣� device: {}, callId: {}", device.getDeviceId(), request.getCallIdHeader().getCallId());
+            return;
+        }
+
+        RemoteAddressInfo remoteAddressInfo = SipUtils.getRemoteAddressFromRequest(request, userSetting.getSipUseSourceIpAsRemoteAddress());
+        if (!device.getIp().equalsIgnoreCase(remoteAddressInfo.getIp()) || device.getPort() != remoteAddressInfo.getPort()) {
+            logger.info("[蹇冭烦] 璁惧{}鍦板潃鍙樺寲, 杩滅▼鍦板潃涓�: {}:{}", device.getDeviceId(), remoteAddressInfo.getIp(), remoteAddressInfo.getPort());
+            device.setPort(remoteAddressInfo.getPort());
+            device.setHostAddress(remoteAddressInfo.getIp().concat(":").concat(String.valueOf(remoteAddressInfo.getPort())));
+            device.setIp(remoteAddressInfo.getIp());
+        }
+        if (device.getKeepaliveTime() == null) {
+            device.setKeepaliveIntervalTime(60);
+        }else {
+            long lastTime = DateUtil.yyyy_MM_dd_HH_mm_ssToTimestamp(device.getKeepaliveTime());
+            if (System.currentTimeMillis()/1000-lastTime > 10) {
+                device.setKeepaliveIntervalTime(Long.valueOf(System.currentTimeMillis()/1000-lastTime).intValue());
+            }
+        }
+
+        device.setKeepaliveTime(DateUtil.getNow());
+
+        if (device.isOnLine()) {
+            deviceService.updateDevice(device);
+        }else {
+            // 瀵逛簬宸茬粡绂荤嚎鐨勮澶囧垽鏂粬鐨勬敞鍐屾槸鍚﹀凡缁忚繃鏈�
+            if (!deviceService.expire(device)){
+                device.setOnLine(false);
+                deviceService.online(device, null);
+            }
+        }
+        // 鍒锋柊杩囨湡浠诲姟
+        String registerExpireTaskKey = VideoManagerConstants.REGISTER_EXPIRE_TASK_KEY_PREFIX + device.getDeviceId();
+        // 濡傛灉涓夋蹇冭烦澶辫触锛屽垯璁剧疆璁惧绂荤嚎
+        dynamicTask.startDelay(registerExpireTaskKey, ()-> deviceService.offline(device.getDeviceId(), "涓夋蹇冭烦澶辫触"), device.getKeepaliveIntervalTime()*1000*3);
+
     }
 
     @Override
     public void handForPlatform(RequestEvent evt, ParentPlatform parentPlatform, Element element) {
-        // 涓嶄細鏀跺埌涓婄骇骞冲彴鐨勫績璺充俊鎭�
-
+        // 涓埆骞冲彴淇濇椿涓嶅洖澶�200OK浼氬垽瀹氱绾�
+        try {
+            responseAck((SIPRequest) evt.getRequest(), Response.OK);
+        } catch (SipException | InvalidArgumentException | ParseException e) {
+            logger.error("[鍛戒护鍙戦�佸け璐 蹇冭烦鍥炲: {}", e.getMessage());
+        }
     }
 }

--
Gitblit v1.8.0