Merge pull request #9 from goxofy/manual-add-device

fix
This commit is contained in:
TinK
2025-06-26 16:24:55 +08:00
committed by GitHub
2 changed files with 18 additions and 11 deletions
@@ -19,6 +19,8 @@ import xiaozhi.modules.agent.service.AgentChatAudioService;
import xiaozhi.modules.agent.service.AgentChatHistoryService;
import xiaozhi.modules.agent.service.AgentService;
import xiaozhi.modules.agent.service.biz.AgentChatHistoryBizService;
import xiaozhi.modules.device.entity.DeviceEntity;
import xiaozhi.modules.device.service.DeviceService;
/**
* {@link AgentChatHistoryBizService} impl
@@ -35,6 +37,7 @@ public class AgentChatHistoryBizServiceImpl implements AgentChatHistoryBizServic
private final AgentChatHistoryService agentChatHistoryService;
private final AgentChatAudioService agentChatAudioService;
private final RedisUtils redisUtils;
private final DeviceService deviceService;
/**
* 处理聊天记录上报,包括文件上传和相关信息记录
@@ -68,6 +71,15 @@ public class AgentChatHistoryBizServiceImpl implements AgentChatHistoryBizServic
// 更新设备最后对话时间
redisUtils.set(RedisKeys.getAgentDeviceLastConnectedAtById(agentId), new Date());
// 更新设备最后连接时间
DeviceEntity device = deviceService.getDeviceByMacAddress(macAddress);
if (device != null) {
deviceService.updateDeviceConnectionInfo(agentId, device.getId(), null);
} else {
log.warn("聊天记录上报时,未找到mac地址为 {} 的设备", macAddress);
}
return Boolean.TRUE;
}
@@ -59,24 +59,19 @@ public class DeviceServiceImpl extends BaseServiceImpl<DeviceDao, DeviceEntity>
@Async
public void updateDeviceConnectionInfo(String agentId, String deviceId, String appVersion) {
log.info("Attempting to update connection info for deviceId: {}", deviceId);
try {
UpdateWrapper<DeviceEntity> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("id", deviceId)
.set("last_connected_at", new Date())
.set("update_date", new Date());
DeviceEntity device = new DeviceEntity();
device.setId(deviceId);
device.setLastConnectedAt(new Date());
if (StringUtils.isNotBlank(appVersion)) {
updateWrapper.set("app_version", appVersion);
device.setAppVersion(appVersion);
}
int updatedRows = deviceDao.update(null, updateWrapper);
log.info("Update result for deviceId {}: {} rows affected.", deviceId, updatedRows);
deviceDao.updateById(device);
if (StringUtils.isNotBlank(agentId)) {
redisUtils.set(RedisKeys.getAgentDeviceLastConnectedAtById(agentId), new Date());
}
} catch (Exception e) {
log.error("Failed to update connection info for deviceId: {}", deviceId, e);
log.error("异步更新设备连接信息失败", e);
}
}