Prechádzať zdrojové kódy

优化设备离线邮件提醒,超过1小时提醒

skyline 1 rok pred
rodič
commit
e6c9d53ab7

+ 5 - 5
admin/src/main/java/com/kym/admin/jobs/OfflineNotifyJob.java

@@ -35,11 +35,11 @@ public class OfflineNotifyJob {
         this.redisTemplate = redisTemplate;
     }
 
-    // 每5min执行一次
-    @Scheduled(cron = "0 0/5 * * * ? ")
+    // 每小时的第30分钟执行一次
+    @Scheduled(cron = "0 30 0/1 * * ? ")
     public void execute() {
-        // 查询redis离线设备消息队列,存在且超过5min的消息则发送邮件提醒
-        Set<String> offlineList = redisTemplate.opsForZSet().rangeByScore(RedisKeys.OFFLINE, System.currentTimeMillis() + 5 * 60 * 1000, System.currentTimeMillis() + 10 * 60 * 1000);
+        // 查询redis离线设备消息队列,存在且超过1小时的消息则发送邮件提醒
+        Set<String> offlineList = redisTemplate.opsForZSet().rangeByScore(RedisKeys.OFFLINE, System.currentTimeMillis() - 60 * 60 * 1000, System.currentTimeMillis() + 60 * 60 * 1000);
         if (!CommUtil.isEmptyOrNull(offlineList)) {
             /*
              * 格式:
@@ -49,7 +49,7 @@ public class OfflineNotifyJob {
              */
             var temp = "【%s】:%s\n";
             var map = new ArrayList<String>(offlineList).stream()
-                    .collect(Collectors.toMap(KymCache.INSTANCE::getShortIdByEquipmentIdOrConnectorId, KymCache.INSTANCE::getStationNameById));
+                    .collect(Collectors.toMap(KymCache.INSTANCE::getShortIdByEquipmentIdOrConnectorId, KymCache.INSTANCE::getStationNameByConnectorId));
             // 以站点名称分组
             var list = map.entrySet().stream().collect(Collectors.groupingBy(Map.Entry::getValue));
             StringBuilder sb = new StringBuilder();

+ 1 - 1
admin/src/main/resources/mail.setting

@@ -7,6 +7,6 @@ from = system@kuaiyuman.cn
 # 用户名,默认为发件人邮箱前缀,如不行请回到邮箱登陆页查看登录时使用的用户名
 user = system@kuaiyuman.cn
 # 密码(注意,某些邮箱需要为SMTP服务单独设置授权码,详情查看相关帮助)
-pass = jRb8hA48ynfFFjzb
+pass = H5fRhYVkk8sD5vaH
 #使用 STARTTLS安全连接,STARTTLS是对纯文本通信协议的扩展。
 starttlsEnable = true

+ 20 - 1
service/src/main/java/com/kym/service/cache/KymCache.java

@@ -1,9 +1,11 @@
 package com.kym.service.cache;
 
 import cn.hutool.extra.spring.SpringUtil;
+import com.kym.entity.admin.ConnectorInfo;
 import com.kym.entity.admin.EquipmentRelation;
 import com.kym.entity.admin.Station;
 import com.kym.entity.common.RedisKeys;
+import com.kym.service.admin.ConnectorInfoService;
 import com.kym.service.admin.EquipmentRelationService;
 import com.kym.service.admin.StationService;
 import org.springframework.boot.context.event.ApplicationStartedEvent;
@@ -100,6 +102,17 @@ public enum KymCache {
         return KymCacheInjector.redisTemplate.opsForValue().get(RedisKeys.STATION_ID_TO_NAME + stationId);
     }
 
+    /**
+     * 通过充电接口id获取站点名称
+     *
+     * @param connectorId
+     * @return
+     */
+    public String getStationNameByConnectorId(String connectorId) {
+        var stationId = getStationIdByEquipmentIdOrConnectorId(connectorId);
+        return KymCacheInjector.redisTemplate.opsForValue().get(RedisKeys.STATION_ID_TO_NAME + stationId);
+    }
+
     /**
      * 操作员对应有权限的站点
      *
@@ -161,10 +174,13 @@ public enum KymCache {
         private final EquipmentRelationService equipmentRelationService;
         private final StationService stationService;
 
+        private final ConnectorInfoService connectorInfoService;
 
-        private KymCacheInjector(EquipmentRelationService equipmentRelationService, StationService stationService) {
+
+        private KymCacheInjector(EquipmentRelationService equipmentRelationService, StationService stationService, ConnectorInfoService connectorInfoService) {
             this.equipmentRelationService = equipmentRelationService;
             this.stationService = stationService;
+            this.connectorInfoService = connectorInfoService;
         }
 
         @Override
@@ -183,6 +199,9 @@ public enum KymCache {
 
             var connectorId2ParkingNo = equipmentRelations.stream().collect(Collectors.toMap(EquipmentRelation::getConnectorId, EquipmentRelation::getParkingNo));
             KymCache.INSTANCE.putConnectorId2ParkingNo(connectorId2ParkingNo);
+
+            var ConnectorId2Status = connectorInfoService.list().stream().collect(Collectors.toMap(ConnectorInfo::getConnectorId, ConnectorInfo::getStatus));
+            KymCache.INSTANCE.putConnectorId2Status(ConnectorId2Status);
         }
     }
 }

+ 2 - 2
service/src/main/java/com/kym/service/enplus/impl/EnNotifyServiceImpl.java

@@ -119,8 +119,8 @@ public class EnNotifyServiceImpl implements EnNotifyService {
                     .setOfflineStatus(connectorStatusInfo.getStatus());
             monitorLogService.save(monitorLog);
 
-            // 离线设备放入队列,5分钟之后如果还未恢复则放入长时间离线设备集合中并发送提醒,上线后发送提醒
-            redisTemplate.opsForZSet().add(RedisKeys.OFFLINE, connectorStatusInfo.getConnectorId(), System.currentTimeMillis() + 5 * 60 * 1000);
+            // 离线设备放入队列,60分钟之后如果还未恢复则放入长时间离线设备集合中并发送提醒,上线后发送提醒
+            redisTemplate.opsForZSet().add(RedisKeys.OFFLINE, connectorStatusInfo.getConnectorId(), System.currentTimeMillis() + 60 * 60 * 1000);
         } else {
             // 先删除离线设备队列的记录,再删除离线超时队列中的记录
             var isDelete = redisTemplate.opsForZSet().remove(RedisKeys.OFFLINE, connectorStatusInfo.getConnectorId());