From 5dc08a45f8398fffee141b0389bd79decccaf277 Mon Sep 17 00:00:00 2001
From: 648540858 <456panlinlin>
Date: 星期日, 24 四月 2022 16:33:59 +0800
Subject: [PATCH] 增加redis通知方式向设备/平台发送报警消息功能

---
 src/main/java/com/genersoft/iot/vmp/gb28181/bean/SubscribeInfo.java                                                       |    3 
 src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java                                           |  108 +++++++++++
 src/main/java/com/genersoft/iot/vmp/conf/RedisConfig.java                                                                 |    5 
 src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/alarm/AlarmController.java                                           |   62 ++++++
 src/main/java/com/genersoft/iot/vmp/conf/SipConfig.java                                                                   |    2 
 src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/AlarmNotifyMessageHandler.java |   96 ++++++++++
 src/main/java/com/genersoft/iot/vmp/gb28181/bean/AlarmChannelMessage.java                                                 |   46 +++++
 src/main/java/com/genersoft/iot/vmp/gb28181/bean/DeviceAlarm.java                                                         |   25 ++
 pom.xml                                                                                                                   |    2 
 src/main/java/com/genersoft/iot/vmp/service/impl/RedisAlarmMsgListener.java                                               |   69 +++++++
 src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/ISIPCommander.java                                               |   12 +
 src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java                                              |    6 
 src/main/java/com/genersoft/iot/vmp/gb28181/utils/SipUtils.java                                                           |    1 
 src/main/java/com/genersoft/iot/vmp/storager/IRedisCatchStorage.java                                                      |    6 
 src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommanderFroPlatform.java                                |   55 +++++
 src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/ISIPCommanderForPlatform.java                                    |   13 +
 16 files changed, 486 insertions(+), 25 deletions(-)

diff --git a/pom.xml b/pom.xml
index befb74f..34a07e1 100644
--- a/pom.xml
+++ b/pom.xml
@@ -11,7 +11,7 @@
 
 	<groupId>com.genersoft</groupId>
 	<artifactId>wvp-pro</artifactId>
-	<version>2.1.1</version>
+	<version>2.2.1</version>
 	<name>web video platform</name>
 	<description>鍥芥爣28181瑙嗛骞冲彴</description>
 
diff --git a/src/main/java/com/genersoft/iot/vmp/conf/RedisConfig.java b/src/main/java/com/genersoft/iot/vmp/conf/RedisConfig.java
index d46a8ea..6b45eb5 100644
--- a/src/main/java/com/genersoft/iot/vmp/conf/RedisConfig.java
+++ b/src/main/java/com/genersoft/iot/vmp/conf/RedisConfig.java
@@ -1,6 +1,7 @@
 package com.genersoft.iot.vmp.conf;
 
 import com.genersoft.iot.vmp.common.VideoManagerConstants;
+import com.genersoft.iot.vmp.service.impl.RedisAlarmMsgListener;
 import com.genersoft.iot.vmp.service.impl.RedisGPSMsgListener;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -48,6 +49,9 @@
 	@Autowired
 	private RedisGPSMsgListener redisGPSMsgListener;
 
