|
|
@@ -154,8 +154,20 @@ public class DevicePingInfoService extends SuperService {
|
|
|
entity.setOnLineDeviceType(onLineDeviceType);
|
|
|
deviceOnLineInfoDao.save(entity);
|
|
|
|
|
|
+ long expirePeriod = CommonTTLTimeConfig.maxDeviceRedisOnLineTime;
|
|
|
+ if (onLineDeviceType == OnLineDeviceType.GateWay) {
|
|
|
+ GateWayInfo gateWayInfo = gateWayInfoDao.findTopByGateWayId(deviceId);
|
|
|
+ if (ObjectUtils.isNotEmpty(gateWayInfo) && gateWayInfo.getHbInterval() != null) {
|
|
|
+ expirePeriod = gateWayInfo.getHbInterval();
|
|
|
+ }
|
|
|
+ } else if (onLineDeviceType == OnLineDeviceType.Device) {
|
|
|
+ DeviceInfo deviceInfo = deviceInfoDao.findTopByDeviceId(deviceId);
|
|
|
+ if (ObjectUtils.isNotEmpty(deviceInfo) && deviceInfo.getHbInterval() != null) {
|
|
|
+ expirePeriod = deviceInfo.getHbInterval();
|
|
|
+ }
|
|
|
+ }
|
|
|
String expiredKey = String.format("%s%s", CommonTTLTimeConfig.OnLineExpiredKey, deviceId);
|
|
|
- redisService.setValue(expiredKey, deviceName, CommonTTLTimeConfig.maxDeviceRedisOnLineTime);
|
|
|
+ redisService.setValueSECONDS(expiredKey, deviceName, expirePeriod);
|
|
|
}
|
|
|
|
|
|
public void redisExpire(String expiredKey) {
|
|
|
@@ -167,7 +179,7 @@ public class DevicePingInfoService extends SuperService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * TTL 到期,设备下线
|
|
|
+ * TTL 到期,设备下线 (redis 设置的值过期触发)
|
|
|
*
|
|
|
* @param dataId
|
|
|
*/
|
|
|
@@ -177,16 +189,23 @@ public class DevicePingInfoService extends SuperService {
|
|
|
entity = deviceOnLineInfoDao.findTopByDeviceId(dataId);
|
|
|
}
|
|
|
|
|
|
+ long expirePeriod = CommonTTLTimeConfig.maxDeviceRedisOnLineTime;
|
|
|
if (ObjectUtils.isNotEmpty(entity)) {
|
|
|
if (entity.getOnLineDeviceType() == OnLineDeviceType.GateWay) {
|
|
|
GateWayInfo gateWayInfo = gateWayInfoDao.findTopByGateWayId(entity.getDeviceId());
|
|
|
gateWayCheckOnLineState(gateWayInfo);
|
|
|
+ if (ObjectUtils.isNotEmpty(gateWayInfo) && gateWayInfo.getHbInterval() != null) {
|
|
|
+ expirePeriod = gateWayInfo.getHbInterval();
|
|
|
+ }
|
|
|
} else if (entity.getOnLineDeviceType() == OnLineDeviceType.Device) {
|
|
|
DeviceInfo deviceInfo = deviceInfoDao.findTopByDeviceId(entity.getDeviceId());
|
|
|
deviceCheckOnLineState(deviceInfo);
|
|
|
+ if (ObjectUtils.isNotEmpty(deviceInfo) && deviceInfo.getHbInterval() != null) {
|
|
|
+ expirePeriod = deviceInfo.getHbInterval();
|
|
|
+ }
|
|
|
}
|
|
|
String expiredKey = String.format("%s%s", CommonTTLTimeConfig.OnLineExpiredKey, entity.getDeviceId());
|
|
|
- redisService.setValue(expiredKey, entity.getDeviceName(), CommonTTLTimeConfig.maxDeviceRedisOnLineTime);
|
|
|
+ redisService.setValueSECONDS(expiredKey, entity.getDeviceName(), expirePeriod);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -211,18 +230,22 @@ public class DevicePingInfoService extends SuperService {
|
|
|
OnLineState onLineState = OnLineState.OffLine;
|
|
|
List<DevicePingInfo> _list = devicePingInfoDao.findTop5ByDeviceIdOrderByCreateTimeDesc(deviceInfo.getDeviceId());
|
|
|
if (_list != null) {
|
|
|
+ long defaultTime = CommonTTLTimeConfig.maxUnPingOnLineTime;
|
|
|
+ if (deviceInfo.getHbInterval() != null) {
|
|
|
+ defaultTime = deviceInfo.getHbInterval() * 1000L + 1000;
|
|
|
+ }
|
|
|
Long firstTime = null;
|
|
|
long avgTime = 0;
|
|
|
if (_list.size() == 1) {
|
|
|
firstTime = _list.get(0).getCreateTime();
|
|
|
- avgTime = CommonTTLTimeConfig.maxUnPingOnLineTime;
|
|
|
+ avgTime = defaultTime;
|
|
|
} else if (_list.size() > 2) {
|
|
|
firstTime = _list.get(0).getCreateTime();
|
|
|
Long lastTime = _list.get(_list.size() - 1).getCreateTime();
|
|
|
// 2 次心跳间隔时间
|
|
|
avgTime = ((firstTime - lastTime) / (_list.size() - 1)) * 2;
|
|
|
- if (avgTime > CommonTTLTimeConfig.maxUnPingOnLineTime) {
|
|
|
- avgTime = CommonTTLTimeConfig.maxUnPingOnLineTime;
|
|
|
+ if (avgTime > defaultTime) {
|
|
|
+ avgTime = defaultTime;
|
|
|
}
|
|
|
}
|
|
|
if (firstTime != null) {
|
|
|
@@ -271,18 +294,22 @@ public class DevicePingInfoService extends SuperService {
|
|
|
// 默认离线
|
|
|
OnLineState onLineState = OnLineState.OffLine;
|
|
|
if (_list != null) {
|
|
|
+ long defaultTime = CommonTTLTimeConfig.maxUnPingOnLineTime;
|
|
|
+ if (gateWayInfo.getHbInterval() != null) {
|
|
|
+ defaultTime = gateWayInfo.getHbInterval() * 1000L * 1000;
|
|
|
+ }
|
|
|
Long firstTime = null;
|
|
|
long avgTime = 0;
|
|
|
if (_list.size() == 1) {
|
|
|
firstTime = _list.get(0).getCreateTime();
|
|
|
- avgTime = CommonTTLTimeConfig.maxUnPingOnLineTime;
|
|
|
+ avgTime = defaultTime;
|
|
|
} else if (_list.size() > 2) {
|
|
|
firstTime = _list.get(0).getCreateTime();
|
|
|
Long lastTime = _list.get(_list.size() - 1).getCreateTime();
|
|
|
// 2 次心跳间隔时间
|
|
|
avgTime = ((firstTime - lastTime) / (_list.size() - 1)) * 2;
|
|
|
- if (avgTime > CommonTTLTimeConfig.maxUnPingOnLineTime) {
|
|
|
- avgTime = CommonTTLTimeConfig.maxUnPingOnLineTime;
|
|
|
+ if (avgTime > defaultTime) {
|
|
|
+ avgTime = defaultTime;
|
|
|
}
|
|
|
}
|
|
|
if (firstTime != null) {
|