Browse Source

device redis缓存 改写

chejianzheng 2 months ago
parent
commit
d5d748e8ea

+ 6 - 0
device-service-application/src/main/java/com/hfln/device/application/service/impl/DeviceEventServiceExtendImpl.java

@@ -96,6 +96,12 @@ public class DeviceEventServiceExtendImpl implements DeviceEventServiceExtend, D
     // ========== DeviceEventPort接口方法空实现 ==========
     // ========== DeviceEventPort接口方法空实现 ==========
     @Override
     @Override
     public void handleDeviceLogin(String deviceId, Map<String, Object> deviceInfo, Map<String, Object> fullPayload) {}
     public void handleDeviceLogin(String deviceId, Map<String, Object> deviceInfo, Map<String, Object> fullPayload) {}
+
+    @Override
+    public void handleDeviceDisconnect(String deviceId, Map<String, Object> fullPayload) {
+
+    }
+
     @Override
     @Override
     public void handleDeviceKeepAlive(String deviceId) {}
     public void handleDeviceKeepAlive(String deviceId) {}
     @Override
     @Override

+ 21 - 8
device-service-application/src/main/java/com/hfln/device/application/service/impl/DeviceEventServiceImpl.java

@@ -3,6 +3,7 @@ package com.hfln.device.application.service.impl;
 import com.alibaba.fastjson2.JSON;
 import com.alibaba.fastjson2.JSON;
 import com.hfln.device.application.service.DeviceEventService;
 import com.hfln.device.application.service.DeviceEventService;
 import com.hfln.device.application.service.DebugConfigService;
 import com.hfln.device.application.service.DebugConfigService;
+import com.hfln.device.common.constant.redis.RedisCacheConstant;
 import com.hfln.device.domain.entity.Device;
 import com.hfln.device.domain.entity.Device;
 import com.hfln.device.domain.gateway.DeviceGateway;
 import com.hfln.device.domain.gateway.DeviceGateway;
 import com.hfln.device.domain.gateway.MqttGateway;
 import com.hfln.device.domain.gateway.MqttGateway;
@@ -23,13 +24,7 @@ import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 import org.springframework.util.CollectionUtils;
 
 
 import javax.annotation.Resource;
 import javax.annotation.Resource;
-import java.util.Arrays;
-import java.util.Calendar;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
+import java.util.*;
 import java.util.stream.Collectors;
 import java.util.stream.Collectors;
 
 
 /**
 /**
@@ -319,7 +314,25 @@ public class DeviceEventServiceImpl implements DeviceEventService {
             log.error("处理设备登录异常: deviceId={}, error={}", deviceId, e.getMessage(), e);
             log.error("处理设备登录异常: deviceId={}, error={}", deviceId, e.getMessage(), e);
         }
         }
     }
     }
-    
+
+    @Override
+    public void handleDeviceDisconnect(String deviceId, Map<String, Object> fullPayload) {
+
+        log.info("处理设备下线事件: deviceId={}", deviceId);
+
+        boolean exist = deviceManagerService.existInCache(deviceId);
+        if (!exist) {
+            log.info("check device exist , no devices in cache");
+            return;
+        }
+
+        Map<String, Object> map = new HashMap<>();
+        map.put("online", 0);
+        deviceManagerService.updateDeviceMapInCache(deviceId, map);
+
+        deviceGateway.updateDeviceOfflineStatus(deviceId);
+    }
+
     /**
     /**
      * 处理设备心跳保活事件
      * 处理设备心跳保活事件
      * 对应Python版本的deal_dev_keepalive方法
      * 对应Python版本的deal_dev_keepalive方法

+ 1 - 0
device-service-common/src/main/java/com/hfln/device/common/constant/mqtt/topic/MqttTopics.java

@@ -24,6 +24,7 @@ public class MqttTopics {
     public static final String DEV_REP_FALL_EVENT = "/dev/+/report_falling_event";
     public static final String DEV_REP_FALL_EVENT = "/dev/+/report_falling_event";
     public static final String DEV_REP_PRES_EVENT = "/dev/+/report_presence_event";
     public static final String DEV_REP_PRES_EVENT = "/dev/+/report_presence_event";
     public static final String DEV_DSP_DATA = "/dev/+/dsp_data";
     public static final String DEV_DSP_DATA = "/dev/+/dsp_data";
+    public static final String DEV_DISCONNECT = "/dev/+/disconnect";
     public static final String DEV_UPDATE_FIRMWARE = "/dev/+/update_firmware";
     public static final String DEV_UPDATE_FIRMWARE = "/dev/+/update_firmware";
     public static final String DEV_REBOOT = "/dev/+/reboot";
     public static final String DEV_REBOOT = "/dev/+/reboot";
     public static final String DEV_REP_DEBUG_PARAM = "/dev/+/report_debug_param";
     public static final String DEV_REP_DEBUG_PARAM = "/dev/+/report_debug_param";

+ 3 - 0
device-service-domain/src/main/java/com/hfln/device/domain/gateway/DeviceGateway.java

@@ -69,6 +69,9 @@ public interface DeviceGateway {
      */
      */
     boolean updateDeviceOnlineStatus(String devId, Integer online);
     boolean updateDeviceOnlineStatus(String devId, Integer online);
 
 
+
+    boolean updateDeviceOfflineStatus(String devId);
+
     /**
     /**
      * 更新设备保活时间
      * 更新设备保活时间
      * @param devId 设备ID
      * @param devId 设备ID

+ 2 - 1
device-service-domain/src/main/java/com/hfln/device/domain/port/DeviceEventPort.java

@@ -21,7 +21,8 @@ public interface DeviceEventPort {
      * @param fullPayload 完整的消息载荷
      * @param fullPayload 完整的消息载荷
      */
      */
     void handleDeviceLogin(String deviceId, Map<String, Object> deviceInfo, Map<String, Object> fullPayload);
     void handleDeviceLogin(String deviceId, Map<String, Object> deviceInfo, Map<String, Object> fullPayload);
-    
+    void handleDeviceDisconnect(String deviceId, Map<String, Object> fullPayload);
+
     /**
     /**
      * 处理设备保活事件
      * 处理设备保活事件
      * 对应Python版本的deal_dev_keepalive方法
      * 对应Python版本的deal_dev_keepalive方法

+ 2 - 15
device-service-domain/src/main/java/com/hfln/device/domain/service/impl/DeviceRedisManagerServiceImpl.java

@@ -449,24 +449,11 @@ public class DeviceRedisManagerServiceImpl implements DeviceManagerService {
                 if (lastKeepAliveTime == null || (currentTimeMillis - lastKeepAliveTime) > timeoutMillis) {
                 if (lastKeepAliveTime == null || (currentTimeMillis - lastKeepAliveTime) > timeoutMillis) {
                     log.info("Device keepalive timeout: {}, last keepalive: {}", devIdObj, lastKeepAliveTime);
                     log.info("Device keepalive timeout: {}, last keepalive: {}", devIdObj, lastKeepAliveTime);
                     redisService.hSet(RedisCacheConstant.KEY_DEVICE_pre + devIdObj, "online", 0);
                     redisService.hSet(RedisCacheConstant.KEY_DEVICE_pre + devIdObj, "online", 0);
-                    // todo 更新数据库 设备状态
-                    deviceGateway.updateDeviceOnlineStatus((String) devIdObj, 0);
+                    // 更新数据库 设备状态
+                    deviceGateway.updateDeviceOfflineStatus((String) devIdObj);
                 }
                 }
             }
             }
         }
         }
-
-//        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);
-//                }
-//            }
-//        });
     }
     }
 
 
     /**
     /**

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

@@ -123,6 +123,7 @@ public class MqttConfig {
 //            MqttTopics.DEV_CLOUDPOINT,
 //            MqttTopics.DEV_CLOUDPOINT,
 //            MqttTopics.DEV_REP_DEV_INFO,
 //            MqttTopics.DEV_REP_DEV_INFO,
             MqttTopics.DEV_REP_DEV_PARAM,
             MqttTopics.DEV_REP_DEV_PARAM,
+            MqttTopics.DEV_DISCONNECT,
 //            MqttTopics.DEV_REP_FALL_EVENT,
 //            MqttTopics.DEV_REP_FALL_EVENT,
 //            MqttTopics.DEV_REP_PRES_EVENT,
 //            MqttTopics.DEV_REP_PRES_EVENT,
 //            MqttTopics.DEV_SET_DEBUG,
 //            MqttTopics.DEV_SET_DEBUG,

+ 43 - 0
device-service-infrastructure/src/main/java/com/hfln/device/infrastructure/gateway/impl/DeviceGatewayImpl.java

@@ -541,6 +541,49 @@ public class DeviceGatewayImpl implements DeviceGateway {
     }
     }
 
 
     @Override
     @Override
+    public boolean updateDeviceOfflineStatus(String devId) {
+        try {
+            log.info("更新设备在线状态: 设备={}", devId);
+
+            // 参数校验
+            if (StringUtils.isBlank(devId)) {
+                log.warn("更新设备在线状态参数无效: 设备={}, 状态={}", devId);
+                return false;
+            }
+
+            // 更新设备在线状态
+            boolean updateResult = devInfoService.update(
+                    Wrappers.<DevInfo>lambdaUpdate()
+                            .set(DevInfo::getOnline, 0)
+                            .set(DevInfo::getOfflineTime, LocalDateTime.now())
+                            .eq(DevInfo::getClientId, devId));
+
+            if (!updateResult) {
+                log.warn("更新设备在线状态失败: 设备不存在或更新失败, ID={}", devId);
+                return false;
+            }
+
+            // 获取更新后的设备信息
+            DevInfo devInfo = devInfoService.getOne(
+                    Wrappers.<DevInfo>lambdaQuery()
+                            .eq(DevInfo::getClientId, devId));
+
+            if (devInfo != null) {
+                // 转换为领域对象
+                Device device = convertToDevice(devInfo);
+
+                // 发送设备状态更新消息
+                mqttHandler.sendDeviceStatusUpdate(device);
+            }
+
+            return true;
+        } catch (Exception e) {
+            log.error("更新设备在线状态失败: {}, {}", devId, e.getMessage(), e);
+            return false;
+        }
+    }
+
+    @Override
     public boolean updateDeviceKeepAliveTime(String devId, Long keepaliveTime) {
     public boolean updateDeviceKeepAliveTime(String devId, Long keepaliveTime) {
         try {
         try {
             log.info("更新设备保活时间: 设备={}, 时间={}", devId, keepaliveTime);
             log.info("更新设备保活时间: 设备={}, 时间={}", devId, keepaliveTime);

+ 469 - 469
device-service-infrastructure/src/main/java/com/hfln/device/infrastructure/gateway/impl/DeviceGatewaySimpleImpl.java

@@ -1,469 +1,469 @@
-package com.hfln.device.infrastructure.gateway.impl;
-
-import com.hfln.device.common.dto.data.device.DeviceDTO;
-import com.hfln.device.common.dto.data.event.EventListDTO;
-import com.hfln.device.common.dto.data.home.HomeInfoDTO;
-import com.hfln.device.common.request.device.DeviceBandingParams;
-import com.hfln.device.common.request.device.DeviceListParams;
-import com.hfln.device.common.request.device.DeviceLocationParams;
-import com.hfln.device.common.request.event.EventListParams;
-import com.hfln.device.common.vo.PageRecord;
-import com.hfln.device.domain.entity.Device;
-import com.hfln.device.domain.entity.Region;
-import com.hfln.device.domain.gateway.DeviceGateway;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.stereotype.Service;
-
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
-import java.util.HashMap;
-import java.util.ArrayList;
-import java.util.UUID;
-
-/**
- * 设备网关简单实现类
- * 用于提供基本功能实现
- */
-@Service("deviceGatewaySimple")
-@Slf4j
-public class DeviceGatewaySimpleImpl implements DeviceGateway {
-
-    @Override
-    public List<Device> findAllDevices() {
-        log.info("查询所有设备");
-        return Collections.emptyList();
-    }
-
-    @Override
-    public Optional<Device> findDeviceById(String devId) {
-        log.info("根据ID查询设备: {}", devId);
-        return Optional.empty();
-    }
-
-    @Override
-    public Device saveDevice(Device device) {
-        log.info("保存设备: {}", device.getDevId());
-        return device;
-    }
-
-    @Override
-    public boolean updateDeviceOnlineStatus(String devId, Integer online) {
-        log.info("更新设备在线状态: {}, 状态: {}", devId, online);
-        return true;
-    }
-
-    @Override
-    public boolean updateDeviceKeepAliveTime(String devId, Long keepaliveTime) {
-        log.info("更新设备保活时间: {}, 时间: {}", devId, keepaliveTime);
-        return true;
-    }
-
-    @Override
-    public boolean recordDeviceStayTime(String devId, Long enterTime, Long leaveTime, String stayTime) {
-        log.info("记录设备停留时间: {}", devId);
-        return true;
-    }
-
-    @Override
-    public boolean recordDeviceRetentionAlarm(String devId, Long alarmTime, String alarmType, String description) {
-        log.info("记录设备滞留告警: {}, 类型: {}", devId, alarmType);
-        return true;
-    }
-
-    @Override
-    public HomeInfoDTO queryHomeInfo(Long userId) {
-        log.info("查询用户首页信息: {}", userId);
-        return new HomeInfoDTO();
-    }
-
-    @Override
-    public List<DeviceDTO> queryDeviceList(DeviceListParams request) {
-        log.info("查询设备列表: {}", request);
-        return Collections.emptyList();
-    }
-
-    @Override
-    public Boolean deviceUnBind(Long userId, String deviceId) {
-        log.info("解绑设备: {}, 用户: {}", deviceId, userId);
-        return true;
-    }
-
-    @Override
-    public Boolean deviceBind(DeviceBandingParams request) {
-        log.info("绑定设备: {}", request);
-        return true;
-    }
-
-    @Override
-    public DeviceDTO queryDeviceById(String deviceId) {
-        log.info("查询设备详情: {}", deviceId);
-        return new DeviceDTO();
-    }
-
-    @Override
-    public PageRecord<EventListDTO> queryEventByPage(EventListParams request) {
-        log.info("分页查询事件: {}", request);
-        return new PageRecord<>();
-    }
-
-    @Override
-    public Boolean handleEvent(Long eventId) {
-        log.info("处理事件: {}", eventId);
-        return true;
-    }
-
-    @Override
-    public Boolean updateDevice(DeviceBandingParams request) {
-        log.info("更新设备: {}", request);
-        return true;
-    }
-
-    @Override
-    public Boolean updateDeviceLocation(DeviceLocationParams params) {
-        log.info("更新设备位置: {}", params);
-        return true;
-    }
-
-    @Override
-    public boolean updateDeviceNetwork(String deviceId, Device.NetworkInfo networkInfo) {
-        log.info("更新设备网络配置: {}", deviceId);
-        return true;
-    }
-
-    @Override
-    public boolean updateDeviceInstallParam(String deviceId, Device.InstallParam installParam) {
-        log.info("更新设备安装参数: {}", deviceId);
-        return true;
-    }
-
-    @Override
-    public boolean updateDeviceAlarmSchedule(String deviceId, Map<String, Object> alarmSchedule) {
-        log.info("更新设备告警计划: {}", deviceId);
-        return true;
-    }
-    
-    @Override
-    public boolean checkDeviceExists(String deviceId) {
-        log.info("检查设备是否存在: {}", deviceId);
-        return true;
-    }
-    
-    @Override
-    public boolean isDeviceBound(String deviceId) {
-        log.info("检查设备是否已绑定: {}", deviceId);
-        return false;
-    }
-    
-    @Override
-    public boolean isUserDevice(String deviceId, Long userId) {
-        log.info("检查设备是否属于指定用户: {}, 用户: {}", deviceId, userId);
-        return true;
-    }
-    
-    @Override
-    public boolean bindDevice(String deviceId, Long userId) {
-        log.info("绑定设备到用户: {}, 用户: {}", deviceId, userId);
-        return true;
-    }
-    
-    @Override
-    public boolean unbindDevice(String deviceId, Long userId) {
-        log.info("解绑用户设备: {}, 用户: {}", deviceId, userId);
-        return true;
-    }
-    
-    /**
-     * 保存告警配置
-     * 
-     * @param globalConfig 全局配置
-     * @return 是否保存成功
-     */
-    @Override
-    public boolean saveAlarmConfig(Map<String, Object> globalConfig) {
-        log.info("保存告警配置: {}", globalConfig);
-        return true;
-    }
-    
-    /**
-     * 创建设备
-     * 
-     * @param deviceId 设备ID
-     * @param deviceInfo 设备信息
-     * @return 创建的设备
-     */
-    @Override
-    public Device createDevice(String deviceId, Map<String, Object> deviceInfo) {
-        log.info("创建设备: {}, 信息: {}", deviceId, deviceInfo);
-        Device device = new Device();
-        device.setDevId(deviceId);
-        return device;
-    }
-    
-    /**
-     * 更新设备信息
-     * 
-     * @param deviceId 设备ID
-     * @param deviceInfo 设备信息
-     * @return 是否更新成功
-     */
-    @Override
-    public boolean updateDeviceInfo(String deviceId, Map<String, Object> deviceInfo) {
-        log.info("更新设备信息: {}, 信息: {}", deviceId, deviceInfo);
-        return true;
-    }
-    
-    /**
-     * 更新告警状态
-     * 
-     * @param deviceId 设备ID
-     * @param eventId 事件ID
-     * @param status 状态
-     * @return 是否更新成功
-     */
-    @Override
-    public boolean updateAlarmStatus(String deviceId, Long eventId, int status) {
-        log.info("更新告警状态: 设备={}, 事件={}, 状态={}", deviceId, eventId, status);
-        return true;
-    }
-    
-    /**
-     * 记录跌倒事件
-     * 
-     * @param deviceId 设备ID
-     * @param pose 姿态
-     * @param targetPoint 目标点
-     * @return 事件ID
-     */
-    @Override
-    public Long recordFallEvent(String deviceId, int pose, List<Float> targetPoint) {
-        log.info("记录跌倒事件: 设备={}, 姿态={}", deviceId, pose);
-        return 1L;
-    }
-    
-    /**
-     * 记录事件
-     * 
-     * @param deviceId 设备ID
-     * @param event 事件类型
-     * @param pose 姿态
-     * @param targetPoint 目标点
-     * @return 是否记录成功
-     */
-    @Override
-    public boolean recordEvent(String deviceId, String event, Integer pose, List<Float> targetPoint) {
-        return recordEvent(deviceId, event, pose, targetPoint, System.currentTimeMillis());
-    }
-    
-    /**
-     * 记录存在事件
-     * 
-     * @param deviceId 设备ID
-     * @param event 事件类型
-     * @return 是否记录成功
-     */
-    @Override
-    public boolean recordExistEvent(String deviceId, String event) {
-        return recordExistEvent(deviceId, event, System.currentTimeMillis());
-    }
-    
-    /**
-     * 记录告警事件
-     * 
-     * @param deviceId 设备ID
-     * @param desc 描述
-     * @param table 表名
-     * @param tableId 表ID
-     * @return 是否记录成功
-     */
-    @Override
-    public boolean recordAlarmEvent(String deviceId, String desc, String table, Integer tableId) {
-        log.info("记录告警事件: 设备={}, 描述={}, 表={}, 表ID={}", deviceId, desc, table, tableId);
-        return true;
-    }
-    
-    @Override
-    public Device updateDevice(Device device) {
-        log.info("更新设备: {}", device.getDevId());
-        return device;
-    }
-    
-    @Override
-    public List<Device> getAllDevices() {
-        log.info("获取所有设备信息");
-        return Collections.emptyList();
-    }
-    
-    @Override
-    public Device getDeviceById(String deviceId) {
-        log.info("根据ID获取设备: {}", deviceId);
-        return null;
-    }
-
-    @Override
-    public HomeInfoDTO getHomeInfo(String homeId) {
-        log.info("查询家庭信息: homeId={}", homeId);
-        return new HomeInfoDTO();
-    }
-
-    @Override
-    public EventListDTO queryDeviceEvents(EventListParams params) {
-        log.info("查询设备事件: {}", params);
-        return new EventListDTO();
-    }
-
-    @Override
-    public boolean deviceReset(String deviceId) {
-        log.info("重置设备: {}", deviceId);
-        return true;
-    }
-
-    @Override
-    public boolean deviceReboot(String deviceId) {
-        log.info("重启设备: {}", deviceId);
-        return true;
-    }
-
-    @Override
-    public boolean recordBehaviorData(String deviceId, String dataType, Map<String, Object> data) {
-        log.info("记录行为数据: deviceId={}, dataType={}", deviceId, dataType);
-        return true;
-    }
-
-    @Override
-    public boolean recordRestBehavior(String deviceId, Long duration, List<Float> location, Long timestamp) {
-        log.info("记录休息行为: deviceId={}, duration={}, timestamp={}", deviceId, duration, timestamp);
-        return true;
-    }
-    
-    @Override
-    public boolean recordAbnormalBehavior(String deviceId, String behaviorType, String description, Double confidence, Long timestamp) {
-        log.info("记录异常行为: deviceId={}, type={}, desc={}, timestamp={}", deviceId, behaviorType, description, timestamp);
-        return true;
-    }
-    
-    @Override
-    public boolean recordNormalBehavior(String deviceId, Double confidence, Long timestamp) {
-        log.info("记录正常行为: deviceId={}, confidence={}, timestamp={}", deviceId, confidence, timestamp);
-        return true;
-    }
-    
-    @Override
-    public boolean recordEvent(String deviceId, String event, Integer pose, List<Float> targetPoint, Long timestamp) {
-        log.info("记录事件: deviceId={}, event={}, pose={}, timestamp={}", deviceId, event, pose, timestamp);
-        return true;
-    }
-    
-    @Override
-    public boolean recordAlarmEvent(String deviceId, String desc, String table, Integer tableId, Long timestamp) {
-        log.info("记录告警事件: deviceId={}, desc={}, timestamp={}", deviceId, desc, timestamp);
-        return true;
-    }
-    
-    @Override
-    public boolean recordExistEvent(String deviceId, String event, Long timestamp) {
-        log.info("记录存在事件: deviceId={}, event={}, timestamp={}", deviceId, event, timestamp);
-        return true;
-    }
-    
-    @Override
-    public boolean recordPointCloud(String deviceId, List<List<Float>> pointCloud, Long timestamp) {
-        log.info("记录点云数据: deviceId={}, timestamp={}", deviceId, timestamp);
-        return true;
-    }
-    
-    @Override
-    public boolean recordRealtimePosition(String deviceId, Integer pose, List<List<Float>> positions, Long timestamp) {
-        log.info("记录实时位置: deviceId={}, pose={}, timestamp={}", deviceId, pose, timestamp);
-        return true;
-    }
-    
-    @Override
-    public Map<String, Object> getDeviceStatus(String deviceId) {
-        log.info("获取设备状态: deviceId={}", deviceId);
-        return new HashMap<>();
-    }
-    
-    @Override
-    public Map<String, Object> getDeviceParams(String deviceId) {
-        log.info("获取设备参数: deviceId={}", deviceId);
-        return new HashMap<>();
-    }
-    
-    @Override
-    public boolean updateDeviceParams(String deviceId, Map<String, Object> params) {
-        log.info("更新设备参数: deviceId={}", deviceId);
-        return true;
-    }
-
-    @Override
-    public boolean recordActivityBehavior(String deviceId, Integer activityLevel, Long duration, Long timestamp) {
-        log.info("记录活动行为: deviceId={}, activityLevel={}, duration={}, timestamp={}", deviceId, activityLevel, duration, timestamp);
-        return true;
-    }
-
-    @Override
-    public PageRecord<DeviceDTO> queryDevices(DeviceListParams params) {
-        log.info("查询设备列表: {}", params);
-        return new PageRecord<>();
-    }
-
-    @Override
-    public boolean updateAlarmAckStatus(Long alarmId, Long userId) {
-        log.info("更新告警确认状态: {}, 用户: {}", alarmId, userId);
-        return true;
-    }
-    
-    @Override
-    public List<Map<String, Object>> getUnacknowledgedAlarms(String deviceId) {
-        log.info("获取设备未确认的告警: {}", deviceId);
-        return Collections.emptyList();
-    }
-    
-    @Override
-    public Long recordAlarmEventWithSeverity(String deviceId, String desc, String table, Integer severity, Long timestamp) {
-        log.info("记录告警事件(带告警级别): {}, 描述: {}, 级别: {}", deviceId, desc, severity);
-        // 返回模拟的告警ID
-        return timestamp != null ? timestamp : System.currentTimeMillis();
-    }
-
-    @Override
-    public boolean saveRegion(Region region) {
-        log.info("保存区域信息: {}", region.getName());
-        return true;
-    }
-    
-    @Override
-    public boolean updateRegion(Region region) {
-        log.info("更新区域信息: {}", region.getId());
-        return true;
-    }
-    
-    @Override
-    public Region getRegionById(String regionId) {
-        log.info("根据ID获取区域: {}", regionId);
-        Region region = new Region();
-        region.setId(regionId);
-        region.setName("模拟区域");
-        return region;
-    }
-    
-    @Override
-    public List<Region> getRegionsByDeviceId(String deviceId) {
-        log.info("获取设备的区域列表: {}", deviceId);
-        return new ArrayList<>();
-    }
-    
-    @Override
-    public boolean deleteRegion(String regionId) {
-        log.info("删除区域: {}", regionId);
-        return true;
-    }
-    
-    @Override
-    public boolean deleteDevice(String deviceId) {
-        log.info("删除设备: {}", deviceId);
-        return true;
-    }
-} 
+//package com.hfln.device.infrastructure.gateway.impl;
+//
+//import com.hfln.device.common.dto.data.device.DeviceDTO;
+//import com.hfln.device.common.dto.data.event.EventListDTO;
+//import com.hfln.device.common.dto.data.home.HomeInfoDTO;
+//import com.hfln.device.common.request.device.DeviceBandingParams;
+//import com.hfln.device.common.request.device.DeviceListParams;
+//import com.hfln.device.common.request.device.DeviceLocationParams;
+//import com.hfln.device.common.request.event.EventListParams;
+//import com.hfln.device.common.vo.PageRecord;
+//import com.hfln.device.domain.entity.Device;
+//import com.hfln.device.domain.entity.Region;
+//import com.hfln.device.domain.gateway.DeviceGateway;
+//import lombok.extern.slf4j.Slf4j;
+//import org.springframework.stereotype.Service;
+//
+//import java.util.Collections;
+//import java.util.List;
+//import java.util.Map;
+//import java.util.Optional;
+//import java.util.HashMap;
+//import java.util.ArrayList;
+//import java.util.UUID;
+//
+///**
+// * 设备网关简单实现类
+// * 用于提供基本功能实现
+// */
+//@Service("deviceGatewaySimple")
+//@Slf4j
+//public class DeviceGatewaySimpleImpl implements DeviceGateway {
+//
+//    @Override
+//    public List<Device> findAllDevices() {
+//        log.info("查询所有设备");
+//        return Collections.emptyList();
+//    }
+//
+//    @Override
+//    public Optional<Device> findDeviceById(String devId) {
+//        log.info("根据ID查询设备: {}", devId);
+//        return Optional.empty();
+//    }
+//
+//    @Override
+//    public Device saveDevice(Device device) {
+//        log.info("保存设备: {}", device.getDevId());
+//        return device;
+//    }
+//
+//    @Override
+//    public boolean updateDeviceOnlineStatus(String devId, Integer online) {
+//        log.info("更新设备在线状态: {}, 状态: {}", devId, online);
+//        return true;
+//    }
+//
+//    @Override
+//    public boolean updateDeviceKeepAliveTime(String devId, Long keepaliveTime) {
+//        log.info("更新设备保活时间: {}, 时间: {}", devId, keepaliveTime);
+//        return true;
+//    }
+//
+//    @Override
+//    public boolean recordDeviceStayTime(String devId, Long enterTime, Long leaveTime, String stayTime) {
+//        log.info("记录设备停留时间: {}", devId);
+//        return true;
+//    }
+//
+//    @Override
+//    public boolean recordDeviceRetentionAlarm(String devId, Long alarmTime, String alarmType, String description) {
+//        log.info("记录设备滞留告警: {}, 类型: {}", devId, alarmType);
+//        return true;
+//    }
+//
+//    @Override
+//    public HomeInfoDTO queryHomeInfo(Long userId) {
+//        log.info("查询用户首页信息: {}", userId);
+//        return new HomeInfoDTO();
+//    }
+//
+//    @Override
+//    public List<DeviceDTO> queryDeviceList(DeviceListParams request) {
+//        log.info("查询设备列表: {}", request);
+//        return Collections.emptyList();
+//    }
+//
+//    @Override
+//    public Boolean deviceUnBind(Long userId, String deviceId) {
+//        log.info("解绑设备: {}, 用户: {}", deviceId, userId);
+//        return true;
+//    }
+//
+//    @Override
+//    public Boolean deviceBind(DeviceBandingParams request) {
+//        log.info("绑定设备: {}", request);
+//        return true;
+//    }
+//
+//    @Override
+//    public DeviceDTO queryDeviceById(String deviceId) {
+//        log.info("查询设备详情: {}", deviceId);
+//        return new DeviceDTO();
+//    }
+//
+//    @Override
+//    public PageRecord<EventListDTO> queryEventByPage(EventListParams request) {
+//        log.info("分页查询事件: {}", request);
+//        return new PageRecord<>();
+//    }
+//
+//    @Override
+//    public Boolean handleEvent(Long eventId) {
+//        log.info("处理事件: {}", eventId);
+//        return true;
+//    }
+//
+//    @Override
+//    public Boolean updateDevice(DeviceBandingParams request) {
+//        log.info("更新设备: {}", request);
+//        return true;
+//    }
+//
+//    @Override
+//    public Boolean updateDeviceLocation(DeviceLocationParams params) {
+//        log.info("更新设备位置: {}", params);
+//        return true;
+//    }
+//
+//    @Override
+//    public boolean updateDeviceNetwork(String deviceId, Device.NetworkInfo networkInfo) {
+//        log.info("更新设备网络配置: {}", deviceId);
+//        return true;
+//    }
+//
+//    @Override
+//    public boolean updateDeviceInstallParam(String deviceId, Device.InstallParam installParam) {
+//        log.info("更新设备安装参数: {}", deviceId);
+//        return true;
+//    }
+//
+//    @Override
+//    public boolean updateDeviceAlarmSchedule(String deviceId, Map<String, Object> alarmSchedule) {
+//        log.info("更新设备告警计划: {}", deviceId);
+//        return true;
+//    }
+//
+//    @Override
+//    public boolean checkDeviceExists(String deviceId) {
+//        log.info("检查设备是否存在: {}", deviceId);
+//        return true;
+//    }
+//
+//    @Override
+//    public boolean isDeviceBound(String deviceId) {
+//        log.info("检查设备是否已绑定: {}", deviceId);
+//        return false;
+//    }
+//
+//    @Override
+//    public boolean isUserDevice(String deviceId, Long userId) {
+//        log.info("检查设备是否属于指定用户: {}, 用户: {}", deviceId, userId);
+//        return true;
+//    }
+//
+//    @Override
+//    public boolean bindDevice(String deviceId, Long userId) {
+//        log.info("绑定设备到用户: {}, 用户: {}", deviceId, userId);
+//        return true;
+//    }
+//
+//    @Override
+//    public boolean unbindDevice(String deviceId, Long userId) {
+//        log.info("解绑用户设备: {}, 用户: {}", deviceId, userId);
+//        return true;
+//    }
+//
+//    /**
+//     * 保存告警配置
+//     *
+//     * @param globalConfig 全局配置
+//     * @return 是否保存成功
+//     */
+//    @Override
+//    public boolean saveAlarmConfig(Map<String, Object> globalConfig) {
+//        log.info("保存告警配置: {}", globalConfig);
+//        return true;
+//    }
+//
+//    /**
+//     * 创建设备
+//     *
+//     * @param deviceId 设备ID
+//     * @param deviceInfo 设备信息
+//     * @return 创建的设备
+//     */
+//    @Override
+//    public Device createDevice(String deviceId, Map<String, Object> deviceInfo) {
+//        log.info("创建设备: {}, 信息: {}", deviceId, deviceInfo);
+//        Device device = new Device();
+//        device.setDevId(deviceId);
+//        return device;
+//    }
+//
+//    /**
+//     * 更新设备信息
+//     *
+//     * @param deviceId 设备ID
+//     * @param deviceInfo 设备信息
+//     * @return 是否更新成功
+//     */
+//    @Override
+//    public boolean updateDeviceInfo(String deviceId, Map<String, Object> deviceInfo) {
+//        log.info("更新设备信息: {}, 信息: {}", deviceId, deviceInfo);
+//        return true;
+//    }
+//
+//    /**
+//     * 更新告警状态
+//     *
+//     * @param deviceId 设备ID
+//     * @param eventId 事件ID
+//     * @param status 状态
+//     * @return 是否更新成功
+//     */
+//    @Override
+//    public boolean updateAlarmStatus(String deviceId, Long eventId, int status) {
+//        log.info("更新告警状态: 设备={}, 事件={}, 状态={}", deviceId, eventId, status);
+//        return true;
+//    }
+//
+//    /**
+//     * 记录跌倒事件
+//     *
+//     * @param deviceId 设备ID
+//     * @param pose 姿态
+//     * @param targetPoint 目标点
+//     * @return 事件ID
+//     */
+//    @Override
+//    public Long recordFallEvent(String deviceId, int pose, List<Float> targetPoint) {
+//        log.info("记录跌倒事件: 设备={}, 姿态={}", deviceId, pose);
+//        return 1L;
+//    }
+//
+//    /**
+//     * 记录事件
+//     *
+//     * @param deviceId 设备ID
+//     * @param event 事件类型
+//     * @param pose 姿态
+//     * @param targetPoint 目标点
+//     * @return 是否记录成功
+//     */
+//    @Override
+//    public boolean recordEvent(String deviceId, String event, Integer pose, List<Float> targetPoint) {
+//        return recordEvent(deviceId, event, pose, targetPoint, System.currentTimeMillis());
+//    }
+//
+//    /**
+//     * 记录存在事件
+//     *
+//     * @param deviceId 设备ID
+//     * @param event 事件类型
+//     * @return 是否记录成功
+//     */
+//    @Override
+//    public boolean recordExistEvent(String deviceId, String event) {
+//        return recordExistEvent(deviceId, event, System.currentTimeMillis());
+//    }
+//
+//    /**
+//     * 记录告警事件
+//     *
+//     * @param deviceId 设备ID
+//     * @param desc 描述
+//     * @param table 表名
+//     * @param tableId 表ID
+//     * @return 是否记录成功
+//     */
+//    @Override
+//    public boolean recordAlarmEvent(String deviceId, String desc, String table, Integer tableId) {
+//        log.info("记录告警事件: 设备={}, 描述={}, 表={}, 表ID={}", deviceId, desc, table, tableId);
+//        return true;
+//    }
+//
+//    @Override
+//    public Device updateDevice(Device device) {
+//        log.info("更新设备: {}", device.getDevId());
+//        return device;
+//    }
+//
+//    @Override
+//    public List<Device> getAllDevices() {
+//        log.info("获取所有设备信息");
+//        return Collections.emptyList();
+//    }
+//
+//    @Override
+//    public Device getDeviceById(String deviceId) {
+//        log.info("根据ID获取设备: {}", deviceId);
+//        return null;
+//    }
+//
+//    @Override
+//    public HomeInfoDTO getHomeInfo(String homeId) {
+//        log.info("查询家庭信息: homeId={}", homeId);
+//        return new HomeInfoDTO();
+//    }
+//
+//    @Override
+//    public EventListDTO queryDeviceEvents(EventListParams params) {
+//        log.info("查询设备事件: {}", params);
+//        return new EventListDTO();
+//    }
+//
+//    @Override
+//    public boolean deviceReset(String deviceId) {
+//        log.info("重置设备: {}", deviceId);
+//        return true;
+//    }
+//
+//    @Override
+//    public boolean deviceReboot(String deviceId) {
+//        log.info("重启设备: {}", deviceId);
+//        return true;
+//    }
+//
+//    @Override
+//    public boolean recordBehaviorData(String deviceId, String dataType, Map<String, Object> data) {
+//        log.info("记录行为数据: deviceId={}, dataType={}", deviceId, dataType);
+//        return true;
+//    }
+//
+//    @Override
+//    public boolean recordRestBehavior(String deviceId, Long duration, List<Float> location, Long timestamp) {
+//        log.info("记录休息行为: deviceId={}, duration={}, timestamp={}", deviceId, duration, timestamp);
+//        return true;
+//    }
+//
+//    @Override
+//    public boolean recordAbnormalBehavior(String deviceId, String behaviorType, String description, Double confidence, Long timestamp) {
+//        log.info("记录异常行为: deviceId={}, type={}, desc={}, timestamp={}", deviceId, behaviorType, description, timestamp);
+//        return true;
+//    }
+//
+//    @Override
+//    public boolean recordNormalBehavior(String deviceId, Double confidence, Long timestamp) {
+//        log.info("记录正常行为: deviceId={}, confidence={}, timestamp={}", deviceId, confidence, timestamp);
+//        return true;
+//    }
+//
+//    @Override
+//    public boolean recordEvent(String deviceId, String event, Integer pose, List<Float> targetPoint, Long timestamp) {
+//        log.info("记录事件: deviceId={}, event={}, pose={}, timestamp={}", deviceId, event, pose, timestamp);
+//        return true;
+//    }
+//
+//    @Override
+//    public boolean recordAlarmEvent(String deviceId, String desc, String table, Integer tableId, Long timestamp) {
+//        log.info("记录告警事件: deviceId={}, desc={}, timestamp={}", deviceId, desc, timestamp);
+//        return true;
+//    }
+//
+//    @Override
+//    public boolean recordExistEvent(String deviceId, String event, Long timestamp) {
+//        log.info("记录存在事件: deviceId={}, event={}, timestamp={}", deviceId, event, timestamp);
+//        return true;
+//    }
+//
+//    @Override
+//    public boolean recordPointCloud(String deviceId, List<List<Float>> pointCloud, Long timestamp) {
+//        log.info("记录点云数据: deviceId={}, timestamp={}", deviceId, timestamp);
+//        return true;
+//    }
+//
+//    @Override
+//    public boolean recordRealtimePosition(String deviceId, Integer pose, List<List<Float>> positions, Long timestamp) {
+//        log.info("记录实时位置: deviceId={}, pose={}, timestamp={}", deviceId, pose, timestamp);
+//        return true;
+//    }
+//
+//    @Override
+//    public Map<String, Object> getDeviceStatus(String deviceId) {
+//        log.info("获取设备状态: deviceId={}", deviceId);
+//        return new HashMap<>();
+//    }
+//
+//    @Override
+//    public Map<String, Object> getDeviceParams(String deviceId) {
+//        log.info("获取设备参数: deviceId={}", deviceId);
+//        return new HashMap<>();
+//    }
+//
+//    @Override
+//    public boolean updateDeviceParams(String deviceId, Map<String, Object> params) {
+//        log.info("更新设备参数: deviceId={}", deviceId);
+//        return true;
+//    }
+//
+//    @Override
+//    public boolean recordActivityBehavior(String deviceId, Integer activityLevel, Long duration, Long timestamp) {
+//        log.info("记录活动行为: deviceId={}, activityLevel={}, duration={}, timestamp={}", deviceId, activityLevel, duration, timestamp);
+//        return true;
+//    }
+//
+//    @Override
+//    public PageRecord<DeviceDTO> queryDevices(DeviceListParams params) {
+//        log.info("查询设备列表: {}", params);
+//        return new PageRecord<>();
+//    }
+//
+//    @Override
+//    public boolean updateAlarmAckStatus(Long alarmId, Long userId) {
+//        log.info("更新告警确认状态: {}, 用户: {}", alarmId, userId);
+//        return true;
+//    }
+//
+//    @Override
+//    public List<Map<String, Object>> getUnacknowledgedAlarms(String deviceId) {
+//        log.info("获取设备未确认的告警: {}", deviceId);
+//        return Collections.emptyList();
+//    }
+//
+//    @Override
+//    public Long recordAlarmEventWithSeverity(String deviceId, String desc, String table, Integer severity, Long timestamp) {
+//        log.info("记录告警事件(带告警级别): {}, 描述: {}, 级别: {}", deviceId, desc, severity);
+//        // 返回模拟的告警ID
+//        return timestamp != null ? timestamp : System.currentTimeMillis();
+//    }
+//
+//    @Override
+//    public boolean saveRegion(Region region) {
+//        log.info("保存区域信息: {}", region.getName());
+//        return true;
+//    }
+//
+//    @Override
+//    public boolean updateRegion(Region region) {
+//        log.info("更新区域信息: {}", region.getId());
+//        return true;
+//    }
+//
+//    @Override
+//    public Region getRegionById(String regionId) {
+//        log.info("根据ID获取区域: {}", regionId);
+//        Region region = new Region();
+//        region.setId(regionId);
+//        region.setName("模拟区域");
+//        return region;
+//    }
+//
+//    @Override
+//    public List<Region> getRegionsByDeviceId(String deviceId) {
+//        log.info("获取设备的区域列表: {}", deviceId);
+//        return new ArrayList<>();
+//    }
+//
+//    @Override
+//    public boolean deleteRegion(String regionId) {
+//        log.info("删除区域: {}", regionId);
+//        return true;
+//    }
+//
+//    @Override
+//    public boolean deleteDevice(String deviceId) {
+//        log.info("删除设备: {}", deviceId);
+//        return true;
+//    }
+//}

