Browse Source

增加同步设备与报警条件关联关系的处理

lixing 3 years ago
parent
commit
e127945802

+ 12 - 0
AlarmEngineStarter/src/main/java/com/persagy/apm/energyalarmstarter/alarmengine/constants/RedisConstants.java

@@ -0,0 +1,12 @@
+package com.persagy.apm.energyalarmstarter.alarmengine.constants;
+
+/**
+ * 类说明
+ *
+ * @author lixing
+ * @version V1.0 2021/11/20 5:20 下午
+ **/
+public class RedisConstants {
+    public static final String ALARM_CONDITIONS = "ALARM_CONDITIONS";
+    public static final String OBJ_CONDITION_REL = "OBJ_CONDITION_REL";
+}

+ 72 - 7
AlarmEngineStarter/src/main/java/com/persagy/apm/energyalarmstarter/alarmengine/jms/AlarmEngineMsgHandler.java

@@ -4,7 +4,12 @@ import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.persagy.apm.energyalarmstarter.alarmdata.mq.model.DmpMessage;
+import com.persagy.apm.energyalarmstarter.alarmengine.constants.RedisConstants;
 import com.persagy.apm.energyalarmstarter.alarmengine.feign.AlarmCondition;
+import com.persagy.apm.energyalarmstarter.alarmengine.feign.ObjConditionRel;
+import com.persagy.apm.energyalarmstarter.alarmengine.jms.model.ItemCodeCondition;
+import com.persagy.apm.energyalarmstarter.alarmengine.jms.model.ObjConditionInfo;
+import com.persagy.apm.energyalarmstarter.alarmengine.redis.RedisUtil;
 import com.persagy.apm.energyalarmstarter.alarmengine.service.AlarmRecordMsgHandler;
 import io.netty.channel.ChannelHandler;
 import lombok.SneakyThrows;
