wePush/src/main/java/com/kimgo/wepush/service/HeartBeatService.java

120 lines
4.6 KiB
Java

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.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) {
String correctAccessToken = tokenService.getApiAccessToken();
logger.info("accessToken: {} correctAccessToken: {}",accessToken,correctAccessToken);
if (!correctAccessToken.equals(accessToken)){
return ServerResponseEntity.fail("Invalid accessToken");
}
DeviceInfoDAO deviceInfoDAO = getDeviceInfoBySN(deviceInfo.getSN());
if (deviceInfoDAO == null) {
// 设备信息不存在,可能是无效的设备
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");
}
}
public void updateClientStatus(DeviceInfo deviceInfo) {
long currentTimeMillis = System.currentTimeMillis();
// 更新该客户端的最后活跃时间
UpdateWrapper<DeviceInfoDAO> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("sn", deviceInfo.getSN());
updateWrapper.set("last_online_time", currentTimeMillis);
int result = deviceInfoDAOMapper.update(null, updateWrapper);
if (result > 0) {
logger.debug("Update successful");
} else {
logger.warn("Update failed: No rows affected");
}
}
public boolean addDeviceInfo(DeviceInfo deviceInfo) {
try {
DeviceInfoDAO deviceInfoDAO = convertToDeviceInfoDAO(deviceInfo);
int result = deviceInfoDAOMapper.insert(deviceInfoDAO);
logger.info("result: {}",result);
return result > 0;
} catch (Exception e) {
logger.error("Error adding device info", e);
return false;
}
}
/**
* 根据 androidId 查询 DeviceInfo。
*
* @param SN 要查询的 SN。
* @return 与 androidId 对应的 DeviceInfoDAO 对象,如果没有找到,则返回 null。
*/
public DeviceInfoDAO getDeviceInfoBySN(String SN) {
QueryWrapper<DeviceInfoDAO> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("sn", SN);
try {
DeviceInfoDAO deviceInfoDAO = deviceInfoDAOMapper.selectOne(queryWrapper);
if (deviceInfoDAO != null) {
logger.info("Data is queried in the database based on the SN");
return deviceInfoDAO;
}
} catch (Exception e) {
logger.error("Error querying device info by SN", e);
}
return null;
}
/**
* 将 DeviceInfo 转换为 DeviceInfoDAO。
*
* @param deviceInfo 要转换的 DeviceInfo 对象。
* @return 转换后的 DeviceInfoDAO 对象。
*/
private DeviceInfoDAO convertToDeviceInfoDAO(DeviceInfo deviceInfo) {
// 这里需要实现将 DeviceInfo 转换为 DeviceInfoDAO 的逻辑
// 示例代码(根据实际字段调整):
DeviceInfoDAO deviceInfoDAO = new DeviceInfoDAO();
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);
return deviceInfoDAO;
}
}