Browse Source

向mq发送报警持续消息

xujiaheng 3 years ago
parent
commit
b176b366ca

+ 29 - 0
src/main/java/com/persagy/cache/AlarmRedisCache.java

@@ -17,6 +17,7 @@ import com.persagy.constant.RedisConstant;
 import com.persagy.entity.AlarmConditionState;
 import com.persagy.entity.ZktAlarmRecordDO;
 import com.persagy.entity.v2.AlarmCondition;
+import com.persagy.entity.v2.ObjConditionInfo;
 import com.persagy.repository.AlarmRecordRepository;
 
 import lombok.extern.slf4j.Slf4j;
@@ -77,4 +78,32 @@ public class AlarmRedisCache implements RedisCache {
 		AlarmCondition alarmCondition = JSON.toJavaObject(object, AlarmCondition.class);
 		return alarmCondition;
 	}
+
+    public ObjConditionInfo getAlarmConditionsByObjId(String meterId) {
+        try {
+            String str = emsRedisTemplate.opsForHash().get(RedisConstant.METER_ID, meterId).toString();
+            if (StringUtils.isNotBlank(str)) {
+                JSONObject object = JSONObject.parseObject(str);
+                ObjConditionInfo objConditionInfo = JSON.toJavaObject(object, ObjConditionInfo.class);
+                return objConditionInfo;
+            }
+        } catch (Exception e) {
+            log.warn("从redis获取设备的报警条件信息失败" + e);
+        }
+        return null;
+    }
+
+    public AlarmCondition getAlarmCondition(String conditionId) {
+        try {
+            String str = emsRedisTemplate.opsForHash().get(RedisConstant.CONDITION_ID, conditionId).toString();
+            if (StringUtils.isNotBlank(str)) {
+                JSONObject object = JSONObject.parseObject(str);
+                AlarmCondition alarmCondition = JSON.toJavaObject(object, AlarmCondition.class);
+                return alarmCondition;
+            }
+        } catch (Exception e) {
+            log.warn("从redis获取报警条件失败" + e);
+        }
+        return null;
+    }
 }

+ 12 - 2
src/main/java/com/persagy/constant/RedisConstant.java

@@ -67,10 +67,20 @@ public class RedisConstant {
      * itemCode-objId拼接,用于根据defineId获取报警定义状态
      */
     public static final String DEFINE_ID = "DEFINE_ID";
-    
+
     /**
      * 报警条件Id REDIS hash 的 id
      */
     public static final String ALARMCONDITION_ID = "ALARMCONDITION_ID";
-    
+
+
+    /**
+     * 设备仪表ID
+     */
+    public static final String METER_ID = "METER_ID";
+
+    /**
+     * 报警条件ID
+     */
+    public static final String CONDITION_ID = "CONDITION_ID";
 }

+ 101 - 81
src/main/java/com/persagy/service/impl/AlarmHandleServiceImpl.java

@@ -18,7 +18,6 @@ import com.persagy.entity.v2.AlarmCondition;
 import com.persagy.entity.v2.DmpMessage;
 import com.persagy.entity.v2.ItemCodeCondition;
 import com.persagy.entity.v2.ObjConditionInfo;
-import com.persagy.enumeration.NettyMsgTypeEnum;
 import com.persagy.job.ExpireAlarmQueue;
 import com.persagy.mq.RabbitConfig;
 import com.persagy.repository.AlarmRecordRepository;
@@ -579,54 +578,59 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
 		alarmLastTimeCache.setAlarmLastMsg(alarmId, projectId, objectNode);
 	}
 