@@ -15,6 +20,7 @@ import org.springframework.data.redis.core.RedisTemplate;
 import javax.annotation.Resource;
 import org.springframework.util.CollectionUtils;
 
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -33,6 +39,9 @@ public class AlarmEngineMsgHandler {
     @Resource(name = "alarmRedisTemplate")
     private RedisTemplate<String, String> alarmRedisTemplate;
 
+    @Autowired
+    private RedisUtil redisUtil;
+
     @SneakyThrows
     public String getAlarmCategory(DmpMessage dmpMessage) {
         JSONObject data = dmpMessage.getExts();
@@ -136,8 +145,63 @@ public class AlarmEngineMsgHandler {
      * @version V1.0 2021/10/26 7:51 下午
      */
     public void syncNewObjConditionRelList(DmpMessage msg) {
-        JSONArray createdRelList = JSONObject.parseArray(msg.getStr1());
-        // TODO: 2021/11/16 更新redis
+        List<ObjConditionRel> createdRelList = JSONObject.parseArray(msg.getStr1(), ObjConditionRel.class);
+        syncCreatedObjConditionRelList2Redis(createdRelList);
+    }
+
+    /**
+     * 新的设备与报警条件关联关系同步至redis
+     *
+     * @param createdRelList 新增的关联关系
+     * @author lixing
+     * @version V1.0 2021/11/20 5:29 下午
+     */
+    private void syncCreatedObjConditionRelList2Redis(List<ObjConditionRel> createdRelList) {
+        if (CollectionUtils.isEmpty(createdRelList)) {
+            return;
+        }
+        for (ObjConditionRel objConditionRel : createdRelList) {
+            // 报警对象id
+            String objId = objConditionRel.getObjId();
+            // 报警对象和报警条件的关联信息
+            ObjConditionInfo objConditionInfo = redisUtil.get(RedisConstants.OBJ_CONDITION_REL, objId, ObjConditionInfo.class);
+            if (objConditionInfo == null) {
+                objConditionInfo = new ObjConditionInfo(objConditionRel.getProjectId());
+            }
+            ItemCodeCondition condition = new ItemCodeCondition();
+            condition.setConditionId(objConditionRel.getConditionId());
+            condition.setItemCode(objConditionRel.getItemCode());
+            // 对象内部自己做去重操作
+            objConditionInfo.addCondition(condition);
+            redisUtil.put(RedisConstants.OBJ_CONDITION_REL, objId, JSON.toJSONString(objConditionInfo));
+        }
+    }
+
+    /**
+     * 删除的设备与报警条件关联关系同步至redis
+     *
+     * @param deletedRelList 删除的关联关系
+     * @author lixing
+     * @version V1.0 2021/11/20 5:29 下午
+     */
+    private void syncDeletedObjConditionRelList2Redis(List<ObjConditionRel> deletedRelList) {
+        if (CollectionUtils.isEmpty(deletedRelList)) {
+            return;
+        }
+        for (ObjConditionRel objConditionRel : deletedRelList) {
+            // 报警对象id
+            String objId = objConditionRel.getObjId();
+            ObjConditionInfo objConditionInfo = redisUtil.get(RedisConstants.OBJ_CONDITION_REL, objId, ObjConditionInfo.class);
+            ItemCodeCondition condition = new ItemCodeCondition();
+            condition.setConditionId(objConditionRel.getConditionId());
+            condition.setItemCode(objConditionRel.getItemCode());
+            objConditionInfo.removeCondition(condition);
+            if (CollectionUtils.isEmpty(objConditionInfo.getConditions())) {
+                redisUtil.delete(RedisConstants.OBJ_CONDITION_REL, objId);
+            } else {
+                redisUtil.put(RedisConstants.OBJ_CONDITION_REL, objId, JSON.toJSONString(objConditionInfo));
+            }
+        }
     }
 
     /**
@@ -148,15 +212,16 @@ public class AlarmEngineMsgHandler {
      * @version V1.0 2021/10/26 7:51 下午
      */
     public void syncDeletedObjConditionRelList(DmpMessage msg) {
-        JSONArray deletedRelList = JSONObject.parseArray(msg.getStr1());
-        // TODO: 2021/11/16 更新redis
+        List<ObjConditionRel> deletedRelList = JSONObject.parseArray(msg.getStr1(), ObjConditionRel.class);
+        syncDeletedObjConditionRelList2Redis(deletedRelList);
     }
 
     public void syncUpdateObjConditionRelList(DmpMessage msg) {
-        JSONArray deletedRelList = JSONObject.parseArray(msg.getStr1());
-        JSONArray createdRelList = JSONObject.parseArray(msg.getStr2());
+        List<ObjConditionRel> deletedRelList = JSONObject.parseArray(msg.getStr1(), ObjConditionRel.class);
+        List<ObjConditionRel> createdRelList = JSONObject.parseArray(msg.getStr2(), ObjConditionRel.class);
         // 先删除,再创建
-        // TODO: 2021/11/16 更新redis
+        syncDeletedObjConditionRelList2Redis(deletedRelList);
+        syncCreatedObjConditionRelList2Redis(createdRelList);
     }
 
     /**

+ 7 - 17
AlarmEngineStarter/src/main/java/com/persagy/apm/energyalarmstarter/alarmengine/jms/JmsConfig.java

@@ -3,6 +3,7 @@ package com.persagy.apm.energyalarmstarter.alarmengine.jms;
 import com.alibaba.fastjson.JSONObject;
 import com.google.common.collect.Sets;
 import com.persagy.apm.energyalarmstarter.alarmdata.mq.model.DmpMessage;
+import com.persagy.apm.energyalarmstarter.alarmengine.redis.RedisUtil;
 import com.rabbitmq.client.Channel;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang.StringUtils;
@@ -29,6 +30,10 @@ import java.util.List;
 @Configuration
 public class JmsConfig {
     /**
+     * 报警记录锁前缀
+     */
+    private static final String alarmRecordLockPrefix = "ALARM_RECORD_LOCK:";
+    /**
      * 子系统可处理的报警系统
      */
     @Value("#{'${alarm.category.list}'.split(',')}")
@@ -42,7 +47,7 @@ public class JmsConfig {
     private AlarmEngineMsgHandler msgHandler;
 
     @Autowired
-    private RedisTemplate redisTemplate;
+    private RedisUtil redisUtil;
 
     /**
      * 交换机
@@ -202,7 +207,7 @@ public class JmsConfig {
             }
             //解锁
             JSONObject data = msg.getExts();
-            unlock(data.getString("id"));
+            redisUtil.unlock(alarmRecordLockPrefix + data.getString("id"));
         } catch (Exception e) {
             log.error("报警记录消息消费失败,{}", e.getMessage());
         }
@@ -210,21 +215,6 @@ public class JmsConfig {
     }
 
     /**
-     * redis解锁
-     *
-     * @param key 要解锁的key
-     * @author lixing
-     * @version V1.0 2021/11/20 2:19 下午
-     */
-    public void unlock(String key) {
-        try {
-            redisTemplate.delete(key);
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
-
-    /**
      * 数据中台消息处理
      *
      * @author lixing

+ 50 - 0
AlarmEngineStarter/src/main/java/com/persagy/apm/energyalarmstarter/alarmengine/redis/RedisUtil.java

@@ -0,0 +1,50 @@
+package com.persagy.apm.energyalarmstarter.alarmengine.redis;
+
+import cn.hutool.json.JSONUtil;
+import com.alibaba.fastjson.JSON;
+import lombok.SneakyThrows;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.poi.ss.formula.functions.T;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.stereotype.Service;
+
+/**
+ * redis工具类
+ *
+ * @author lixing
+ * @version V1.0 2021/11/20 5:00 下午
+ **/
+@Service
+public class RedisUtil {
+    @Autowired
+    RedisTemplate redisTemplate;
+
+    /**
+     * redis解锁
+     *
+     * @param key 要解锁的key
+     * @author lixing
+     * @version V1.0 2021/11/20 2:19 下午
+     */
+    @SneakyThrows
+    public void unlock(String key) {
+        redisTemplate.delete(key);
+    }
+
+    public <T> T get(String mapName, String key, Class<T> resultClazz) {
+        Object obj = redisTemplate.opsForHash().get(mapName, key);
+        if (obj == null) {
+            return null;
+        }
+        return JSONUtil.toBean(obj.toString(), resultClazz);
+    }
+
+    public void put(String mapName, String key, String value) {
+        redisTemplate.opsForHash().put(mapName, key, value);
+    }
+
+    public void delete(String mapName, String key) {
+        redisTemplate.opsForHash().delete(mapName, key);
+    }
+}