Przeglądaj źródła

修复 多条件预警规则记录生成格式异常的问题

zhaiyifei 9 miesięcy temu
rodzic
commit
eac7211321

+ 16 - 0
src/main/java/com/yunfeiyun/agmp/iots/warn/model/WarnStatusDto.java

@@ -0,0 +1,16 @@
+package com.yunfeiyun.agmp.iots.warn.model;
+
+import lombok.Data;
+
+@Data
+public class WarnStatusDto {
+    private String devType;
+    private String devCode;
+    private String name;
+    private String value;
+    private String unit;
+    private String opt;
+    private String indicatorValue;
+
+    private boolean isWarn;
+}

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

@@ -13,6 +13,7 @@ import com.yunfeiyun.agmp.iot.common.enums.EnumWarnRuleOp;
 import com.yunfeiyun.agmp.iots.service.IIotDevicefactorService;
 import com.yunfeiyun.agmp.iots.warn.model.WarnConfigInfo;
 import com.yunfeiyun.agmp.iots.warn.model.WarnResult;
+import com.yunfeiyun.agmp.iots.warn.model.WarnStatusDto;
 import com.yunfeiyun.agmp.iots.warn.util.CompareUtil;
 import com.yunfeiyun.agmp.iots.warn.util.WarnMessageBuilderUtil;
 import lombok.extern.slf4j.Slf4j;
@@ -23,10 +24,7 @@ import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
 import java.math.BigDecimal;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
