diff --git a/src/main/java/com/kimgo/wepush/service/MonitorService.java b/src/main/java/com/kimgo/wepush/service/MonitorService.java index d6bf83a..c9d6755 100644 --- a/src/main/java/com/kimgo/wepush/service/MonitorService.java +++ b/src/main/java/com/kimgo/wepush/service/MonitorService.java @@ -60,8 +60,8 @@ public class MonitorService { for (DeviceInfoDAO device : devices) { try { // 如果设备不被监控,则跳过该设备的处理 - if (device.getIsMonitored() != 1){ - logger.info("device is not monitored ,device SN: {}",device.getSN()); + if (device.getIsMonitored() != 1) { + logger.info("device is not monitored ,device SN: {}", device.getSN()); continue; } // 计算设备最后上线时间与当前时间的差值 @@ -80,12 +80,11 @@ public class MonitorService { updateStatus(device, 1); // 更新设备状态为在线 logger.info("update the device status to online."); } - } catch (DatabaseUpdateException dbe){ + } catch (DatabaseUpdateException dbe) { // 更新数据库出错时的处理逻辑 logger.error("update to mysql error."); return; - } - catch (Exception e) { + } catch (Exception e) { // 处理设备信息处理过程中出现的其他异常 logger.error("Error processing device with SN " + device.getSN(), e); } @@ -106,35 +105,57 @@ public class MonitorService { return deviceInfoDAOs; } catch (Exception 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); + // 获取访问令牌 String accessToken = tokenService.getAccessToken(); + // 获取发送文本卡片消息的URL String url = qyWeChatURLService.getSendTextCardMessageUrl(); + // 发送请求,并获取响应 QyWeChatSendMessageApiResponse qyWeChatSendMessageApiResponse = sendQyWeChatMsgRequest.okhttpRequest(url, - accessToken,setTextCardMessage(deviceInfoDAO)); - if (qyWeChatSendMessageApiResponse ==null){ + accessToken, setTextCardMessage(deviceInfoDAO)); + // 如果发送响应为空,则记录发送失败 + if (qyWeChatSendMessageApiResponse == null) { logger.info("Send Notification Fail"); } + // 如果发送响应不为空且错误码为0,记录发送成功 if (qyWeChatSendMessageApiResponse != null && qyWeChatSendMessageApiResponse.getErrcode() == 0) { logger.info("Send Notification Success"); } } + + /** + * 构建并设置文本卡片消息的内容。 + * @param deviceInfoDAO 设备信息数据访问对象,用于获取设备的详细信息。 + * @return TextCardMessage 返回构建完成的文本卡片消息对象。 + */ public TextCardMessage setTextCardMessage(DeviceInfoDAO deviceInfoDAO) { TextCardMessage textCardMessage = new TextCardMessage(); + // 设置消息的基本信息,如接收用户、消息类型和企业ID 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("
设备信息
" + "
品牌: " + deviceInfoDAO.getDeviceBrand() + "
" + "
设备型号: " + deviceInfoDAO.getDeviceModel() + "
" + @@ -143,28 +164,42 @@ public class MonitorService { "
监测到掉线时间: " + formattedCurrentTime + "
" + "检测到设备已掉线,请及时查看设备状态."); textCard.setUrl("https://kimgo.cn"); - // 将TextCard对象设置到TextCardMessage中 + // 将构建的TextCard对象设置到TextCardMessage中 textCardMessage.setTextcard(textCard); + // 记录文本卡片消息的内容 logger.info("TextCardMessage: {}", textCardMessage); return textCardMessage; } + + + /** + * 更新设备的状态信息。 + * @param device 设备信息对象,包含需要更新的设备的SN(序列号)。 + * @param status 需要更新到的新状态。 + * @throws DatabaseUpdateException 如果数据库更新过程中发生异常,则抛出此异常。 + */ private void updateStatus(DeviceInfoDAO device, int status) throws DatabaseUpdateException { try { + // 构建更新条件,设置更新的状态值 UpdateWrapper 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); - throw new DatabaseUpdateException("update to mysql error.",e); + throw new DatabaseUpdateException("update to mysql error.", e); } } + }