Explorar o código

新增 气象站预警上报检测功能

zhaiyifei hai 9 meses
pai
achega
bb59eff23c

+ 2 - 2
src/main/java/com/yunfeiyun/agmp/iots/warn/model/WarnConfigInfo.java

@@ -17,8 +17,8 @@ public class WarnConfigInfo {
     private String wiUnit;
     private String wiExpression;
     private String wiValue;
-    private String woType;
-    private String woObjid;
+    private String devtypeBid;
+    private String devBid;
     private String tid;
 
 }

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

@@ -1,10 +1,15 @@
 package com.yunfeiyun.agmp.iots.warn.service;
 
+import com.alibaba.fastjson2.JSONArray;
 import com.alibaba.fastjson2.JSONObject;
+import com.yunfeiyun.agmp.common.utils.StringUtils;
+import com.yunfeiyun.agmp.iot.common.constant.device.ElementFormatUtil;
 import com.yunfeiyun.agmp.iot.common.constant.devicetype.IotDeviceDictEnum;
 import com.yunfeiyun.agmp.iot.common.constant.devicetype.IotDeviceTypeLv1Enum;
 import com.yunfeiyun.agmp.iot.common.domain.IotDevice;
+import com.yunfeiyun.agmp.iot.common.domain.IotDevicefactor;
 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.util.CompareUtil;
