lengfaqiang 2 лет назад
Родитель
Сommit
c5c193e3bc

+ 103 - 1
slope-modules/slope-detection/src/main/java/com/sckw/slope/detection/consumer/MqttCallbackHandler.java

@@ -17,6 +17,7 @@ import com.sckw.slope.detection.dao.tdengine.SlopeDataMapper;
 import com.sckw.slope.detection.model.dos.mysql.*;
 import com.sckw.slope.detection.model.dos.tdengine.Devices;
 import com.sckw.slope.detection.model.dos.tdengine.SlopeData;
+import com.sckw.slope.detection.model.dto.AlarmList;
 import com.sckw.slope.detection.model.dto.DevicesAlarm;
 import com.sckw.slope.detection.model.dto.DevicesItem;
 import com.sckw.slope.detection.model.param.SmsMessageParam;
@@ -200,7 +201,7 @@ public class MqttCallbackHandler extends AbstractHandler {
                 //获取到当前测量值--现在只有单一值
                 List<SlopeData> slopeData = slopeDataMapper.selectListByTwoLine(snCode, inter.getPartNames());
                 if (!Objects.isNull(slopeData)) {
-                    double offset = commonService.returnIntegrationData(snCode,slopeData,inter);
+                    double offset = commonService.returnIntegrationData(snCode, slopeData, inter);
                 }
 
             }
