更新 HeartBeatService.java

pull/1/head
wangsiyuan 2023-12-14 11:35:03 +08:00
parent c7284d2c8e
commit fbaf0b5888
1 changed files with 36 additions and 21 deletions

View File

@ -2,31 +2,35 @@ package com.kimgo.wepush.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kimgo.wepush.exceptions.DatabaseQueryException;
import com.kimgo.wepush.mapper.DeviceInfoDAOMapper;
import com.kimgo.wepush.model.DeviceInfo;
import com.kimgo.wepush.model.DeviceInfoDAO;
import com.kimgo.wepush.response.ServerResponseEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class HeartBeatService {
private final Logger logger = LoggerFactory.getLogger(HeartBeatService.class);
private final long TIME_OUT_PERIOD = 600;
@Autowired
TokenService tokenService;
@Autowired
private DeviceInfoDAOMapper deviceInfoDAOMapper;
private final TokenService tokenService;
private final DeviceInfoDAOMapper deviceInfoDAOMapper;
public HeartBeatService(TokenService tokenService, DeviceInfoDAOMapper deviceInfoDAOMapper) {
this.tokenService = tokenService;
this.deviceInfoDAOMapper = deviceInfoDAOMapper;
}
public ServerResponseEntity handleHeartbeatSignal(String accessToken, DeviceInfo deviceInfo) {
String correctAccessToken = tokenService.getApiAccessToken();
logger.info("accessToken: {} correctAccessToken: {}", accessToken, correctAccessToken);
if (!correctAccessToken.equals(accessToken)) {
return ServerResponseEntity.fail("Invalid accessToken");
}
try {
DeviceInfoDAO deviceInfoDAO = getDeviceInfoBySN(deviceInfo.getSN());
logger.info("deviceInfoDAO: {}",deviceInfoDAO);
if (deviceInfoDAO == null) {
// 设备信息不存在,可能是无效的设备
logger.info("device not find,start add device to mysql.");
@ -35,8 +39,17 @@ public class HeartBeatService {
return ServerResponseEntity.success();
} else {
logger.info("add to mysql error");
return ServerResponseEntity.fail("server error,please check log.");
}
}
} catch (DatabaseQueryException e) {
// 处理查询失败的情况
logger.error("Database query failed.", e);
return ServerResponseEntity.fail("Database query failed");
} catch (Exception e) {
logger.error("update client status error.");
return ServerResponseEntity.fail("Error updating client status");
}
try {
updateClientStatus(deviceInfo);
return ServerResponseEntity.success();
@ -65,6 +78,7 @@ public class HeartBeatService {
}
public boolean addDeviceInfo(DeviceInfo deviceInfo) {
logger.info("start add device. device info: {}", deviceInfo);
try {
DeviceInfoDAO deviceInfoDAO = convertToDeviceInfoDAO(deviceInfo);
int result = deviceInfoDAOMapper.insert(deviceInfoDAO);
@ -82,7 +96,7 @@ public class HeartBeatService {
* @param SN SN
* @return androidId DeviceInfoDAO null
*/
public DeviceInfoDAO getDeviceInfoBySN(String SN) {
public DeviceInfoDAO getDeviceInfoBySN(String SN) throws DatabaseQueryException {
QueryWrapper<DeviceInfoDAO> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("sn", SN);
try {
@ -91,10 +105,11 @@ public class HeartBeatService {
logger.info("Data is queried in the database based on the SN");
return deviceInfoDAO;
}
return null; // 数据不存在
} catch (Exception e) {
logger.error("Error querying device info by SN", e);
throw new DatabaseQueryException("Error querying device info by SN", e);
}
return null;
}