+import java.util.*;
 import java.util.concurrent.CompletableFuture;
 
 /**
@@ -190,16 +188,25 @@ public class WarnService {
         return currentValueMap;
     }
 
-    /**
-     * 【气象站】检测该设备上报的要素和配置是否达到预警条件
-     * @param config
-     * @param factorMap
-     * @param currentValueMap
-     * @param devCode
-     * @return
-     */
-    private Map<String, Object> getQxzCondition(WarnConfigInfo config, Map<String, IotDevicefactor> factorMap,
-                                     Map<String, String> currentValueMap, String devCode) {
+    private String getDisplayName(String key, Map<String, IotDevicefactor> factorMap){
+        String wiName = "";
+        if(factorMap.containsKey(key)){
+            IotDevicefactor iotDevicefactor = factorMap.get(key);
+            // 如果禁用,直接返回null,不进行预警判断
+            if(Objects.equals(iotDevicefactor.getDfDisable(), "1")){
+                return null;
+            }
+            String displayname = iotDevicefactor.getDfDisplayname();
+            if(StringUtils.isNotEmpty(displayname)){
+                wiName = displayname;
+            }
+        }
+
+        return wiName;
+    }
+
+    private WarnStatusDto getQxzWarnStatusDto(WarnConfigInfo config, Map<String, String> currentValueMap, Map<String,
+            IotDevicefactor> factorMap, String devCode) {
         String targetValue = config.getWiValue();
         String wiAddress = config.getWiAddress();
         String wiCode = config.getWiCode();
@@ -208,6 +215,12 @@ public class WarnService {
         String key = wiAddress + wiCode;
         String expression = config.getWiExpression();
 
+        // 如果配置禁用,或者要素禁用,直接返回null,不进行预警判断
+        boolean status = "0".equals(wcStatus) && "0".equals(wiStatus);
+        if(!status){
+            return null;
+        }
+
         // 如果上报数据中没有预警配置中的要素,直接返回null,不进行预警判断
         if (!currentValueMap.containsKey(key)) {
             return null;
@@ -219,18 +232,15 @@ public class WarnService {
         }
 
         String wiName = config.getWiName();
-        if(factorMap.containsKey(key)){
-            IotDevicefactor iotDevicefactor = factorMap.get(key);
-            // 如果禁用,直接返回null,不进行预警判断
-            if(Objects.equals(iotDevicefactor.getDfDisable(), "1")){
-                return null;
-            }
-            String displayname = iotDevicefactor.getDfDisplayname();
-            if(StringUtils.isNotEmpty(displayname)){
-                wiName = displayname;
-            }
+        // 如果禁用,直接返回null,不进行预警判断
+        String displayname = getDisplayName(key, factorMap);
+        if(displayname == null){
+            return null;
         }
 
+        if(StringUtils.isNotEmpty(displayname)){
+            wiName = displayname;
+        }
         String wiUnit = config.getWiUnit();
         String currentValue = currentValueMap.get(key);
 
@@ -241,23 +251,112 @@ public class WarnService {
         }
 
         String ruleName = warnRuleOp.getName();
+        boolean tempSuccess = CompareUtil.comp(currentValue, expression, targetValue);
 
-        // 如果配置禁用,或者要素禁用,直接返回null,不进行预警判断
-        boolean status = "0".equals(wcStatus) && "0".equals(wiStatus);
-        if(!status){
-            return null;
+        WarnStatusDto warnStatusDto = new WarnStatusDto();
+        warnStatusDto.setDevType("气象站");
+        warnStatusDto.setDevCode(devCode);
+        warnStatusDto.setName(wiName);
+        warnStatusDto.setValue(currentValue);
+        warnStatusDto.setUnit(wiUnit);
+        warnStatusDto.setOpt(ruleName);
+        warnStatusDto.setIndicatorValue(targetValue);
+        warnStatusDto.setWarn(tempSuccess);
+        return warnStatusDto;
+    }
+
+    /**
+     * 【气象站】比较该设备上报的任一要素否达到预警条件
+     * @param warnResult
+     * @param configList
+     * @param factorMap
+     * @param currentValueMap
+     * @param devCode
+     * @return
+     */
+    public WarnResult comparableQxzSingleIndicator(WarnResult warnResult, List<WarnConfigInfo> configList, Map<String, IotDevicefactor> factorMap,
+                                                   Map<String, String> currentValueMap, String devCode) {
+        WarnStatusDto warnStatusDto = null;
+        for (WarnConfigInfo config : configList) {
+            warnStatusDto = getQxzWarnStatusDto(config, currentValueMap, factorMap, devCode);
+            // 如果没有预警配置,直接返回null,不进行预警判断
+            if(warnStatusDto == null){
+                continue;
+            }
+            if(warnStatusDto.isWarn()){
+                break;
+            }
+        }
+        if(warnStatusDto != null && warnStatusDto.isWarn()){
+            String message = WarnMessageBuilderUtil.buildQxzWarningMessage(
+                    warnStatusDto.getDevType(),
+                    warnStatusDto.getDevCode(),
+                    warnStatusDto.getName(),
+                    warnStatusDto.getValue(),
+                    warnStatusDto.getUnit(),
+                    warnStatusDto.getOpt(),
+                    warnStatusDto.getIndicatorValue()
+            );
+            warnResult.setTriggered(true);
+            warnResult.setMessage(message);
         }
-        boolean tempSuccess = CompareUtil.comp(currentValue, expression, targetValue);
 
-        String warnMessage = WarnMessageBuilderUtil.buildQxzWarningMessage(
-                "气象站", devCode, wiName, currentValue, wiUnit, ruleName, targetValue);
+        return warnResult;
+    }
 
-        Map<String, Object> map = new HashMap<>();
-        map.put("warnMessage", warnMessage);
-        map.put("tempSuccess", tempSuccess);
-        return map;
+    /**
+     * 处理多个指标是否同时满足预警条件。
+     *
+     * @param devId      设备ID
+     * @param config     告警规则
+     * @param jsonObject 上报的数据
+     * @return WarnResult 包含是否触发告警的信息
+     */
+    public WarnResult comparableQxzMultipleIndicators(WarnResult warnResult, List<WarnConfigInfo> configList, Map<String, IotDevicefactor> factorMap,
+                                                      Map<String, String> currentValueMap, String devCode) {
+        WarnStatusDto warnStatusDto = null;
+        List<WarnStatusDto> warnStatusDtos = new ArrayList<>();
+        for (WarnConfigInfo config : configList) {
+            warnStatusDto = getQxzWarnStatusDto(config, currentValueMap, factorMap, devCode);
+            // 如果没有预警配置,直接返回null,不进行预警判断
+            if(warnStatusDto == null){
+                continue;
+            }
+            if(!warnStatusDto.isWarn()){
+                return warnResult;
+            }
+            warnStatusDtos.add(warnStatusDto);
+        }
+        if(warnStatusDtos.isEmpty()){
+            return warnResult;
+        }
+
+        StringBuilder messageBuilder = new StringBuilder();
+        for(int i = 0; i < warnStatusDtos.size(); i++){
+            warnStatusDto = warnStatusDtos.get(i);
+            String devType = null;
+            String dCode = null;
+            if(i == 0){
+                devType = warnStatusDto.getDevType();
+                dCode = warnStatusDto.getDevCode();
+            }
+            String message = WarnMessageBuilderUtil.buildQxzWarningMessage(
+                    devType,
+                    dCode,
+                    warnStatusDto.getName(),
+                    warnStatusDto.getValue(),
+                    warnStatusDto.getUnit(),
+                    warnStatusDto.getOpt(),
+                    warnStatusDto.getIndicatorValue()
+            );
+            messageBuilder.append(message).append("\n");
+        }
+        warnResult.setTriggered(true);
+        warnResult.setMessage(messageBuilder.toString());
+        return warnResult;
     }
 
+
     /**
      * 【气象站】比较该设备上报的要素和配置是否达到预警条件
      *
@@ -288,37 +387,10 @@ public class WarnService {
         warnResult.setDevtypeBid(iotDevice.getDevtypeBid());
         warnResult.setConfig(iotWarnconfig);
         warnResult.setTriggered(false);
-
-        String message = null;
-        StringBuilder messageBuilder = new StringBuilder();
-        for (WarnConfigInfo config : configList) {
-            Map<String, Object> conditionResult = getQxzCondition(config, factorMap, currentValueMap, devCode);
-            if (conditionResult == null) {
-                continue;
-            }
-            String warnMessage = (String) conditionResult.get("warnMessage");
-            warnResult.setMessage(warnMessage);
-            boolean tempSuccess = (boolean) conditionResult.get("tempSuccess");
-
-            if ("0".equals(wcCondition)) {
-                if (tempSuccess) {
-                    message = warnMessage;
-                    warnResult.setMessage(message);
-                    warnResult.setTriggered(true);
-                    return warnResult;
-                }
-            } else {
-                if (!tempSuccess) {
-                    return warnResult;
-                }
-                messageBuilder.append(warnMessage).append("\n");
-            }
-        }
-
-        if(messageBuilder.length() > 0){
-            warnResult.setMessage(messageBuilder.toString());
-            warnResult.setTriggered(true);
-            return warnResult;
+        if("0".equals(wcCondition)){
+            warnResult = comparableQxzSingleIndicator(warnResult, configList, factorMap, currentValueMap, devCode);
+        } else {
+            warnResult = comparableQxzMultipleIndicators(warnResult, configList, factorMap, currentValueMap, devCode);
         }
         return warnResult;
     }

+ 7 - 3
src/main/java/com/yunfeiyun/agmp/iots/warn/util/WarnMessageBuilderUtil.java

@@ -14,9 +14,13 @@ public class WarnMessageBuilderUtil {
      * @return 格式化后的告警信息。
      */
     public static String buildQxzWarningMessage(String devType, String devCode, String name, String value, String unit, String opt, String indicatorValue) {
-        return String.format("%s设备(%s),触发告警,告警原因(%s 上报值 %s%s),%s设置的阀值%s",
-                devType,
-                devCode,
+        // 格式化字符串,将设备类型、设备唯一编码、告警参数名、当前值、单位、动作和目标对比值插入到模板中
+        String tag = "";
+        if(devType != null && devCode != null){
+            tag = String.format("%s设备(%s), 触发告警,告警原因: \n", devType, devCode);
+        }
+        return String.format("%s %s 上报值 %s%s,%s设置的阀值%s。",
+                tag,
                 name,
                 value,
                 unit,