+	@Autowired
+	private RedisAlarmMsgListener redisAlarmMsgListener;
+
 	@Bean
 	public JedisPool jedisPool() {
 		if (StringUtils.isBlank(password)) {
@@ -93,6 +97,7 @@
         RedisMessageListenerContainer container = new RedisMessageListenerContainer();
         container.setConnectionFactory(connectionFactory);
 		container.addMessageListener(redisGPSMsgListener, new PatternTopic(VideoManagerConstants.VM_MSG_GPS));
+		container.addMessageListener(redisAlarmMsgListener, new PatternTopic(VideoManagerConstants.VM_MSG_SUBSCRIBE_ALARM_RECEIVE));
         return container;
     }
 
diff --git a/src/main/java/com/genersoft/iot/vmp/conf/SipConfig.java b/src/main/java/com/genersoft/iot/vmp/conf/SipConfig.java
index 6fa802d..bbc946b 100644
--- a/src/main/java/com/genersoft/iot/vmp/conf/SipConfig.java
+++ b/src/main/java/com/genersoft/iot/vmp/conf/SipConfig.java
@@ -29,7 +29,7 @@
 
 	Integer registerTimeInterval = 120;
 
-	private boolean alarm = false;
+	private boolean alarm;
 
 	public void setIp(String ip) {
 		this.ip = ip;
diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/bean/AlarmChannelMessage.java b/src/main/java/com/genersoft/iot/vmp/gb28181/bean/AlarmChannelMessage.java
new file mode 100644
index 0000000..96d25c4
--- /dev/null
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/bean/AlarmChannelMessage.java
@@ -0,0 +1,46 @@
+package com.genersoft.iot.vmp.gb28181.bean;
+
+/**
+ * 閫氳繃redis鍒嗗彂鎶ヨ娑堟伅
+ */
+public class AlarmChannelMessage {
+    /**
+     * 鍥芥爣缂栧彿
+     */
+    private String gbId;
+
+    /**
+     * 鎶ヨ缂栧彿
+     */
+    private int alarmSn;
+
+
+    /**
+     * 鎶ヨ鎻忚堪
+     */
+    private String alarmDescription;
+
+    public String getGbId() {
+        return gbId;
+    }
+
+    public void setGbId(String gbId) {
+        this.gbId = gbId;
+    }
+
+    public int getAlarmSn() {
+        return alarmSn;
+    }
+
+    public void setAlarmSn(int alarmSn) {
+        this.alarmSn = alarmSn;
+    }
+
+    public String getAlarmDescription() {
+        return alarmDescription;
+    }
+
+    public void setAlarmDescription(String alarmDescription) {
+        this.alarmDescription = alarmDescription;
+    }
+}
diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/bean/DeviceAlarm.java b/src/main/java/com/genersoft/iot/vmp/gb28181/bean/DeviceAlarm.java
index 8176c1c..530bce2 100644
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/bean/DeviceAlarm.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/bean/DeviceAlarm.java
@@ -50,7 +50,30 @@
 	private double latitude;
 
 	/**
-	 * 鎶ヨ绫诲瀷
+	 * 鎶ヨ绫诲瀷,
+	 * 鎶ヨ鏂瑰紡涓�2鏃�,涓嶆惡甯� AlarmType涓洪粯璁ょ殑鎶ヨ璁惧鎶ヨ,
+	 * 鎼哄甫 AlarmType鍙栧�煎強瀵瑰簲鎶ヨ绫诲瀷濡備笅:
+	 * 		1-瑙嗛涓㈠け鎶ヨ;
+	 * 		2-璁惧闃叉媶鎶ヨ;
+	 * 		3-瀛樺偍璁惧纾佺洏婊℃姤璀�;
+	 * 		4-璁惧楂樻俯鎶ヨ;
+	 * 		5-璁惧浣庢俯鎶ヨ銆�
+	 * 鎶ヨ鏂瑰紡涓�5鏃�,鍙栧�煎涓�:
+	 * 		1-浜哄伐瑙嗛鎶ヨ;
+	 * 		2-杩愬姩鐩爣妫�娴嬫姤璀�;
+	 * 		3-閬楃暀鐗╂娴嬫姤璀�;
+	 * 		4-鐗╀綋绉婚櫎妫�娴嬫姤璀�;
+	 * 		5-缁婄嚎妫�娴嬫姤璀�;
+	 * 		6-鍏ヤ镜妫�娴嬫姤璀�;
+	 * 		7-閫嗚妫�娴嬫姤璀�;
+	 * 		8-寰樺緤妫�娴嬫姤璀�;
+	 * 		9-娴侀噺缁熻鎶ヨ;
+	 * 		10-瀵嗗害妫�娴嬫姤璀�;
+	 * 		11-瑙嗛寮傚父妫�娴嬫姤璀�;
+	 * 		12-蹇�熺Щ鍔ㄦ姤璀︺��
+	 * 鎶ヨ鏂瑰紡涓�6鏃�,鍙栧�间笅:
+	 * 		1-瀛樺偍璁惧纾佺洏鏁呴殰鎶ヨ;
+	 * 		2-瀛樺偍璁惧椋庢墖鏁呴殰鎶ヨ銆�
 	 */
 	private String alarmType;
 
diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/bean/SubscribeInfo.java b/src/main/java/com/genersoft/iot/vmp/gb28181/bean/SubscribeInfo.java
index feb6a72..1958b44 100644
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/bean/SubscribeInfo.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/bean/SubscribeInfo.java
@@ -25,6 +25,9 @@
         this.callId = callIdHeader.getCallId();
     }
 
+    public SubscribeInfo() {
+    }
+
     private String id;
     private int expires;
     private String callId;
diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/ISIPCommander.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/ISIPCommander.java
index aea37b6..a8fae0f 100644
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/ISIPCommander.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/ISIPCommander.java
@@ -1,8 +1,7 @@
 package com.genersoft.iot.vmp.gb28181.transmit.cmd;
 
 import com.genersoft.iot.vmp.common.StreamInfo;
-import com.genersoft.iot.vmp.gb28181.bean.Device;
-import com.genersoft.iot.vmp.gb28181.bean.InviteStreamCallback;
+import com.genersoft.iot.vmp.gb28181.bean.*;
 import com.genersoft.iot.vmp.gb28181.event.SipSubscribe;
 import com.genersoft.iot.vmp.media.zlm.ZLMHttpHookSubscribe;
 import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
@@ -336,4 +335,13 @@
 	 * @param cmdString 鍓嶇鎺у埗鎸囦护涓�
 	 */
 	boolean dragZoomCmd(Device device, String channelId, String cmdString);
+
+
+	/**
+	 * 鍚戣澶囧彂閫佹姤璀OTIFY娑堟伅锛� 鐢ㄤ簬浜掕仈缁撴瀯涓嬶紝姝ゆ椂灏嗚澶囧綋鎴愪竴涓钩绾у钩鍙扮湅寰�
+	 * @param device 璁惧
+	 * @param deviceAlarm 鎶ヨ淇℃伅淇℃伅
+	 * @return
+	 */
+	boolean sendAlarmMessage(Device device, DeviceAlarm deviceAlarm);
 }
diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/ISIPCommanderForPlatform.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/ISIPCommanderForPlatform.java
index a900819..7007e5a 100644
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/ISIPCommanderForPlatform.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/ISIPCommanderForPlatform.java
@@ -1,9 +1,6 @@
 package com.genersoft.iot.vmp.gb28181.transmit.cmd;
 
-import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
-import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform;
-import com.genersoft.iot.vmp.gb28181.bean.RecordInfo;
-import com.genersoft.iot.vmp.gb28181.bean.SubscribeInfo;
+import com.genersoft.iot.vmp.gb28181.bean.*;
 import com.genersoft.iot.vmp.gb28181.event.SipSubscribe;
 import com.genersoft.iot.vmp.service.bean.GPSMsgInfo;
 
@@ -76,6 +73,14 @@
     boolean sendNotifyMobilePosition(ParentPlatform parentPlatform, GPSMsgInfo gpsMsgInfo, SubscribeInfo subscribeInfo);
 
     /**
+     * 鍚戜笂绾у洖澶嶆姤璀︽秷鎭�
+     * @param parentPlatform 骞冲彴淇℃伅
+     * @param deviceAlarm 鎶ヨ淇℃伅淇℃伅
+     * @return
+     */
+    boolean sendAlarmMessage(ParentPlatform parentPlatform, DeviceAlarm deviceAlarm);
+
+    /**
      * 鍥炲catalog浜嬩欢-澧炲姞/鏇存柊
      * @param parentPlatform
      * @param deviceChannels
diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java
index 89e70d0..3fcf2fb 100644
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java
@@ -5,10 +5,7 @@
 import com.genersoft.iot.vmp.conf.DynamicTask;
 import com.genersoft.iot.vmp.conf.SipConfig;
 import com.genersoft.iot.vmp.conf.UserSetting;
-import com.genersoft.iot.vmp.gb28181.bean.Device;
-import com.genersoft.iot.vmp.gb28181.bean.InviteStreamCallback;
-import com.genersoft.iot.vmp.gb28181.bean.InviteStreamInfo;
-import com.genersoft.iot.vmp.gb28181.bean.SsrcTransaction;
+import com.genersoft.iot.vmp.gb28181.bean.*;
 import com.genersoft.iot.vmp.gb28181.event.SipSubscribe;
 import com.genersoft.iot.vmp.gb28181.session.VideoStreamSessionManager;
 import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommander;
@@ -23,6 +20,7 @@
 import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
 import gov.nist.javax.sip.SipProviderImpl;
 import gov.nist.javax.sip.SipStackImpl;
+import gov.nist.javax.sip.message.MessageFactoryImpl;
 import gov.nist.javax.sip.message.SIPRequest;
 import gov.nist.javax.sip.stack.SIPDialog;
 import org.slf4j.Logger;
@@ -35,10 +33,7 @@
 
 import javax.sip.*;
 import javax.sip.address.SipURI;
-import javax.sip.header.CallIdHeader;
-import javax.sip.header.ContentTypeHeader;
-import javax.sip.header.ExpiresHeader;
-import javax.sip.header.ViaHeader;
+import javax.sip.header.*;
 import javax.sip.message.Request;
 import java.lang.reflect.Field;
 import java.text.ParseException;
@@ -1777,4 +1772,101 @@
 			e.printStackTrace();
 		}
 	}
+
+	@Override
+	public boolean sendAlarmMessage(Device device, DeviceAlarm deviceAlarm) {
+		if (device == null) {
+			return false;
+		}
+		logger.info("[鍙戦�� 鎶ヨ閫氱煡] {}/{}->{},{}", device.getDeviceId(), deviceAlarm.getChannelId(),
+				deviceAlarm.getLongitude(), deviceAlarm.getLatitude());
+		try {
+			String characterSet = device.getCharset();
+			StringBuffer deviceStatusXml = new StringBuffer(600);
+			deviceStatusXml.append("<?xml version=\"1.0\" encoding=\"" + characterSet + "\"?>\r\n");
+			deviceStatusXml.append("<Notify>\r\n");
+			deviceStatusXml.append("<CmdType>Alarm</CmdType>\r\n");
+			deviceStatusXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n");
+			deviceStatusXml.append("<DeviceID>" + deviceAlarm.getChannelId() + "</DeviceID>\r\n");
+			deviceStatusXml.append("<AlarmPriority>" + deviceAlarm.getAlarmPriority() + "</AlarmPriority>\r\n");
+			deviceStatusXml.append("<AlarmMethod>" + deviceAlarm.getAlarmMethod() + "</AlarmMethod>\r\n");
+			deviceStatusXml.append("<AlarmTime>" + deviceAlarm.getAlarmTime() + "</AlarmTime>\r\n");
+			deviceStatusXml.append("<AlarmDescription>" + deviceAlarm.getAlarmDescription() + "</AlarmDescription>\r\n");
+			deviceStatusXml.append("<Longitude>" + deviceAlarm.getLongitude() + "</Longitude>\r\n");
+			deviceStatusXml.append("<Latitude>" + deviceAlarm.getLatitude() + "</Latitude>\r\n");
+			deviceStatusXml.append("<info>\r\n");
+			deviceStatusXml.append("<AlarmType>" + deviceAlarm.getAlarmType() + "</AlarmType>\r\n");
+			deviceStatusXml.append("</info>\r\n");
+			deviceStatusXml.append("</Notify>\r\n");
+
+			CallIdHeader callIdHeader = device.getTransport().equals("TCP") ? tcpSipProvider.getNewCallId()
+					: udpSipProvider.getNewCallId();
+			String tm = Long.toString(System.currentTimeMillis());
+			Request request = headerProvider.createMessageRequest(device, deviceStatusXml.toString(), "z9hG4bK-ViaPtz-" + tm, "FromPtz" + tm, null, callIdHeader);
+			transmitRequest(device, request);
+
+
+		} catch (SipException | ParseException  e) {
+			e.printStackTrace();
+			return false;
+		} catch (InvalidArgumentException e) {
+			throw new RuntimeException(e);
+		}
+		return true;
+	}
+
+	private void sendNotify(Device device, String catalogXmlContent,
+							SubscribeInfo subscribeInfo, SipSubscribe.Event errorEvent,  SipSubscribe.Event okEvent )
+			throws NoSuchFieldException, IllegalAccessException, SipException, ParseException {
+		MessageFactoryImpl messageFactory = (MessageFactoryImpl) sipFactory.createMessageFactory();
+		String characterSet = device.getCharset();
+		// 璁剧疆缂栫爜锛� 闃叉涓枃涔辩爜
+		messageFactory.setDefaultContentEncodingCharset(characterSet);
+		Dialog dialog  = subscribeInfo.getDialog();
+		if (dialog == null || !dialog.getState().equals(DialogState.CONFIRMED)) return;
+		SIPRequest notifyRequest = (SIPRequest)dialog.createRequest(Request.NOTIFY);
+		ContentTypeHeader contentTypeHeader = sipFactory.createHeaderFactory().createContentTypeHeader("Application", "MANSCDP+xml");
+		notifyRequest.setContent(catalogXmlContent, contentTypeHeader);
+
+		SubscriptionStateHeader subscriptionState = sipFactory.createHeaderFactory()
+				.createSubscriptionStateHeader(SubscriptionStateHeader.ACTIVE);
+		notifyRequest.addHeader(subscriptionState);
+
+		EventHeader event = sipFactory.createHeaderFactory().createEventHeader(subscribeInfo.getEventType());
+		if (subscribeInfo.getEventId() != null) {
+			event.setEventId(subscribeInfo.getEventId());
+		}
+		notifyRequest.addHeader(event);
+
+		SipURI sipURI = (SipURI) notifyRequest.getRequestURI();
+		if (subscribeInfo.getTransaction() != null) {
+			SIPRequest request = (SIPRequest) subscribeInfo.getTransaction().getRequest();
+			sipURI.setHost(request.getRemoteAddress().getHostAddress());
+			sipURI.setPort(request.getRemotePort());
+		}else {
+			sipURI.setHost(device.getIp());
+			sipURI.setPort(device.getPort());
+		}
+
+		ClientTransaction transaction = null;
+		if ("TCP".equals(device.getTransport())) {
+			transaction = tcpSipProvider.getNewClientTransaction(notifyRequest);
+		} else if ("UDP".equals(device.getTransport())) {
+			transaction = udpSipProvider.getNewClientTransaction(notifyRequest);
+		}
+		// 娣诲姞閿欒璁㈤槄
+		if (errorEvent != null) {
+			sipSubscribe.addErrorSubscribe(subscribeInfo.getCallId(), errorEvent);
+		}
+		// 娣诲姞璁㈤槄
+		if (okEvent != null) {
+			sipSubscribe.addOkSubscribe(subscribeInfo.getCallId(), okEvent);
+		}
+		if (transaction == null) {
+			logger.error("骞冲彴{}鐨凾ransport閿欒锛歿}",device.getDeviceId(), device.getTransport());
+			return;
+		}
+		dialog.sendRequest(transaction);
+
+	}
 }
diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommanderFroPlatform.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommanderFroPlatform.java
index 0fd8cc5..66af757 100644
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommanderFroPlatform.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommanderFroPlatform.java
@@ -436,6 +436,48 @@
     }
 
     @Override
+    public boolean sendAlarmMessage(ParentPlatform parentPlatform, DeviceAlarm deviceAlarm) {
+        if (parentPlatform == null) {
+            return false;
+        }
+        logger.info("[鍙戦�� 鎶ヨ璁㈤槄] {}/{}->{},{}", parentPlatform.getServerGBId(), deviceAlarm.getChannelId(),
+                deviceAlarm.getLongitude(), deviceAlarm.getLatitude());
+        try {
+            String characterSet = parentPlatform.getCharacterSet();
+            StringBuffer deviceStatusXml = new StringBuffer(600);
+            deviceStatusXml.append("<?xml version=\"1.0\" encoding=\"" + characterSet + "\"?>\r\n");
+            deviceStatusXml.append("<Notify>\r\n");
+            deviceStatusXml.append("<CmdType>Alarm</CmdType>\r\n");
+            deviceStatusXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n");
+            deviceStatusXml.append("<DeviceID>" + deviceAlarm.getChannelId() + "</DeviceID>\r\n");
+            deviceStatusXml.append("<AlarmPriority>" + deviceAlarm.getAlarmPriority() + "</AlarmPriority>\r\n");
+            deviceStatusXml.append("<AlarmMethod>" + deviceAlarm.getAlarmMethod() + "</AlarmMethod>\r\n");
+            deviceStatusXml.append("<AlarmTime>" + deviceAlarm.getAlarmTime() + "</AlarmTime>\r\n");
+            deviceStatusXml.append("<AlarmDescription>" + deviceAlarm.getAlarmDescription() + "</AlarmDescription>\r\n");
+            deviceStatusXml.append("<Longitude>" + deviceAlarm.getLongitude() + "</Longitude>\r\n");
+            deviceStatusXml.append("<Latitude>" + deviceAlarm.getLatitude() + "</Latitude>\r\n");
+            deviceStatusXml.append("<info>\r\n");
+            deviceStatusXml.append("<AlarmType>" + deviceAlarm.getAlarmType() + "</AlarmType>\r\n");
+            deviceStatusXml.append("</info>\r\n");
+            deviceStatusXml.append("</Notify>\r\n");
+
+            CallIdHeader callIdHeader = parentPlatform.getTransport().equals("TCP") ? tcpSipProvider.getNewCallId()
+                    : udpSipProvider.getNewCallId();
+
+            String tm = Long.toString(System.currentTimeMillis());
+            Request request = headerProviderPlarformProvider.createMessageRequest(parentPlatform, deviceStatusXml.toString(), "FromPtz" + tm, callIdHeader);
+            transmitRequest(parentPlatform, request);
+
+        } catch (SipException | ParseException  e) {
+            e.printStackTrace();
+            return false;
+        } catch (InvalidArgumentException e) {
+            throw new RuntimeException(e);
+        }
+        return true;
+    }
+
+    @Override
     public boolean sendNotifyForCatalogAddOrUpdate(String type, ParentPlatform parentPlatform, List<DeviceChannel> deviceChannels, SubscribeInfo subscribeInfo, Integer index) {
         if (parentPlatform == null || deviceChannels == null || deviceChannels.size() == 0 || subscribeInfo == null) {
             return false;
@@ -495,11 +537,16 @@
             event.setEventId(subscribeInfo.getEventId());
         }
         notifyRequest.addHeader(event);
-
         SipURI sipURI = (SipURI) notifyRequest.getRequestURI();
-        SIPRequest request = (SIPRequest) subscribeInfo.getTransaction().getRequest();
-        sipURI.setHost(request.getRemoteAddress().getHostAddress());
-        sipURI.setPort(request.getRemotePort());
+        if (subscribeInfo.getTransaction() != null) {
+            SIPRequest request = (SIPRequest) subscribeInfo.getTransaction().getRequest();
+            sipURI.setHost(request.getRemoteAddress().getHostAddress());
+            sipURI.setPort(request.getRemotePort());
+        }else {
+            sipURI.setHost(parentPlatform.getServerIP());
+            sipURI.setPort(parentPlatform.getServerPort());
+        }
+
         ClientTransaction transaction = null;
         if ("TCP".equals(parentPlatform.getTransport())) {
             transaction = tcpSipProvider.getNewClientTransaction(notifyRequest);
diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/AlarmNotifyMessageHandler.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/AlarmNotifyMessageHandler.java
index 8977fc4..e5d7aa0 100644
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/AlarmNotifyMessageHandler.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/AlarmNotifyMessageHandler.java
@@ -1,5 +1,6 @@
 package com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.notify.cmd;
 
+import com.alibaba.fastjson.JSON;
 import com.genersoft.iot.vmp.conf.SipConfig;
 import com.genersoft.iot.vmp.conf.UserSetting;
 import com.genersoft.iot.vmp.gb28181.bean.*;
@@ -11,6 +12,7 @@
 import com.genersoft.iot.vmp.gb28181.utils.Coordtransform;
 import com.genersoft.iot.vmp.gb28181.utils.NumericUtil;
 import com.genersoft.iot.vmp.service.IDeviceAlarmService;
+import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
 import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
 import com.genersoft.iot.vmp.utils.GpsUtil;
 import org.dom4j.Element;
@@ -21,7 +23,12 @@
 import org.springframework.stereotype.Component;
 import org.springframework.util.StringUtils;
 
+import javax.sip.InvalidArgumentException;
 import javax.sip.RequestEvent;
+import javax.sip.SipException;
+import javax.sip.message.Response;
+
+import java.text.ParseException;
 
 import static com.genersoft.iot.vmp.gb28181.utils.XmlUtil.*;
 
@@ -47,6 +54,9 @@
     private IVideoManagerStorage storager;
 
     @Autowired
+    private IRedisCatchStorage redisCatchStorage;
+
+    @Autowired
     private IDeviceAlarmService deviceAlarmService;
 
     @Autowired
@@ -59,11 +69,22 @@
 
     @Override
     public void handForDevice(RequestEvent evt, Device device, Element rootElement) {
-        if (!sipConfig.isAlarm()) {
-            return;
+        logger.info("鏀跺埌鏉ヨ嚜璁惧[{}]鐨勬姤璀﹂�氱煡", device.getDeviceId());
+        // 鍥炲200 OK
+        try {
+            responseAck(evt, Response.OK);
+        } catch (SipException e) {
+            throw new RuntimeException(e);
+        } catch (InvalidArgumentException e) {
+            throw new RuntimeException(e);
+        } catch (ParseException e) {
+            throw new RuntimeException(e);
         }
+
         Element deviceIdElement = rootElement.element("DeviceID");
         String channelId = deviceIdElement.getText().toString();
+
+
         DeviceAlarm deviceAlarm = new DeviceAlarm();
         deviceAlarm.setDeviceId(device.getDeviceId());
         deviceAlarm.setChannelId(channelId);
@@ -111,9 +132,24 @@
                 deviceAlarm.setAlarmType(getText(rootElement.element("Info"), "AlarmType"));
             }
         }
+
+        if (channelId.equals(sipConfig.getId())) {
+            // 鍙戦�佺粰骞冲彴鐨勬姤璀︿俊鎭�� 鍙戦�乺edis閫氱煡
+            AlarmChannelMessage alarmChannelMessage = new AlarmChannelMessage();
+            alarmChannelMessage.setAlarmSn(Integer.parseInt(deviceAlarm.getAlarmMethod()));
+            alarmChannelMessage.setAlarmDescription(deviceAlarm.getAlarmDescription());
+            alarmChannelMessage.setGbId(channelId);
+            redisCatchStorage.sendAlarmMsg(alarmChannelMessage);
+
+            return;
+        }
+
         logger.debug("瀛樺偍鎶ヨ淇℃伅銆佹姤璀﹀垎绫�");
         // 瀛樺偍鎶ヨ淇℃伅銆佹姤璀﹀垎绫�
-        deviceAlarmService.add(deviceAlarm);
+        if (sipConfig.isAlarm()) {
+            deviceAlarmService.add(deviceAlarm);
+        }
+
 
         if (offLineDetector.isOnline(device.getDeviceId())) {
             publisher.deviceAlarmEventPublish(deviceAlarm);
@@ -121,7 +157,59 @@
     }
 
     @Override
-    public void handForPlatform(RequestEvent evt, ParentPlatform parentPlatform, Element element) {
+    public void handForPlatform(RequestEvent evt, ParentPlatform parentPlatform, Element rootElement) {
+        logger.info("鏀跺埌鏉ヨ嚜骞冲彴[{}]鐨勬姤璀﹂�氱煡", parentPlatform.getServerGBId());
+        // 鍥炲200 OK
+        try {
+            responseAck(evt, Response.OK);
+        } catch (SipException e) {
+            throw new RuntimeException(e);
+        } catch (InvalidArgumentException e) {
+            throw new RuntimeException(e);
+        } catch (ParseException e) {
+            throw new RuntimeException(e);
+        }
+        Element deviceIdElement = rootElement.element("DeviceID");
+        String channelId = deviceIdElement.getText().toString();
 
+
+        DeviceAlarm deviceAlarm = new DeviceAlarm();
+        deviceAlarm.setDeviceId(parentPlatform.getServerGBId());
+        deviceAlarm.setChannelId(channelId);
+        deviceAlarm.setAlarmPriority(getText(rootElement, "AlarmPriority"));
+        deviceAlarm.setAlarmMethod(getText(rootElement, "AlarmMethod"));
+        deviceAlarm.setAlarmTime(getText(rootElement, "AlarmTime"));
+        if (getText(rootElement, "AlarmDescription") == null) {
+            deviceAlarm.setAlarmDescription("");
+        } else {
+            deviceAlarm.setAlarmDescription(getText(rootElement, "AlarmDescription"));
+        }
+        if (NumericUtil.isDouble(getText(rootElement, "Longitude"))) {
+            deviceAlarm.setLongitude(Double.parseDouble(getText(rootElement, "Longitude")));
+        } else {
+            deviceAlarm.setLongitude(0.00);
+        }
+        if (NumericUtil.isDouble(getText(rootElement, "Latitude"))) {
+            deviceAlarm.setLatitude(Double.parseDouble(getText(rootElement, "Latitude")));
+        } else {
+            deviceAlarm.setLatitude(0.00);
+        }
+
+        if (!StringUtils.isEmpty(deviceAlarm.getAlarmMethod())) {
+
+            if (deviceAlarm.getAlarmMethod().equals("5")) {
+                deviceAlarm.setAlarmType(getText(rootElement.element("Info"), "AlarmType"));
+            }
+        }
+
+        if (channelId.equals(parentPlatform.getDeviceGBId())) {
+            // 鍙戦�佺粰骞冲彴鐨勬姤璀︿俊鎭�� 鍙戦�乺edis閫氱煡
+            AlarmChannelMessage alarmChannelMessage = new AlarmChannelMessage();
+            alarmChannelMessage.setAlarmSn(Integer.parseInt(deviceAlarm.getAlarmMethod()));
+            alarmChannelMessage.setAlarmDescription(deviceAlarm.getAlarmDescription());
+            alarmChannelMessage.setGbId(channelId);
+            redisCatchStorage.sendAlarmMsg(alarmChannelMessage);
+            return;
+        }
     }
 }
diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/utils/SipUtils.java b/src/main/java/com/genersoft/iot/vmp/gb28181/utils/SipUtils.java
index 8025818..eed3763 100644
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/utils/SipUtils.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/utils/SipUtils.java
@@ -25,6 +25,7 @@
      * */
     public static String getChannelIdFromHeader(Request request) {
         Header subject = request.getHeader("subject");
+        if (subject == null) return null;
         return ((Subject) subject).getSubject().split(":")[0];
     }
 
