Selaa lähdekoodia

Merge branch 'v2.0.0' of http://39.106.8.246:3003/apm/energy-alarm-starter.git into v2.0.0

gaoyu 3 vuotta sitten
vanhempi
commit
9852cfebba

+ 2 - 2
AlarmDataStarter/src/main/java/com/persagy/apm/energyalarmstarter/alarmdata/model/dto/AlarmCondition.java

@@ -39,7 +39,7 @@ public class AlarmCondition {
     /**
      * 存储用户自定义的配置
      */
-    private JSONArray configs;
+    private String configs;
     /**
      * 报警规则
      */
@@ -47,5 +47,5 @@ public class AlarmCondition {
     /**
      * 报警生效时间
      */
-    private AlarmEffectTime effectTime;
+    private String effectTime;
 }

+ 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";
+}

+ 6 - 1
AlarmEngineStarter/src/main/java/com/persagy/apm/energyalarmstarter/alarmengine/feign/AlarmCondition.java

@@ -81,5 +81,10 @@ public class AlarmCondition {
     /**
      * 生效时间详情
      */
-    private JSONObject effectTimeDetail;
+    private String effectTimeDetail;
+
+    /**
+     * 数据中台中需要展示给前端的内容
+     */
+    private String configs;
 }

+ 177 - 13
AlarmEngineStarter/src/main/java/com/persagy/apm/energyalarmstarter/alarmengine/jms/AlarmEngineMsgHandler.java

@@ -1,15 +1,32 @@
 package com.persagy.apm.energyalarmstarter.alarmengine.jms;
 
+import cn.hutool.core.collection.CollectionUtil;
+import cn.hutool.json.JSONUtil;
+import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.persagy.apm.energyalarmstarter.alarmdata.model.vo.AlarmConfigItem;
 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.RedisUtil4EngineStater;
 import com.persagy.apm.energyalarmstarter.alarmengine.service.AlarmRecordMsgHandler;
+import io.micrometer.core.instrument.util.JsonUtils;
 import io.netty.channel.ChannelHandler;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 
