Просмотр исходного кода

重启后对心跳超时的设备设置为离线。

64850858 4 лет назад
Родитель
Сommit
ea32cd2673

+ 36 - 0
src/main/java/com/genersoft/iot/vmp/conf/SipDeviceRunner.java

@@ -0,0 +1,36 @@
+package com.genersoft.iot.vmp.conf;
+
+import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
+import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.CommandLineRunner;
+import org.springframework.core.annotation.Order;
+import org.springframework.stereotype.Component;
+
+import java.util.List;
+
+
+/**
+ * 系统启动时控制设备离线
+ */
+@Component
+@Order(value=4)
+public class SipDeviceRunner implements CommandLineRunner {
+
+    @Autowired
+    private IVideoManagerStorager storager;
+
+    @Autowired
+    private IRedisCatchStorage redisCatchStorage;
+
+    @Override
+    public void run(String... args) throws Exception {
+        // 读取redis没有心跳信息的则设置为离线,等收到下次心跳设置为在线
+        // 设置所有设备离线
+        storager.outlineForAll();
+        List<String> onlineForAll = redisCatchStorage.getOnlineForAll();
+        for (String deviceId : onlineForAll) {
+            storager.online(deviceId);
+        }
+    }
+}

+ 21 - 9
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/MessageRequestProcessor.java

@@ -771,19 +771,31 @@ public class MessageRequestProcessor extends SIPRequestAbstractProcessor {
 			Element rootElement = getRootElement(evt);
 			Element rootElement = getRootElement(evt);
 			String deviceId = XmlUtil.getText(rootElement, "DeviceID");
 			String deviceId = XmlUtil.getText(rootElement, "DeviceID");
 			Device device = storager.queryVideoDevice(deviceId);
 			Device device = storager.queryVideoDevice(deviceId);
-			// 检查设备是否存在并在线, 不存在则不回复
-			if (device != null && device.getOnline() == 1) {
+
+			// 检查设备是否存在并在线, 不在线则设置为在线
+			if (device != null ) {
 				// 回复200 OK
 				// 回复200 OK
 				responseAck(evt);
 				responseAck(evt);
-				if (offLineDetector.isOnline(deviceId)) {
-					publisher.onlineEventPublish(device, VideoManagerConstants.EVENT_ONLINE_KEEPLIVE);
-				} else {
-				}
-			}else {
-				logger.warn("收到[ "+deviceId+" ]心跳信息, 但是设备" + (device == null? "不存在":"离线") + ", 回复401");
-				Response response = getMessageFactory().createResponse(Response.UNAUTHORIZED, evt.getRequest());
+				publisher.onlineEventPublish(device, VideoManagerConstants.EVENT_ONLINE_KEEPLIVE);
+			}else{
+				logger.warn("收到[ "+deviceId+" ]心跳信息, 但是设备不存在, 回复404");
+				Response response = getMessageFactory().createResponse(Response.NOT_FOUND, evt.getRequest());
 				getServerTransaction(evt).sendResponse(response);
 				getServerTransaction(evt).sendResponse(response);
 			}
 			}
+
+//			if (device != null && device.getOnline() == 1) {
+//
+//				if (offLineDetector.isOnline(deviceId)) {
+//					publisher.onlineEventPublish(device, VideoManagerConstants.EVENT_ONLINE_KEEPLIVE);
+//				} else {
+//				}
+//			}else {
+////				logger.warn("收到[ "+deviceId+" ]心跳信息, 但是设备" + (device == null? "不存在":"离线") + ", 回复401");
+////				Response response = getMessageFactory().createResponse(Response.UNAUTHORIZED, evt.getRequest());
+////				getServerTransaction(evt).sendResponse(response);
+//				publisher.onlineEventPublish(device, VideoManagerConstants.EVENT_ONLINE_KEEPLIVE);
+//
+//			}
 		} catch (ParseException | SipException | InvalidArgumentException | DocumentException e) {
 		} catch (ParseException | SipException | InvalidArgumentException | DocumentException e) {
 			e.printStackTrace();
 			e.printStackTrace();
 		}
 		}

+ 5 - 0
src/main/java/com/genersoft/iot/vmp/storager/IRedisCatchStorage.java

@@ -112,6 +112,11 @@ public interface IRedisCatchStorage {
      */
      */
     void outlineForAll();
     void outlineForAll();
 
 
+    /**
+     * 获取所有在线的
+     */
+    List<String> getOnlineForAll();
+
     /**
     /**
      * 在redis添加wvp的信息
      * 在redis添加wvp的信息
      */
      */

+ 11 - 0
src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java

@@ -283,6 +283,17 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage {
         }
         }
     }
     }
 
 
+    @Override
+    public List<String> getOnlineForAll() {
+        List<String> result = new ArrayList<>();
+        List<Object> onlineDevices = redis.scan(VideoManagerConstants.KEEPLIVEKEY_PREFIX + "*" );
+        for (int i = 0; i < onlineDevices.size(); i++) {
+            String key = (String) onlineDevices.get(i);
+            result.add((String) redis.get(key));
+        }
+        return result;
+    }
+
     @Override
     @Override
     public void updateWVPInfo(JSONObject jsonObject) {
     public void updateWVPInfo(JSONObject jsonObject) {