Browse Source

设备离线上线邮件通知

skyline 2 năm trước cách đây
mục cha
commit
ab45395e28

+ 3 - 0
entity/src/main/java/com/kym/entity/admin/MonitorLog.java

@@ -24,6 +24,9 @@ public class MonitorLog extends BaseEntity {
 
     private static final long serialVersionUID = 1L;
 
+    public static int IS_RECOVER_未恢复;
+    public static int IS_RECOVER_已恢复;
+
     /**
      * 站点id
      */

+ 2 - 5
mapper/pom.xml

@@ -37,16 +37,13 @@
             <version>2.3.32</version>
         </dependency>
 
-
-
         <!-- 多数据源配置 -->
         <dependency>
             <groupId>com.baomidou</groupId>
-            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
-            <version>3.6.0</version>
+            <artifactId>dynamic-datasource-spring-boot3-starter</artifactId>
+            <version>4.1.3</version>
         </dependency>
 
-
         <dependency>
             <groupId>com.alibaba</groupId>
             <artifactId>druid-spring-boot-starter</artifactId>

+ 4 - 1
miniapp/src/main/resources/application-dev.yml

@@ -86,4 +86,7 @@ spring:
     type: redis
     redis:
       # 缓存过期时间:7天
-      time-to-live: 604800
+      time-to-live: 604800
+
+kym:
+  notify-email: skyline@kuaiyuman.cn

+ 4 - 1
miniapp/src/main/resources/application-prod.yml

@@ -86,4 +86,7 @@ spring:
     type: redis
     redis:
       # 缓存过期时间:7天
-      time-to-live: 604800
+      time-to-live: 604800
+
+kym:
+  notify-email: zaizai@kuaiyuman.cn,skyline@kuaiyuman.cn

+ 10 - 6
miniapp/src/main/resources/mail.setting

@@ -1,8 +1,12 @@
-# 发件人
+# 邮件服务器的SMTP地址,可选,默认为smtp.<发件人邮箱后缀>
+host = smtp.exmail.qq.com
+# 邮件服务器的SMTP端口,可选,默认25,企业邮箱中配置的为465
+port = 465
+# 发件人(必须正确,否则发送失败)
 from = system@kuaiyuman.cn
-# 用户名,默认为发件人邮箱前缀
-user = system
+# 用户名,默认为发件人邮箱前缀,如不行请回到邮箱登陆页查看登录时使用的用户名
+user = system@kuaiyuman.cn
 # 密码(注意,某些邮箱需要为SMTP服务单独设置授权码,详情查看相关帮助)
-pass = Uocj6qhGXeaqUAUC
-# 使用SSL安全连接
-sslEnable = true
+pass = jRb8hA48ynfFFjzb
+#使用 STARTTLS安全连接,STARTTLS是对纯文本通信协议的扩展。
+starttlsEnable = true

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

@@ -2,6 +2,7 @@ package com.kym.service.enplus.impl;
 
 import cn.hutool.extra.mail.MailUtil;
 import com.alibaba.fastjson2.JSONObject;
+import com.baomidou.dynamic.datasource.annotation.DS;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.kym.entity.admin.MonitorLog;
 import com.kym.entity.common.RedisKeys;
@@ -17,6 +18,7 @@ import com.kym.service.miniapp.WalletDetailService;
 import com.kym.service.utils.KymCache;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -49,6 +51,9 @@ public class EnNotifyServiceImpl implements EnNotifyService {
 
     private final RedisTemplate<String, String> redisTemplate;
 
+    @Value("${kym.notify-email}")
+    private String notifyEmail;
+
     public EnNotifyServiceImpl(EnPlusService enPlusService, ChargeOrderService chargeOrderService, AccountService accountService, WalletDetailService walletDetailService, MonitorLogService monitorLogService, KymCache kymCache, RedisTemplate<String, String> redisTemplate) {
         this.enPlusService = enPlusService;
         this.chargeOrderService = chargeOrderService;
@@ -60,7 +65,6 @@ public class EnNotifyServiceImpl implements EnNotifyService {
     }
 
 
-
     /**
      * EN+ 充电站设备状态变化推送
      *
@@ -68,6 +72,7 @@ public class EnNotifyServiceImpl implements EnNotifyService {
      * @return
      */
     @Override
+    @DS("db-admin")
     public String handleNotificationStationStatus(JSONObject json) {
         // TODO: 2023-08-30 注意!!!EN+分布式事务BUG导致无法接收到离线状态(0)的推送,待对方解决后再开发测试
         var data = enPlusService.signValidation(json);
@@ -84,18 +89,21 @@ public class EnNotifyServiceImpl implements EnNotifyService {
                     .setOfflineStatus(connectorStatusInfo.getStatus());
             monitorLogService.save(monitorLog);
             redisTemplate.opsForValue().set(RedisKeys.OFFLINE.concat(connectorStatusInfo.getConnectorId()), "", 1, TimeUnit.DAYS);
-            MailUtil.send("skyline@kuaiyuman.cn", "通知", "站点:%s,设备%s离线".formatted(monitorLog.getStationId(), monitorLog.getSn()), false);
+            MailUtil.send(notifyEmail, "【设备离线通知】", "站点:%s,设备%s离线"
+                    .formatted(kymCache.getStationName(monitorLog.getStationId()), kymCache.getShortId(monitorLog.getSn())), false);
         } else {
             // 查询redis是否有记录,则是之前离线的机器上线了,有就删除并更新数据库恢复时间
             var exist = redisTemplate.hasKey(RedisKeys.OFFLINE.concat(connectorStatusInfo.getConnectorId()));
             if (Boolean.TRUE.equals(exist)) {
                 monitorLogService.lambdaUpdate()
                         .eq(MonitorLog::getSn, connectorStatusInfo.getConnectorId())
-                        .eq(MonitorLog::getIsRecover, 0) // 未恢复的记录
+                        .eq(MonitorLog::getIsRecover, MonitorLog.IS_RECOVER_未恢复) // 未恢复的记录
                         .set(MonitorLog::getRecoverTime, LocalDateTime.now())
-                        .set(MonitorLog::getIsRecover, 1) // 设置为已恢复
+                        .set(MonitorLog::getIsRecover, MonitorLog.IS_RECOVER_已恢复) // 设置为已恢复
                         .update();
                 redisTemplate.delete(RedisKeys.OFFLINE.concat(connectorStatusInfo.getConnectorId()));
+                MailUtil.send(notifyEmail, "【设备上线通知】", "站点:%s,设备%s恢复上线"
+                        .formatted(kymCache.getStationNameByConnectorId(connectorStatusInfo.getConnectorId()), kymCache.getShortId(connectorStatusInfo.getConnectorId())), false);
             }
         }
 

+ 8 - 0
service/src/main/java/com/kym/service/utils/KymCache.java

@@ -30,6 +30,9 @@ public class KymCache {
     }
 
     public String getShortId(String equipmentId) {
+        if (equipmentId.length() == 17) {
+            equipmentId = equipmentId.substring(0, 16);
+        }
         return SHORT_ID_MAPPING.get(equipmentId);
     }
 
@@ -37,6 +40,11 @@ public class KymCache {
         return CONNECTOR_STATION_MAPPING.get(connectorId);
     }
 
+    public String getStationNameByConnectorId(String connectorId) {
+        var stationId = CONNECTOR_STATION_MAPPING.get(connectorId);
+        return getStationName(stationId);
+    }
+
     public String getStationName(String stationId) {
         return STATION_MAPPING.get(stationId);
     }