From 2b1f7a47394363e95deb4dfa0f1c67d41e747f7f Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期三, 01 二月 2023 10:56:40 +0800
Subject: [PATCH] Merge branch 'wvp-28181-2.0' into fix-269

---
 src/main/java/com/genersoft/iot/vmp/service/redisMsg/RedisAlarmMsgListener.java |  114 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 114 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/service/redisMsg/RedisAlarmMsgListener.java b/src/main/java/com/genersoft/iot/vmp/service/redisMsg/RedisAlarmMsgListener.java
new file mode 100644
index 0000000..9bb3bbd
--- /dev/null
+++ b/src/main/java/com/genersoft/iot/vmp/service/redisMsg/RedisAlarmMsgListener.java
@@ -0,0 +1,114 @@
+package com.genersoft.iot.vmp.service.redisMsg;
+
+import com.alibaba.fastjson2.JSON;
+import com.genersoft.iot.vmp.gb28181.bean.*;
+import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommander;
+import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommanderForPlatform;
+import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
+import com.genersoft.iot.vmp.utils.DateUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.data.redis.connection.Message;
+import org.springframework.data.redis.connection.MessageListener;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
+import org.springframework.stereotype.Component;
+import org.springframework.util.ObjectUtils;
+
+import javax.sip.InvalidArgumentException;
+import javax.sip.SipException;
+import javax.validation.constraints.NotNull;
+import java.text.ParseException;
+import java.util.List;
+import java.util.concurrent.ConcurrentLinkedQueue;
+
+
+@Component
+public class RedisAlarmMsgListener implements MessageListener {
+
+    private final static Logger logger = LoggerFactory.getLogger(RedisAlarmMsgListener.class);
+
+    @Autowired
+    private ISIPCommander commander;
+
+    @Autowired
+    private ISIPCommanderForPlatform commanderForPlatform;
+
+    @Autowired
+    private IVideoManagerStorage storage;
+
+    private ConcurrentLinkedQueue<Message> taskQueue = new ConcurrentLinkedQueue<>();
+
+    @Qualifier("taskExecutor")
+    @Autowired
+    private ThreadPoolTaskExecutor taskExecutor;
+
+    @Override
+    public void onMessage(@NotNull Message message, byte[] bytes) {
+        logger.info("鏀跺埌鏉ヨ嚜REDIS鐨凙LARM閫氱煡锛� {}", new String(message.getBody()));
+        boolean isEmpty = taskQueue.isEmpty();
+        taskQueue.offer(message);
+        if (isEmpty) {
+            logger.info("[绾跨▼姹犱俊鎭痌娲诲姩绾跨▼鏁帮細{}, 鏈�澶х嚎绋嬫暟锛� {}", taskExecutor.getActiveCount(), taskExecutor.getMaxPoolSize());
+            taskExecutor.execute(() -> {
+                while (!taskQueue.isEmpty()) {
+                    Message msg = taskQueue.poll();
+                    try {
+                        AlarmChannelMessage alarmChannelMessage = JSON.parseObject(msg.getBody(), AlarmChannelMessage.class);
+                        if (alarmChannelMessage == null) {
+                            logger.warn("[REDIS鐨凙LARM閫氱煡]娑堟伅瑙f瀽澶辫触");
+                            continue;
+                        }
+                        String gbId = alarmChannelMessage.getGbId();
+
+                        DeviceAlarm deviceAlarm = new DeviceAlarm();
+                        deviceAlarm.setCreateTime(DateUtil.getNow());
+                        deviceAlarm.setChannelId(gbId);
+                        deviceAlarm.setAlarmDescription(alarmChannelMessage.getAlarmDescription());
+                        deviceAlarm.setAlarmMethod("" + alarmChannelMessage.getAlarmSn());
+                        deviceAlarm.setAlarmPriority("1");
+                        deviceAlarm.setAlarmTime(DateUtil.getNowForISO8601());
+                        deviceAlarm.setAlarmType("1");
+                        deviceAlarm.setLongitude(0);
+                        deviceAlarm.setLatitude(0);
+
+                        if (ObjectUtils.isEmpty(gbId)) {
+                            // 鍙戦�佺粰鎵�鏈夌殑涓婄骇
+                            List<ParentPlatform> parentPlatforms = storage.queryEnableParentPlatformList(true);
+                            if (parentPlatforms.size() > 0) {
+                                for (ParentPlatform parentPlatform : parentPlatforms) {
+                                    try {
+                                        commanderForPlatform.sendAlarmMessage(parentPlatform, deviceAlarm);
+                                    } catch (SipException | InvalidArgumentException | ParseException e) {
+                                        logger.error("[鍛戒护鍙戦�佸け璐 鍥芥爣绾ц仈 鍙戦�佹姤璀�: {}", e.getMessage());
+                                    }
+                                }
+                            }
+                        }else {
+                            Device device = storage.queryVideoDevice(gbId);
+                            ParentPlatform platform = storage.queryParentPlatByServerGBId(gbId);
+                            if (device != null && platform == null) {
+                                try {
+                                    commander.sendAlarmMessage(device, deviceAlarm);
+                                } catch (InvalidArgumentException | SipException | ParseException e) {
+                                    logger.error("[鍛戒护鍙戦�佸け璐 鍙戦�佹姤璀�: {}", e.getMessage());
+                                }
+                            }else if (device == null && platform != null){
+                                try {
+                                    commanderForPlatform.sendAlarmMessage(platform, deviceAlarm);
+                                } catch (InvalidArgumentException | SipException | ParseException e) {
+                                    logger.error("[鍛戒护鍙戦�佸け璐 鍙戦�佹姤璀�: {}", e.getMessage());
+                                }
+                            }else {
+                                logger.warn("鏃犳硶纭畾" + gbId + "鏄钩鍙拌繕鏄澶�");
+                            }
+                        }
+                    }catch (Exception e) {
+                        logger.warn("[REDIS鐨凙LARM閫氱煡] 鍙戠幇鏈鐞嗙殑寮傚父, {}",e.getMessage());
+                    }
+                }
+            });
+        }
+    }
+}

--
Gitblit v1.8.0