Commit ea32cd2673b83b9481e8cc45705d2d3a84e884bb
1 parent
c3ee8ec4
重启后对心跳超时的设备设置为离线。
Showing
4 changed files
with
73 additions
and
9 deletions
src/main/java/com/genersoft/iot/vmp/conf/SipDeviceRunner.java
0 → 100644
| 1 | +package com.genersoft.iot.vmp.conf; | ||
| 2 | + | ||
| 3 | +import com.genersoft.iot.vmp.storager.IRedisCatchStorage; | ||
| 4 | +import com.genersoft.iot.vmp.storager.IVideoManagerStorager; | ||
| 5 | +import org.springframework.beans.factory.annotation.Autowired; | ||
| 6 | +import org.springframework.boot.CommandLineRunner; | ||
| 7 | +import org.springframework.core.annotation.Order; | ||
| 8 | +import org.springframework.stereotype.Component; | ||
| 9 | + | ||
| 10 | +import java.util.List; | ||
| 11 | + | ||
| 12 | + | ||
| 13 | +/** | ||
| 14 | + * 系统启动时控制设备离线 | ||
| 15 | + */ | ||
| 16 | +@Component | ||
| 17 | +@Order(value=4) | ||
| 18 | +public class SipDeviceRunner implements CommandLineRunner { | ||
| 19 | + | ||
| 20 | + @Autowired | ||
| 21 | + private IVideoManagerStorager storager; | ||
| 22 | + | ||
| 23 | + @Autowired | ||
| 24 | + private IRedisCatchStorage redisCatchStorage; | ||
| 25 | + | ||
| 26 | + @Override | ||
| 27 | + public void run(String... args) throws Exception { | ||
| 28 | + // 读取redis没有心跳信息的则设置为离线,等收到下次心跳设置为在线 | ||
| 29 | + // 设置所有设备离线 | ||
| 30 | + storager.outlineForAll(); | ||
| 31 | + List<String> onlineForAll = redisCatchStorage.getOnlineForAll(); | ||
| 32 | + for (String deviceId : onlineForAll) { | ||
| 33 | + storager.online(deviceId); | ||
| 34 | + } | ||
| 35 | + } | ||
| 36 | +} |
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/MessageRequestProcessor.java
| @@ -771,19 +771,31 @@ public class MessageRequestProcessor extends SIPRequestAbstractProcessor { | @@ -771,19 +771,31 @@ public class MessageRequestProcessor extends SIPRequestAbstractProcessor { | ||
| 771 | Element rootElement = getRootElement(evt); | 771 | Element rootElement = getRootElement(evt); |
| 772 | String deviceId = XmlUtil.getText(rootElement, "DeviceID"); | 772 | String deviceId = XmlUtil.getText(rootElement, "DeviceID"); |
| 773 | Device device = storager.queryVideoDevice(deviceId); | 773 | Device device = storager.queryVideoDevice(deviceId); |
| 774 | - // 检查设备是否存在并在线, 不存在则不回复 | ||
| 775 | - if (device != null && device.getOnline() == 1) { | 774 | + |
| 775 | + // 检查设备是否存在并在线, 不在线则设置为在线 | ||
| 776 | + if (device != null ) { | ||
| 776 | // 回复200 OK | 777 | // 回复200 OK |
| 777 | responseAck(evt); | 778 | responseAck(evt); |
| 778 | - if (offLineDetector.isOnline(deviceId)) { | ||
| 779 | - publisher.onlineEventPublish(device, VideoManagerConstants.EVENT_ONLINE_KEEPLIVE); | ||
| 780 | - } else { | ||
| 781 | - } | ||
| 782 | - }else { | ||
| 783 | - logger.warn("收到[ "+deviceId+" ]心跳信息, 但是设备" + (device == null? "不存在":"离线") + ", 回复401"); | ||
| 784 | - Response response = getMessageFactory().createResponse(Response.UNAUTHORIZED, evt.getRequest()); | 779 | + publisher.onlineEventPublish(device, VideoManagerConstants.EVENT_ONLINE_KEEPLIVE); |
| 780 | + }else{ | ||
| 781 | + logger.warn("收到[ "+deviceId+" ]心跳信息, 但是设备不存在, 回复404"); | ||
| 782 | + Response response = getMessageFactory().createResponse(Response.NOT_FOUND, evt.getRequest()); | ||
| 785 | getServerTransaction(evt).sendResponse(response); | 783 | getServerTransaction(evt).sendResponse(response); |
| 786 | } | 784 | } |
| 785 | + | ||
| 786 | +// if (device != null && device.getOnline() == 1) { | ||
| 787 | +// | ||
| 788 | +// if (offLineDetector.isOnline(deviceId)) { | ||
| 789 | +// publisher.onlineEventPublish(device, VideoManagerConstants.EVENT_ONLINE_KEEPLIVE); | ||
| 790 | +// } else { | ||
| 791 | +// } | ||
| 792 | +// }else { | ||
| 793 | +//// logger.warn("收到[ "+deviceId+" ]心跳信息, 但是设备" + (device == null? "不存在":"离线") + ", 回复401"); | ||
| 794 | +//// Response response = getMessageFactory().createResponse(Response.UNAUTHORIZED, evt.getRequest()); | ||
| 795 | +//// getServerTransaction(evt).sendResponse(response); | ||
| 796 | +// publisher.onlineEventPublish(device, VideoManagerConstants.EVENT_ONLINE_KEEPLIVE); | ||
| 797 | +// | ||
| 798 | +// } | ||
| 787 | } catch (ParseException | SipException | InvalidArgumentException | DocumentException e) { | 799 | } catch (ParseException | SipException | InvalidArgumentException | DocumentException e) { |
| 788 | e.printStackTrace(); | 800 | e.printStackTrace(); |
| 789 | } | 801 | } |
src/main/java/com/genersoft/iot/vmp/storager/IRedisCatchStorage.java
| @@ -113,6 +113,11 @@ public interface IRedisCatchStorage { | @@ -113,6 +113,11 @@ public interface IRedisCatchStorage { | ||
| 113 | void outlineForAll(); | 113 | void outlineForAll(); |
| 114 | 114 | ||
| 115 | /** | 115 | /** |
| 116 | + * 获取所有在线的 | ||
| 117 | + */ | ||
| 118 | + List<String> getOnlineForAll(); | ||
| 119 | + | ||
| 120 | + /** | ||
| 116 | * 在redis添加wvp的信息 | 121 | * 在redis添加wvp的信息 |
| 117 | */ | 122 | */ |
| 118 | void updateWVPInfo(JSONObject jsonObject); | 123 | void updateWVPInfo(JSONObject jsonObject); |
src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java
| @@ -284,6 +284,17 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { | @@ -284,6 +284,17 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { | ||
| 284 | } | 284 | } |
| 285 | 285 | ||
| 286 | @Override | 286 | @Override |
| 287 | + public List<String> getOnlineForAll() { | ||
| 288 | + List<String> result = new ArrayList<>(); | ||
| 289 | + List<Object> onlineDevices = redis.scan(VideoManagerConstants.KEEPLIVEKEY_PREFIX + "*" ); | ||
| 290 | + for (int i = 0; i < onlineDevices.size(); i++) { | ||
| 291 | + String key = (String) onlineDevices.get(i); | ||
| 292 | + result.add((String) redis.get(key)); | ||
| 293 | + } | ||
| 294 | + return result; | ||
| 295 | + } | ||
| 296 | + | ||
| 297 | + @Override | ||
| 287 | public void updateWVPInfo(JSONObject jsonObject) { | 298 | public void updateWVPInfo(JSONObject jsonObject) { |
| 288 | 299 | ||
| 289 | } | 300 | } |