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) {