From a82b831b8b2b96dfa5791808f1207500e16697cf Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期三, 04 一月 2023 10:39:40 +0800
Subject: [PATCH] 优化设备状态保持,自动记录心跳间隔,三次心跳失败则设备离线,不在使用设备有效期字段作为唯一判断标准,提高容错能力和稳定性。

---
 src/main/java/com/genersoft/iot/vmp/storager/dao/DeviceMapper.java                                                            |    3 +
 src/main/java/com/genersoft/iot/vmp/gb28181/bean/Device.java                                                                  |   15 +++++++
 src/main/java/com/genersoft/iot/vmp/service/impl/DeviceServiceImpl.java                                                       |   19 +++++----
 sql/update.sql                                                                                                                |   44 +---------------------
 pom.xml                                                                                                                       |    2 
 src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/KeepaliveNotifyMessageHandler.java |   18 +++++++++
 src/main/java/com/genersoft/iot/vmp/common/VideoManagerConstants.java                                                         |    2 +
 7 files changed, 51 insertions(+), 52 deletions(-)

diff --git a/pom.xml b/pom.xml
index 4c26f33..c0c9900 100644
--- a/pom.xml
+++ b/pom.xml
@@ -11,7 +11,7 @@
 
 	<groupId>com.genersoft</groupId>
 	<artifactId>wvp-pro</artifactId>
-	<version>2.6.6</version>
+	<version>2.6.7</version>
 	<name>web video platform</name>
 	<description>鍥芥爣28181瑙嗛骞冲彴</description>
 
diff --git a/sql/update.sql b/sql/update.sql
index 877e247..2e5d569 100644
--- a/sql/update.sql
+++ b/sql/update.sql
@@ -1,43 +1,3 @@
-alter table media_server
-    drop column streamNoneReaderDelayMS;
-
-alter table media_server
-    drop column sendRtpPortRange;
-
-alter table stream_proxy
-    add enable_disable_none_reader bit(1) default null;
-
+-- 2.6.6->2.6.7
 alter table device
-    add mediaServerId varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT 'auto';
-
-alter table device
-    add custom_name varchar(255) default null;
-
-alter table device
-    add sdpIp varchar(50) default null;
-
-alter table device
-    add localIp varchar(50) default null;
-
-alter table device
-    add password varchar(255) default null;
-
-alter table device
-    modify ip varchar(50) null;
-
-alter table device
-    modify port int null;
-
-alter table device
-    modify expires int null;
-
-alter table device
-    modify subscribeCycleForCatalog int null;
-
-alter table device
-    modify hostAddress varchar(50) null;
-
-alter table stream_proxy
-    change enable_hls enable_audio bit null;
-
-
+    add keepaliveIntervalTime int default null;
\ No newline at end of file
diff --git a/src/main/java/com/genersoft/iot/vmp/common/VideoManagerConstants.java b/src/main/java/com/genersoft/iot/vmp/common/VideoManagerConstants.java
index 47e51ba..b92f6b1 100644
--- a/src/main/java/com/genersoft/iot/vmp/common/VideoManagerConstants.java
+++ b/src/main/java/com/genersoft/iot/vmp/common/VideoManagerConstants.java
@@ -70,6 +70,8 @@
 
 	public static final String SYSTEM_INFO_DISK_PREFIX = "VMP_SYSTEM_INFO_DISK_";
 
+	public static final String REGISTER_EXPIRE_TASK_KEY_PREFIX = "VMP_device_register_expire_";
+
 
 
 
diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/bean/Device.java b/src/main/java/com/genersoft/iot/vmp/gb28181/bean/Device.java
index 36b84ff..e454896 100644
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/bean/Device.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/bean/Device.java
@@ -94,6 +94,13 @@
 	@Schema(description = "蹇冭烦鏃堕棿")
 	private String keepaliveTime;
 
+
+	/**
+	 * 蹇冭烦闂撮殧
+	 */
+	@Schema(description = "蹇冭烦闂撮殧")
+	private int keepaliveIntervalTime;
+
 	/**
 	 * 閫氶亾涓暟
 	 */
@@ -413,4 +420,12 @@
 	public void setLocalIp(String localIp) {
 		this.localIp = localIp;
 	}
+
+	public int getKeepaliveIntervalTime() {
+		return keepaliveIntervalTime;
+	}
+
+	public void setKeepaliveIntervalTime(int keepaliveIntervalTime) {
+		this.keepaliveIntervalTime = keepaliveIntervalTime;
+	}
 }
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
index 5751a11..98c1a96 100644
--- 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,5 +1,7 @@
 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;
@@ -43,6 +45,9 @@
     @Autowired
     private UserSetting userSetting;
 
+    @Autowired
+    private DynamicTask dynamicTask;
+
     @Override
     public void afterPropertiesSet() throws Exception {
         notifyMessageHandler.addHandler(cmdType, this);
@@ -68,6 +73,13 @@
             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());
+            device.setKeepaliveIntervalTime(new Long(System.currentTimeMillis()/1000-lastTime).intValue());
+        }
+
         device.setKeepaliveTime(DateUtil.getNow());
 
         if (device.getOnline() == 1) {
@@ -75,9 +87,15 @@
         }else {
             // 瀵逛簬宸茬粡绂荤嚎鐨勮澶囧垽鏂粬鐨勬敞鍐屾槸鍚﹀凡缁忚繃鏈�
             if (!deviceService.expire(device)){
+                device.setOnline(0);
                 deviceService.online(device);
             }
         }
