Browse Source

更改报警回复逻辑

gaoyu 3 years ago
parent
commit
69180e9aa8
1 changed files with 109 additions and 126 deletions
  1. 109 126
      src/main/java/com/persagy/service/impl/AlarmHandleServiceImpl.java

+ 109 - 126
src/main/java/com/persagy/service/impl/AlarmHandleServiceImpl.java

@@ -1,6 +1,24 @@
 package com.persagy.service.impl;
 
-import cn.hutool.core.date.DateUtil;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.format.DateTimeFormatter;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+import org.apache.commons.lang3.StringUtils;
+import org.quartz.JobDataMap;
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
 
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
@@ -13,7 +31,12 @@ import com.persagy.cache.AlarmLastTimeCache;
 import com.persagy.cache.AlarmRedisCache;
 import com.persagy.cache.CreatedAlarmIdsCache;
 import com.persagy.client.GroupNettyClient;
-import com.persagy.entity.*;
+import com.persagy.entity.AlarmConditionState;
+import com.persagy.entity.AlarmDefine;
+import com.persagy.entity.AlarmRecord;
+import com.persagy.entity.Condition;
+import com.persagy.entity.ExpireAlarmMessage;
+import com.persagy.entity.ZktAlarmRecordDO;
 import com.persagy.entity.v2.AlarmCondition;
 import com.persagy.entity.v2.DmpMessage;
 import com.persagy.entity.v2.ItemCodeCondition;
@@ -27,21 +50,9 @@ import com.persagy.utils.DateUtils;
 import com.persagy.utils.StringUtil;
 import com.persagy.utils.ValidateUtils;
 import com.persagy.utils.condition.ConditionUtils;
-import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.lang3.StringUtils;
-import org.quartz.JobDataMap;
-import org.springframework.amqp.rabbit.core.RabbitTemplate;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.stereotype.Service;
-import org.springframework.util.CollectionUtils;
 
-import java.time.LocalDate;
-import java.time.LocalDateTime;
-import java.time.LocalTime;
-import java.time.format.DateTimeFormatter;
-import java.util.*;
-import java.util.stream.Collectors;
+import cn.hutool.core.date.DateUtil;
+import lombok.extern.slf4j.Slf4j;
 
 /**
  * @description:报警处理实现类:包含报警产生和报警恢复
@@ -402,27 +413,14 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
 			if (alarmSuspendLastTime < uphold) {
 				return;
 			}
-
 			/* 超过报警恢复设置的持续时间 */
 			log.debug("报警恢复持续时间大于设定时间:[{}]>[{}]", alarmSuspendLastTime, uphold);
-
 			ZktAlarmRecordDO alarmRecordDO = getZktAlarmRecordDOWhenAlarmSuspend(defineId, dataTime, paramMap);
 			String alarmId = alarmRecordDO.getAlarmId();
-			// 报警恢复时,alarmLastTimeCache中对应的报警标记为可删除
-//            alarmLastTimeCache.setDeleteAvailable(defineId);
-			// 如果云端已经完成报警记录的创建,直接发送更新报警状态消息,并删除数据库中的报警信息
-			if (createdAlarmIdsCache.contains(alarmId)) {
-				deleteZktAlarmRecordWhenAlarmSuspend(defineId);
-				log.debug("发送报警恢复消息,报警id: [{}]", alarmId);
-				// 发送报警恢复消息
-				sendAlarmRecoverMessage(defineId, alarmSuspendStartTime, paramMap, alarmId);
-				// 报警恢复后,从缓存中移除报警id
-				createdAlarmIdsCache.remove(alarmId);
-			} else {
-				log.debug("已创建的报警id中不包含[{}], 3分钟后重试发送报警恢复消息", alarmId);
-				// 创建恢复定时任务
-				createAlarmRecoverTimingJob(dataTime, paramMap, alarmRecordDO);
-			}
+			// 发送报警恢复消息
+			sendAlarmRecoverMessage(defineId, alarmSuspendStartTime, paramMap, alarmId);
+			// 报警恢复后,从缓存中移除报警id
+			createdAlarmIdsCache.remove(alarmId);
 			// 取消过期任务
 			cancelRelatedExpireJob(alarmId);
 			// 报警恢复,报警状态重置回默认
@@ -578,59 +576,58 @@ 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 = 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发送报警持续消息
+	/**
+	 * @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);
-					this.sendToMQ(JSONObject.parseObject(JSONObject.toJSONString(objectNode)),alarmId,alarmDefine);
+					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);
+					this.sendToMQ(JSONObject.parseObject(JSONObject.toJSONString(objectNode)), alarmId, alarmDefine);
+				}
+			}
+			// 之前是是正常状态
+			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);
 				}
-            }
-            //之前是是正常状态
-            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);
@@ -777,56 +774,42 @@ 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();
+		// TODO: 2021/11/16 消息发送至mq
+		this.sendToMQ(JSONObject.parseObject(JSONObject.toJSONString(alarmRecord)), alarmId, alarmDefine);
+	}
 
-    /**
-     * @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();
-        // TODO: 2021/11/16 消息发送至mq
-		this.sendToMQ(JSONObject.parseObject(JSONObject.toJSONString(alarmRecord)),alarmId,alarmDefine);
-    }
-
-	public void sendToMQ(JSONObject message,String alarmId,AlarmDefine alarmDefine){
+	public void sendToMQ(JSONObject message, String alarmId, AlarmDefine alarmDefine) {
 		DmpMessage dmpMessage = new DmpMessage();
 		dmpMessage.setDefaultValue();
 		dmpMessage.setMid(alarmId);
 		dmpMessage.setProjectId(alarmDefine.getProjectId());
 		dmpMessage.setGroupCode(groupCode);
 		dmpMessage.setExts(message);
-		rabbitTemplate.convertAndSend(RabbitConfig.exchange, RabbitConfig.alarmMsgRoutingKey, JSON.toJSONString(dmpMessage));
+		rabbitTemplate.convertAndSend(RabbitConfig.exchange, RabbitConfig.alarmMsgRoutingKey,
+				JSON.toJSONString(dmpMessage));
 	}
 }