@@ -15,7 +20,8 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
-import java.util.ArrayList;
+import java.math.BigDecimal;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.CompletableFuture;
@@ -36,6 +42,23 @@ public class WarnService {
     @Autowired
     private IotWarnBussinessService iotWarnBussinessService;
 
+    @Autowired
+    private IIotDevicefactorService iotDevicefactorService;
+
+    private Map<String, IotDevicefactor> getDevicefactorMap(String devBid){
+        IotDevicefactor param = new IotDevicefactor();
+        param.setDevBid(devBid);
+        List<IotDevicefactor> factorList = iotDevicefactorService.selectIotDevicefactorList(param);
+        Map<String, IotDevicefactor> factorMap = new HashMap<>();
+        for(IotDevicefactor factor : factorList){
+            String dfCode = factor.getDfCode();
+            String dfAddress = factor.getDfAddress();
+            String key = dfAddress + dfCode;
+            factorMap.put(key, factor);
+        }
+        return factorMap;
+    }
+
     /**
      * 统一处理上报数据
      *
@@ -68,8 +91,7 @@ public class WarnService {
             // 获取该设备有哪些告警配置
             WarnConfigInfo warnConfigInfo = new WarnConfigInfo();
             warnConfigInfo.setTid(iotDevice.getTid());
-            warnConfigInfo.setWoObjid(devId);
-            warnConfigInfo.setWoType("1");
+            warnConfigInfo.setDevBid(devId);
 
             Map<String, List<WarnConfigInfo>> configMap = iotWarnBussinessService.selectIotWarnConfigInfoMap(warnConfigInfo);
             //配置一个个检查
@@ -78,7 +100,7 @@ public class WarnService {
                 WarnResult warnResult = null;
                 switch (iotDeviceTypeLv1Enum) {
                     case QXZ: {
-                        warnResult = comparableQxzReportData(devId, configList, data);
+                        warnResult = comparableQxzReportData(iotDevice, configList, data);
                         break;
                     }
 //                    case SQZ: {
@@ -149,14 +171,78 @@ public class WarnService {
      * @param config     对应的配置
      * @param jsonObject 上报的数据
      */
-    WarnResult comparableQxzReportData(String devId, List<WarnConfigInfo> configList, JSONObject jsonObject) {
-        WarnResult warnResult = null;
-//        if ("指标类型" == "多指标满足") {
-//            warnResult = comparableQxzMultipleIndicators(devId, config, jsonObject);
-//        } else {
-//            warnResult = comparableQxzSingleIndicator(devId, config, jsonObject);
-//        }
-        return warnResult;
+    WarnResult comparableQxzReportData(IotDevice iotDevice, List<WarnConfigInfo> configList, JSONObject jsonObject) {
+        String devCode = iotDevice.getDevCode();
+        String devBid = iotDevice.getDevBid();
+        WarnConfigInfo configInfo = configList.get(0);
+        String wcCondition = configInfo.getWcCondition();
+        Map<String, String> elementMap = new HashMap<>();
+        JSONArray jsonArray = JSONArray.from(jsonObject);
+        BigDecimal errorValue = new BigDecimal("-99");
+        for(Object obj : jsonArray) {
+            JSONObject ob = (JSONObject) obj;
+            String eNum = ob.getString("eNum");
+            String eName = ob.getString("eName");
+            String eKey = ob.getString("eKey");
+            String address = ElementFormatUtil.getAddress(eName, eKey);
+            String key = address + eNum;
+            BigDecimal eValue = null;
+            try{
+                eValue = ob.getBigDecimal("eValue");
+                if(eValue.compareTo(errorValue) <= 0){
+                    throw new Exception();
+                }
+            }catch (Exception e){
+                log.error("[设备告警] 数据类型转换异常,devId:{}, eNum:{}, eName:{}, eKey:{}", devCode, eNum, eName, eKey);
+                continue;
+            }
+            elementMap.put(key, String.valueOf(eValue));
+        }
+        Map<String, IotDevicefactor> factorMap = getDevicefactorMap(devBid);
+        String message = null;
+        StringBuilder messageBuilder = new StringBuilder();
+        for (WarnConfigInfo config : configList) {
+            String currentValue = config.getWiValue();
+            String wiAddress = config.getWiAddress();
+            String wiCode = config.getWiCode();
+            String key = wiAddress + wiCode;
+            String wiName = config.getWiName();
+            if(factorMap.containsKey(key)){
+                IotDevicefactor iotDevicefactor = factorMap.get(key);
+                String displayname = iotDevicefactor.getDfDisplayname();
+                if(StringUtils.isNotEmpty(displayname)){
+                    wiName = displayname;
+                }
+            }
+            String wiUnit = config.getWiUnit();
+            if (!elementMap.containsKey(key)) {
+                continue;
+            }
+            String targetValue = elementMap.get(key);
+            String expression = config.getWiExpression();
+            EnumWarnRuleOp warnRuleOp = EnumWarnRuleOp.findEnumByCode(expression);
+            String ruleName = warnRuleOp.getName();
+            String warnMessage = WarnMessageBuilderUtil.buildQxzWarningMessage(
+                    "气象站设备", devCode, wiName, currentValue, wiUnit, ruleName, targetValue);
+
+            boolean tempSuccess = CompareUtil.comp(currentValue, expression, targetValue);
+            if ("0".equals(wcCondition)) {
+                if (tempSuccess) {
+                    message = warnMessage;
+                    return new WarnResult(true, message);
+                }
+            } else {
+                if (!tempSuccess) {
+                    return new WarnResult(false, null);
+                }
+                messageBuilder.append(warnMessage);
+            }
+        }
+
+        if(messageBuilder.length() > 0){
+            return new WarnResult(true, messageBuilder.toString());
+        }
+        return new WarnResult(false, null);
     }
 
     /**
@@ -167,26 +253,26 @@ public class WarnService {
      * @param jsonObject 上报的数据
      * @return WarnResult 包含是否触发告警的信息
      */
-    public WarnResult comparableQxzSingleIndicator(String devId, Object config, JSONObject jsonObject) {
-        //从config取出需要匹配的要素列表 todo
-        List<Object> items = new ArrayList<>();
-        //循环要素,一个个对比
-        for (Object item : items) {
-            // 取出来需要比较的要素 todo
-            String comparableItem = "item.getField";
-            //要对比的目标值 todo
-            String targetValue = "item.targetValue";
-            //取出来当前的最新值 todo
-            String currentValue = "jsonObject.get[item.getField]";
-            // 字段表达式比较
-            boolean result = CompareUtil.comp(currentValue, EnumWarnRuleOp.EQUAL.getCode(), targetValue);
-            if (result) {
-                String message = WarnMessageBuilderUtil.buildQxzWarningMessage("气象站设备", devId, comparableItem, Double.parseDouble(currentValue), "℃", "超过", targetValue);
-                return new WarnResult(true, message);
-            }
-        }
-        return new WarnResult(false, null);
-    }
+//    public WarnResult comparableQxzSingleIndicator(String devId, Object config, JSONObject jsonObject) {
+//        //从config取出需要匹配的要素列表 todo
+//        List<Object> items = new ArrayList<>();
+//        //循环要素,一个个对比
+//        for (Object item : items) {
+//            // 取出来需要比较的要素 todo
+//            String comparableItem = "item.getField";
+//            //要对比的目标值 todo
+//            String targetValue = "item.targetValue";
+//            //取出来当前的最新值 todo
+//            String currentValue = "jsonObject.get[item.getField]";
+//            // 字段表达式比较
+//            boolean result = CompareUtil.comp(currentValue, EnumWarnRuleOp.EQUAL.getCode(), targetValue);
+//            if (result) {
+//                String message = WarnMessageBuilderUtil.buildQxzWarningMessage("气象站设备", devId, comparableItem, Double.parseDouble(currentValue), "℃", "超过", targetValue);
+//                return new WarnResult(true, message);
+//            }
+//        }
+//        return new WarnResult(false, null);
+//    }
 
     /**
      * 处理多个指标是否同时满足预警条件。
@@ -196,33 +282,33 @@ public class WarnService {
      * @param jsonObject 上报的数据
      * @return WarnResult 包含是否触发告警的信息
      */
-    public WarnResult comparableQxzMultipleIndicators(String devId, Object config, JSONObject jsonObject) {
-        //从config取出需要匹配的要素列表 todo
-        List<Object> items = new ArrayList<>();
-        int successCount = 0;
-        StringBuilder messages = new StringBuilder();
-        //循环要素,一个个对比
-        for (Object item : items) {
-            // 取出来需要比较的要素 todo
-            String comparableItem = "item.getField";
-            //要对比的目标值 todo
-            String targetValue = "item.targetValue";
-            //取出来当前的最新值 todo
-            String currentValue = "jsonObject.get[item.getField]";
-            // 字段表达式比较
-            boolean result = CompareUtil.comp(currentValue, EnumWarnRuleOp.EQUAL.getCode(), targetValue);
-            if (result) {
-                messages.append(WarnMessageBuilderUtil.buildQxzWarningMessage("气象站设备", devId, comparableItem, Double.parseDouble(currentValue), "℃", "超过", targetValue));
-                successCount += 1;
-            }
-        }
-
-        if (successCount == items.size()) {
-            return new WarnResult(true, messages.toString());
-        } else {
-            return new WarnResult(false, null);
-        }
-    }
+//    public WarnResult comparableQxzMultipleIndicators(String devId, Object config, JSONObject jsonObject) {
+//        //从config取出需要匹配的要素列表 todo
+//        List<Object> items = new ArrayList<>();
+//        int successCount = 0;
+//        StringBuilder messages = new StringBuilder();
+//        //循环要素,一个个对比
+//        for (Object item : items) {
+//            // 取出来需要比较的要素 todo
+//            String comparableItem = "item.getField";
+//            //要对比的目标值 todo
+//            String targetValue = "item.targetValue";
+//            //取出来当前的最新值 todo
+//            String currentValue = "jsonObject.get[item.getField]";
+//            // 字段表达式比较
+//            boolean result = CompareUtil.comp(currentValue, EnumWarnRuleOp.EQUAL.getCode(), targetValue);
+//            if (result) {
+//                messages.append(WarnMessageBuilderUtil.buildQxzWarningMessage("气象站设备", devId, comparableItem, Double.parseDouble(currentValue), "℃", "超过", targetValue));
+//                successCount += 1;
+//            }
+//        }
+//
+//        if (successCount == items.size()) {
+//            return new WarnResult(true, messages.toString());
+//        } else {
+//            return new WarnResult(false, null);
+//        }
+//    }
 
 
     /**

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

@@ -6,18 +6,18 @@ public class WarnMessageBuilderUtil {
      *
      * @param devType        设备类型,例如“气象站设备”。
      * @param devCode        设备唯一编码,例如“87872877485”。
-     * @param code           告警参数名,例如“温度”。
+     * @param name           告警参数名,例如“温度”。
      * @param value          当前值,例如42。
      * @param unit           单位,例如“℃”。
      * @param opt            触发预警目标值的动作,例如超过,低于、大于等。
      * @param indicatorValue 设定的目标对比值,例如“10-35℃”。
      * @return 格式化后的告警信息。
      */
-    public static String buildQxzWarningMessage(String devType, String devCode, String code, double value, String unit, String opt, String indicatorValue) {
+    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,
-                code,
+                name,
                 value,
                 unit,
                 opt,
@@ -25,7 +25,7 @@ public class WarnMessageBuilderUtil {
     }
 
     public static void main(String[] args) {
-        String message = buildQxzWarningMessage("气象站设备", "87872877485", "温度", 42, "℃", "超过", "10-35℃");
+        String message = buildQxzWarningMessage("气象站设备", "87872877485", "温度", "42", "℃", "超过", "10-35℃");
         System.out.println(message);
     }
 }