更新 HeartBeatService.java
parent
c7284d2c8e
commit
fbaf0b5888
|
|
@ -2,40 +2,53 @@ 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;
|
||||
public ServerResponseEntity handleHeartbeatSignal(String accessToken,DeviceInfo deviceInfo) {
|
||||
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)){
|
||||
logger.info("accessToken: {} correctAccessToken: {}", accessToken, correctAccessToken);
|
||||
if (!correctAccessToken.equals(accessToken)) {
|
||||
return ServerResponseEntity.fail("Invalid accessToken");
|
||||
}
|
||||
DeviceInfoDAO deviceInfoDAO = getDeviceInfoBySN(deviceInfo.getSN());
|
||||
logger.info("deviceInfoDAO: {}",deviceInfoDAO);
|
||||
if (deviceInfoDAO == null) {
|
||||
// 设备信息不存在,可能是无效的设备
|
||||
logger.info("device not find,start add device to mysql.");
|
||||
boolean isSuccess = addDeviceInfo(deviceInfo);
|
||||
if (isSuccess){
|
||||
return ServerResponseEntity.success();
|
||||
} else {
|
||||
logger.info("add to mysql error");
|
||||
try {
|
||||
DeviceInfoDAO deviceInfoDAO = getDeviceInfoBySN(deviceInfo.getSN());
|
||||
if (deviceInfoDAO == null) {
|
||||
// 设备信息不存在,可能是无效的设备
|
||||
logger.info("device not find,start add device to mysql.");
|
||||
boolean isSuccess = addDeviceInfo(deviceInfo);
|
||||
if (isSuccess) {
|
||||
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);
|
||||
|
|
@ -65,10 +78,11 @@ 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);
|
||||
logger.info("result: {}",result);
|
||||
logger.info("result: {}", result);
|
||||
return result > 0;
|
||||
} catch (Exception e) {
|
||||
logger.error("Error adding device info", e);
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue