Bladeren bron

更新!

TRX 1 jaar geleden
bovenliggende
commit
1060cdc6b6

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

@@ -16,4 +16,5 @@ public class CommonTTLTimeConfig {
 
     public static final Long maxDeviceRedisOnLineTime = 60 * 1000L;
 
+    public static final String OnLineExpiredKey = "expiredKey_";
 }

+ 24 - 13
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/mqtt/DevicePingInfoService.java

@@ -154,19 +154,19 @@ public class DevicePingInfoService extends SuperService {
 
         entity.setDeviceId(deviceId);
         entity.setDeviceName(deviceName);
-        entity.setTTL(new Date(System.currentTimeMillis() + expireTime + 1000));
+        entity.setTTL(new Date(System.currentTimeMillis() + expireTime));
         entity.setTimeStr(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
         entity.setOnLineDeviceType(onLineDeviceType);
         deviceOnLineInfoDao.save(entity);
 
-        String expiredKey = String.format("expiredKey_%s", deviceId);
+        String expiredKey = String.format("%s%s", CommonTTLTimeConfig.OnLineExpiredKey, deviceId);
         redisService.setValue(expiredKey, deviceName, CommonTTLTimeConfig.maxDeviceRedisOnLineTime);
     }
 
     public void redisExpire(String expiredKey) {
-        if (StringUtils.isNotEmpty(expiredKey) && expiredKey.startsWith("expiredKey_")) {
+        if (StringUtils.isNotEmpty(expiredKey) && expiredKey.startsWith(CommonTTLTimeConfig.OnLineExpiredKey)) {
             // 设备在线状态
-            expiredKey = expiredKey.replace("expiredKey_", "");
+            expiredKey = expiredKey.replace(CommonTTLTimeConfig.OnLineExpiredKey, "");
             deviceTTLUnLine(expiredKey);
         }
     }
@@ -190,6 +190,8 @@ public class DevicePingInfoService extends SuperService {
                 DeviceInfo deviceInfo = deviceInfoDao.findTopByDeviceId(entity.getDeviceId());
                 deviceCheckOnLineState(deviceInfo);
             }
+            String expiredKey = String.format("%s%s", CommonTTLTimeConfig.OnLineExpiredKey, entity.getDeviceId());
+            redisService.setValue(expiredKey, entity.getDeviceName(), CommonTTLTimeConfig.maxDeviceRedisOnLineTime);
         }
     }
 
@@ -239,13 +241,18 @@ public class DevicePingInfoService extends SuperService {
         }
         if (onLineState != deviceInfo.getOnLineState()) {
             log.info("设备在线状态改变:{} {}", deviceInfo.getDeviceName(), onLineState);
-            Map<String, Object> standardData = new HashMap<String, Object>();
-            standardData.put("id", deviceInfo.getId());
-            standardData.put("onLineState", onLineState);
-            commonService.updateData(standardData, DeviceInfo.class.getSimpleName());
-            deviceSyncFullCardService.noticeSyncDeviceOnlineStateChange(deviceInfo.getDeviceId());
+            updateDeviceOnLine(deviceInfo.getId(), deviceInfo.getDeviceId(), onLineState);
         }
+    }
 
+    private void updateDeviceOnLine(String id, String deviceId, OnLineState onLineState) {
+        Map<String, Object> standardData = new HashMap<String, Object>();
+        standardData.put("id", id);
+        standardData.put("onLineState", onLineState);
+        commonService.updateData(standardData, DeviceInfo.class.getSimpleName());
+
+        // 通知业务平台
+        deviceSyncFullCardService.noticeSyncDeviceOnlineStateChange(deviceId);
     }
 
     /**
@@ -312,11 +319,15 @@ public class DevicePingInfoService extends SuperService {
 
         if (onLineState != gateWayInfo.getOnLineState()) {
             log.info("网关在线状态改变:{} {}", gateWayInfo.getGateWayName(), onLineState);
-            Map<String, Object> standardData = new HashMap<String, Object>();
-            standardData.put("id", gateWayInfo.getId());
-            standardData.put("onLineState", onLineState);
-            commonService.updateData(standardData, GateWayInfo.class.getSimpleName());
+            updateGateWayOnLine(gateWayInfo.getId(), onLineState);
         }
     }
 
+    private void updateGateWayOnLine(String id, OnLineState onLineState) {
+        Map<String, Object> standardData = new HashMap<String, Object>();
+        standardData.put("id", id);
+        standardData.put("onLineState", onLineState);
+        commonService.updateData(standardData, GateWayInfo.class.getSimpleName());
+    }
+
 }