Преглед на файлове

向mq发送报警持续消息

xujiaheng преди 3 години
родител
ревизия
0b933a1e35

+ 7 - 7
src/main/java/com/persagy/cache/AlarmRedisCache.java

@@ -26,15 +26,15 @@ import lombok.extern.slf4j.Slf4j;
 @Slf4j
 public class AlarmRedisCache implements RedisCache {
 
-	@Resource(name = "emsRedisTemplate")
-	private RedisTemplate<String, String> emsRedisTemplate;
+	@Resource(name = "alarmRedisTemplate")
+	private RedisTemplate<String, String> alarmRedisTemplate;
 
 	@Autowired
 	AlarmRecordRepository alarmRecordRepository;
 
 	public AlarmConditionState getAlarmDefineState(String defineId) {
 		AlarmConditionState alarmConditionState = null;
-		String str = emsRedisTemplate.opsForHash().get(RedisConstant.DEFINE_ID, defineId).toString();
+		String str = alarmRedisTemplate.opsForHash().get(RedisConstant.DEFINE_ID, defineId).toString();
 		if (StringUtils.isNotBlank(str)) {
 			try {
 				JSONObject object = JSONObject.parseObject(str);
@@ -65,11 +65,11 @@ public class AlarmRedisCache implements RedisCache {
 	}
 
 	public void setAlarmState(String defineId, AlarmConditionState alarmConditionState) {
-		emsRedisTemplate.opsForHash().put(RedisConstant.DEFINE_ID, defineId, JSON.toJSONString(alarmConditionState));
+		alarmRedisTemplate.opsForHash().put(RedisConstant.DEFINE_ID, defineId, JSON.toJSONString(alarmConditionState));
 	}
 
 	public AlarmCondition getAlarmConditionRedis(String conditionId) {
-		String str = emsRedisTemplate.execute((RedisCallback<String>) conn -> {
+		String str = alarmRedisTemplate.execute((RedisCallback<String>) conn -> {
 			conn.select(14);
 			byte[] condition = conn.hGet(RedisConstant.ALARMCONDITION_ID.getBytes(), conditionId.getBytes());
 			return new String(condition);
@@ -81,7 +81,7 @@ public class AlarmRedisCache implements RedisCache {
 
     public ObjConditionInfo getAlarmConditionsByObjId(String meterId) {
         try {
-            String str = emsRedisTemplate.opsForHash().get(RedisConstant.METER_ID, meterId).toString();
+            String str = alarmRedisTemplate.opsForHash().get(RedisConstant.METER_ID, meterId).toString();
             if (StringUtils.isNotBlank(str)) {
                 JSONObject object = JSONObject.parseObject(str);
                 ObjConditionInfo objConditionInfo = JSON.toJavaObject(object, ObjConditionInfo.class);
@@ -95,7 +95,7 @@ public class AlarmRedisCache implements RedisCache {
 
     public AlarmCondition getAlarmCondition(String conditionId) {
         try {
-            String str = emsRedisTemplate.opsForHash().get(RedisConstant.CONDITION_ID, conditionId).toString();
+            String str = alarmRedisTemplate.opsForHash().get(RedisConstant.ALARMCONDITION_ID, conditionId).toString();
             if (StringUtils.isNotBlank(str)) {
                 JSONObject object = JSONObject.parseObject(str);
                 AlarmCondition alarmCondition = JSON.toJavaObject(object, AlarmCondition.class);

+ 22 - 3
src/main/java/com/persagy/config/RedisConfig.java

@@ -114,6 +114,9 @@ public class RedisConfig {
 
 	@Value("${spring.redis.database.fifteen:12}")
 	private Integer springRedisDatabaseFifteen;
+
+	@Value("${spring.redis.database}")
+	private Integer springRedisDatabaseForteen;
 	
 	/**
 	 * 哨兵集群名称
@@ -265,10 +268,10 @@ public class RedisConfig {
 		template.setDefaultSerializer(new StringRedisSerializer());
         return template;
     }
-	
+
 	/**
-	 * 
-	 *<p>Description: 访问量统计:使用索引为15的Redis库 .</p> 
+	 *
+	 *<p>Description: 访问量统计:使用索引为15的Redis库 .</p>
 	 *<p>@return RedisTemplate<String,Object>
 	 *@since 2019年5月10日: 下午5:27:24
 	 *@author gezhanbin
@@ -282,6 +285,22 @@ public class RedisConfig {
 		return template;
 	}
 
+	/**
+	 * 
+	 *<p>Description: 访问量统计:使用索引为14的Redis库 .</p>
+	 *<p>@return RedisTemplate<String,Object>
+	 *@since 2019年5月10日: 下午5:27:24
+	 *@author gezhanbin
+	 *@version 1.0
+	 */
+	@Bean
+	public RedisTemplate<String, Object> alarmRedisTemplate() {
+		RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
+		template.setConnectionFactory(redisConnectionFactory(springRedisDatabaseForteen));
+		template.setDefaultSerializer(new StringRedisSerializer());
+		return template;
+	}
+
 	
 	/**
 	 * 

+ 0 - 4
src/main/java/com/persagy/constant/RedisConstant.java

@@ -79,8 +79,4 @@ public class RedisConstant {
      */
     public static final String METER_ID = "METER_ID";
 
-    /**
-     * 报警条件ID
-     */
-    public static final String CONDITION_ID = "CONDITION_ID";
 }

+ 15 - 11
src/main/java/com/persagy/service/impl/AlarmHandleServiceImpl.java

@@ -173,7 +173,7 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
 	private void triggerOrEndAlarm(String dataTime, String meterId, String itemCode, HashMap<String, Object> paramMap)
 			throws Exception {
 		// 获取设备的报警条件
-		ObjConditionInfo objConditionInfo = alarmInfoCache.getAlarmConditionsByObjId(meterId);
+		ObjConditionInfo objConditionInfo = alarmRedisCache.getAlarmConditionsByObjId(meterId);
 		if (objConditionInfo == null || CollectionUtils.isEmpty(objConditionInfo.getConditions())) {
 			log.warn("未获取到设备{}的报警条件", meterId);
 			return;
@@ -188,7 +188,7 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
 		for (ItemCodeCondition itemCodeCondition : itemCodeConditions) {
 			String conditionId = itemCodeCondition.getConditionId();
 //            AlarmCondition alarmCondition = alarmInfoCache.getAlarmCondition(conditionId);
-			AlarmCondition alarmCondition = alarmRedisCache.getAlarmConditionRedis(conditionId);
+			AlarmCondition alarmCondition = alarmRedisCache.getAlarmCondition(conditionId);
 
 			// 解析触发和恢复条件
 			String trigger = alarmCondition.getTriggerBackend();
@@ -618,8 +618,8 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
                     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));
-                }
+					this.sendToMQ(JSONObject.parseObject(JSONObject.toJSONString(objectNode)),alarmId,alarmDefine);
+				}
             }
             //之前是是正常状态
             if (AlarmConditionState.State.NORMAL.getType().equals(alarmConditionState.getState())) {
@@ -816,13 +816,17 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
                 .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));
+		this.sendToMQ(JSONObject.parseObject(JSONObject.toJSONString(alarmRecord)),alarmId,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));
+	}
 }

+ 1 - 1
src/main/resources/application.yml

@@ -50,7 +50,7 @@ spring:
   redis:
     host: 192.168.100.93
     port: 9944
-    database: 12
+    database: 14
     pool:
      max-active: 300
      max-wait: 3000