diff --git a/src/main/java/com/genersoft/iot/vmp/service/impl/RedisAlarmMsgListener.java b/src/main/java/com/genersoft/iot/vmp/service/impl/RedisAlarmMsgListener.java
new file mode 100644
index 0000000..2bff864
--- /dev/null
+++ b/src/main/java/com/genersoft/iot/vmp/service/impl/RedisAlarmMsgListener.java
@@ -0,0 +1,69 @@
+package com.genersoft.iot.vmp.service.impl;
+
+import com.alibaba.fastjson.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.service.bean.GPSMsgInfo;
+import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
+import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
+import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.connection.Message;
+import org.springframework.data.redis.connection.MessageListener;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Component;
+
+import java.text.SimpleDateFormat;
+
+@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 final SimpleDateFormat formatForGB = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
+
+    @Override
+    public void onMessage(Message message, byte[] bytes) {
+        logger.info("鏀跺埌鏉ヨ嚜REDIS鐨凙LARM閫氱煡锛� {}", new String(message.getBody()));
+        AlarmChannelMessage alarmChannelMessage = JSON.parseObject(message.getBody(), AlarmChannelMessage.class);
+        if (alarmChannelMessage == null) {
+            logger.warn("[REDIS鐨凙LARM閫氱煡]娑堟伅瑙f瀽澶辫触");
+            return;
+        }
+        String gbId = alarmChannelMessage.getGbId();
+        Device device = storage.queryVideoDevice(gbId);
+        ParentPlatform platform = storage.queryParentPlatByServerGBId(gbId);
+
+        DeviceAlarm deviceAlarm = new DeviceAlarm();
+        deviceAlarm.setChannelId(gbId);
+        deviceAlarm.setAlarmDescription(alarmChannelMessage.getAlarmDescription());
+        deviceAlarm.setAlarmMethod("" + alarmChannelMessage.getAlarmSn());
+        deviceAlarm.setAlarmPriority("1");
+        deviceAlarm.setAlarmTime(formatForGB.format(System.currentTimeMillis()));
+        deviceAlarm.setAlarmType("1");
+        deviceAlarm.setLongitude(0);
+        deviceAlarm.setLatitude(0);
+
+
+        if (device != null && platform == null) {
+            commander.sendAlarmMessage(device, deviceAlarm);
+        }else if (device == null && platform != null){
+            commanderForPlatform.sendAlarmMessage(platform, deviceAlarm);
+        }else {
+           logger.warn("鏃犳硶纭畾" + gbId + "鏄钩鍙拌繕鏄澶�");
+        }
+    }
+}
diff --git a/src/main/java/com/genersoft/iot/vmp/storager/IRedisCatchStorage.java b/src/main/java/com/genersoft/iot/vmp/storager/IRedisCatchStorage.java
index c6b7e64..8d18f52 100644
--- a/src/main/java/com/genersoft/iot/vmp/storager/IRedisCatchStorage.java
+++ b/src/main/java/com/genersoft/iot/vmp/storager/IRedisCatchStorage.java
@@ -140,6 +140,12 @@
     void sendStreamChangeMsg(String type, JSONObject jsonObject);
 
     /**
+     * 鍙戦�佹姤璀︽秷鎭�
+     * @param msg 娑堟伅鍐呭
+     */
+    void sendAlarmMsg(AlarmChannelMessage msg);
+
+    /**
      * 娣诲姞娴佷俊鎭埌redis
      * @param mediaServerItem
      * @param app
diff --git a/src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java b/src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java
index fc78de4..e541c31 100644
--- a/src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java
+++ b/src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java
@@ -639,4 +639,10 @@
         redis.convertAndSend(key, (JSONObject)JSON.toJSON(msg));
     }
 
+    @Override
+    public void sendAlarmMsg(AlarmChannelMessage msg) {
+        String key = VideoManagerConstants.VM_MSG_SUBSCRIBE_ALARM;
+        logger.info("[redis 鎶ヨ閫氱煡] {}: {}", key, JSON.toJSON(msg));
+        redis.convertAndSend(key, (JSONObject)JSON.toJSON(msg));
+    }
 }
diff --git a/src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/alarm/AlarmController.java b/src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/alarm/AlarmController.java
index 434bbd4..e785856 100644
--- a/src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/alarm/AlarmController.java
+++ b/src/main/java/com/genersoft/iot/vmp/vmanager/gb28181/alarm/AlarmController.java
@@ -1,8 +1,14 @@
 package com.genersoft.iot.vmp.vmanager.gb28181.alarm;
 
+import com.genersoft.iot.vmp.gb28181.bean.Device;
 import com.genersoft.iot.vmp.gb28181.bean.DeviceAlarm;
+import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform;
+import com.genersoft.iot.vmp.gb28181.bean.SubscribeInfo;
+import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommander;
+import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommanderForPlatform;
 import com.genersoft.iot.vmp.service.IDeviceAlarmService;
 import com.genersoft.iot.vmp.service.IGbStreamService;
+import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
 import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
 import com.github.pagehelper.PageInfo;
 import io.swagger.annotations.Api;
@@ -31,7 +37,17 @@
     @Autowired
     private IDeviceAlarmService deviceAlarmService;
 
+    @Autowired
+    private ISIPCommander commander;
+
+    @Autowired
+    private ISIPCommanderForPlatform commanderForPlatform;
+
+    @Autowired
+    private IVideoManagerStorage storage;
+
     private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+    private SimpleDateFormat formatForGB = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
 
     /**
      *  鍒嗛〉鏌ヨ鎶ヨ
@@ -133,5 +149,51 @@
         return new ResponseEntity<WVPResult<String>>(wvpResult, HttpStatus.OK);
     }
 
+    /**
+     *  娴嬭瘯鍚戜笂绾�/璁惧鍙戦�佹ā鎷熸姤璀﹂�氱煡
+     *
+     * @param deviceId 鎶ヨid
+     * @return
+     */
+    @ApiOperation("娴嬭瘯鍚戜笂绾�/璁惧鍙戦�佹ā鎷熸姤璀﹂�氱煡")
+    @GetMapping("/test/notify/alarm")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name="deviceId", value = "deviceId", required = true ,dataTypeClass = Integer.class)
+    })
+    public ResponseEntity<WVPResult<String>> delete(
+            @RequestParam(required = false) String deviceId
+    ) {
+        if (StringUtils.isEmpty(deviceId)) {
+            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
+        }
+        Device device = storage.queryVideoDevice(deviceId);
+        ParentPlatform platform = storage.queryParentPlatByServerGBId(deviceId);
+        DeviceAlarm deviceAlarm = new DeviceAlarm();
+        deviceAlarm.setChannelId(deviceId);
+        deviceAlarm.setAlarmDescription("test");
+        deviceAlarm.setAlarmMethod("1");
+        deviceAlarm.setAlarmPriority("1");
+        deviceAlarm.setAlarmTime(formatForGB.format(System.currentTimeMillis()));
+        deviceAlarm.setAlarmType("1");
+        deviceAlarm.setLongitude(115.33333);
+        deviceAlarm.setLatitude(39.33333);
+
+        if (device != null && platform == null) {
+            commander.sendAlarmMessage(device, deviceAlarm);
+        }else if (device == null && platform != null){
+            commanderForPlatform.sendAlarmMessage(platform, deviceAlarm);
+        }else {
+            WVPResult wvpResult = new WVPResult();
+            wvpResult.setCode(0);
+            wvpResult.setMsg("鏃犳硶纭畾" + deviceId + "鏄钩鍙拌繕鏄澶�");
+            return new ResponseEntity<WVPResult<String>>(wvpResult, HttpStatus.OK);
+        }
+
+        WVPResult wvpResult = new WVPResult();
+        wvpResult.setCode(0);
+        wvpResult.setMsg("success");
+        return new ResponseEntity<WVPResult<String>>(wvpResult, HttpStatus.OK);
+    }
+
 
 }

--
Gitblit v1.8.0