Kaynağa Gözat

Merge branch 'dev' of http://192.168.1.43:3000/v2/zd-parents into dev

chaiyunlong 2 yıl önce
ebeveyn
işleme
7afb6623e0

+ 59 - 0
zd-common/common-core/src/main/java/com/zd/common/core/alert/DingTalkAlert.java

@@ -0,0 +1,59 @@
+package com.zd.common.core.alert;
+
+import com.zd.common.core.utils.HttpUtils;
+import com.zd.common.core.utils.StringUtils;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+/**
+ * <p>钉钉告警</p>
+ *
+ * @author linft
+ * @version 1.0
+ * @date 3/2/2023
+ */
+@Component
+public class DingTalkAlert {
+
+    /**
+     * 告警类应用
+     */
+    private static String SUBJECT = "设备监控";
+
+    private static Boolean dingTalkNotice;
+    private static String dingTalkSubject;
+    private static String dingTalkUrl;
+
+    @Value("${monitor.alert.dingTalkNotice}")
+    public void setDingTalkNotice(Boolean dingTalkNotice) {
+        this.dingTalkNotice = dingTalkNotice;
+    }
+
+    @Value("${monitor.alert.dingTalkSubject}")
+    public void setDingTalkSubject(String dingTalkSubject) {
+        this.dingTalkSubject = dingTalkSubject;
+    }
+
+    @Value("${monitor.alert.dingTalkUrl}")
+    public void setDingTalkUrl(String dingTalkUrl) {
+        this.dingTalkUrl = dingTalkUrl;
+    }
+
+    /**
+     * 发送告警
+     * @param content 告警内容描述
+     * @return
+     */
+    public static String sendAlert(String content) {
+        if (dingTalkNotice != null && dingTalkNotice && StringUtils.isNotEmpty(dingTalkUrl)) {
+            StringBuffer buffer = new StringBuffer();
+            buffer.append("【" + dingTalkSubject +"--"+ SUBJECT + "】\n");
+            buffer.append(content);
+            //封装请求内容
+            String msg = "{\"msgtype\": \"text\",\"text\": {\"content\":\"" + buffer.toString() + "\"}}";
+            return HttpUtils.sendPost(dingTalkUrl, msg, "application/json", null);
+        }
+        return "告警未启动或参数未配置!";
+    }
+
+}

+ 1 - 1
zd-model/src/main/java/com/zd/model/domain/R.java

@@ -36,7 +36,7 @@ public class R<T> implements Serializable {
     }
     }
 
 
     public static <T> R<T> ok(T data) {
     public static <T> R<T> ok(T data) {
-        return restResult(data, SUCCESS, null);
+        return restResult(data, SUCCESS, "操作成功");
     }
     }
 
 
     public static <T> R<T> ok(T data, Integer count) {
     public static <T> R<T> ok(T data, Integer count) {

+ 4 - 9
zd-modules/zd-algorithm/src/main/java/com/zd/alg/monitor/HardwareDeviceMonitor.java

@@ -2,6 +2,7 @@ package com.zd.alg.monitor;
 
 
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.alibaba.fastjson.JSONObject;
+import com.zd.common.core.alert.DingTalkAlert;
 import com.zd.common.core.redis.RedisService;
 import com.zd.common.core.redis.RedisService;
 import com.zd.common.core.utils.HttpUtils;
 import com.zd.common.core.utils.HttpUtils;
 import com.zd.common.core.utils.StringUtils;
 import com.zd.common.core.utils.StringUtils;
@@ -39,12 +40,6 @@ public class HardwareDeviceMonitor {
     @Value("${monitor.alert.dingTalkNotice}")
     @Value("${monitor.alert.dingTalkNotice}")
     private Boolean dingTalkNotice;
     private Boolean dingTalkNotice;
 
 
-    @Value("${monitor.alert.dingTalkSubject}")
-    private String dingTalkSubject;
-
-    @Value("${monitor.alert.dingTalkUrl}")
-    private String dingTalkUrl;
-
     private String redisPrefix = "HARDWARE_DEVICE_MONITOR:";
     private String redisPrefix = "HARDWARE_DEVICE_MONITOR:";
 
 
     private long expire = 30L;
     private long expire = 30L;
@@ -57,7 +52,7 @@ public class HardwareDeviceMonitor {
 
 
     @Scheduled(cron = "0 */9 * * * ?")
     @Scheduled(cron = "0 */9 * * * ?")
     public void checkPowerUse() {
     public void checkPowerUse() {
-        if (dingTalkNotice != null && dingTalkNotice && StringUtils.isNotEmpty(dingTalkUrl)) {
+        if (dingTalkNotice != null && dingTalkNotice) {
             log.info("【设备监听提醒】 开始执行检查..............");
             log.info("【设备监听提醒】 开始执行检查..............");
             ResultData result = remoteLabHardwareService.statistics();
             ResultData result = remoteLabHardwareService.statistics();
             if (result != null && result.getCode() == HttpStatus.SUCCESS && result.getData() != null) {
             if (result != null && result.getCode() == HttpStatus.SUCCESS && result.getData() != null) {
@@ -86,7 +81,7 @@ public class HardwareDeviceMonitor {
                             if (!map.isEmpty()) {
                             if (!map.isEmpty()) {
                                 Set<String> code = map.keySet();
                                 Set<String> code = map.keySet();
                                 StringBuffer buffer = new StringBuffer();
                                 StringBuffer buffer = new StringBuffer();
-                                buffer.append("【"+dingTalkSubject+"-设备掉线提醒】离线继电器信息:\n");
+                                buffer.append("系统接入继电器设备离线信息:\n");
                                 boolean isSend = Boolean.FALSE;
                                 boolean isSend = Boolean.FALSE;
                                 for (String key : code) {
                                 for (String key : code) {
                                     Integer num = redisService.getCacheObject(redisPrefix+key);
                                     Integer num = redisService.getCacheObject(redisPrefix+key);
@@ -102,7 +97,7 @@ public class HardwareDeviceMonitor {
                                 //通知
                                 //通知
                                 if (isSend) {
                                 if (isSend) {
                                     String msg = "{\"msgtype\": \"text\",\"text\": {\"content\":\"" + buffer.toString() + "\"}}";
                                     String msg = "{\"msgtype\": \"text\",\"text\": {\"content\":\"" + buffer.toString() + "\"}}";
-                                    String rs = HttpUtils.sendPost(dingTalkUrl, msg, "application/json", null);
+                                    DingTalkAlert.sendAlert(msg);
                                     //log.info("【设备监听提醒】 钉钉通知响应:" + rs);
                                     //log.info("【设备监听提醒】 钉钉通知响应:" + rs);
                                 }
                                 }
                             }
                             }