116 lines
4.6 KiB
Java
116 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.model.QyWeChatAppInfo;
|
|
import com.kimgo.wepush.model.QyWeChatURL;
|
|
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;
|
|
|
|
import java.sql.Time;
|
|
|
|
@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 = getDeviceInfoByAndroidId(deviceInfo.getAndroidId());
|
|
if (deviceInfoDAO == null) {
|
|
// 设备信息不存在,可能是无效的设备
|
|
addDeviceInfo(deviceInfo);
|
|
return ServerResponseEntity.success();
|
|
}
|
|
try {
|
|
updateClientStatus(deviceInfo);
|
|
return ServerResponseEntity.success();
|
|
} catch (Exception e) {
|
|
// 处理更新状态时的异常
|
|
return ServerResponseEntity.fail("Error updating client status");
|
|
}
|
|
}
|
|
|
|
|
|
public void updateClientStatus(DeviceInfo deviceInfo) {
|
|
long currentTimeMillis = System.currentTimeMillis();
|
|
// 更新该客户端的最后活跃时间
|
|
|
|
UpdateWrapper<DeviceInfoDAO> updateWrapper = new UpdateWrapper<>();
|
|
updateWrapper.eq("android_id", deviceInfo.getAndroidId());
|
|
updateWrapper.set("last_online_time", currentTimeMillis); // 设置新的 accessToken
|
|
|
|
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 androidId 要查询的 Android ID。
|
|
* @return 与 androidId 对应的 DeviceInfoDAO 对象,如果没有找到,则返回 null。
|
|
*/
|
|
public DeviceInfoDAO getDeviceInfoByAndroidId(String androidId) {
|
|
QueryWrapper<DeviceInfoDAO> queryWrapper = new QueryWrapper<>();
|
|
queryWrapper.eq("android_id", androidId);
|
|
try {
|
|
DeviceInfoDAO deviceInfoDAO = deviceInfoDAOMapper.selectOne(queryWrapper);
|
|
if (deviceInfoDAO != null) {
|
|
logger.info("Data is queried in the database based on the device id");
|
|
return deviceInfoDAO;
|
|
}
|
|
} catch (Exception e) {
|
|
logger.error("Error querying device info by android ID", e);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
|
|
/**
|
|
* 将 DeviceInfo 转换为 DeviceInfoDAO。
|
|
*
|
|
* @param deviceInfo 要转换的 DeviceInfo 对象。
|
|
* @return 转换后的 DeviceInfoDAO 对象。
|
|
*/
|
|
private DeviceInfoDAO convertToDeviceInfoDAO(DeviceInfo deviceInfo) {
|
|
// 这里需要实现将 DeviceInfo 转换为 DeviceInfoDAO 的逻辑
|
|
// 示例代码(根据实际字段调整):
|
|
DeviceInfoDAO deviceInfoDAO = new DeviceInfoDAO();
|
|
deviceInfoDAO.setAndroidId(deviceInfo.getAndroidId());
|
|
deviceInfoDAO.setSerialNumber(deviceInfo.getSerialNumber());
|
|
deviceInfoDAO.setDeviceModel(deviceInfo.getDeviceModel());
|
|
deviceInfoDAO.setTimeOutPeriod(TIME_OUT_PERIOD);
|
|
deviceInfoDAO.setLastOnlineTime(System.currentTimeMillis());
|
|
deviceInfoDAO.setStatus(1);
|
|
return deviceInfoDAO;
|
|
}
|
|
}
|