+        // 鍒锋柊杩囨湡浠诲姟
+        String registerExpireTaskKey = VideoManagerConstants.REGISTER_EXPIRE_TASK_KEY_PREFIX + device.getDeviceId();
+        // 濡傛灉涓夋蹇冭烦澶辫触锛屽垯璁剧疆璁惧绂荤嚎
+        dynamicTask.startDelay(registerExpireTaskKey, ()-> deviceService.offline(device.getDeviceId()), device.getKeepaliveIntervalTime()*1000*3);
+
     }
 
     @Override
diff --git a/src/main/java/com/genersoft/iot/vmp/service/impl/DeviceServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/service/impl/DeviceServiceImpl.java
index a47e244..f8ac20f 100644
--- a/src/main/java/com/genersoft/iot/vmp/service/impl/DeviceServiceImpl.java
+++ b/src/main/java/com/genersoft/iot/vmp/service/impl/DeviceServiceImpl.java
@@ -1,5 +1,6 @@
 package com.genersoft.iot.vmp.service.impl;
 
+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.*;
@@ -44,8 +45,6 @@
 public class DeviceServiceImpl implements IDeviceService {
 
     private final static Logger logger = LoggerFactory.getLogger(DeviceServiceImpl.class);
-
-    private final String  registerExpireTaskKeyPrefix = "device-register-expire-";
 
     @Autowired
     private DynamicTask dynamicTask;
@@ -101,7 +100,10 @@
             redisCatchStorage.clearCatchByDeviceId(device.getDeviceId());
         }
         device.setUpdateTime(now);
-
+        if (device.getKeepaliveIntervalTime() == 0) {
+            // 榛樿蹇冭烦闂撮殧60
+            device.setKeepaliveIntervalTime(60);
+        }
         // 绗竴娆′笂绾� 鎴栧垯璁惧涔嬪墠鏄绾跨姸鎬�--杩涜閫氶亾鍚屾鍜岃澶囦俊鎭煡璇�
         if (device.getCreateTime() == null) {
             device.setOnline(1);
@@ -116,7 +118,6 @@
             }
             sync(device);
         }else {
-
             if(device.getOnline() == 0){
                 device.setOnline(1);
                 device.setCreateTime(now);
@@ -153,19 +154,19 @@
             addMobilePositionSubscribe(device);
         }
         // 鍒锋柊杩囨湡浠诲姟
-        String registerExpireTaskKey = registerExpireTaskKeyPrefix + device.getDeviceId();
-        // 澧炲姞涓�涓�10绉掔粰璁惧閲嶅彂娑堟伅鐨勬満浼�
-        dynamicTask.startDelay(registerExpireTaskKey, ()-> offline(device.getDeviceId()), (device.getExpires() + 10) * 1000);
+        String registerExpireTaskKey = VideoManagerConstants.REGISTER_EXPIRE_TASK_KEY_PREFIX + device.getDeviceId();
+        // 濡傛灉绗竴娆℃敞鍐岄偅涔堝繀椤诲湪60 * 3鏃堕棿鍐呮敹鍒颁竴涓績璺筹紝鍚﹀垯璁惧绂荤嚎
+        dynamicTask.startDelay(registerExpireTaskKey, ()-> offline(device.getDeviceId()), device.getKeepaliveIntervalTime() * 1000 * 3);
     }
 
     @Override
     public void offline(String deviceId) {
-        logger.info("[璁惧绂荤嚎]锛� device锛歿}", deviceId);
+        logger.error("[璁惧绂荤嚎]锛� device锛歿}", deviceId);
         Device device = deviceMapper.getDeviceByDeviceId(deviceId);
         if (device == null) {
             return;
         }
-        String registerExpireTaskKey = registerExpireTaskKeyPrefix + deviceId;
+        String registerExpireTaskKey = VideoManagerConstants.REGISTER_EXPIRE_TASK_KEY_PREFIX + deviceId;
         dynamicTask.stop(registerExpireTaskKey);
         device.setOnline(0);
         redisCatchStorage.updateDevice(device);
diff --git a/src/main/java/com/genersoft/iot/vmp/storager/dao/DeviceMapper.java b/src/main/java/com/genersoft/iot/vmp/storager/dao/DeviceMapper.java
index 0e61692..8182e6d 100644
--- a/src/main/java/com/genersoft/iot/vmp/storager/dao/DeviceMapper.java
+++ b/src/main/java/com/genersoft/iot/vmp/storager/dao/DeviceMapper.java
@@ -61,6 +61,7 @@
                 "expires," +
                 "registerTime," +
                 "keepaliveTime," +
+                "keepaliveIntervalTime," +
                 "createTime," +
                 "updateTime," +
                 "charset," +
@@ -88,6 +89,7 @@
                 "#{expires}," +
                 "#{registerTime}," +
                 "#{keepaliveTime}," +
+                "#{keepaliveIntervalTime}," +
                 "#{createTime}," +
                 "#{updateTime}," +
                 "#{charset}," +
@@ -117,6 +119,7 @@
                 "<if test=\"online != null\">, online=${online}</if>" +
                 "<if test=\"registerTime != null\">, registerTime='${registerTime}'</if>" +
                 "<if test=\"keepaliveTime != null\">, keepaliveTime='${keepaliveTime}'</if>" +
+                "<if test=\"keepaliveIntervalTime != null\">, keepaliveIntervalTime='${keepaliveIntervalTime}'</if>" +
                 "<if test=\"expires != null\">, expires=${expires}</if>" +
                 "WHERE deviceId='${deviceId}'"+
             " </script>"})

--
Gitblit v1.8.0