From ea32cd2673b83b9481e8cc45705d2d3a84e884bb Mon Sep 17 00:00:00 2001 From: 64850858 <648540858@qq.com> Date: Mon, 26 Jul 2021 13:21:03 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=90=AF=E5=90=8E=E5=AF=B9=E5=BF=83?= =?UTF-8?q?=E8=B7=B3=E8=B6=85=E6=97=B6=E7=9A=84=E8=AE=BE=E5=A4=87=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E4=B8=BA=E7=A6=BB=E7=BA=BF=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iot/vmp/conf/SipDeviceRunner.java | 36 +++++++++++++++++++ .../request/impl/MessageRequestProcessor.java | 30 +++++++++++----- .../iot/vmp/storager/IRedisCatchStorage.java | 5 +++ .../storager/impl/RedisCatchStorageImpl.java | 11 ++++++ 4 files changed, 73 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/genersoft/iot/vmp/conf/SipDeviceRunner.java diff --git a/src/main/java/com/genersoft/iot/vmp/conf/SipDeviceRunner.java b/src/main/java/com/genersoft/iot/vmp/conf/SipDeviceRunner.java new file mode 100644 index 00000000..68bb1a61 --- /dev/null +++ b/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 onlineForAll = redisCatchStorage.getOnlineForAll(); + for (String deviceId : onlineForAll) { + storager.online(deviceId); + } + } +} diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/MessageRequestProcessor.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/MessageRequestProcessor.java index 70e64a5e..d46c0c40 100644 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/MessageRequestProcessor.java +++ b/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); String deviceId = XmlUtil.getText(rootElement, "DeviceID"); Device device = storager.queryVideoDevice(deviceId); - // 检查设备是否存在并在线, 不存在则不回复 - if (device != null && device.getOnline() == 1) { + + // 检查设备是否存在并在线, 不在线则设置为在线 + if (device != null ) { // 回复200 OK 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); } + +// 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) { e.printStackTrace(); } 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 f5b2b1b8..e4313d90 100644 --- a/src/main/java/com/genersoft/iot/vmp/storager/IRedisCatchStorage.java +++ b/src/main/java/com/genersoft/iot/vmp/storager/IRedisCatchStorage.java @@ -112,6 +112,11 @@ public interface IRedisCatchStorage { */ void outlineForAll(); + /** + * 获取所有在线的 + */ + List getOnlineForAll(); + /** * 在redis添加wvp的信息 */ 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 08eb9d13..e5b1269d 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 @@ -283,6 +283,17 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { } } + @Override + public List getOnlineForAll() { + List result = new ArrayList<>(); + List 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 public void updateWVPInfo(JSONObject jsonObject) {