-	/**
-	 * @param alarmDefine 报警定义
-	 * @param dataTime    :IOT数据采集时间
-	 * @param paramMap    报警触发值
-	 * @description: 处理当前值报警的情况
-	 * @exception:
-	 * @author: LuoGuangyi
-	 * @company: Persagy Technology Co.,Ltd
-	 * @return: void
-	 * @since: 2020/10/20 22:31
-	 * @version: V1.0
-	 */
-	private void handleAlarmTriggerData(AlarmDefine alarmDefine, String dataTime, HashMap<String, Object> paramMap)
-			throws Exception {
-		// 根据defineId获取报警定义状态
-		String defineId = AlarmInfoCache.getAlarmDefineId(alarmDefine);
-//      AlarmConditionState alarmConditionState = getAlarmDefineStateFromCacheOrDb(defineId);
-
-		AlarmConditionState alarmConditionState = alarmRedisCache.getAlarmDefineState(defineId);
-
-		Condition condition = alarmDefine.getCondition();
-		// 判断报警是否在有效期内
-		boolean inEffectTime = ConditionUtils.inEffectiveTime(condition, dataTime);
-
-		if (inEffectTime) {
-			// 已经产生报警
-			if (AlarmConditionState.State.NOT_DEAL.getType().equals(alarmConditionState.getState())) {
-				// 获取报警持续时间
-				String alarmStartTime = alarmConditionState.getAlarmStartTime();
-				long lastTime = DateUtils.betweenTwoTimeSecond(alarmStartTime, dataTime);
-				String alarmId = alarmRecordRepository.findById(defineId).orElse(new ZktAlarmRecordDO()).getAlarmId();
-				if (StringUtils.isNotBlank(alarmId)) {
-					// TODO: 2021/11/17 移除alarmLastTimeCache,向mq发送报警持续消息
-					alarmLastTimeCache.put(alarmId, Integer.valueOf(String.valueOf(lastTime)));
-					log.debug("开始拼装报警持续消息");
-					initAlarmContinueMsg(alarmId, lastTime, paramMap, alarmDefine.getProjectId());
-				}
-			}
-			// 之前是是正常状态
-			if (AlarmConditionState.State.NORMAL.getType().equals(alarmConditionState.getState())) {
-				// 报警持续时间
-				long alarmLastTime = 0;
-				if (StringUtil.isNotEmpty(alarmConditionState.getAlarmStartTime())) {
-					alarmLastTime = DateUtils.betweenTwoTimeSecond(alarmConditionState.getAlarmStartTime(), dataTime);
-				} else {
-					// 设置开始报警时间
-					alarmConditionState.setAlarmStartTime(dataTime);
-				}
+    /**
+     * @param alarmDefine 报警定义
+     * @param dataTime    :IOT数据采集时间
+     * @param paramMap    报警触发值
+     * @description: 处理当前值报警的情况
+     * @exception:
+     * @author: LuoGuangyi
+     * @company: Persagy Technology Co.,Ltd
+     * @return: void
+     * @since: 2020/10/20 22:31
+     * @version: V1.0
+     */
+    private void handleAlarmTriggerData(
+            AlarmDefine alarmDefine, String dataTime,
+            HashMap<String, Object> paramMap) throws Exception {
+        // 根据defineId获取报警定义状态
+        String defineId = AlarmInfoCache.getAlarmDefineId(alarmDefine);
+        AlarmConditionState alarmConditionState = alarmRedisCache.getAlarmDefineState(defineId);
+        Condition condition = alarmDefine.getCondition();
+        // 判断报警是否在有效期内
+        boolean inEffectTime = ConditionUtils.inEffectiveTime(condition, dataTime);
+
+        if (inEffectTime) {
+            // 已经产生报警
+            if (AlarmConditionState.State.NOT_DEAL.getType().equals(alarmConditionState.getState())) {
+                // 获取报警持续时间
+                String alarmStartTime = alarmConditionState.getAlarmStartTime();
+                long lastTime = DateUtils.betweenTwoTimeSecond(alarmStartTime, dataTime);
+                String alarmId = alarmRecordRepository.findById(defineId).orElse(new ZktAlarmRecordDO()).getAlarmId();
+                if (StringUtils.isNotBlank(alarmId)) {
+                    // TODO: 2021/11/17 移除alarmLastTimeCache,向mq发送报警持续消息
+//                    alarmLastTimeCache.put(alarmId, Integer.valueOf(String.valueOf(lastTime)));
+//                    log.debug("开始拼装报警持续消息");
+//                    initAlarmContinueMsg(alarmId, lastTime, paramMap, alarmDefine.getProjectId());
+                    ObjectMapper objectMapper = new ObjectMapper();
+                    ObjectNode objectNode = objectMapper.createObjectNode();
+                    objectNode.put("id", alarmId);
+                    objectNode.put("lastTime", lastTime);
+                    objectNode.set("iotData", objectMapper.readTree(objectMapper.writeValueAsString(paramMap)));
+                    objectNode.put("groupCode", groupCode);
+                    rabbitTemplate.convertAndSend(RabbitConfig.exchange, RabbitConfig.alarmMsgRoutingKey, JSON.toJSONString(objectNode));
+                }
+            }
+            //之前是是正常状态
+            if (AlarmConditionState.State.NORMAL.getType().equals(alarmConditionState.getState())) {
+                // 报警持续时间
+                long alarmLastTime = 0;
+                if (StringUtil.isNotEmpty(alarmConditionState.getAlarmStartTime())) {
+                    alarmLastTime = DateUtils.betweenTwoTimeSecond(alarmConditionState.getAlarmStartTime(), dataTime);
+                } else {
+                    //设置开始报警时间
+                    alarmConditionState.setAlarmStartTime(dataTime);
+                }
 
 				// 获取报警过期时间
 				LocalDateTime expireDateTime = getExpireLocalDateTime(dataTime, condition);
@@ -773,36 +777,52 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
 		return zktAlarmRecordDO;
 	}
 
-	/**
-	 * @description: 发送创建报警记录消息
-	 * @param: alarmId     报警id
-	 * @param: alarmDefine 报警定义
-	 * @param: triggerTime 触发时间
-	 * @param: triggerInfo 触发值
-	 * @param: expireDate  过期时间
-	 * @return: void
-	 * @exception:
-	 * @author: lixing
-	 * @company: Persagy Technology Co.,Ltd
-	 * @since: 2021/1/8 下午5:38
-	 * @version: V1.0
-	 */
-	private void sendCreateAlarmRecordMessage(String alarmId, AlarmDefine alarmDefine, String triggerTime,
-			HashMap<String, Object> triggerInfo, Date expireDate) throws InterruptedException {
-		Condition condition = alarmDefine.getCondition();
-		AlarmRecord alarmRecord = AlarmRecord.builder().id(alarmId).category(alarmDefine.getCategory())
-				.concern(alarmDefine.getConcern()).level(alarmDefine.getLevel()).projectId(alarmDefine.getProjectId())
-				.state(1).triggerTime(DateUtils.parseDate(triggerTime)).remark(alarmDefine.getRemark())
-				.triggerInfo(JSONObject.toJSONString(triggerInfo)).condition(condition.toString())
-				.effectEndTime(expireDate).groupCode(groupCode).itemCode(alarmDefine.getItemCode())
-				.objId(alarmDefine.getObjId()).classCode(alarmDefine.getClassCode()).createUser(systemId).treatState(1)
-				.build();
-		DmpMessage dmpMessage = new DmpMessage();
-		dmpMessage.setDefaultValue();
-		dmpMessage.setMid(alarmId);
-		dmpMessage.setProjectId(alarmDefine.getProjectId());
-		dmpMessage.setGroupCode(groupCode);
-		dmpMessage.setExts(JSONObject.parseObject(JSONObject.toJSONString(alarmRecord)));
-		// TODO: 2021/11/16 消息发送至mq
-	}
+
+    /**
+     * @description: 发送创建报警记录消息
+     * @param: alarmId 报警id
+     * @param: alarmDefine 报警定义
+     * @param: triggerTime 触发时间
+     * @param: triggerInfo 触发值
+     * @param: expireDate 过期时间
+     * @return: void
+     * @exception:
+     * @author: lixing
+     * @company: Persagy Technology Co.,Ltd
+     * @since: 2021/1/8 下午5:38
+     * @version: V1.0
+     */
+    private void sendCreateAlarmRecordMessage(
+            String alarmId,
+            AlarmDefine alarmDefine, String triggerTime,
+            HashMap<String, Object> triggerInfo, Date expireDate) throws InterruptedException {
+        Condition condition = alarmDefine.getCondition();
+        AlarmRecord alarmRecord = AlarmRecord.builder()
+                .id(alarmId)
+                .category(alarmDefine.getCategory())
+                .concern(alarmDefine.getConcern())
+                .level(alarmDefine.getLevel())
+                .projectId(alarmDefine.getProjectId())
+                .state(1)
+                .triggerTime(DateUtils.parseDate(triggerTime))
+                .remark(alarmDefine.getRemark())
+                .triggerInfo(JSONObject.toJSONString(triggerInfo))
+                .condition(condition.toString())
+                .effectEndTime(expireDate)
+                .groupCode(groupCode)
+                .itemCode(alarmDefine.getItemCode())
+                .objId(alarmDefine.getObjId())
+                .classCode(alarmDefine.getClassCode())
+                .createUser(systemId)
+                .treatState(1)
+                .build();
+        DmpMessage dmpMessage = new DmpMessage();
+        dmpMessage.setDefaultValue();
+        dmpMessage.setMid(alarmId);
+        dmpMessage.setProjectId(alarmDefine.getProjectId());
+        dmpMessage.setGroupCode(groupCode);
+        dmpMessage.setExts(JSONObject.parseObject(JSONObject.toJSONString(alarmRecord)));
+        // TODO: 2021/11/16 消息发送至mq
+        rabbitTemplate.convertAndSend(RabbitConfig.exchange, RabbitConfig.alarmMsgRoutingKey, JSON.toJSONString(dmpMessage));
+    }
 }