From a82b831b8b2b96dfa5791808f1207500e16697cf Mon Sep 17 00:00:00 2001 From: 648540858 <648540858@qq.com> Date: Wed, 4 Jan 2023 10:39:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=AE=BE=E5=A4=87=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E4=BF=9D=E6=8C=81=EF=BC=8C=E8=87=AA=E5=8A=A8=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=E5=BF=83=E8=B7=B3=E9=97=B4=E9=9A=94=EF=BC=8C=E4=B8=89?= =?UTF-8?q?=E6=AC=A1=E5=BF=83=E8=B7=B3=E5=A4=B1=E8=B4=A5=E5=88=99=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E7=A6=BB=E7=BA=BF=EF=BC=8C=E4=B8=8D=E5=9C=A8=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E8=AE=BE=E5=A4=87=E6=9C=89=E6=95=88=E6=9C=9F=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E4=BD=9C=E4=B8=BA=E5=94=AF=E4=B8=80=E5=88=A4=E6=96=AD?= =?UTF-8?q?=E6=A0=87=E5=87=86=EF=BC=8C=E6=8F=90=E9=AB=98=E5=AE=B9=E9=94=99?= =?UTF-8?q?=E8=83=BD=E5=8A=9B=E5=92=8C=E7=A8=B3=E5=AE=9A=E6=80=A7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- sql/update.sql | 44 +------------------ .../iot/vmp/common/VideoManagerConstants.java | 2 + .../iot/vmp/gb28181/bean/Device.java | 15 +++++++ .../cmd/KeepaliveNotifyMessageHandler.java | 18 ++++++++ .../vmp/service/impl/DeviceServiceImpl.java | 19 ++++---- .../iot/vmp/storager/dao/DeviceMapper.java | 3 ++ 7 files changed, 51 insertions(+), 52 deletions(-) diff --git a/pom.xml b/pom.xml index 4c26f332..c0c99004 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ com.genersoft wvp-pro - 2.6.6 + 2.6.7 web video platform 国标28181视频平台 diff --git a/sql/update.sql b/sql/update.sql index 877e2477..2e5d5691 100644 --- a/sql/update.sql +++ b/sql/update.sql @@ -1,43 +1,3 @@ -alter table media_server - drop column streamNoneReaderDelayMS; - -alter table media_server - drop column sendRtpPortRange; - -alter table stream_proxy - add enable_disable_none_reader bit(1) default null; - +-- 2.6.6->2.6.7 alter table device - add mediaServerId varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT 'auto'; - -alter table device - add custom_name varchar(255) default null; - -alter table device - add sdpIp varchar(50) default null; - -alter table device - add localIp varchar(50) default null; - -alter table device - add password varchar(255) default null; - -alter table device - modify ip varchar(50) null; - -alter table device - modify port int null; - -alter table device - modify expires int null; - -alter table device - modify subscribeCycleForCatalog int null; - -alter table device - modify hostAddress varchar(50) null; - -alter table stream_proxy - change enable_hls enable_audio bit null; - - + add keepaliveIntervalTime int default null; \ No newline at end of file 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 47e51baa..b92f6b16 100644 --- a/src/main/java/com/genersoft/iot/vmp/common/VideoManagerConstants.java +++ b/src/main/java/com/genersoft/iot/vmp/common/VideoManagerConstants.java @@ -70,6 +70,8 @@ public class VideoManagerConstants { public static final String SYSTEM_INFO_DISK_PREFIX = "VMP_SYSTEM_INFO_DISK_"; + public static final String REGISTER_EXPIRE_TASK_KEY_PREFIX = "VMP_device_register_expire_"; + diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/bean/Device.java b/src/main/java/com/genersoft/iot/vmp/gb28181/bean/Device.java index 36b84ff1..e454896e 100644 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/bean/Device.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/bean/Device.java @@ -94,6 +94,13 @@ public class Device { @Schema(description = "心跳时间") private String keepaliveTime; + + /** + * 心跳间隔 + */ + @Schema(description = "心跳间隔") + private int keepaliveIntervalTime; + /** * 通道个数 */ @@ -413,4 +420,12 @@ public class Device { public void setLocalIp(String localIp) { this.localIp = localIp; } + + public int getKeepaliveIntervalTime() { + return keepaliveIntervalTime; + } + + public void setKeepaliveIntervalTime(int keepaliveIntervalTime) { + this.keepaliveIntervalTime = keepaliveIntervalTime; + } } diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/KeepaliveNotifyMessageHandler.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/KeepaliveNotifyMessageHandler.java index 5751a11a..98c1a96f 100644 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/KeepaliveNotifyMessageHandler.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/KeepaliveNotifyMessageHandler.java @@ -1,5 +1,7 @@ package com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.notify.cmd; +import com.genersoft.iot.vmp.common.VideoManagerConstants; +import com.genersoft.iot.vmp.conf.DynamicTask; import com.genersoft.iot.vmp.conf.UserSetting; import com.genersoft.iot.vmp.gb28181.bean.Device; import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform; @@ -43,6 +45,9 @@ public class KeepaliveNotifyMessageHandler extends SIPRequestProcessorParent imp @Autowired private UserSetting userSetting; + @Autowired + private DynamicTask dynamicTask; + @Override public void afterPropertiesSet() throws Exception { notifyMessageHandler.addHandler(cmdType, this); @@ -68,6 +73,13 @@ public class KeepaliveNotifyMessageHandler extends SIPRequestProcessorParent imp device.setHostAddress(remoteAddressInfo.getIp().concat(":").concat(String.valueOf(remoteAddressInfo.getPort()))); device.setIp(remoteAddressInfo.getIp()); } + if (device.getKeepaliveTime() == null) { + device.setKeepaliveIntervalTime(60); + }else { + long lastTime = DateUtil.yyyy_MM_dd_HH_mm_ssToTimestamp(device.getKeepaliveTime()); + device.setKeepaliveIntervalTime(new Long(System.currentTimeMillis()/1000-lastTime).intValue()); + } + device.setKeepaliveTime(DateUtil.getNow()); if (device.getOnline() == 1) { @@ -75,9 +87,15 @@ public class KeepaliveNotifyMessageHandler extends SIPRequestProcessorParent imp }else { // 对于已经离线的设备判断他的注册是否已经过期 if (!deviceService.expire(device)){ + device.setOnline(0); deviceService.online(device); } } + // 刷新过期任务 + String registerExpireTaskKey = VideoManagerConstants.REGISTER_EXPIRE_TASK_KEY_PREFIX + device.getDeviceId(); + // 如果三次心跳失败,则设置设备离线 + dynamicTask.startDelay(registerExpireTaskKey, ()-> deviceService.offline(device.getDeviceId()), device.getKeepaliveIntervalTime()*1000*3); + } @Override 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 a47e2445..f8ac20fc 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 @@ -1,5 +1,6 @@ package com.genersoft.iot.vmp.service.impl; +import com.genersoft.iot.vmp.common.VideoManagerConstants; import com.genersoft.iot.vmp.conf.DynamicTask; import com.genersoft.iot.vmp.conf.UserSetting; import com.genersoft.iot.vmp.gb28181.bean.*; @@ -45,8 +46,6 @@ public class DeviceServiceImpl implements IDeviceService { private final static Logger logger = LoggerFactory.getLogger(DeviceServiceImpl.class); - private final String registerExpireTaskKeyPrefix = "device-register-expire-"; - @Autowired private DynamicTask dynamicTask; @@ -101,7 +100,10 @@ public class DeviceServiceImpl implements IDeviceService { redisCatchStorage.clearCatchByDeviceId(device.getDeviceId()); } device.setUpdateTime(now); - + if (device.getKeepaliveIntervalTime() == 0) { + // 默认心跳间隔60 + device.setKeepaliveIntervalTime(60); + } // 第一次上线 或则设备之前是离线状态--进行通道同步和设备信息查询 if (device.getCreateTime() == null) { device.setOnline(1); @@ -116,7 +118,6 @@ public class DeviceServiceImpl implements IDeviceService { } sync(device); }else { - if(device.getOnline() == 0){ device.setOnline(1); device.setCreateTime(now); @@ -153,19 +154,19 @@ public class DeviceServiceImpl implements IDeviceService { addMobilePositionSubscribe(device); } // 刷新过期任务 - String registerExpireTaskKey = registerExpireTaskKeyPrefix + device.getDeviceId(); - // 增加一个10秒给设备重发消息的机会 - dynamicTask.startDelay(registerExpireTaskKey, ()-> offline(device.getDeviceId()), (device.getExpires() + 10) * 1000); + String registerExpireTaskKey = VideoManagerConstants.REGISTER_EXPIRE_TASK_KEY_PREFIX + device.getDeviceId(); + // 如果第一次注册那么必须在60 * 3时间内收到一个心跳,否则设备离线 + dynamicTask.startDelay(registerExpireTaskKey, ()-> offline(device.getDeviceId()), device.getKeepaliveIntervalTime() * 1000 * 3); } @Override public void offline(String deviceId) { - logger.info("[设备离线], device:{}", deviceId); + logger.error("[设备离线], device:{}", deviceId); Device device = deviceMapper.getDeviceByDeviceId(deviceId); if (device == null) { return; } - String registerExpireTaskKey = registerExpireTaskKeyPrefix + deviceId; + String registerExpireTaskKey = VideoManagerConstants.REGISTER_EXPIRE_TASK_KEY_PREFIX + deviceId; dynamicTask.stop(registerExpireTaskKey); device.setOnline(0); redisCatchStorage.updateDevice(device); diff --git a/src/main/java/com/genersoft/iot/vmp/storager/dao/DeviceMapper.java b/src/main/java/com/genersoft/iot/vmp/storager/dao/DeviceMapper.java index 0e616921..8182e6d6 100644 --- a/src/main/java/com/genersoft/iot/vmp/storager/dao/DeviceMapper.java +++ b/src/main/java/com/genersoft/iot/vmp/storager/dao/DeviceMapper.java @@ -61,6 +61,7 @@ public interface DeviceMapper { "expires," + "registerTime," + "keepaliveTime," + + "keepaliveIntervalTime," + "createTime," + "updateTime," + "charset," + @@ -88,6 +89,7 @@ public interface DeviceMapper { "#{expires}," + "#{registerTime}," + "#{keepaliveTime}," + + "#{keepaliveIntervalTime}," + "#{createTime}," + "#{updateTime}," + "#{charset}," + @@ -117,6 +119,7 @@ public interface DeviceMapper { ", online=${online}" + ", registerTime='${registerTime}'" + ", keepaliveTime='${keepaliveTime}'" + + ", keepaliveIntervalTime='${keepaliveIntervalTime}'" + ", expires=${expires}" + "WHERE deviceId='${deviceId}'"+ " "})