From 0f509049928733e49cd530d91f6463317e0c00ee Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期四, 20 四月 2023 10:26:42 +0800
Subject: [PATCH] 支持设备/通道状态变化时发送redis通知

---
 src/main/java/com/genersoft/iot/vmp/service/impl/DeviceServiceImpl.java                                       |   10 ++++++++++
 src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/NotifyRequestForCatalogProcessor.java |   17 +++++++++++++++++
 src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java                                  |   14 ++++++++++++++
 src/main/java/com/genersoft/iot/vmp/storager/IRedisCatchStorage.java                                          |    2 ++
 src/main/java/com/genersoft/iot/vmp/conf/UserSetting.java                                                     |   10 ++++++++++
 src/main/resources/all-application.yml                                                                        |    2 ++
 src/main/java/com/genersoft/iot/vmp/common/VideoManagerConstants.java                                         |    1 +
 7 files changed, 56 insertions(+), 0 deletions(-)

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 f143fad..ccfe77e 100644
--- a/src/main/java/com/genersoft/iot/vmp/common/VideoManagerConstants.java
+++ b/src/main/java/com/genersoft/iot/vmp/common/VideoManagerConstants.java
@@ -122,6 +122,7 @@
 	 */
 	public static final String VM_MSG_SUBSCRIBE_ALARM = "alarm";
 
+
 	/**
 	 * 鎶ヨ閫氱煡鐨勫彂閫� 锛堟敹鍒皉edis鍙戝嚭鐨勯�氱煡锛岃浆鍙戠粰鍏朵粬骞冲彴锛�
 	 */
diff --git a/src/main/java/com/genersoft/iot/vmp/conf/UserSetting.java b/src/main/java/com/genersoft/iot/vmp/conf/UserSetting.java
index 31fe7a4..539198f 100644
--- a/src/main/java/com/genersoft/iot/vmp/conf/UserSetting.java
+++ b/src/main/java/com/genersoft/iot/vmp/conf/UserSetting.java
@@ -52,6 +52,8 @@
 
     private Boolean refuseChannelStatusChannelFormNotify = Boolean.FALSE;
 
+    private Boolean deviceStatusNotify = Boolean.FALSE;
+
     private String serverId = "000000";
 
     private String recordPath = null;
@@ -267,4 +269,12 @@
     public void setMaxNotifyCountQueue(int maxNotifyCountQueue) {
         this.maxNotifyCountQueue = maxNotifyCountQueue;
     }
+
+    public Boolean getDeviceStatusNotify() {
+        return deviceStatusNotify;
+    }
+
+    public void setDeviceStatusNotify(Boolean deviceStatusNotify) {
+        this.deviceStatusNotify = deviceStatusNotify;
+    }
 }
diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/NotifyRequestForCatalogProcessor.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/NotifyRequestForCatalogProcessor.java
index 56fa187..f9f8fc2 100644
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/NotifyRequestForCatalogProcessor.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/NotifyRequestForCatalogProcessor.java
@@ -108,6 +108,11 @@
 							if (updateChannelOnlineList.size() > 300) {
 								executeSaveForOnline();
 							}
+							if (userSetting.getDeviceStatusNotify()) {
+								// 鍙戦�乺edis娑堟伅
+								redisCatchStorage.sendDeviceOrChannelStatus(device.getDeviceId(), channel.getChannelId(), true);
+							}
+
 							break;
 						case CatalogEvent.OFF :
 							// 绂荤嚎
@@ -116,6 +121,10 @@
 								updateChannelOfflineList.add(channel);
 								if (updateChannelOfflineList.size() > 300) {
 									executeSaveForOffline();
+								}
+								if (userSetting.getDeviceStatusNotify()) {
+									// 鍙戦�乺edis娑堟伅
+									redisCatchStorage.sendDeviceOrChannelStatus(device.getDeviceId(), channel.getChannelId(), false);
 								}
 							}else {
 								logger.info("[鏀跺埌閫氶亾绂荤嚎閫氱煡] 浣嗘槸骞冲彴宸查厤缃嫆缁濇娑堟伅锛屾潵鑷澶�: {}, 閫氶亾 {}", device.getDeviceId(), channel.getChannelId());
@@ -129,6 +138,10 @@
 								if (updateChannelOfflineList.size() > 300) {
 									executeSaveForOffline();
 								}
+								if (userSetting.getDeviceStatusNotify()) {
+									// 鍙戦�乺edis娑堟伅
+									redisCatchStorage.sendDeviceOrChannelStatus(device.getDeviceId(), channel.getChannelId(), false);
+								}
 							}else {
 								logger.info("[鏀跺埌閫氶亾瑙嗛涓㈠け閫氱煡] 浣嗘槸骞冲彴宸查厤缃嫆缁濇娑堟伅锛屾潵鑷澶�: {}, 閫氶亾 {}", device.getDeviceId(), channel.getChannelId());
 							}
