Просмотр исходного кода

告警:离线转在线,告警处理

yf_zn 9 месяцев назад
Родитель
Сommit
c28c5bfbd7

+ 11 - 1
src/main/java/com/yunfeiyun/agmp/iots/service/impl/IotDeviceServiceImpl.java

@@ -9,6 +9,7 @@ import com.yunfeiyun.agmp.iot.common.model.device.IotDeviceStatusResVo;
 import com.yunfeiyun.agmp.iots.device.mapper.IotDeviceMapper;
 import com.yunfeiyun.agmp.iots.service.IIotDeviceService;
 import com.yunfeiyun.agmp.iot.common.service.IotDeviceGeoLocationCommonService;
+import com.yunfeiyun.agmp.iots.warn.service.WarnService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -36,6 +37,9 @@ public class IotDeviceServiceImpl implements IIotDeviceService {
     @Autowired
     private IotDeviceGeoLocationCommonService iotDeviceGeoLocationCommonService;
 
+    @Autowired
+    private WarnService warnService;
+
     /**
      * 查询设备基础
      *
@@ -99,12 +103,18 @@ public class IotDeviceServiceImpl implements IIotDeviceService {
     public int updateIotDeviceBatch(List<IotDevice> iotDeviceList) {
         int status = iotDeviceMapper.updateIotDeviceBatch(iotDeviceList);
         iotDeviceGeoLocationCommonService.insertOrUpdateBatchData(iotDeviceList);
+        //检查离线告警消息
+        warnService.processWarningOfflineToOnlineDeviceData(iotDeviceList);
         return status;
     }
 
     @Override
     public IotDevice selectIotDeviceByDevBid(String devBid) {
-        return iotDeviceMapper.selectIotDeviceByDevBid(devBid);
+        IotDevice iotDevice = iotDeviceMapper.selectIotDeviceByDevBid(devBid);
+        if (iotDevice != null) {
+            iotDevice.setDevOriginalStatus(iotDevice.getDevStatus());
+        }
+        return iotDevice;
     }
 
     @Override

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

@@ -58,4 +58,5 @@ public interface IotWarnBussinessMapper {
 
     int insertIotOfflineWarnconfig(IotWarnconfig iotWarnconfig);
 
+    void autoDealWarnOfflineLog(IotWarnlog iotWarnlog);
 }

+ 30 - 12
src/main/java/com/yunfeiyun/agmp/iots/warn/service/IotWarnBussinessService.java

@@ -26,7 +26,7 @@ import java.util.Map;
 public class IotWarnBussinessService {
 
     @Autowired
-    private IotWarnBussinessMapper iotWarncountMapper;
+    private IotWarnBussinessMapper iotWarnBussinessMapper;
 
     @Resource
     private RedisCacheManager redisCacheManager;
@@ -38,7 +38,7 @@ public class IotWarnBussinessService {
             log.info("从缓存中获取到重复次数: {}", count);
             return count;
         }
-        IotWarncount iotWarncount = iotWarncountMapper.selectIotWarnCountByDevAndConfig(devId, configId);
+        IotWarncount iotWarncount = iotWarnBussinessMapper.selectIotWarnCountByDevAndConfig(devId, configId);
         if (iotWarncount == null) {
             log.warn("数据库中未找到设备ID: {}, 配置ID: {} 的重复次数数据", devId, configId);
             return null;
@@ -60,12 +60,12 @@ public class IotWarnBussinessService {
             iotWarncount.setLastUpdateTime(DateUtils.dateTimeNow());
             iotWarncount.setWctCount(1L);
             iotWarncount.setTid(tid);
-            int result = iotWarncountMapper.insertIncrementReCount(iotWarncount);
+            int result = iotWarnBussinessMapper.insertIncrementReCount(iotWarncount);
             redisCacheManager.setCacheObject(RedisCacheKey.IOT_WARN_RE_COUNTS, configId + ":" + devId, iotWarncount.getWctCount());
             log.info("初始化重复次数成功, 结果: {}", result);
             return result;
         } else {
-            int result = iotWarncountMapper.updateIncrementReCount(devId, configId);
+            int result = iotWarnBussinessMapper.updateIncrementReCount(devId, configId);
             redisCacheManager.setCacheObject(RedisCacheKey.IOT_WARN_RE_COUNTS, configId + ":" + devId, reCount + 1);
             log.info("更新重复次数成功, 新的重复次数: {}", reCount + 1);
             return result;
@@ -78,7 +78,7 @@ public class IotWarnBussinessService {
 
     public int resetReCount() {
         log.info("重置所有重复次数");
-        List<IotWarncount> iotWarncounts = iotWarncountMapper.getAllReCount();
+        List<IotWarncount> iotWarncounts = iotWarnBussinessMapper.getAllReCount();
         for (IotWarncount iotWarncount : iotWarncounts) {
             try {
                 redisCacheManager.deleteObject(RedisCacheKey.IOT_WARN_RE_COUNTS, iotWarncount.getWcBid() + ":" + iotWarncount.getDevBid());
@@ -87,7 +87,7 @@ public class IotWarnBussinessService {
                 log.info("重置所有重复次数:配置id:{},设备id{},异常:{}", iotWarncount.getWcBid(), iotWarncount.getDevBid(), e);
             }
         }
-        int result = iotWarncountMapper.resetReCount();
+        int result = iotWarnBussinessMapper.resetReCount();
         log.info("重置所有重复次数结果: {}", result);
         return result;
     }
@@ -95,28 +95,28 @@ public class IotWarnBussinessService {
     public int resetReCountByDevIdAndConfigId(String devId, String configId) {
         log.info("重置设备ID: {}, 配置ID: {} 的重复次数", devId, configId);
         redisCacheManager.deleteObject(RedisCacheKey.IOT_WARN_RE_COUNTS, configId + ":" + devId);
-        int result = iotWarncountMapper.resetReCountByDevIdAndConfigId(devId, configId);
+        int result = iotWarnBussinessMapper.resetReCountByDevIdAndConfigId(devId, configId);
         log.info("重置结果: {}", result);
         return result;
     }
 
     public int insertWarnRecord(IotWarnlog iotWarnlog) {
         log.info("插入预警记录: {}", iotWarnlog);
-        int result = iotWarncountMapper.insertWarnRecord(iotWarnlog);
+        int result = iotWarnBussinessMapper.insertWarnRecord(iotWarnlog);
         log.info("插入预警记录结果: {}", result);
         return result;
     }
 
     public List<WarnConfigInfo> selectIotWarnConfigInfoList(WarnConfigInfo warnConfigInfo) {
         log.info("查询预警配置信息列表");
-        List<WarnConfigInfo> list = iotWarncountMapper.selectIotWarnConfigInfoList(warnConfigInfo);
+        List<WarnConfigInfo> list = iotWarnBussinessMapper.selectIotWarnConfigInfoList(warnConfigInfo);
         log.info("查询到 {} 条预警配置信息", list.size());
         return list;
     }
 
     public IotWarnconfig selectIotWarnOfflineConfigInfo(String tid) {
         log.info("查询预警离线配置tid:{}", tid);
-        IotWarnconfig list = iotWarncountMapper.selectIotWarnOfflineConfigInfo(tid);
+        IotWarnconfig list = iotWarnBussinessMapper.selectIotWarnOfflineConfigInfo(tid);
         return list;
     }
 
@@ -136,9 +136,14 @@ public class IotWarnBussinessService {
     }
 
     public List<String> selectAllTid() {
-        return iotWarncountMapper.selectAllTid();
+        return iotWarnBussinessMapper.selectAllTid();
     }
 
+    /**
+     * 插入离线告警规则
+     * @param tid
+     * @return
+     */
     public int insertIotOfflineWarnConfig(String tid) {
         IotWarnconfig iotWarnconfig = new IotWarnconfig();
         iotWarnconfig.setWcBid(IdUtils.fastUUID());
@@ -150,6 +155,19 @@ public class IotWarnBussinessService {
         iotWarnconfig.setWcCreator("system");
         iotWarnconfig.setWcCreateddate(DateUtils.dateTimeNow());
         iotWarnconfig.setTid(tid);
-        return iotWarncountMapper.insertIotOfflineWarnconfig(iotWarnconfig);
+        return iotWarnBussinessMapper.insertIotOfflineWarnconfig(iotWarnconfig);
+    }
+
+    /**
+     * 设备若正常上报数据后,则离线告警自动处理
+     * @param devId
+     */
+    public void autoDealWarnOfflineLog(String devId) {
+        IotWarnlog iotWarnlog = new IotWarnlog();
+        iotWarnlog.setDevBid(devId);
+        iotWarnlog.setWlDealtime(DateUtils.dateTimeNow());
+        iotWarnlog.setWlDealresult("该设备已于" + DateUtils.dateTimeNow() + "上报数据,离线告警自动恢复。");
+        iotWarnlog.setStatus("1");
+        iotWarnBussinessMapper.autoDealWarnOfflineLog(iotWarnlog);
     }
 }

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

@@ -531,4 +531,31 @@ public class WarnService {
 
     }
 
+    /**
+     * 拦截处理设备变更:如果离线转离线进行预警处理
+     *
+     * @param iotDeviceList
+     */
+    public void processWarningOfflineToOnlineDeviceData(List<IotDevice> iotDeviceList) {
+        try {
+            CompletableFuture.runAsync(() -> {
+                for (IotDevice iotDevice : iotDeviceList) {
+                    //离线转在线
+                    if (IotDeviceStatusTypeEnum.OFFLINE.getCode().equals(iotDevice.getDevOriginalStatus())
+                            && IotDeviceStatusTypeEnum.ONLINE.getCode().equals(iotDevice.getDevStatus()
+                    )) {
+                        log.info("[离线告警]自动处理离线告警消息: {}", iotDevice.getDevBid());
+                        autoDealWarnOfflineLog(iotDevice.getDevBid());
+                    }
+                }
+
+            }, threadPoolTaskExecutor);
+        } catch (Exception e) {
+            log.error("[离线告警]自动处理离线告警消息:失败 {}", e);
+        }
+    }
+
+    private void autoDealWarnOfflineLog(String devId) {
+        iotWarnBussinessService.autoDealWarnOfflineLog(devId);
+    }
 }

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

@@ -74,6 +74,7 @@
               update IotWarncount set wctCount=0 where devBid=#{devBid} and wcBid=#{wcBid};
     </update>
 
+
     <select id="selectIotWarnCountByDevAndConfig"
             resultType="com.yunfeiyun.agmp.iot.common.domain.IotWarncount">
         select  * from IotWarncount where devBid=#{devBid} and wcBid=#{wcBid};
@@ -140,4 +141,14 @@
             <if test="tid != null and tid != ''">#{tid},</if>
         </trim>
     </insert>
+    <update id="autoDealWarnOfflineLog">
+        update IotWarnlog
+        <trim prefix="SET" suffixOverrides=",">
+            <if test="status != null">status = #{status},</if>
+            <if test="wlDealresult != null">wlDealresult = #{wlDealresult},</if>
+            <if test="wlDealtime != null">wlDealtime = #{wlDealtime},</if>
+        </trim>
+        where devBid = #{devBid}
+
+    </update>
 </mapper>