+import javax.annotation.Resource;
+
+import org.springframework.util.CollectionUtils;
+
+import java.util.*;
+
 /**
  * @description: 报警引擎消息处理类
  * @author: lixing
@@ -23,11 +40,14 @@ public class AlarmEngineMsgHandler {
     @Autowired
     private AlarmRecordMsgHandler alarmRecordMsgHandler;
 
+    @Autowired
+    private RedisUtil4EngineStater redisUtil;
+
     @SneakyThrows
     public String getAlarmCategory(DmpMessage dmpMessage) {
         JSONObject data = dmpMessage.getExts();
         String itemCode = data.getString("itemCode");
-        return alarmRecordMsgHandler.getAlarmCategory(alarmRecordMsgHandler.getAlarmUrlParam(data), itemCode);
+        return alarmRecordMsgHandler.getAlarmCategory(alarmRecordMsgHandler.getAlarmUrlParam(dmpMessage), itemCode);
     }
 
     /**
@@ -87,7 +107,7 @@ public class AlarmEngineMsgHandler {
      */
     public void syncNewCondition(DmpMessage msg) {
         AlarmCondition alarmCondition = JSONObject.parseObject(msg.getStr1(), AlarmCondition.class);
-        // TODO: 2021/11/16  更新redis
+        redisUtil.put(RedisConstants.ALARM_CONDITIONS, alarmCondition.getId(), alarmCondition);
     }
 
     /**
@@ -99,7 +119,7 @@ public class AlarmEngineMsgHandler {
      */
     public void syncUpdatedCondition(DmpMessage msg) {
         AlarmCondition alarmCondition = JSONObject.parseObject(msg.getStr1(), AlarmCondition.class);
-        // TODO: 2021/11/16  更新redis
+        redisUtil.put(RedisConstants.ALARM_CONDITIONS, alarmCondition.getId(), alarmCondition);
     }
 
     /**
@@ -110,9 +130,8 @@ public class AlarmEngineMsgHandler {
      * @version V1.0 2021/10/26 7:51 下午
      */
     public void syncDeletedCondition(DmpMessage msg) {
-        AlarmCondition alarmCondition = new AlarmCondition();
-        alarmCondition.setId(msg.getStr1());
-        // TODO: 2021/11/16 更新redis
+        String conditionId = msg.getStr1();
+        redisUtil.delete(RedisConstants.ALARM_CONDITIONS, conditionId);
     }
 
     /**
@@ -123,8 +142,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, 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, objConditionInfo);
+            }
+        }
     }
 
     /**
@@ -135,14 +209,104 @@ 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);
+    }
+
+    /**
+     * 增量同步报警定义
+     *
+     * @author lixing
+     * @version V1.0 2021/11/20 3:08 下午
+     */
+    @SneakyThrows
+    public void incrementSyncAlarmConfig(DmpMessage dmpMessage) {
+        Map<String, JSONArray> changedAlarmConfigs = alarmRecordMsgHandler.queryChangedAlarmConfigs(dmpMessage);
+        JSONArray createdConfigUniques = changedAlarmConfigs.get("createdConfigUniques");
+        JSONArray deletedConfigUniques = changedAlarmConfigs.get("deletedConfigUniques");
+        if (!CollectionUtils.isEmpty(deletedConfigUniques)) {
+            // 处理删除的报警定义 // TODO: 2021/11/20
+            // 报警定义中不包含报警条件的id
+            // 需要根据报警定义中的报警类型,从设备与报警条件的关联关系中找到对应的报警条件id。
+            // 删除报警条件
+            // 删除关联关系
+            List<AlarmConfigItem> alarmConfigItems = deletedConfigUniques.toJavaList(AlarmConfigItem.class);
+            for (AlarmConfigItem alarmConfigItem : alarmConfigItems) {
+                //从redis拿到设备的报警条件信息
+                ObjConditionInfo objConditionInfo = redisUtil.get(RedisConstants.OBJ_CONDITION_REL, alarmConfigItem.getObjId(), ObjConditionInfo.class);
+                LinkedList<ItemCodeCondition> conditions = objConditionInfo.getConditions();
+                LinkedList<ItemCodeCondition> conditionsRemain = objConditionInfo.getConditions();
+                for (ItemCodeCondition condition : conditions) {
+                    if (alarmConfigItem.getItemCode().equals(condition.getItemCode())) {
+                        //删除报警条件
+                        redisUtil.delete(RedisConstants.ALARM_CONDITIONS, condition.getConditionId());
+                    } else {
+                        conditionsRemain.add(condition);
+                    }
+                }
+                if (CollectionUtil.isEmpty(conditionsRemain)) {
+                    redisUtil.delete(RedisConstants.OBJ_CONDITION_REL, alarmConfigItem.getObjId());
+                } else {
+                    redisUtil.put(RedisConstants.OBJ_CONDITION_REL, alarmConfigItem.getObjId(), conditionsRemain);
+                }
+            }
+        }
+        if (!CollectionUtils.isEmpty(createdConfigUniques)) {
+            List<AlarmConfigItem> alarmConfigItems = createdConfigUniques.toJavaList(AlarmConfigItem.class);
+            List<ObjConditionRel> createdRelList = new ArrayList<>();
+            for (AlarmConfigItem alarmConfigItem : alarmConfigItems) {
+                // 数据中台报警条件转化为业务中台报警条件和设备与条件的关联关系
+                AlarmCondition alarmCondition = avgAlarmDmpCondition(alarmConfigItem);
+                if (alarmCondition == null) {
+                    continue;
+                }
+                // 存储报警条件
+                redisUtil.put(RedisConstants.ALARM_CONDITIONS, alarmCondition.getId(), alarmCondition);
+                // 存储设备与条件的关联关系
+                ObjConditionRel objConditionRel = new ObjConditionRel();
+                objConditionRel.setConditionId(alarmCondition.getId());
+                objConditionRel.setObjId(alarmConfigItem.getObjId());
+                objConditionRel.setItemCode(alarmConfigItem.getItemCode());
+                objConditionRel.setProjectId(alarmConfigItem.getProjectId());
+                createdRelList.add(objConditionRel);
+            }
+            syncCreatedObjConditionRelList2Redis(createdRelList);
+        }
+    }
+
+    private  AlarmCondition  avgAlarmDmpCondition(AlarmConfigItem alarmConfigItem){
+        AlarmCondition alarmCondition = new AlarmCondition();
+        alarmCondition.setId(alarmConfigItem.getId());
+        List<String> infoCodes = new ArrayList<>();
+        if (alarmConfigItem.getCondition() == null) {
+            return null;
+        }
+        for (String infoCode : alarmConfigItem.getCondition().getInfoCode()) {
+            infoCodes.add(infoCode.replaceAll(alarmConfigItem.getObjId()+"_",""));
+        }
+        alarmCondition.setInfoCodes(infoCodes);
+        alarmCondition.setConfigs(alarmConfigItem.getCondition().getConfigs());
+        alarmCondition.setTriggerBackend(alarmConfigItem.getCondition().getTrigger().replaceAll(alarmConfigItem.getObjId()+"_",""));
+        alarmCondition.setTriggerUphold(alarmConfigItem.getCondition().getTriggerUphold()/60);
+        alarmCondition.setAlarmLevel(alarmConfigItem.getLevel());
+        alarmCondition.setEndUphold(alarmConfigItem.getCondition().getEndUphold()/60);
+        //alarmCondition.setTriggerRules(alarmDmpCondition.getRules());
+        alarmCondition.setEndBackend(alarmConfigItem.getCondition().getEnd().replaceAll(alarmConfigItem.getObjId()+"_",""));
+        //  alarmCondition.setEndRules(null);
+        String effectTime = alarmConfigItem.getCondition().getEffectTime();
+        if (StringUtils.isNotBlank(effectTime)) {
+            JSONObject effectTimeObj = JSONObject.parseObject(effectTime);
+            alarmCondition.setEffectTimeType(effectTimeObj.getString("type"));
+            alarmCondition.setEffectTimeDetail(effectTime);
+        }
+        return alarmCondition;
     }
 }

