TRX 1 سال پیش
والد
کامیت
02c2ae829e

+ 2 - 1
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/dataConfig/CommonTTLTimeConfig.java

@@ -14,7 +14,8 @@ public class CommonTTLTimeConfig {
     // 最大的心跳时间间隔 5分钟
     public static final Long maxUnPingOnLineTime = 5 * 60 * 1000L;
 
-    public static final Long maxDeviceRedisOnLineTime = 60 * 1000L;
+    // 默认心跳间隔时间
+    public static final Long maxDeviceRedisOnLineTime = 60L;
 
     public static final String OnLineExpiredKey = "expiredKey_";
 }

+ 8 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/base/RedisService.java

@@ -62,6 +62,14 @@ public class RedisService {
         }
     }
 
+    public void setValueSECONDS(String key, String value, long expirePeriod) {
+        try {
+            template.opsForValue().set(key, value, expirePeriod, TimeUnit.SECONDS);
+        } catch (Exception e) {
+            throw new ServiceException("系统故障,请联系服务商");
+        }
+    }
+
     public String getValue(String key) {
         try {
             return template.opsForValue().get(key);

+ 36 - 9
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/device/DevicePingInfoService.java

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