创建 MonitorService.java
parent
4f1a67f622
commit
b315a4b2df
|
|
@ -0,0 +1,104 @@
|
||||||
|
package com.kimgo.wepush.service;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
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.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;
|
||||||
|
public void monitorOnlineDevices(){
|
||||||
|
List<DeviceInfoDAO> devices = getAllDeviceIds();
|
||||||
|
for (int i = 0; i < devices.size(); i++) {
|
||||||
|
long currentTimeMillis = System.currentTimeMillis();
|
||||||
|
if (currentTimeMillis - devices.get(i).getLastOnlineTime() > devices.get(i).getTimeOutPeriod()){
|
||||||
|
sendNotification(devices.get(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 获取数据库中所有DeviceInfoDAO对象的id。
|
||||||
|
*
|
||||||
|
* @return 包含所有id的列表。
|
||||||
|
*/
|
||||||
|
private List<DeviceInfoDAO> getAllDeviceIds() {
|
||||||
|
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.getErrcode() == 0){
|
||||||
|
logger.info("Send Notification Success");
|
||||||
|
} else if (qyWeChatSendMessageApiResponse.getErrcode() == 42001 || qyWeChatSendMessageApiResponse.getErrcode() == 40014){
|
||||||
|
tokenService.setAccessToken();
|
||||||
|
accessToken = tokenService.getAccessToken();
|
||||||
|
QyWeChatSendMessageApiResponse qyApiResponse = request.okhttpRequest(url,accessToken,deviceInfoDAO);
|
||||||
|
if (qyApiResponse.getErrcode() == 0){
|
||||||
|
logger.info("Send Notification Success");
|
||||||
|
} else {
|
||||||
|
logger.error("Send Notification Fail");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
logger.error("Send Notification Fail");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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'>设备id: " + deviceInfoDAO.getAndroidId() + "</div>" +
|
||||||
|
"<div class='highlight'>设备型号: " + deviceInfoDAO.getDeviceModel() + "</div>" +
|
||||||
|
"<div class='highlight'>检测到掉线时间: " + formattedCurrentTime + "</div>"+
|
||||||
|
"检测到设备已掉线,请及时查看设备状态.");
|
||||||
|
textCard.setUrl("https://kimgo.cn");
|
||||||
|
// 将TextCard对象设置到TextCardMessage中
|
||||||
|
textCardMessage.setTextcard(textCard);
|
||||||
|
|
||||||
|
logger.info("TextCardMessage: {}", textCardMessage.toString());
|
||||||
|
|
||||||
|
return textCardMessage;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue