Parcourir la source

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

zhaiyifei il y a 9 mois
Parent
commit
020c05ef2b

+ 9 - 1
src/main/java/com/yunfeiyun/agmp/iots/device/serviceImp/YfQxzDeviceImpl.java

@@ -29,6 +29,7 @@ import com.yunfeiyun.agmp.iots.service.IIotDeviceService;
 import com.yunfeiyun.agmp.iots.service.IIotDeviceconfigService;
 import com.yunfeiyun.agmp.iots.service.IIotDevicelasteddataService;
 import com.yunfeiyun.agmp.iots.service.impl.IotDeviceAddressService;
+import com.yunfeiyun.agmp.iots.warn.service.WarnService;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.http.util.TextUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -73,10 +74,12 @@ public class YfQxzDeviceImpl extends DeviceAbstractImpl implements IYfQxzDevice
     @Autowired
     private IIotCmdlogService iIotCmdlogService;
 
-
     @Autowired
     private IotDeviceAddressService iotDeviceAddressService;
 
+    @Autowired
+    private WarnService warnService;
+
 
 
     @Override
@@ -330,6 +333,11 @@ public class YfQxzDeviceImpl extends DeviceAbstractImpl implements IYfQxzDevice
         }
         iIotDevicelasteddataService.updateDeviceLastedData(iotDeviceOld,jarrData.toString(), devUpdateddate);
 
+        // 发送告警消息
+        String devBid = iotDeviceOld.getDevBid();
+        JSONObject jsonObject = JSONObject.from(iotYfqxzdataList);
+        warnService.processWarningReportData(iotDeviceOld, jsonObject);
+
         //预警
         //云飞 气象站 和 墒情站  都从这里进数据
 /*        if( IotDeviceDictConst.TYPE_YF_QXZ.equals(iotDeviceOld.getDevtypeBid()) ){

+ 5 - 0
src/main/java/com/yunfeiyun/agmp/iots/warn/mapper/IotWarnBussinessMapper.java

@@ -2,8 +2,11 @@ package com.yunfeiyun.agmp.iots.warn.mapper;
 
 import com.yunfeiyun.agmp.iot.common.domain.IotWarncount;
 import com.yunfeiyun.agmp.iot.common.domain.IotWarnlog;
+import com.yunfeiyun.agmp.iots.warn.model.WarnConfigInfo;
 import org.apache.ibatis.annotations.Param;
 
+import java.util.List;
+
 public interface IotWarnBussinessMapper {
     /**
      * 查询设备+配置的重复次数
@@ -42,5 +45,7 @@ public interface IotWarnBussinessMapper {
 
     int insertIncrementReCount(IotWarncount iotWarncount);
 
+    List<WarnConfigInfo> selectIotWarnConfigInfoList(WarnConfigInfo warnConfigInfo);
+
     int resetReCountByDevIdAndConfigId(@Param("devId") String devId, @Param("wcBid") String configId);
 }

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

@@ -0,0 +1,24 @@
+package com.yunfeiyun.agmp.iots.warn.model;
+
+import lombok.Data;
+
+@Data
+public class WarnConfigInfo {
+    private String wcBid;
+    private String wcName;
+    private String wcLevel;
+    private String wcTouchtype;
+    private String wcCondition;
+    private String wcRepeatnum;
+    private String wiBid;
+    private String wiAddress;
+    private String wiCode;
+    private String wiName;
+    private String wiUnit;
+    private String wiExpression;
+    private String wiValue;
+    private String woType;
+    private String woObjid;
+    private String tid;
+
+}

+ 29 - 0
src/main/java/com/yunfeiyun/agmp/iots/warn/service/IotWarnBussinessService.java

@@ -5,10 +5,16 @@ import com.yunfeiyun.agmp.common.utils.uuid.IdUtils;
 import com.yunfeiyun.agmp.iot.common.domain.IotWarncount;
 import com.yunfeiyun.agmp.iot.common.domain.IotWarnlog;
 import com.yunfeiyun.agmp.iots.warn.mapper.IotWarnBussinessMapper;
+import com.yunfeiyun.agmp.iots.warn.model.WarnConfigInfo;
 import com.yunfeiyun.agmp.iots.warn.model.WarnResult;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
 @Service
 public class IotWarnBussinessService {
 
@@ -87,4 +93,27 @@ public class IotWarnBussinessService {
         return iotWarncountMapper.insertWarnRecord(iotWarnlog);
     }
 
+    /**
+     * 查询预警配置信息
+     *
+     * @return
+     *
+     */
+    List<WarnConfigInfo> selectIotWarnConfigInfoList(WarnConfigInfo warnConfigInfo) {
+        return iotWarncountMapper.selectIotWarnConfigInfoList(warnConfigInfo);
+    }
+
+    Map<String, List<WarnConfigInfo>> selectIotWarnConfigInfoMap(WarnConfigInfo warnConfigInfo){
+        List<WarnConfigInfo> warnConfigInfoList = selectIotWarnConfigInfoList(warnConfigInfo);
+        Map<String, List<WarnConfigInfo>> warnConfigInfoMap = new HashMap<>();
+        for (WarnConfigInfo info : warnConfigInfoList) {
+            String wcBid = info.getWcBid();
+            if(!warnConfigInfoMap.containsKey(wcBid)){
+                warnConfigInfoMap.put(wcBid, new ArrayList<>());
+            }
+            warnConfigInfoMap.get(wcBid).add(info);
+        }
+        return warnConfigInfoMap;
+    }
+
 }

+ 69 - 23
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.JSONObject;
+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.enums.EnumWarnRuleOp;
+import com.yunfeiyun.agmp.iots.warn.model.WarnConfigInfo;
 import com.yunfeiyun.agmp.iots.warn.model.WarnResult;
 import com.yunfeiyun.agmp.iots.warn.util.CompareUtil;
 import com.yunfeiyun.agmp.iots.warn.util.WarnMessageBuilderUtil;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 import org.springframework.stereotype.Service;
@@ -12,11 +17,13 @@ import org.springframework.stereotype.Service;
 import javax.annotation.Resource;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.CompletableFuture;
 
 /**
  * 预警核心服务类
  */
+@Slf4j
 @Service
 public class WarnService {
 
@@ -26,15 +33,18 @@ public class WarnService {
     @Autowired
     private ReCountService reCountService;
 
+    @Autowired
+    private IotWarnBussinessService iotWarnBussinessService;
+
     /**
      * 统一处理上报数据
      *
      * @param devId 设备id
      * @param data  上报的数据对象
      */
-    public void processWarningReportData(String devId, JSONObject data) {
+    public void processWarningReportData(IotDevice iotDevice, JSONObject data) {
         //转异步处理
-        processWarningReportDataSyn(devId, data);
+        processWarningReportDataSyn(iotDevice, data);
     }
 
 
@@ -44,27 +54,41 @@ public class WarnService {
      * @param devId 设备id
      * @param data  上报的数据对象
      */
-    private void processWarningReportDataSyn(String devId, JSONObject data) {
+    private void processWarningReportDataSyn(IotDevice iotDevice, JSONObject data) {
+        String devId = iotDevice.getDevBid();
+        String devtypeBid = iotDevice.getDevtypeBid();
+        String devClass =  IotDeviceDictEnum.getLv1CodeByCode(devtypeBid);
+        IotDeviceTypeLv1Enum iotDeviceTypeLv1Enum = IotDeviceTypeLv1Enum.findEnumByCode(devClass);
+        if (iotDeviceTypeLv1Enum == null) {
+            log.error("[设备告警] 设备大类不存在,devId:{}, devtypeBid:{}", devId, devtypeBid);
+            return;
+        }
+
         CompletableFuture.runAsync(() -> {
-            String devClass = "设备大类"; // todo
             // 获取该设备有哪些告警配置
-            List<Object> objects = getConfigByDevId(devId);
+            WarnConfigInfo warnConfigInfo = new WarnConfigInfo();
+            warnConfigInfo.setTid(iotDevice.getTid());
+            warnConfigInfo.setWoObjid(devId);
+            warnConfigInfo.setWoType("1");
+
+            Map<String, List<WarnConfigInfo>> configMap = iotWarnBussinessService.selectIotWarnConfigInfoMap(warnConfigInfo);
             //配置一个个检查
-            for (Object config : objects) {
+            for(Map.Entry<String, List<WarnConfigInfo>> entry : configMap.entrySet()) {
+                List<WarnConfigInfo> configList = entry.getValue();
                 WarnResult warnResult = null;
-                switch (devClass) {
-                    case "气象站": {
-                        warnResult = comparableQxzReportData(devId, config, data);
-                        break;
-                    }
-                    case "墒情站": {
-                        warnResult = comparableSqzReportData(devId, config, data);
-                        break;
-                    }
-                    case "病虫害": {
-                        warnResult = comparableBchReportData(devId, config, data);
+                switch (iotDeviceTypeLv1Enum) {
+                    case QXZ: {
+                        warnResult = comparableQxzReportData(devId, configList, data);
                         break;
                     }
+//                    case SQZ: {
+//                        warnResult = comparableSqzReportData(devId, config, data);
+//                        break;
+//                    }
+//                    case "病虫害": {
+//                        warnResult = comparableBchReportData(devId, config, data);
+//                        break;
+//                    }
                     default:
                         break;
                 }
@@ -72,6 +96,28 @@ public class WarnService {
                     handleWarnRecord(warnResult);
                 }
             }
+//            for (Object config : objects) {
+//                WarnResult warnResult = null;
+//                switch (iotDeviceTypeLv1Enum) {
+//                    case QXZ: {
+//                        warnResult = comparableQxzReportData(devId, config, data);
+//                        break;
+//                    }
+//                    case SQZ: {
+//                        warnResult = comparableSqzReportData(devId, config, data);
+//                        break;
+//                    }
+////                    case "病虫害": {
+////                        warnResult = comparableBchReportData(devId, config, data);
+////                        break;
+////                    }
+//                    default:
+//                        break;
+//                }
+//                if (warnResult != null) {
+//                    handleWarnRecord(warnResult);
+//                }
+//            }
         }, threadPoolTaskExecutor);
     }
 
@@ -103,13 +149,13 @@ public class WarnService {
      * @param config     对应的配置
      * @param jsonObject 上报的数据
      */
-    WarnResult comparableQxzReportData(String devId, Object config, JSONObject 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);
-        }
+//        if ("指标类型" == "多指标满足") {
+//            warnResult = comparableQxzMultipleIndicators(devId, config, jsonObject);
+//        } else {
+//            warnResult = comparableQxzSingleIndicator(devId, config, jsonObject);
+//        }
         return warnResult;
     }
 

+ 19 - 0
src/main/resources/mapper/IotWarnBusinessMapper.xml

@@ -76,4 +76,23 @@
             resultType="com.yunfeiyun.agmp.iot.common.domain.IotWarncount">
         select  * from IotWarncount where devBid=#{devBid} and wcBid=#{wcBid};
     </select>
+
+    <select id="selectIotWarnConfigInfoList" parameterType="WarnConfigInfo" resultType="com.yunfeiyun.agmp.iots.warn.model.WarnConfigInfo">
+        SELECT wc.wcBid, wc.wcName, wc.wcLevel, wc.wcTouchtype, wc.wcCondition, wc.wcRepeatnum, wi.wiBid, wi.wiAddress,
+            wi.wiCode, wi.wiName, wi.wiUnit, wi.wiExpression, wi.wiValue
+        FROM IotWarnconfig AS wc
+            LEFT JOIN IotWarnindicator AS wi ON wi.wcBid = wc.wcBid
+            <if test="devBid != null">
+                LEFT JOIN IotWarnobject AS wo ON wo.wcBid = wc.wcBid
+            </if>
+
+        WHERE wc.wcStatus = '0' AND wc.tid = #{tid} AND wi.wiStatus = '0'
+            AND wi.wiBid IS NOT NULL
+            <if test="devBid != null">
+                AND wo.devBid = #{devBid} AND wo.woBid IS NOT NULL
+            </if>
+            <if test="devtypeBid!= null">
+                AND wc.devtypeBid = #{devtypeBid}
+            </if>
+    </select>
 </mapper>