+ 3 - 3
AlarmEngineStarter/src/main/java/com/persagy/apm/energyalarmstarter/alarmengine/jms/JmsAlarmRecordEnum.java

@@ -15,9 +15,9 @@ public enum JmsAlarmRecordEnum {
     /**
      * 字典类型
      */
-    NEW_ALARM_RECORD("new_alarmRecord", "新增报警记录"),
-    UPDATE_ALARM_RECORD("update_alarmRecord", "更新报警记录"),
-    CONDITION_ALARM("continue_alarm", "报警持续处理");
+    NEW_ALARM_RECORD("new_alarm_record", "新增报警记录"),
+    UPDATE_ALARM_RECORD("update_alarm_record", "更新报警记录"),
+    CONTINUE_ALARM("continue_alarm", "报警持续处理");
 
     @Setter
     @Getter

+ 64 - 11
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.RedisUtil4EngineStater;
 import com.rabbitmq.client.Channel;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang.StringUtils;
@@ -13,7 +14,6 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
-import org.springframework.data.redis.core.RedisTemplate;
 
 import java.util.HashSet;
 import java.util.List;
@@ -29,6 +29,10 @@ import java.util.List;
 @Configuration
 public class JmsConfig {
     /**
+     * 报警记录锁前缀
+     */
+    private static final String alarmRecordLockPrefix = "ALARM_RECORD_LOCK:";
+    /**
      * 子系统可处理的报警系统
      */
     @Value("#{'${alarm.category.list}'.split(',')}")
@@ -42,7 +46,7 @@ public class JmsConfig {
     private AlarmEngineMsgHandler msgHandler;
 
     @Autowired
-    private RedisTemplate redisTemplate;
+    private RedisUtil4EngineStater redisUtil;
 
     /**
      * 交换机
@@ -60,6 +64,11 @@ public class JmsConfig {
     private final String objConditionRelRoutingKey = "obj-condition-rel-routing-key";
 
     /**
+     * 报警定义路由键
+     */
+    private static final String alarmConfigRoutingKey = "alarm-config-routing-key";
+
+    /**
      * 报警记录 路由键
      */
     private final String alarmRecordRoutingKey = "alarm-msg-routing-key";
@@ -70,6 +79,11 @@ public class JmsConfig {
     private final String conditionQueue = "condition-queue";
 
     /**
+     * 报警定义队列(数据中台设备的报警条件)
+     */
+    private final String alarmConfigQueue = "alarm-config-queue";
+
+    /**
      * 设备与报警条件关联关系队列
      */
     private final String objConditionRelQueue = "obj-condition-rel-queue";
@@ -89,6 +103,11 @@ public class JmsConfig {
     }
 
     @Bean
+    public Queue alarmConfigQueue() {
+        return new Queue(alarmConfigQueue, true);
+    }
+
+    @Bean
     public Queue alarmRecordQueue() {
         return new Queue(alarmRecordQueue, true);
     }
@@ -109,13 +128,24 @@ public class JmsConfig {
     }
 
     @Bean
+    public Binding alarmConfigBinding() {
+        return BindingBuilder.bind(alarmConfigQueue()).to(alarmServiceExchange()).with(alarmConfigRoutingKey);
+    }
+
+    @Bean
     public Binding alarmRecordBinding() {
         return BindingBuilder.bind(alarmRecordQueue()).to(alarmServiceExchange()).with(alarmRecordRoutingKey);
     }
 
+    /**
+     * 处理业务中台的消息
+     *
+     * @author lixing
+     * @version V1.0 2021/11/19 10:43 下午
+     */
     @RabbitHandler
     @RabbitListener(queues = {conditionQueue, objConditionRelQueue})
-    public void dealMsg(DmpMessage msg, Channel channel, Message message) throws Exception {
+    public void dealAlarmServiceMsg(DmpMessage msg, Channel channel, Message message) throws Exception {
         try {
             log.debug("============================== Receive:" + msg);
             if (JmsTypeEnum.NEW_CONDITION.getType().equals(msg.getType())) {
@@ -142,12 +172,21 @@ public class JmsConfig {
         channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
     }
 
+    /**
+     * 处理报警引擎报警消息
+     *
+     * @author lixing
+     * @version V1.0 2021/11/20 2:19 下午
+     */
     @RabbitHandler
     @RabbitListener(queues = {alarmRecordQueue})
     public void dealAlarmRecordMsg(DmpMessage msg, Channel channel, Message message) throws Exception {
         try {
             // 根据报警消息获取报警系统
             String alarmCategory = msgHandler.getAlarmCategory(msg);
+            if (StringUtils.isBlank(alarmCategory)) {
+                return;
+            }
             HashSet<String> categorySet = Sets.newHashSet(categoryList);
             if (!categorySet.contains(alarmCategory)) {
                 // 如果子系统不能处理此报警系统的消息,将消息扔回消息队列
@@ -162,27 +201,41 @@ public class JmsConfig {
             if (JmsAlarmRecordEnum.UPDATE_ALARM_RECORD.getType().equals(msg.getType())) {
                 msgHandler.updateAlarmRecord(msg);
             }
-            if (JmsAlarmRecordEnum.CONDITION_ALARM.getType().equals(msg.getType())) {
+            if (JmsAlarmRecordEnum.CONTINUE_ALARM.getType().equals(msg.getType())) {
                 msgHandler.alarmContinue(msg);
             }
             //解锁
             JSONObject data = msg.getExts();
-            unlock(data.getString("id"));
+            redisUtil.unlock(alarmRecordLockPrefix + data.getString("id"));
         } catch (Exception e) {
             log.error("报警记录消息消费失败,{}", e.getMessage());
         }
         channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
     }
 
-
-    public void unlock(String key) {
+    /**
+     * 数据中台消息处理
+     *
+     * @author lixing
+     * @version V1.0 2021/11/20 2:20 下午
+     */
+    @RabbitHandler
+    @RabbitListener(queues = {alarmConfigQueue})
+    public void dealDmpAlarmMsg(DmpMessage msg, Channel channel, Message message) {
         try {
-            String currentValue = String.valueOf(redisTemplate.opsForValue().get(key));
-            if (!StringUtils.isEmpty(currentValue)) {
-                redisTemplate.opsForValue().getOperations().delete(key);
+            log.debug("============================== Receive:" + msg);
+            //报警定义变化
+            if (JmsTypeEnum.ALARM_CONFIGS_CHANGE.getType().equals(msg.getType())) {
+                log.debug("================收到一条报警定义变化通知==============");
+                try {
+                    msgHandler.incrementSyncAlarmConfig(msg);
+                } catch (Exception e) {
+                    log.error("error", e);
+                }
             }
+            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
         } catch (Exception e) {
-            e.printStackTrace();
+            log.error("消息消费失败,{}", e.getMessage());
         }
     }
 }

+ 2 - 1
AlarmEngineStarter/src/main/java/com/persagy/apm/energyalarmstarter/alarmengine/jms/JmsTypeEnum.java

@@ -23,7 +23,8 @@ public enum JmsTypeEnum {
     DELETE_CONDITION("delete_condition", "删除报警条件"),
     NEW_OBJ_CONDITION_REL("new_obj_condition_rel", "新增条件和设备的关联关系"),
     DELETE_OBJ_CONDITION_REL("delete_obj_condition_rel", "删除条件和设备的关联关系"),
-    UPDATE_OBJ_CONDITION_REL("update_obj_condition_rel", "更新条件和设备的关联关系");
+    UPDATE_OBJ_CONDITION_REL("update_obj_condition_rel", "更新条件和设备的关联关系"),
+    ALARM_CONFIGS_CHANGE("alarm_configs_change", "数据中台报警条件更新");
 
 
     @Setter

+ 25 - 0
AlarmEngineStarter/src/main/java/com/persagy/apm/energyalarmstarter/alarmengine/jms/model/ItemCodeCondition.java

@@ -0,0 +1,25 @@
+package com.persagy.apm.energyalarmstarter.alarmengine.jms.model;
+
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * 设备与报警条件的关联
+ *
+ * @author lixing
+ * @version V1.0 2021/10/22 9:42 上午
+ **/
+public class ItemCodeCondition {
+    /**
+     * 报警类型编码
+     */
+    @Setter
+    @Getter
+    private String itemCode;
+    /**
+     * 报警条件id
+     */
+    @Setter
+    @Getter
+    private String conditionId;
+}

+ 66 - 0
AlarmEngineStarter/src/main/java/com/persagy/apm/energyalarmstarter/alarmengine/jms/model/ObjConditionInfo.java

@@ -0,0 +1,66 @@
+package com.persagy.apm.energyalarmstarter.alarmengine.jms.model;
+
+import lombok.Getter;
+import lombok.Setter;
+
+import java.util.LinkedList;
+
+/**
+ * 设备的报警条件信息
+ *
+ * @author lixing
+ * @version V1.0 2021/10/22 9:42 上午
+ **/
+public class ObjConditionInfo {
+
+    public ObjConditionInfo(String projectId) {
+        this.projectId = projectId;
+        this.conditions = new LinkedList<>();
+    }
+    /**
+     * 项目id
+     */
+    @Setter
+    @Getter
+    private String projectId;
+
+    /**
+     * 报警条件列表[{itemCode, conditionId}]
+     */
+    @Setter
+    @Getter
+    private LinkedList<ItemCodeCondition> conditions;
+
+    /**
+     * 设备报警条件列表中添加一个条件
+     *
+     * @param itemCodeCondition 条件对象
+     * @author lixing
+     * @version V1.0 2021/10/25 11:30 上午
+     */
+    public void addCondition(ItemCodeCondition itemCodeCondition) {
+        if (itemCodeCondition == null) {
+            return;
+        }
+        if (conditions == null) {
+            conditions = new LinkedList<>();
+        }
+        // 如果存在相同报警类型的配置,删除之前的配置,添加新的配置
+        conditions.removeIf(condition -> itemCodeCondition.getItemCode().equals(condition.getItemCode()));
+        conditions.add(itemCodeCondition);
+    }
+
+    /**
+     * 移除设备关联的条件
+     *
+     * @param condition 要移除的条件
+     * @author lixing
+     * @version V1.0 2021/10/25 9:14 下午
+     */
+    public void removeCondition(ItemCodeCondition condition) {
+        if (conditions == null) {
+            return;
+        }
+        conditions.removeIf(item -> item.getItemCode().equals(condition.getItemCode()));
+    }
+}

+ 48 - 0
AlarmEngineStarter/src/main/java/com/persagy/apm/energyalarmstarter/alarmengine/redis/RedisUtil4EngineStater.java

@@ -0,0 +1,48 @@
+package com.persagy.apm.energyalarmstarter.alarmengine.redis;
+
+import cn.hutool.json.JSONUtil;
+import com.alibaba.fastjson.JSON;
+import lombok.SneakyThrows;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.stereotype.Service;
+
+/**
+ * redis工具类(因为要注册到spring boot,要注意名称不能重复)
+ *
+ * @author lixing
+ * @version V1.0 2021/11/20 5:00 下午
+ **/
+@Service
+public class RedisUtil4EngineStater {
+    @Autowired
+    RedisTemplate alarmRedisTemplate;
+
+    /**
+     * redis解锁
+     *
+     * @param key 要解锁的key
+     * @author lixing
+     * @version V1.0 2021/11/20 2:19 下午
+     */
+    @SneakyThrows
+    public void unlock(String key) {
+        alarmRedisTemplate.delete(key);
+    }
+
+    public <T> T get(String mapName, String key, Class<T> resultClazz) {
+        Object obj = alarmRedisTemplate.opsForHash().get(mapName, key);
+        if (obj == null) {
+            return null;
+        }
+        return JSONUtil.toBean(JSON.toJSONString(obj), resultClazz);
+    }
+
+    public void put(String mapName, String key, Object value) {
+        alarmRedisTemplate.opsForHash().put(mapName, key, value);
+    }
+
+    public void delete(String mapName, String key) {
+        alarmRedisTemplate.opsForHash().delete(mapName, key);
+    }
+}

+ 21 - 0
AlarmEngineStarter/src/main/java/com/persagy/apm/energyalarmstarter/alarmengine/service/BaseService.java

@@ -2,6 +2,7 @@ package com.persagy.apm.energyalarmstarter.alarmengine.service;
 
 import com.alibaba.fastjson.JSONObject;
 import com.persagy.apm.energyalarmstarter.alarmdata.feign.AlarmUrlParam;
+import com.persagy.apm.energyalarmstarter.alarmdata.mq.model.DmpMessage;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.util.CollectionUtils;
 
@@ -70,6 +71,26 @@ public class BaseService {
     }
 
     /**
+     * 从数据中台消息中获取AlarmUrlParam
+     *
+     * @param msg 数据中台消息
+     * @return AlarmUrlParam
+     * @author lixing
+     * @version V1.0 2021/11/19 10:15 下午
+     */
+    public AlarmUrlParam getAlarmUrlParam(DmpMessage msg) throws Exception {
+        String projectId = StringUtils.isBlank(msg.getProjectId())? "0": msg.getProjectId();
+
+
+        return new AlarmUrlParam(
+                msg.getUserId(),
+                msg.getGroupCode(),
+                projectId,
+                msg.getAppId()
+        );
+    }
+
+    /**
      * @description: 获取request中的body
      * @param: obj
      * @return: com.alibaba.fastjson.JSONObject