Browse Source

device redis缓存 改写

chejianzheng 3 tháng trước cách đây
mục cha
commit
e0b810686c

+ 44 - 33
device-service-domain/src/main/java/com/hfln/device/domain/service/impl/DeviceRedisManagerServiceImpl.java

@@ -11,10 +11,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Optional;
+import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
 
 /**
@@ -78,60 +75,60 @@ public class DeviceRedisManagerServiceImpl implements DeviceManagerService {
             installParam.setMountPlain(deviceMap.get("mountPlain").toString());
         }
         if (deviceMap.get("isCeiling") != null) {
-            installParam.setIsCeiling(Integer.parseInt(deviceMap.get("isCeiling").toString()));
+            installParam.setIsCeiling((int) deviceMap.get("isCeiling"));
         }
         if (deviceMap.get("height") != null) {
-            installParam.setHeight(Float.parseFloat(deviceMap.get("height").toString()));
+            installParam.setHeight((float) deviceMap.get("height"));
         }
         Device.TrackingRegion trackingRegion = new Device.TrackingRegion();
         if (deviceMap.get("startX") != null) {
-            trackingRegion.setStartX(Integer.parseInt(deviceMap.get("startX").toString()));
+            trackingRegion.setStartX((int) deviceMap.get("startX"));
         }
         if (deviceMap.get("startY") != null) {
-            trackingRegion.setStartY(Integer.parseInt(deviceMap.get("startY").toString()));
+            trackingRegion.setStartY((int) deviceMap.get("startY"));
         }
         if (deviceMap.get("startZ") != null) {
-            trackingRegion.setStartZ(Integer.parseInt(deviceMap.get("startZ").toString()));
+            trackingRegion.setStartZ((int) deviceMap.get("startZ"));
         }
         if (deviceMap.get("stopX") != null) {
-            trackingRegion.setStopX(Integer.parseInt(deviceMap.get("stopX").toString()));
+            trackingRegion.setStopX((int) deviceMap.get("stopX"));
         }
         if (deviceMap.get("stopY") != null) {
-            trackingRegion.setStopY(Integer.parseInt(deviceMap.get("stopY").toString()));
+            trackingRegion.setStopY((int) deviceMap.get("stopY"));
         }
         if (deviceMap.get("stopZ") != null) {
-            trackingRegion.setStopZ(Integer.parseInt(deviceMap.get("stopZ").toString()));
+            trackingRegion.setStopZ((int) deviceMap.get("stopZ"));
         }
         installParam.setTrackingRegion(trackingRegion);
         device.setInstallParam(installParam);
 
         if (deviceMap.get("online") != null) {
-            device.setOnline(Integer.parseInt(deviceMap.get("online").toString()));
+            device.setOnline((int) deviceMap.get("online"));
         }
         if (deviceMap.get("keepAliveTime") != null) {
-            device.setKeepaliveTime(Long.parseLong(deviceMap.get("keepAliveTime").toString()));
+            device.setKeepaliveTime((long) deviceMap.get("keepAliveTime"));
         }
         if (deviceMap.get("expireTime") != null) {
-            device.setKeepaliveTime(Long.parseLong(deviceMap.get("expireTime").toString()));
+            device.setKeepaliveTime((long) deviceMap.get("expireTime"));
         }
 
         if (deviceMap.get("lastReportFallTime") != null) {
-            device.setLastReportFallTime(Long.parseLong(deviceMap.get("lastReportFallTime").toString()));
+            device.setLastReportFallTime((long) deviceMap.get("lastReportFallTime"));
         }
         if (deviceMap.get("alarmInterval") != null) {
-            device.setAlarmInterval(Long.parseLong(deviceMap.get("alarmInterval").toString()));
+            device.setAlarmInterval((long) deviceMap.get("alarmInterval"));
         }
         if (deviceMap.get("alarmAck") != null) {
-            device.setAlarmAck(Boolean.parseBoolean(deviceMap.get("alarmAck").toString()));
+            device.setAlarmAck((boolean) deviceMap.get("alarmAck"));
         }
         if (deviceMap.get("lastAlarmAckTime") != null) {
-            device.setLastAlarmAckTime(Long.parseLong(deviceMap.get("lastAlarmAckTime").toString()));
+            device.setLastAlarmAckTime((long) deviceMap.get("lastAlarmAckTime"));
         }
         if (deviceMap.get("alarmAckInterval") != null) {
-            device.setAlarmAckInterval(Long.parseLong(deviceMap.get("alarmAckInterval").toString()));
+            device.setAlarmAckInterval((long) deviceMap.get("alarmAckInterval"));
         }
         if (deviceMap.get("falling") != null) {
-            device.setFalling(Integer.valueOf(deviceMap.get("falling").toString()));
+            device.setFalling((int) deviceMap.get("falling"));
         }
 
         return Optional.ofNullable(device);
@@ -238,18 +235,32 @@ public class DeviceRedisManagerServiceImpl implements DeviceManagerService {
 
     @Override
     public void checkDeviceKeepAlive(long currentTimeMillis, long timeoutMillis) {
-        deviceCache.forEach((devId, device) -> {
-            // 只检查在线设备
-            if (device.getOnline() != null && device.getOnline() == 1) {
-                Long lastKeepAliveTime = device.getKeepaliveTime();
-                
-                // 如果设备无保活时间或超时,则设置为离线
-                if (lastKeepAliveTime == null || (currentTimeMillis - lastKeepAliveTime) > timeoutMillis) {
-                    log.info("Device keepalive timeout: {}, last keepalive: {}", devId, lastKeepAliveTime);
-                    deviceStatusService.handleDeviceOffline(device);
-                }
-            }
-        });
+
+        Set<Object> devIdSet = redisService.sMembers(RedisCacheConstant.KEY_DEVICE_ID);
+        if (CollectionUtils.isEmpty(devIdSet)) {
+            log.info("check device keepalive , no devices in cache");
+            return;
+        }
+
+//        for (Object devIdObj : devIdSet) {
+//            Object onlineObj = redisService.hGet(RedisCacheConstant.KEY_DEVICE_pre + devIdObj, "online");
+//            if (onlineObj != null && Integer.parseInt(onlineObj.toString()) == 1) {
+//                Long lastKeepAliveTime = redisService.hGet(RedisCacheConstant.KEY_DEVICE_pre + devIdObj, "keepAliveTime");
+//            }
+//        }
+//
+//        deviceCache.forEach((devId, device) -> {
+//            // 只检查在线设备
+//            if (device.getOnline() != null && device.getOnline() == 1) {
+//                Long lastKeepAliveTime = device.getKeepaliveTime();
+//
+//                // 如果设备无保活时间或超时,则设置为离线
+//                if (lastKeepAliveTime == null || (currentTimeMillis - lastKeepAliveTime) > timeoutMillis) {
+//                    log.info("Device keepalive timeout: {}, last keepalive: {}", devId, lastKeepAliveTime);
+//                    deviceStatusService.handleDeviceOffline(device);
+//                }
+//            }
+//        });
     }
 
     /**

+ 3 - 3
device-service-infrastructure/src/main/java/com/hfln/device/infrastructure/config/MqttConfig.java

@@ -154,12 +154,12 @@ public class MqttConfig {
     // 应用消息通道和适配器
     // ===========================================
 
-    @Bean
+//    @Bean
     public MessageChannel appInputChannel() {
         return new DirectChannel();
     }
 
-    @Bean
+//    @Bean
     public MessageProducer appInbound() {
         String[] topics = {
             MqttTopics.APP_FALL_EVENT_ACK,
@@ -178,7 +178,7 @@ public class MqttConfig {
         return adapter;
     }
 
-    @Bean
+//    @Bean
     @ServiceActivator(inputChannel = "appInputChannel")
     public MessageHandler appMqttMessageHandler(AppMessageHandler handler) {
         return new MessageHandler() {

+ 1 - 1
device-service-infrastructure/src/main/java/com/hfln/device/infrastructure/mqtt/handler/AppMessageHandler.java

@@ -35,7 +35,7 @@ import java.util.Optional;
  * @author 设备接入服务
  * @version 1.0
  */
-@Component
+//@Component
 @Slf4j
 public class AppMessageHandler {
     

+ 0 - 14
device-service-server/src/main/resources/bootstrap-local.yml

@@ -41,20 +41,6 @@ lnxx:
       basePackage: com.hfln.device.application.controller
       title: DEVICE-SERVICE-SERVER
       description: 设备服务
-  # 微信小程序
-  wechat:
-    appid: wx60b2cd643b46d5eb
-    secret: 15ebd7bed7b73d806eba2944f4e07592
-  # 短信验证码相关
-  sms:
-    tencent:
-      secretId: AKID40jFYdUCqMqFUXO2SecOvKYYKsGRP9rT
-      secretKey: Y3RcbMtO0V0bI2gzFShpocHjy1qSq0xf
-      loginId: 2368397
-      registerId: 2368393
-      notifyId: 2368474
-      region: ap-guangzhou
-      sdkAppId: 1400966707
 
 
 mqtt: