hxd 2 kuukautta sitten
vanhempi
commit
87e7f5e910

+ 11 - 0
portal-service-common/src/main/java/com/hfln/portal/common/dto/data/device/DeviceDTO.java

@@ -180,6 +180,17 @@ public class DeviceDTO extends BaseVO {
     @Schema(description = "激活时间")
     private LocalDateTime activeTime;
 
+    /**
+     * 设备离线时间
+     */
+    @Schema(description = "设备离线时间")
+    private LocalDateTime offlineTime;
+
+    /**
+     * 激活时间
+     */
+    @Schema(description = "目标离开时间")
+    private LocalDateTime lastTargetTime;
 //    /**
 //     * 安装坐标x
 //     */

+ 44 - 0
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/gateway/impl/DeviceGatewayImpl.java

@@ -1,6 +1,7 @@
 package com.hfln.portal.infrastructure.gateway.impl;
 
 import cn.hfln.framework.extension.BizException;
+import cn.hfln.framework.redis.util.RedisUtil;
 import com.alibaba.fastjson2.JSONArray;
 import com.alibaba.fastjson2.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@@ -49,7 +50,9 @@ import org.springframework.web.multipart.MultipartFile;
 import java.io.IOException;
 import java.io.InputStream;
 import java.math.BigDecimal;
+import java.time.Instant;
 import java.time.LocalDateTime;
+import java.time.ZoneOffset;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -105,6 +108,12 @@ public class DeviceGatewayImpl implements DeviceGateway {
     @Autowired
     private StayTimeService stayTimeService;
 
+    @Autowired
+    private RedisUtil redisService;
+
+    @Autowired
+    private org.springframework.data.redis.core.RedisTemplate<String, Object> redisTemplate;
+
 
     @Override
     public HomeInfoDTO queryHomeInfo(Long userId) {
@@ -181,6 +190,41 @@ public class DeviceGatewayImpl implements DeviceGateway {
         for (DevInfo devInfo : devInfos) {
             DeviceDTO dto = new DeviceDTO();
             BeanUtils.copyProperties(devInfo, dto);
+
+            //从Redis中获取目标最后离开时间
+            String redisKey = "hfln:device:" + devInfo.getClientId();  //构建Redis的Key
+            try {
+                // 检查key是否存在
+                if (!redisService.hasKey(redisKey)) {
+                    log.debug("Redis中不存在key: {}", redisKey);
+                    deviceDTOs.add(dto);
+                    continue;
+                }
+                
+                // 直接从Hash中获取lastTargetTime字段
+                Object lastTargetTimeObj = redisTemplate.opsForHash().get(redisKey, "lastTargetTime");
+                
+                // 处理获取到的数据
+                if (lastTargetTimeObj != null) {
+                    try {
+                        //将时间戳转换为LocalDatetime
+                        long lastTargetTimeMillis = Long.parseLong(lastTargetTimeObj.toString());
+                        LocalDateTime lastTargetTime = Instant.ofEpochMilli(lastTargetTimeMillis)
+                                .atZone(ZoneOffset.systemDefault())
+                                .toLocalDateTime();
+
+                        dto.setLastTargetTime(lastTargetTime);
+                        log.debug("成功设置lastTargetTime,clientId: {}, time: {}", devInfo.getClientId(), lastTargetTime);
+                    } catch (NumberFormatException e) {
+                        log.warn("Redis中存储的时间戳格式错误,clientId: {}, value: {}", devInfo.getClientId(), lastTargetTimeObj);
+                    }
+                } else {
+                    log.debug("未找到lastTargetTime数据,clientId: {}", devInfo.getClientId());
+                }
+            } catch (Exception e) {
+                log.warn("获取Redis数据失败,clientId: {}, error: {}", devInfo.getClientId(), e.getMessage());
+                // 如果获取失败,不设置lastTargetTime,继续处理其他设备
+            }
             deviceDTOs.add(dto);
         }
         return deviceDTOs;