Update MonitorService.java

pull/1/head
wangsiyuan 2024-04-19 16:50:59 +08:00
parent 3d01fffebe
commit f9c47e25e4
1 changed files with 46 additions and 11 deletions

View File

@ -60,8 +60,8 @@ public class MonitorService {
for (DeviceInfoDAO device : devices) { for (DeviceInfoDAO device : devices) {
try { try {
// 如果设备不被监控,则跳过该设备的处理 // 如果设备不被监控,则跳过该设备的处理
if (device.getIsMonitored() != 1){ if (device.getIsMonitored() != 1) {
logger.info("device is not monitored ,device SN: {}",device.getSN()); logger.info("device is not monitored ,device SN: {}", device.getSN());
continue; continue;
} }
// 计算设备最后上线时间与当前时间的差值 // 计算设备最后上线时间与当前时间的差值
@ -80,12 +80,11 @@ public class MonitorService {
updateStatus(device, 1); // 更新设备状态为在线 updateStatus(device, 1); // 更新设备状态为在线
logger.info("update the device status to online."); logger.info("update the device status to online.");
} }
} catch (DatabaseUpdateException dbe){ } catch (DatabaseUpdateException dbe) {
// 更新数据库出错时的处理逻辑 // 更新数据库出错时的处理逻辑
logger.error("update to mysql error."); logger.error("update to mysql error.");
return; return;
} } catch (Exception e) {
catch (Exception e) {
// 处理设备信息处理过程中出现的其他异常 // 处理设备信息处理过程中出现的其他异常
logger.error("Error processing device with SN " + device.getSN(), e); logger.error("Error processing device with SN " + device.getSN(), e);
} }
@ -106,35 +105,57 @@ public class MonitorService {
return deviceInfoDAOs; return deviceInfoDAOs;
} catch (Exception e) { } catch (Exception e) {
logger.error("Error querying all device info IDs", e); logger.error("Error querying all device info IDs", e);
throw new DatabaseQueryException("get all devices error.",e); throw new DatabaseQueryException("get all devices error.", e);
} }
} }
private void sendNotification(DeviceInfoDAO deviceInfoDAO){ /**
*
*
* @param deviceInfoDAO 访
*
*/
private void sendNotification(DeviceInfoDAO deviceInfoDAO) {
// 创建发送企业微信消息的请求对象,并初始化它
SendQyWeChatMsgRequest sendQyWeChatMsgRequest = new SendQyWeChatMsgRequest(tokenService); SendQyWeChatMsgRequest sendQyWeChatMsgRequest = new SendQyWeChatMsgRequest(tokenService);
// 获取访问令牌
String accessToken = tokenService.getAccessToken(); String accessToken = tokenService.getAccessToken();
// 获取发送文本卡片消息的URL
String url = qyWeChatURLService.getSendTextCardMessageUrl(); String url = qyWeChatURLService.getSendTextCardMessageUrl();
// 发送请求,并获取响应
QyWeChatSendMessageApiResponse qyWeChatSendMessageApiResponse = sendQyWeChatMsgRequest.okhttpRequest(url, QyWeChatSendMessageApiResponse qyWeChatSendMessageApiResponse = sendQyWeChatMsgRequest.okhttpRequest(url,
accessToken,setTextCardMessage(deviceInfoDAO)); accessToken, setTextCardMessage(deviceInfoDAO));
if (qyWeChatSendMessageApiResponse ==null){ // 如果发送响应为空,则记录发送失败
if (qyWeChatSendMessageApiResponse == null) {
logger.info("Send Notification Fail"); logger.info("Send Notification Fail");
} }
// 如果发送响应不为空且错误码为0记录发送成功
if (qyWeChatSendMessageApiResponse != null && qyWeChatSendMessageApiResponse.getErrcode() == 0) { if (qyWeChatSendMessageApiResponse != null && qyWeChatSendMessageApiResponse.getErrcode() == 0) {
logger.info("Send Notification Success"); logger.info("Send Notification Success");
} }
} }
/**
*
* @param deviceInfoDAO 访
* @return TextCardMessage
*/
public TextCardMessage setTextCardMessage(DeviceInfoDAO deviceInfoDAO) { public TextCardMessage setTextCardMessage(DeviceInfoDAO deviceInfoDAO) {
TextCardMessage textCardMessage = new TextCardMessage(); TextCardMessage textCardMessage = new TextCardMessage();
// 设置消息的基本信息如接收用户、消息类型和企业ID
textCardMessage.setTouser(apiSettingService.getApiSetting().getTouser()); textCardMessage.setTouser(apiSettingService.getApiSetting().getTouser());
textCardMessage.setMsgtype(apiSettingService.getApiSetting().getMsgtype()); textCardMessage.setMsgtype(apiSettingService.getApiSetting().getMsgtype());
textCardMessage.setAgentid(apiSettingService.getApiSetting().getAgentid()); textCardMessage.setAgentid(apiSettingService.getApiSetting().getAgentid());
textCardMessage.setEnable_duplicate_check(0); textCardMessage.setEnable_duplicate_check(0);
// 创建文本卡片对象,并设置其内容
TextCardMessage.TextCard textCard = new TextCardMessage.TextCard(); TextCardMessage.TextCard textCard = new TextCardMessage.TextCard();
textCard.setTitle("设备掉线通知"); textCard.setTitle("设备掉线通知");
// 使用当前时间格式化字符串
String formattedCurrentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); String formattedCurrentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
// 设置卡片的描述信息,包含设备的详细掉线通知
textCard.setDescription("<div class='gray'>设备信息</div>" + textCard.setDescription("<div class='gray'>设备信息</div>" +
"<div class='highlight'>品牌: " + deviceInfoDAO.getDeviceBrand() + "</div>" + "<div class='highlight'>品牌: " + deviceInfoDAO.getDeviceBrand() + "</div>" +
"<div class='highlight'>设备型号: " + deviceInfoDAO.getDeviceModel() + "</div>" + "<div class='highlight'>设备型号: " + deviceInfoDAO.getDeviceModel() + "</div>" +
@ -143,28 +164,42 @@ public class MonitorService {
"<div class='highlight'>监测到掉线时间: " + formattedCurrentTime + "</div>" + "<div class='highlight'>监测到掉线时间: " + formattedCurrentTime + "</div>" +
"检测到设备已掉线,请及时查看设备状态."); "检测到设备已掉线,请及时查看设备状态.");
textCard.setUrl("https://kimgo.cn"); textCard.setUrl("https://kimgo.cn");
// 将TextCard对象设置到TextCardMessage中 // 将构建的TextCard对象设置到TextCardMessage中
textCardMessage.setTextcard(textCard); textCardMessage.setTextcard(textCard);
// 记录文本卡片消息的内容
logger.info("TextCardMessage: {}", textCardMessage); logger.info("TextCardMessage: {}", textCardMessage);
return textCardMessage; return textCardMessage;
} }
/**
*
* @param device SN
* @param status
* @throws DatabaseUpdateException
*/
private void updateStatus(DeviceInfoDAO device, int status) throws DatabaseUpdateException { private void updateStatus(DeviceInfoDAO device, int status) throws DatabaseUpdateException {
try { try {
// 构建更新条件,设置更新的状态值
UpdateWrapper<DeviceInfoDAO> updateWrapper = new UpdateWrapper<>(); UpdateWrapper<DeviceInfoDAO> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("SN", device.getSN()); updateWrapper.eq("SN", device.getSN());
updateWrapper.set("status", status); updateWrapper.set("status", status);
// 执行更新操作
int result = deviceInfoDAOMapper.update(null, updateWrapper); int result = deviceInfoDAOMapper.update(null, updateWrapper);
// 根据更新结果进行日志记录
if (result > 0) { if (result > 0) {
logger.debug("Update successful for SN: " + device.getSN()); logger.debug("Update successful for SN: " + device.getSN());
} else { } else {
logger.warn("Update failed: No rows affected for SN: " + device.getSN()); logger.warn("Update failed: No rows affected for SN: " + device.getSN());
} }
} catch (Exception e) { } catch (Exception e) {
// 捕获异常,记录错误日志,并抛出数据库更新异常
logger.error("Error updating status for SN: " + device.getSN(), e); logger.error("Error updating status for SN: " + device.getSN(), e);
throw new DatabaseUpdateException("update to mysql error.",e); throw new DatabaseUpdateException("update to mysql error.", e);
} }
} }
} }