Browse Source

告警阶段提交,调整离线预警内容

yf_zn 9 months ago
parent
commit
aca800952a

+ 1 - 1
src/main/java/com/yunfeiyun/agmp/iots/warn/service/WarnService.java

@@ -86,7 +86,7 @@ public class WarnService {
                     warnResult.setConfig(warnConfigInfo);
                     warnResult.setConfig(warnConfigInfo);
                     warnResult.setOffline(true);
                     warnResult.setOffline(true);
                     warnResult.setTriggered(true);
                     warnResult.setTriggered(true);
-                    warnResult.setMessage(WarnMessageBuilderUtil.buildWarningOfflineMessage(iotDevice.getDevtypeBid(), iotDevice.getDevCode(), iotDevice.getDevName()));
+                    warnResult.setMessage(WarnMessageBuilderUtil.buildWarningOfflineMessage(iotDevice.getDevtypeBid(), iotDevice.getDevCode(), iotDevice.getDevName(),iotDevice.getDevUpdateddate()));
                     handleWarnRecord(warnResult);
                     handleWarnRecord(warnResult);
                 }
                 }
             } else {
             } else {

+ 33 - 9
src/main/java/com/yunfeiyun/agmp/iots/warn/util/WarnMessageBuilderUtil.java

@@ -1,5 +1,8 @@
 package com.yunfeiyun.agmp.iots.warn.util;
 package com.yunfeiyun.agmp.iots.warn.util;
 
 
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
 public class WarnMessageBuilderUtil {
 public class WarnMessageBuilderUtil {
     /**
     /**
      * 构建告警信息。
      * 构建告警信息。
@@ -36,16 +39,37 @@ public class WarnMessageBuilderUtil {
      * @param name
      * @param name
      * @return
      * @return
      */
      */
-    public static String buildWarningOfflineMessage(String devType, String devCode, String name) {
-        String tag = "";
-        if (devType != null && devCode != null) {
-            tag = String.format("%s设备(%s), 触发告警,告警原因: \n", devType, devCode);
+    public static String buildWarningOfflineMessage(String devType, String devCode, String name, String lastUpdateTime) {
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        try {
+            Date lastUpdate = sdf.parse(lastUpdateTime);
+            long diffInMillis = System.currentTimeMillis() - lastUpdate.getTime();
+
+            long diffMinutes = diffInMillis / (60 * 1000) % 60;
+            long diffHours = diffInMillis / (60 * 60 * 1000) % 24;
+            long diffDays = diffInMillis / (24 * 60 * 60 * 1000);
+
+            String durationStr;
+            if (diffDays > 0) {
+                durationStr = String.format("已离线%d天%d小时%d分", diffDays, diffHours, diffMinutes);
+            } else if (diffHours > 0) {
+                durationStr = String.format("已离线%d小时%d分", diffHours, diffMinutes);
+            } else {
+                durationStr = String.format("已离线%d分钟", diffMinutes);
+            }
+
+            String content = String.format("%s设备(%s)%s;", devType, devCode, durationStr);
+            return content;
+
+        } catch (Exception e) {
+            e.printStackTrace();
+            return "无法解析离线时间";
         }
         }
-        return String.format("%s %s 离线。", tag, name);
     }
     }
 
 
-    public static void main(String[] args) {
-        String message = buildQxzWarningMessage("气象站", "87872877485", "温度", "42", "℃", "超过", "10-35℃");
-        System.out.println(message);
-    }
+//    public static void main(String[] args) {
+//        String message = buildQxzWarningMessage("气象站", "87872877485", "温度", "42", "℃", "超过", "10-35℃");
+//        System.out.println(message);
+//        System.out.println(buildWarningOfflineMessage("温度计", "T123", "温度监控器A", "2025-03-19 12:35:00"));
+//    }
 }
 }