138 lines
6.1 KiB
Java
138 lines
6.1 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.DeviceInfoDAO;
|
|
import com.kimgo.wepush.model.TextCardMessage;
|
|
import com.kimgo.wepush.request.Request;
|
|
import com.kimgo.wepush.response.QyWeChatSendMessageApiResponse;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.time.LocalDateTime;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
|
|
@Service
|
|
public class MonitorService {
|
|
@Autowired
|
|
private ApiSettingService apiSettingService;
|
|
@Autowired
|
|
private QyWeChatURLService qyWeChatURLService;
|
|
@Autowired
|
|
private TokenService tokenService;
|
|
private final Logger logger = LoggerFactory.getLogger(MonitorService.class);
|
|
@Autowired
|
|
private DeviceInfoDAOMapper deviceInfoDAOMapper;
|
|
|
|
@Scheduled(fixedDelay = 480000)
|
|
public void monitorOnlineDevices() {
|
|
List<DeviceInfoDAO> devices = getAllDevices();
|
|
if (devices.isEmpty()) {
|
|
// 没有找到数据的处理逻辑
|
|
logger.info("no data was queried.");
|
|
}
|
|
for (DeviceInfoDAO device : devices) {
|
|
try {
|
|
if (device.getIsMonitored() != 1){
|
|
logger.info("device is not monitored ,device SN: {}",device.getSN());
|
|
continue;
|
|
}
|
|
long currentTimeMillis = System.currentTimeMillis();
|
|
long timeDifferenceMillis = currentTimeMillis - device.getLastOnlineTime();
|
|
long timeDifferenceSeconds = timeDifferenceMillis / 1000;
|
|
if (timeDifferenceSeconds > device.getTimeOutPeriod()) {
|
|
logger.info("currentTimeMillis: {},device LastOnlineTime: {},时间差: {},设备超时时间: {}",currentTimeMillis,
|
|
device.getLastOnlineTime(),timeDifferenceSeconds,device.getTimeOutPeriod());
|
|
updateStatus(device, 0); // 设备离线
|
|
logger.info("Update the device status to offline.");
|
|
sendNotification(device);
|
|
} else {
|
|
updateStatus(device, 1); // 设备在线
|
|
logger.info("update the device status to online.");
|
|
}
|
|
} catch (Exception e) {
|
|
logger.error("Error processing device with SN " + device.getSN(), e);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取数据库中所有DeviceInfoDAO对象的id。
|
|
*
|
|
* @return 包含所有id的列表。
|
|
*/
|
|
private List<DeviceInfoDAO> getAllDevices() {
|
|
try {
|
|
// 查询所有DeviceInfoDAO对象
|
|
List<DeviceInfoDAO> deviceInfoDAOs = deviceInfoDAOMapper.selectList(new QueryWrapper<>());
|
|
// 提取并返回所有对象的id
|
|
return deviceInfoDAOs;
|
|
} catch (Exception e) {
|
|
logger.error("Error querying all device info IDs", e);
|
|
}
|
|
return Collections.emptyList();
|
|
}
|
|
|
|
|
|
private void sendNotification(DeviceInfoDAO deviceInfoDAO){
|
|
Request request = new Request();
|
|
String accessToken = tokenService.getAccessToken();
|
|
String url = qyWeChatURLService.getSendTextCardMessageUrl() + "?access_token=" + accessToken;
|
|
QyWeChatSendMessageApiResponse qyWeChatSendMessageApiResponse = request.okhttpRequest(url,accessToken,setTextCardMessage(deviceInfoDAO));
|
|
if (qyWeChatSendMessageApiResponse ==null){
|
|
logger.info("Send Notification Fail");
|
|
}
|
|
if (qyWeChatSendMessageApiResponse != null && qyWeChatSendMessageApiResponse.getErrcode() == 0) {
|
|
logger.info("Send Notification Success");
|
|
}
|
|
}
|
|
public TextCardMessage setTextCardMessage(DeviceInfoDAO deviceInfoDAO) {
|
|
TextCardMessage textCardMessage = new TextCardMessage();
|
|
|
|
textCardMessage.setTouser(apiSettingService.getApiSetting().getTouser());
|
|
textCardMessage.setMsgtype(apiSettingService.getApiSetting().getMsgtype());
|
|
textCardMessage.setAgentid(apiSettingService.getApiSetting().getAgentid());
|
|
textCardMessage.setEnable_duplicate_check(0);
|
|
|
|
TextCardMessage.TextCard textCard = new TextCardMessage.TextCard();
|
|
textCard.setTitle("设备掉线通知");
|
|
String formattedCurrentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
|
textCard.setDescription("<div class='gray'>设备信息</div>" +
|
|
"<div class='highlight'>品牌: " + deviceInfoDAO.getDeviceBrand() + "</div>" +
|
|
"<div class='highlight'>设备型号: " + deviceInfoDAO.getDeviceModel() + "</div>" +
|
|
"<div class='highlight'>安卓版本: " + deviceInfoDAO.getAndroidVersion() + "</div>" +
|
|
"<div class='highlight'>SN: " + deviceInfoDAO.getSN() + "</div>" +
|
|
"<div class='highlight'>监测到掉线时间: " + formattedCurrentTime + "</div>" +
|
|
"检测到设备已掉线,请及时查看设备状态.");
|
|
textCard.setUrl("https://kimgo.cn");
|
|
// 将TextCard对象设置到TextCardMessage中
|
|
textCardMessage.setTextcard(textCard);
|
|
|
|
logger.info("TextCardMessage: {}", textCardMessage);
|
|
|
|
return textCardMessage;
|
|
}
|
|
private void updateStatus(DeviceInfoDAO device, int status) {
|
|
try {
|
|
UpdateWrapper<DeviceInfoDAO> updateWrapper = new UpdateWrapper<>();
|
|
updateWrapper.eq("SN", device.getSN());
|
|
updateWrapper.set("status", status);
|
|
|
|
int result = deviceInfoDAOMapper.update(null, updateWrapper);
|
|
if (result > 0) {
|
|
logger.debug("Update successful for SN: " + device.getSN());
|
|
} else {
|
|
logger.warn("Update failed: No rows affected for SN: " + device.getSN());
|
|
}
|
|
} catch (Exception e) {
|
|
logger.error("Error updating status for SN: " + device.getSN(), e);
|
|
}
|
|
}
|
|
}
|