From f92c0313a6f8d07cdcb03cf19498148e8800f8b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=89=91=E9=9B=A8?= <2375294554@qq.com> Date: Wed, 30 Apr 2025 09:41:38 +0800 Subject: [PATCH 1/9] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E6=B5=8B=E8=AF=95=E6=96=B9=E6=B3=95=E5=92=8C?= =?UTF-8?q?=E6=A8=A1=E6=8B=9F=E8=AE=BE=E5=A4=87=E8=BF=9E=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E8=BF=87=E6=9D=A5=E7=9A=84=E6=96=B9=E6=B3=95=EF=BC=8C=E6=96=B9?= =?UTF-8?q?=E4=BE=BF=E6=96=B0=E5=BC=80=E5=8F=91=E8=80=85=E8=B0=83=E8=AF=95?= =?UTF-8?q?=20--DeviceTest.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xiaozhi/modules/device/DeviceTest.java | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/main/manager-api/src/test/java/xiaozhi/modules/device/DeviceTest.java b/main/manager-api/src/test/java/xiaozhi/modules/device/DeviceTest.java index 8647692c..4c13bcdb 100644 --- a/main/manager-api/src/test/java/xiaozhi/modules/device/DeviceTest.java +++ b/main/manager-api/src/test/java/xiaozhi/modules/device/DeviceTest.java @@ -10,6 +10,10 @@ import org.springframework.test.context.ActiveProfiles; import lombok.extern.slf4j.Slf4j; import xiaozhi.common.redis.RedisKeys; import xiaozhi.common.redis.RedisUtils; +import xiaozhi.modules.sys.dto.SysUserDTO; +import xiaozhi.modules.sys.service.SysUserService; + +import java.util.HashMap; @Slf4j @SpringBootTest @@ -19,18 +23,38 @@ public class DeviceTest { @Autowired private RedisUtils redisUtils; + @Autowired + private SysUserService sysUserService; + + @Test + public void testSaveUser() { + SysUserDTO userDTO = new SysUserDTO(); + userDTO.setUsername("13536468486"); + userDTO.setPassword("0218jianyuQ!"); + sysUserService.save(userDTO); + } @Test @DisplayName("测试写入设备信息") public void testWriteDeviceInfo() { log.info("开始测试写入设备信息..."); - // 模拟设备MAC地址 - String macAddress = "00:11:22:33:44:55"; + String macAddress = "00:11:22:33:44:66"; // 模拟设备验证码 String deviceCode = "123456"; - String redisKey = RedisKeys.getDeviceCaptchaKey(deviceCode); + HashMap map = new HashMap<>(); + map.put("mac_address",macAddress); + map.put("activation_code",deviceCode); + map.put("board","硬件型号"); + map.put("app_version","0.3.13"); + + String safeDeviceId = macAddress.replace(":", "_").toLowerCase(); + String cacheDeviceKey = String.format("ota:activation:data:%s", safeDeviceId); + redisUtils.set(cacheDeviceKey, map, 300); + + + String redisKey = "ota:activation:code:"+deviceCode; log.info("Redis Key: {}", redisKey); // 将设备信息写入Redis From 9b0088f4f0a13276c94b78b956dc4d39022614d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=89=91=E9=9B=A8?= <2375294554@qq.com> Date: Wed, 30 Apr 2025 10:44:54 +0800 Subject: [PATCH 2/9] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=AD=A4=E6=99=BA=E8=83=BD=E4=BD=93=E5=85=A8?= =?UTF-8?q?=E9=83=A8=E8=AE=BE=E5=A4=87=E7=9A=84=E6=9C=80=E5=90=8E=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E6=97=B6=E9=97=B4=EF=BC=8C=E6=96=B9=E6=B3=95=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E5=92=8Csql=EF=BC=8C=E4=B8=8D=E4=BD=BF=E7=94=A8mysql-?= =?UTF-8?q?plus=E7=9A=84=E6=96=B9=E6=B3=95=EF=BC=8C=E6=98=AF=E4=B8=BA?= =?UTF-8?q?=E4=BA=86=E5=87=8F=E5=B0=91=E6=95=B0=E6=8D=AE=E5=BA=93=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E4=BC=A0=E8=BE=93=EF=BC=8C=E5=9B=A0=E4=B8=BA=E5=8F=AA?= =?UTF-8?q?=E9=9C=80=E8=A6=81=E6=9C=80=E5=90=8E=E8=BF=9E=E6=8E=A5=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E5=AD=97=E6=AE=B5=20--DeviceDao.java=20=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E5=AE=9A=E4=B9=89=20--DeviceDao.xml=20sql?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/xiaozhi/modules/device/dao/DeviceDao.java | 10 ++++++++++ .../src/main/resources/mapper/device/DeviceDao.xml | 8 ++++++++ 2 files changed, 18 insertions(+) create mode 100644 main/manager-api/src/main/resources/mapper/device/DeviceDao.xml diff --git a/main/manager-api/src/main/java/xiaozhi/modules/device/dao/DeviceDao.java b/main/manager-api/src/main/java/xiaozhi/modules/device/dao/DeviceDao.java index 5ede3d6e..abff8ab3 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/device/dao/DeviceDao.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/device/dao/DeviceDao.java @@ -6,6 +6,16 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper; import xiaozhi.modules.device.entity.DeviceEntity; +import java.util.Date; +import java.util.List; + @Mapper public interface DeviceDao extends BaseMapper { + /** + * 获取此智能体全部设备的最后连接时间 + * @param agentId 智能体id + * @return + */ + List getAllLastConnectedAtByAgentId(String agentId); + } \ No newline at end of file diff --git a/main/manager-api/src/main/resources/mapper/device/DeviceDao.xml b/main/manager-api/src/main/resources/mapper/device/DeviceDao.xml new file mode 100644 index 00000000..09f16760 --- /dev/null +++ b/main/manager-api/src/main/resources/mapper/device/DeviceDao.xml @@ -0,0 +1,8 @@ + + + + + + \ No newline at end of file From 1d627d570d37446b73a8fbefeaeb88d6120bcb69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=89=91=E9=9B=A8?= <2375294554@qq.com> Date: Wed, 30 Apr 2025 10:45:49 +0800 Subject: [PATCH 3/9] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E8=BF=99=E4=B8=AA=E6=99=BA=E8=83=BD=E4=BD=93=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E7=90=86=E7=9A=84=E6=9C=80=E8=BF=91=E7=9A=84=E6=9C=80=E5=90=8E?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E6=97=B6=E9=97=B4=E5=AE=9A=E4=B9=89=E5=92=8C?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=20--DeviceService.java=20=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=20--DeviceServiceImpl.java=20=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/device/service/DeviceService.java | 10 ++++++++++ .../device/service/impl/DeviceServiceImpl.java | 13 +++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/main/manager-api/src/main/java/xiaozhi/modules/device/service/DeviceService.java b/main/manager-api/src/main/java/xiaozhi/modules/device/service/DeviceService.java index aff31bff..fb91ff89 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/device/service/DeviceService.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/device/service/DeviceService.java @@ -1,5 +1,6 @@ package xiaozhi.modules.device.service; +import java.util.Date; import java.util.List; import xiaozhi.common.page.PageData; @@ -78,4 +79,13 @@ public interface DeviceService extends BaseService { * @return 激活码 */ String geCodeByDeviceId(String deviceId); + + /** + * 获取这个智能体设备理的最近的最后连接时间 + * @param agentId 智能体id + * @return 返回设备最近的最后连接时间 + */ + Date getLatestLastConnectionTime(String agentId); + + } \ No newline at end of file diff --git a/main/manager-api/src/main/java/xiaozhi/modules/device/service/impl/DeviceServiceImpl.java b/main/manager-api/src/main/java/xiaozhi/modules/device/service/impl/DeviceServiceImpl.java index 8eb23a93..99b40a38 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/device/service/impl/DeviceServiceImpl.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/device/service/impl/DeviceServiceImpl.java @@ -1,12 +1,7 @@ package xiaozhi.modules.device.service.impl; import java.time.Instant; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.TimeZone; -import java.util.UUID; +import java.util.*; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; @@ -258,6 +253,12 @@ public class DeviceServiceImpl extends BaseServiceImpl return null; } + @Override + public Date getLatestLastConnectionTime(String agentId) { + List list = deviceDao.getAllLastConnectedAtByAgentId(agentId); + return Collections.max(list, Comparator.comparing(Date::getTime)); + } + private String getDeviceCacheKey(String deviceId) { String safeDeviceId = deviceId.replace(":", "_").toLowerCase(); String dataKey = String.format("ota:activation:data:%s", safeDeviceId); From 7d74e853ae0ec674e3b6bc9c20b4a195331e242e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=89=91=E9=9B=A8?= <2375294554@qq.com> Date: Wed, 30 Apr 2025 10:46:34 +0800 Subject: [PATCH 4/9] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=99=BA=E8=83=BD?= =?UTF-8?q?=E4=BD=93=E6=9C=80=E8=BF=91=E7=9A=84=E6=9C=80=E5=90=8E=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E6=97=B6=E9=97=B4=E4=B8=BA=E7=A9=BA=E7=9A=84bug=20--A?= =?UTF-8?q?gentServiceImpl.java=20=E4=BF=AE=E5=A4=8Dbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/agent/service/impl/AgentServiceImpl.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/main/manager-api/src/main/java/xiaozhi/modules/agent/service/impl/AgentServiceImpl.java b/main/manager-api/src/main/java/xiaozhi/modules/agent/service/impl/AgentServiceImpl.java index ff284ef0..ff883a57 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/agent/service/impl/AgentServiceImpl.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/agent/service/impl/AgentServiceImpl.java @@ -21,6 +21,7 @@ import xiaozhi.modules.agent.dao.AgentDao; import xiaozhi.modules.agent.dto.AgentDTO; import xiaozhi.modules.agent.entity.AgentEntity; import xiaozhi.modules.agent.service.AgentService; +import xiaozhi.modules.device.service.DeviceService; import xiaozhi.modules.model.service.ModelConfigService; import xiaozhi.modules.timbre.service.TimbreService; @@ -31,6 +32,9 @@ public class AgentServiceImpl extends BaseServiceImpl imp @Autowired private TimbreService timbreModelService; + @Autowired + private DeviceService deviceService; + @Autowired private ModelConfigService modelConfigService; @@ -104,6 +108,11 @@ public class AgentServiceImpl extends BaseServiceImpl imp // 获取设备数量 dto.setDeviceCount(getDeviceCountByAgentId(agent.getId())); + // 获取智能体最近的最后连接时长 + dto.setLastConnectedAt(deviceService.getLatestLastConnectionTime(agent.getId())); + + + return dto; }).collect(Collectors.toList()); } From 6f7ff9f858bee0c154aa00ed552bed5636c96a0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=89=91=E9=9B=A8?= <2375294554@qq.com> Date: Wed, 30 Apr 2025 11:05:20 +0800 Subject: [PATCH 5/9] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E8=BF=99=E4=B8=AA=E6=99=BA=E8=83=BD=E4=BD=93=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E7=90=86=E7=9A=84=E6=9C=80=E8=BF=91=E7=9A=84=E6=9C=80=E5=90=8E?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E6=97=B6=E9=97=B4=E6=96=B9=E6=B3=95=EF=BC=8C?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=97=B6=E9=97=B4=E5=92=8C=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E6=95=B0=E9=87=8F=20--DeviceServiceImpl.java=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../device/service/impl/DeviceServiceImpl.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/main/manager-api/src/main/java/xiaozhi/modules/device/service/impl/DeviceServiceImpl.java b/main/manager-api/src/main/java/xiaozhi/modules/device/service/impl/DeviceServiceImpl.java index 99b40a38..0475f2e9 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/device/service/impl/DeviceServiceImpl.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/device/service/impl/DeviceServiceImpl.java @@ -255,8 +255,19 @@ public class DeviceServiceImpl extends BaseServiceImpl @Override public Date getLatestLastConnectionTime(String agentId) { + // 查询是否有缓存时间,有则返回 + Date cachedDate = (Date) redisUtils.get(RedisKeys.getAgentDeviceLastConnectedAtById(agentId)); + if (cachedDate != null) { + return cachedDate; + } List list = deviceDao.getAllLastConnectedAtByAgentId(agentId); - return Collections.max(list, Comparator.comparing(Date::getTime)); + Date maxDate = Collections.max(list, Comparator.comparing(Date::getTime)); + // 将结果存入Redis, 存储设备最近最后连接时间,和设备数量 + redisUtils.set(RedisKeys.getAgentDeviceCountById(agentId), list.size(), 60); + if (maxDate != null) { + redisUtils.set(RedisKeys.getAgentDeviceLastConnectedAtById(agentId), maxDate, 60); + } + return maxDate; } private String getDeviceCacheKey(String deviceId) { From 09c17b2dc5325754bf579af8d8f30ab83dffa7d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=89=91=E9=9B=A8?= <2375294554@qq.com> Date: Wed, 30 Apr 2025 11:07:53 +0800 Subject: [PATCH 6/9] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=96=B9=E6=B3=95?= =?UTF-8?q?=EF=BC=8C=E6=8A=8A=E8=8E=B7=E5=8F=96=E6=9C=80=E8=BF=91=E6=9C=80?= =?UTF-8?q?=E5=90=8E=E6=97=B6=E9=97=B4=E8=8E=B7=E5=8F=96=EF=BC=8C=E6=94=BE?= =?UTF-8?q?=E5=9C=A8=E8=8E=B7=E5=8F=96=E8=AE=BE=E5=A4=87=E6=95=B0=E9=87=8F?= =?UTF-8?q?=E5=89=8D=E9=9D=A2=E5=89=8D=EF=BC=8C=E5=8F=AF=E4=BB=A5=E5=87=8F?= =?UTF-8?q?=E5=B0=91=E4=B8=80=E6=AC=A1sql=E6=9F=A5=E8=AF=A2=EF=BC=8C?= =?UTF-8?q?=E5=9B=A0=E4=B8=BA=E5=9C=A8=E8=8E=B7=E5=8F=96=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E7=9A=84=E6=97=B6=E5=80=99=EF=BC=8C=E5=B7=B2=E7=BB=8F=E9=A1=BA?= =?UTF-8?q?=E4=BE=BF=E7=BC=93=E5=AD=98=E7=9A=84=E8=AE=BE=E5=A4=87=E6=95=B0?= =?UTF-8?q?=E9=87=8F=E4=BA=86=20--AgentServiceImpl.java=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=EF=BC=9A=E5=87=8F=E5=B0=91sql=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/agent/service/impl/AgentServiceImpl.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/main/manager-api/src/main/java/xiaozhi/modules/agent/service/impl/AgentServiceImpl.java b/main/manager-api/src/main/java/xiaozhi/modules/agent/service/impl/AgentServiceImpl.java index ff883a57..c731c2fa 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/agent/service/impl/AgentServiceImpl.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/agent/service/impl/AgentServiceImpl.java @@ -105,14 +105,11 @@ public class AgentServiceImpl extends BaseServiceImpl imp // 获取 TTS 音色名称 dto.setTtsVoiceName(timbreModelService.getTimbreNameById(agent.getTtsVoiceId())); - // 获取设备数量 - dto.setDeviceCount(getDeviceCountByAgentId(agent.getId())); - // 获取智能体最近的最后连接时长 dto.setLastConnectedAt(deviceService.getLatestLastConnectionTime(agent.getId())); - - + // 获取设备数量 + dto.setDeviceCount(getDeviceCountByAgentId(agent.getId())); return dto; }).collect(Collectors.toList()); } From b4944f23ef873b27eb808b23fb97e0e4766d36b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=89=91=E9=9B=A8?= <2375294554@qq.com> Date: Wed, 30 Apr 2025 11:08:59 +0800 Subject: [PATCH 7/9] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E6=96=B0=E7=9A=84redis:key=20--RedisKeys.java=20?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E8=AE=BE=E5=A4=87=E6=9C=80=E8=BF=91=E6=9C=80?= =?UTF-8?q?=E4=B9=85=E6=97=B6=E9=97=B4=E7=9A=84key?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/xiaozhi/common/redis/RedisKeys.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/main/manager-api/src/main/java/xiaozhi/common/redis/RedisKeys.java b/main/manager-api/src/main/java/xiaozhi/common/redis/RedisKeys.java index 5d3b4f32..5e5ec309 100644 --- a/main/manager-api/src/main/java/xiaozhi/common/redis/RedisKeys.java +++ b/main/manager-api/src/main/java/xiaozhi/common/redis/RedisKeys.java @@ -62,6 +62,13 @@ public class RedisKeys { return "agent:device:count:" + id; } + /** + * 获取智能体最后连接时间缓存key + */ + public static String getAgentDeviceLastConnectedAtById(String id) { + return "agent:device:lastConnected:" + id; + } + /** * 获取系统配置缓存key */ From 770a198772c573a62e0f4c211a7d258d58306d71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=89=91=E9=9B=A8?= <2375294554@qq.com> Date: Wed, 7 May 2025 11:04:14 +0800 Subject: [PATCH 8/9] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E6=9C=80=E5=A4=A7=E7=9A=84=E6=9C=80=E8=BF=91=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E3=80=82=E6=94=B9=E4=B8=BA=E5=9C=A8=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E9=87=8C=E6=8E=92=E5=BA=8F=E5=A5=BD=E5=90=8E?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E7=BB=99=E7=B3=BB=E7=BB=9F=EF=BC=8C=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E6=97=B6=E9=97=B4=E4=BF=AE=E6=94=B9=E4=B8=BA2?= =?UTF-8?q?=E5=88=86=E9=92=9F=20--DeviceDao.java=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E8=BF=94=E5=9B=9E=E5=80=BC=20--DeviceDao.xml?= =?UTF-8?q?=20=E4=BF=AE=E6=94=B9sql=EF=BC=8C=E5=9C=A8=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E9=87=8C=E6=8E=92=E5=BA=8F=E8=BF=94=E5=9B=9E=20--Devi?= =?UTF-8?q?ceServiceImpl.java=20=E4=BF=AE=E6=94=B9=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E4=B8=BA2=E5=88=86=E9=92=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/xiaozhi/modules/device/dao/DeviceDao.java | 2 +- .../modules/device/service/impl/DeviceServiceImpl.java | 7 ++----- .../src/main/resources/mapper/device/DeviceDao.xml | 6 +++++- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/main/manager-api/src/main/java/xiaozhi/modules/device/dao/DeviceDao.java b/main/manager-api/src/main/java/xiaozhi/modules/device/dao/DeviceDao.java index abff8ab3..6570b89b 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/device/dao/DeviceDao.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/device/dao/DeviceDao.java @@ -16,6 +16,6 @@ public interface DeviceDao extends BaseMapper { * @param agentId 智能体id * @return */ - List getAllLastConnectedAtByAgentId(String agentId); + Date getAllLastConnectedAtByAgentId(String agentId); } \ No newline at end of file diff --git a/main/manager-api/src/main/java/xiaozhi/modules/device/service/impl/DeviceServiceImpl.java b/main/manager-api/src/main/java/xiaozhi/modules/device/service/impl/DeviceServiceImpl.java index 0475f2e9..59e329fa 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/device/service/impl/DeviceServiceImpl.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/device/service/impl/DeviceServiceImpl.java @@ -260,12 +260,9 @@ public class DeviceServiceImpl extends BaseServiceImpl if (cachedDate != null) { return cachedDate; } - List list = deviceDao.getAllLastConnectedAtByAgentId(agentId); - Date maxDate = Collections.max(list, Comparator.comparing(Date::getTime)); - // 将结果存入Redis, 存储设备最近最后连接时间,和设备数量 - redisUtils.set(RedisKeys.getAgentDeviceCountById(agentId), list.size(), 60); + Date maxDate = deviceDao.getAllLastConnectedAtByAgentId(agentId); if (maxDate != null) { - redisUtils.set(RedisKeys.getAgentDeviceLastConnectedAtById(agentId), maxDate, 60); + redisUtils.set(RedisKeys.getAgentDeviceLastConnectedAtById(agentId), maxDate, 60 * 2); } return maxDate; } diff --git a/main/manager-api/src/main/resources/mapper/device/DeviceDao.xml b/main/manager-api/src/main/resources/mapper/device/DeviceDao.xml index 09f16760..54de47c9 100644 --- a/main/manager-api/src/main/resources/mapper/device/DeviceDao.xml +++ b/main/manager-api/src/main/resources/mapper/device/DeviceDao.xml @@ -3,6 +3,10 @@ \ No newline at end of file From 571080c1d623d299ddf483254363661f528fd005 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Wed, 7 May 2025 22:56:51 +0800 Subject: [PATCH 9/9] =?UTF-8?q?update:=E4=BC=98=E5=8C=96=E6=9C=80=E8=BF=91?= =?UTF-8?q?=E5=AF=B9=E8=AF=9D=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/AgentChatHistoryBizServiceImpl.java | 6 +++++ .../agent/service/impl/AgentServiceImpl.java | 1 + .../xiaozhi/modules/device/DeviceTest.java | 23 +++++++++---------- .../manager-web/src/components/DeviceItem.vue | 21 ++++++++++++++++- 4 files changed, 38 insertions(+), 13 deletions(-) diff --git a/main/manager-api/src/main/java/xiaozhi/modules/agent/service/biz/impl/AgentChatHistoryBizServiceImpl.java b/main/manager-api/src/main/java/xiaozhi/modules/agent/service/biz/impl/AgentChatHistoryBizServiceImpl.java index b839e9e4..8b405e49 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/agent/service/biz/impl/AgentChatHistoryBizServiceImpl.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/agent/service/biz/impl/AgentChatHistoryBizServiceImpl.java @@ -1,12 +1,15 @@ package xiaozhi.modules.agent.service.biz.impl; import java.util.Base64; +import java.util.Date; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import xiaozhi.common.redis.RedisKeys; +import xiaozhi.common.redis.RedisUtils; import xiaozhi.modules.agent.dto.AgentChatHistoryReportDTO; import xiaozhi.modules.agent.entity.AgentChatHistoryEntity; import xiaozhi.modules.agent.entity.AgentEntity; @@ -29,6 +32,7 @@ public class AgentChatHistoryBizServiceImpl implements AgentChatHistoryBizServic private final AgentService agentService; private final AgentChatHistoryService agentChatHistoryService; private final AgentChatAudioService agentChatAudioService; + private final RedisUtils redisUtils; /** * 处理聊天记录上报,包括文件上传和相关信息记录 @@ -77,6 +81,8 @@ public class AgentChatHistoryBizServiceImpl implements AgentChatHistoryBizServic // 3. 保存数据 agentChatHistoryService.save(entity); + // 4. 更新设备最后对话时间 + redisUtils.set(RedisKeys.getAgentDeviceLastConnectedAtById(agentId), new Date(), 60 * 2); return Boolean.TRUE; } } diff --git a/main/manager-api/src/main/java/xiaozhi/modules/agent/service/impl/AgentServiceImpl.java b/main/manager-api/src/main/java/xiaozhi/modules/agent/service/impl/AgentServiceImpl.java index 8fb85f06..ae650c85 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/agent/service/impl/AgentServiceImpl.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/agent/service/impl/AgentServiceImpl.java @@ -34,6 +34,7 @@ public class AgentServiceImpl extends BaseServiceImpl imp private final TimbreService timbreModelService; private final ModelConfigService modelConfigService; private final RedisUtils redisUtils; + private final DeviceService deviceService; @Override public PageData adminAgentList(Map params) { diff --git a/main/manager-api/src/test/java/xiaozhi/modules/device/DeviceTest.java b/main/manager-api/src/test/java/xiaozhi/modules/device/DeviceTest.java index 4c13bcdb..37f67cab 100644 --- a/main/manager-api/src/test/java/xiaozhi/modules/device/DeviceTest.java +++ b/main/manager-api/src/test/java/xiaozhi/modules/device/DeviceTest.java @@ -1,5 +1,8 @@ package xiaozhi.modules.device; +import java.util.HashMap; +import java.util.UUID; + import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -8,13 +11,10 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; import lombok.extern.slf4j.Slf4j; -import xiaozhi.common.redis.RedisKeys; import xiaozhi.common.redis.RedisUtils; import xiaozhi.modules.sys.dto.SysUserDTO; import xiaozhi.modules.sys.service.SysUserService; -import java.util.HashMap; - @Slf4j @SpringBootTest @ActiveProfiles("dev") @@ -29,8 +29,8 @@ public class DeviceTest { @Test public void testSaveUser() { SysUserDTO userDTO = new SysUserDTO(); - userDTO.setUsername("13536468486"); - userDTO.setPassword("0218jianyuQ!"); + userDTO.setUsername("test"); + userDTO.setPassword(UUID.randomUUID().toString()); sysUserService.save(userDTO); } @@ -43,18 +43,17 @@ public class DeviceTest { // 模拟设备验证码 String deviceCode = "123456"; - HashMap map = new HashMap<>(); - map.put("mac_address",macAddress); - map.put("activation_code",deviceCode); - map.put("board","硬件型号"); - map.put("app_version","0.3.13"); + HashMap map = new HashMap<>(); + map.put("mac_address", macAddress); + map.put("activation_code", deviceCode); + map.put("board", "硬件型号"); + map.put("app_version", "0.3.13"); String safeDeviceId = macAddress.replace(":", "_").toLowerCase(); String cacheDeviceKey = String.format("ota:activation:data:%s", safeDeviceId); redisUtils.set(cacheDeviceKey, map, 300); - - String redisKey = "ota:activation:code:"+deviceCode; + String redisKey = "ota:activation:code:" + deviceCode; log.info("Redis Key: {}", redisKey); // 将设备信息写入Redis diff --git a/main/manager-web/src/components/DeviceItem.vue b/main/manager-web/src/components/DeviceItem.vue index fa81a04d..c8acfde2 100644 --- a/main/manager-web/src/components/DeviceItem.vue +++ b/main/manager-web/src/components/DeviceItem.vue @@ -31,7 +31,7 @@
-
最近对话:{{ device.lastConnectedAt }}
+
最近对话:{{ formattedLastConnectedTime }}
@@ -45,6 +45,25 @@ export default { data() { return { switchValue: false } }, + computed: { + formattedLastConnectedTime() { + if (!this.device.lastConnectedAt) return '暂未对话'; + + const lastTime = new Date(this.device.lastConnectedAt); + const now = new Date(); + const diffMinutes = Math.floor((now - lastTime) / (1000 * 60)); + + if (diffMinutes < 60) { + return `${diffMinutes}分钟前`; + } else if (diffMinutes < 24 * 60) { + const hours = Math.floor(diffMinutes / 60); + const minutes = diffMinutes % 60; + return `${hours}小时${minutes > 0 ? minutes + '分钟' : ''}前`; + } else { + return this.device.lastConnectedAt; + } + } + }, methods: { handleDelete() { this.$emit('delete', this.device.agentId)