+ 42 - 17
device-service-infrastructure/src/main/java/com/hfln/device/infrastructure/mqtt/handler/DeviceMessageHandler.java

@@ -94,25 +94,29 @@ public class DeviceMessageHandler {
                     case "dsp_data":
                     case "dsp_data":
                         handleDeviceDspData(topic, payload);
                         handleDeviceDspData(topic, payload);
                         break;
                         break;
-                    case "cloudpoint":
-                        // todo 目前没有 lna 设备,暂不考虑点云数据改造
-                        handleDeviceCloudPoint(topic, payload);
-                        break;
-                    case "report_falling_event":
-                        // todo 这个 主题 待确认是否废弃
-                        handleDeviceReportFallEvent(topic, payload);
-                        break;
-                    case "report_presence_event":
-                        handleDeviceReportPresenceEvent(topic, payload);
-                        break;
 
 
-                        // todo 待确认是否废弃
-                    case "set_debug_param":
-                        handleSetDebugParam(topic, payload);
-                        break;
-                    case "get_debug_param":
-                        handleGetDebugParam(topic, payload);
+                    case "disconnect":
+                        handleDeviceDisconnect(topic, payload);
                         break;
                         break;
+//                    case "cloudpoint":
+//                        // todo 目前没有 lna 设备,暂不考虑点云数据改造
+//                        handleDeviceCloudPoint(topic, payload);
+//                        break;
+//                    case "report_falling_event":
+//                        // todo 这个 主题 待确认是否废弃
+//                        handleDeviceReportFallEvent(topic, payload);
+//                        break;
+//                    case "report_presence_event":
+//                        handleDeviceReportPresenceEvent(topic, payload);
+//                        break;
+//
+//                        // todo 待确认是否废弃
+//                    case "set_debug_param":
+//                        handleSetDebugParam(topic, payload);
+//                        break;
+//                    case "get_debug_param":
+//                        handleGetDebugParam(topic, payload);
+//                        break;
                     default:
                     default:
                         log.debug("Unhandled device topic action: {} for topic: {}", action, topic);
                         log.debug("Unhandled device topic action: {} for topic: {}", action, topic);
                         break;
                         break;
@@ -126,6 +130,27 @@ public class DeviceMessageHandler {
         }
         }
     }
     }
 
 
+    private void handleDeviceDisconnect(String topic, String payload) {
+        try {
+            log.info("Processing device login message: {}", topic);
+
+            Map<String, Object> messageData = JsonUtil.parseMap(payload);
+            String deviceId = (String) messageData.get("dev_id");
+
+            if (deviceId == null) {
+                log.warn("Invalid device login message, missing device_info: {}", payload);
+                return;
+            }
+
+            // 委托给应用层服务处理
+            // 应用层将处理:设备认证、状态更新、缓存刷新、响应发送
+            deviceEventPort.handleDeviceDisconnect(deviceId, messageData);
+
+        } catch (Exception e) {
+            log.error("Error handling device login: {}", e.getMessage(), e);
+        }
+    }
+
     /**
     /**
      * 处理设备登录消息
      * 处理设备登录消息
      * Python对应方法:deal_dev_login
      * Python对应方法:deal_dev_login