@@ -304,6 +305,23 @@ public class MqttCallbackHandler extends AbstractHandler {
         checkItemName.add(DictItemEnum.ALTITUDE_Z.getValue());
         checkItemName.add(DictItemEnum.LONGITUDE_X.getValue());
         checkItemName.add(DictItemEnum.LATITUDE_Y.getValue());
+        //放入告警数值-告警等级
+        //放入告警等级-告警主键id
+        Map<String, String> map = new HashMap<>();
+        if (checkItemName.contains(itemName)) {
+            BigDecimal offset = commonService.computeOffset(itemValue, itemName, deviceRelation);
+            map = newAlarmCompute(offset, kwsThresholds);
+        } else {
+            map = newAlarmCompute(new BigDecimal(itemValue), kwsThresholds);
+        }
+//        map = oldAlarmCompute(checkItemName, itemName, itemValue, deviceRelation, kwsThresholds);
+        return map;
+    }
+
+    /**
+     * 之前的阈值告警计算方式
+     */
+    private Map<String, String> oldAlarmCompute(List<String> checkItemName, String itemName, String itemValue, KwsDeviceReference deviceRelation, List<KwsThreshold> kwsThresholds) {
         //需要计算的阈值
         if (checkItemName.contains(itemName)) {
             BigDecimal offset = commonService.computeOffset(itemValue, itemName, deviceRelation);
@@ -445,6 +463,65 @@ public class MqttCallbackHandler extends AbstractHandler {
         }
     }
 
+    private Map<String, String> newAlarmCompute(BigDecimal offset, List<KwsThreshold> kwsThresholds) {
+        //最大值从大到小计算 告警阈值
+        List<AlarmList> maxAlarmList = new ArrayList<>();
+        for (KwsThreshold kwsThreshold : kwsThresholds) {
+            if (kwsThreshold.getMax() != null) {
+                AlarmList alarmList = new AlarmList();
+                alarmList.setLevel(kwsThreshold.getLevel().toString());
+                alarmList.setId(kwsThreshold.getId());
+                alarmList.setValue(new BigDecimal(kwsThreshold.getMax()));
+                maxAlarmList.add(alarmList);
+            }
+        }
+        //最小值从小到大计算 告警阈值
+        List<AlarmList> minAlarmList = new ArrayList<>();
+        for (KwsThreshold kwsThreshold : kwsThresholds) {
+            if (kwsThreshold.getMin() != null) {
+                AlarmList alarmList = new AlarmList();
+                alarmList.setLevel(kwsThreshold.getLevel().toString());
+                alarmList.setId(kwsThreshold.getId());
+                alarmList.setValue(new BigDecimal(kwsThreshold.getMin()));
+                minAlarmList.add(alarmList);
+            }
+        }
+        maxAlarmList = maxAlarmList.stream().sorted(Comparator.comparing(AlarmList::getValue).reversed()).collect(Collectors.toList());
+//            HashMap<Object, Object> maxAlarmMap = kwsThresholds.stream().collect(HashMap::new,
+//                    (m, v) -> m.put(v.getMax(), v.getId()), HashMap::putAll);
+        minAlarmList = minAlarmList.stream().sorted(Comparator.comparing(AlarmList::getValue)).collect(Collectors.toList());
+//            HashMap<Object, Object> mixAlarmMap = kwsThresholds.stream().collect(HashMap::new,
+//                    (m, v) -> m.put(v.getMax(), v.getId()), HashMap::putAll);
+        String maxLevel = getListMaxNumber(offset, maxAlarmList);
+        String minLevel = getListMinNumber(offset, minAlarmList);
+        /**存在阈值触发*/
+        Map<String, String> map = new HashMap<>();
+        map.put("level", "1");
+        map.put("thresholdId", "1");
+        map.put("flag", "1");
+        if (maxLevel != null || minLevel != null) {
+            if (maxLevel != null) {
+                for (AlarmList alarmList : maxAlarmList) {
+                    if (maxLevel.equals(alarmList.getLevel())) {
+                        map.put("level", maxLevel);
+                        map.put("thresholdId", alarmList.getId().toString());
+                        map.put("flag", "2");
+                    }
+                }
+            }
+            if (minLevel != null) {
+                for (AlarmList alarmList : maxAlarmList) {
+                    if (minLevel.equals(alarmList.getLevel())) {
+                        map.put("level", minLevel);
+                        map.put("thresholdId", alarmList.getId().toString());
+                        map.put("flag", "2");
+                    }
+                }
+            }
+        }
+        return map;
+    }
+
     /**
      * 保存阈值主表以及明细表
      *
@@ -636,6 +713,31 @@ public class MqttCallbackHandler extends AbstractHandler {
         return null;
     }
 
+    private static String getListMaxNumber(BigDecimal number, List<AlarmList> alarmLists) {
+        for (AlarmList list : alarmLists) {
+            if (number.compareTo(list.getValue()) > 0) {
+                return list.getLevel();
+            }
+        }
+        return null;
+    }
+
+    /**
+     * 计算最小值
+     *
+     * @param number
+     * @param alarmLists
+     * @return
+     */
+    private static String getListMinNumber(BigDecimal number, List<AlarmList> alarmLists) {
+        for (AlarmList list : alarmLists) {
+            if (number.compareTo(list.getValue()) < 0) {
+                return list.getLevel();
+            }
+        }
+        return null;
+    }
+
     public static void main(String[] args) {
         String s = "2023-11-30 22:00:00";
         Date date = DateUtils.formatDate(s);

+ 28 - 0
slope-modules/slope-detection/src/main/java/com/sckw/slope/detection/model/dto/AlarmList.java

@@ -0,0 +1,28 @@
+package com.sckw.slope.detection.model.dto;
+
+import lombok.Data;
+
+import java.io.Serializable;
+import java.math.BigDecimal;
+
+/**
+ * @author lfdc
+ * @description 阈值计算实体
+ * @date 2023-12-01 17:12:32
+ */
+@Data
+public class AlarmList implements Serializable {
+    /**
+     * 阈值等级
+     */
+    private String level;
+    /**
+     * 阈值id
+     */
+    private Long id;
+    /**
+     * 阈值
+     */
+    private BigDecimal value;
+
+}

+ 1 - 0
slope-modules/slope-detection/src/main/java/com/sckw/slope/detection/service/KwsAlarmService.java

@@ -244,6 +244,7 @@ public class KwsAlarmService {
                     .eq(KwsAlarm::getDeviceId, alarm.getDeviceId())
                     .eq(KwsAlarm::getCompanyId, alarm.getCompanyId())
                     .eq(KwsAlarm::getIsHistory, NumberConstant.ONE)
+                    .eq(KwsAlarm::getDelFlag, NumberConstant.ZERO)
             );
             if (historyAlarm != null) {
                 alarmMapper.update(null, new LambdaUpdateWrapper<KwsAlarm>()