优化设备状态保持,自动记录心跳间隔,三次心跳失败则设备离线,不在使用设备有效期字段作为唯一判断标准,提高容错能力和稳定性。
This commit is contained in:
@@ -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_";
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 {
|
||||
"<if test=\"online != null\">, online=${online}</if>" +
|
||||
"<if test=\"registerTime != null\">, registerTime='${registerTime}'</if>" +
|
||||
"<if test=\"keepaliveTime != null\">, keepaliveTime='${keepaliveTime}'</if>" +
|
||||
"<if test=\"keepaliveIntervalTime != null\">, keepaliveIntervalTime='${keepaliveIntervalTime}'</if>" +
|
||||
"<if test=\"expires != null\">, expires=${expires}</if>" +
|
||||
"WHERE deviceId='${deviceId}'"+
|
||||
" </script>"})
|
||||
|
||||
Reference in New Issue
Block a user