更新 HeartBeatService.java

pull/1/head
wangsiyuan 2023-12-09 11:24:16 +08:00
parent 645c2ddd90
commit ff590d8c05
1 changed files with 17 additions and 9 deletions

View File

@ -25,16 +25,21 @@ public class HeartBeatService {
if (!correctAccessToken.equals(accessToken)){
return ServerResponseEntity.fail("Invalid accessToken");
}
DeviceInfoDAO deviceInfoDAO = getDeviceInfoByAndroidId(deviceInfo.getAndroidId());
DeviceInfoDAO deviceInfoDAO = getDeviceInfoBySN(deviceInfo.getSN());
if (deviceInfoDAO == null) {
// 设备信息不存在,可能是无效的设备
addDeviceInfo(deviceInfo);
return ServerResponseEntity.success();
boolean isSuccess = addDeviceInfo(deviceInfo);
if (isSuccess){
return ServerResponseEntity.success();
} else {
logger.info("add to mysql error");
}
}
try {
updateClientStatus(deviceInfo);
return ServerResponseEntity.success();
} catch (Exception e) {
logger.error("update client status error.");
// 处理更新状态时的异常
return ServerResponseEntity.fail("Error updating client status");
}
@ -46,7 +51,7 @@ public class HeartBeatService {
// 更新该客户端的最后活跃时间
UpdateWrapper<DeviceInfoDAO> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("android_id", deviceInfo.getAndroidId());
updateWrapper.eq("sn", deviceInfo.getSN());
updateWrapper.set("last_online_time", currentTimeMillis); // 设置新的 accessToken
int result = deviceInfoDAOMapper.update(null, updateWrapper);
@ -56,6 +61,7 @@ public class HeartBeatService {
logger.warn("Update failed: No rows affected");
}
}
public boolean addDeviceInfo(DeviceInfo deviceInfo) {
try {
DeviceInfoDAO deviceInfoDAO = convertToDeviceInfoDAO(deviceInfo);
@ -71,12 +77,12 @@ public class HeartBeatService {
/**
* androidId DeviceInfo
*
* @param androidId Android ID
* @param SN SN
* @return androidId DeviceInfoDAO null
*/
public DeviceInfoDAO getDeviceInfoByAndroidId(String androidId) {
public DeviceInfoDAO getDeviceInfoBySN(String SN) {
QueryWrapper<DeviceInfoDAO> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("android_id", androidId);
queryWrapper.eq("sn", SN);
try {
DeviceInfoDAO deviceInfoDAO = deviceInfoDAOMapper.selectOne(queryWrapper);
if (deviceInfoDAO != null) {
@ -100,9 +106,11 @@ public class HeartBeatService {
// 这里需要实现将 DeviceInfo 转换为 DeviceInfoDAO 的逻辑
// 示例代码(根据实际字段调整):
DeviceInfoDAO deviceInfoDAO = new DeviceInfoDAO();
deviceInfoDAO.setAndroidId(deviceInfo.getAndroidId());
deviceInfoDAO.setSerialNumber(deviceInfo.getSerialNumber());
deviceInfoDAO.setIsMonitored(1);
deviceInfoDAO.setDeviceBrand(deviceInfo.getDeviceBrand());
deviceInfoDAO.setSN(deviceInfo.getSN());
deviceInfoDAO.setDeviceModel(deviceInfo.getDeviceModel());
deviceInfoDAO.setAndroidVersion(deviceInfo.getAndroidVersion());
deviceInfoDAO.setTimeOutPeriod(TIME_OUT_PERIOD);
deviceInfoDAO.setLastOnlineTime(System.currentTimeMillis());
deviceInfoDAO.setStatus(1);