@@ -141,6 +154,10 @@
 								if (updateChannelOfflineList.size() > 300) {
 									executeSaveForOffline();
 								}
+								if (userSetting.getDeviceStatusNotify()) {
+									// 鍙戦�乺edis娑堟伅
+									redisCatchStorage.sendDeviceOrChannelStatus(device.getDeviceId(), channel.getChannelId(), false);
+								}
 							}else {
 								logger.info("[鏀跺埌閫氶亾瑙嗛鏁呴殰閫氱煡] 浣嗘槸骞冲彴宸查厤缃嫆缁濇娑堟伅锛屾潵鑷澶�: {}, 閫氶亾 {}", device.getDeviceId(), channel.getChannelId());
 							}
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 48de5d2..106caec 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
@@ -165,6 +165,11 @@
         String registerExpireTaskKey = VideoManagerConstants.REGISTER_EXPIRE_TASK_KEY_PREFIX + device.getDeviceId();
         // 濡傛灉绗竴娆℃敞鍐岄偅涔堝繀椤诲湪60 * 3鏃堕棿鍐呮敹鍒颁竴涓績璺筹紝鍚﹀垯璁惧绂荤嚎
         dynamicTask.startDelay(registerExpireTaskKey, ()-> offline(device.getDeviceId(), "棣栨娉ㄥ唽鍚庢湭鑳芥敹鍒板績璺�"), device.getKeepaliveIntervalTime() * 1000 * 3);
+        if (userSetting.getDeviceStatusNotify()) {
+            // 鍙戦�乺edis娑堟伅
+            redisCatchStorage.sendDeviceOrChannelStatus(device.getDeviceId(), null, true);
+        }
+
     }
 
     @Override
@@ -193,6 +198,11 @@
         // 绉婚櫎璁㈤槄
         removeCatalogSubscribe(device);
         removeMobilePositionSubscribe(device);
+        if (userSetting.getDeviceStatusNotify()) {
+            // 鍙戦�乺edis娑堟伅
+            redisCatchStorage.sendDeviceOrChannelStatus(device.getDeviceId(), null, false);
+        }
+
     }
 
     @Override
diff --git a/src/main/java/com/genersoft/iot/vmp/storager/IRedisCatchStorage.java b/src/main/java/com/genersoft/iot/vmp/storager/IRedisCatchStorage.java
index 1e10469..42708f7 100644
--- a/src/main/java/com/genersoft/iot/vmp/storager/IRedisCatchStorage.java
+++ b/src/main/java/com/genersoft/iot/vmp/storager/IRedisCatchStorage.java
@@ -261,4 +261,6 @@
     List<Device> getAllDevices();
 
     void removeAllDevice();
+
+    void sendDeviceOrChannelStatus(String deviceId, String channelId, boolean online);
 }
diff --git a/src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java b/src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java
index 67f2e7e..facd54c 100644
--- a/src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java
+++ b/src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java
@@ -902,4 +902,18 @@
                 + userSetting.getServerId() + "_*_" + id + "_*";
         return RedisUtil.scan(redisTemplate, key).size();
     }
+
+    @Override
+    public void sendDeviceOrChannelStatus(String deviceId, String channelId, boolean online) {
+        String key = VideoManagerConstants.VM_MSG_SUBSCRIBE_DEVICE_STATUS;
+        logger.info("[redis閫氱煡] 鎺ㄩ�佽澶�/閫氶亾鐘舵�侊紝 {}/{}-{}", deviceId, channelId, online);
+        StringBuilder msg = new StringBuilder();
+        msg.append(deviceId);
+        if (channelId != null) {
+            msg.append(":").append(channelId);
+        }
+        msg.append(" ").append(online? "ON":"OFF");
+
+        redisTemplate.convertAndSend(key, msg.toString());
+    }
 }
diff --git a/src/main/resources/all-application.yml b/src/main/resources/all-application.yml
index bae9904..7f8b278 100644
--- a/src/main/resources/all-application.yml
+++ b/src/main/resources/all-application.yml
@@ -180,6 +180,8 @@
     refuse-channel-status-channel-form-notify: false
     # 璁剧疆notify缂撳瓨闃熷垪鏈�澶ч暱搴︼紝瓒呰繃姝ら暱搴︾殑鏁版嵁灏嗚繑鍥�486 BUSY_HERE锛屾秷鎭涪寮�, 榛樿10000
     max-notify-count-queue: 10000
+    # 璁惧/閫氶亾鐘舵�佸彉鍖栨椂鍙戦�佹秷鎭�
+    device-status-notify: false
     # 璺ㄥ煙閰嶇疆锛岄厤缃綘璁块棶鍓嶇椤甸潰鐨勫湴鍧�鍗冲彲锛� 鍙互閰嶇疆澶氫釜
     allowed-origins:
         - http://localhost:8008

--
Gitblit v1.8.0