648540858
2022-10-17 f6320c7dd3dfd25e98bb63f41d79a33af5009a93
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/SubscribeRequestProcessor.java
@@ -1,22 +1,24 @@
package com.genersoft.iot.vmp.gb28181.transmit.event.request.impl;
import com.genersoft.iot.vmp.common.VideoManagerConstants;
import com.genersoft.iot.vmp.conf.DynamicTask;
import com.genersoft.iot.vmp.conf.UserSetting;
import com.genersoft.iot.vmp.gb28181.bean.CmdType;
import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform;
import com.genersoft.iot.vmp.gb28181.bean.SubscribeHolder;
import com.genersoft.iot.vmp.gb28181.bean.SubscribeInfo;
import com.genersoft.iot.vmp.gb28181.task.impl.MobilePositionSubscribeHandlerTask;
import com.genersoft.iot.vmp.gb28181.transmit.SIPProcessorObserver;
import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommanderForPlatform;
import com.genersoft.iot.vmp.gb28181.transmit.event.request.ISIPRequestProcessor;
import com.genersoft.iot.vmp.gb28181.transmit.event.request.SIPRequestProcessorParent;
import com.genersoft.iot.vmp.gb28181.utils.SipUtils;
import com.genersoft.iot.vmp.gb28181.utils.XmlUtil;
import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
import gov.nist.javax.sip.SipProviderImpl;
import gov.nist.javax.sip.message.SIPRequest;
import gov.nist.javax.sip.message.SIPResponse;
import gov.nist.javax.sip.stack.SIPClientTransaction;
import gov.nist.javax.sip.stack.SIPDialog;
import gov.nist.javax.sip.stack.SIPServerTransaction;
import gov.nist.javax.sip.stack.SIPServerTransactionImpl;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.slf4j.Logger;
@@ -49,22 +51,6 @@
   @Autowired
   private IVideoManagerStorage storager;
   @Lazy
   @Autowired
   @Qualifier(value="tcpSipProvider")
   private SipProviderImpl tcpSipProvider;
   @Lazy
   @Autowired
   @Qualifier(value="udpSipProvider")
   private SipProviderImpl udpSipProvider;
   @Autowired
   private DynamicTask dynamicTask;
   @Autowired
   private UserSetting userSetting;
   @Autowired
   private SubscribeHolder subscribeHolder;
@@ -81,18 +67,22 @@
    */
   @Override
   public void process(RequestEvent evt) {
      ServerTransaction serverTransaction = getServerTransaction(evt);
      Request request = evt.getRequest();
      try {
         Element rootElement = getRootElement(evt);
         if (rootElement == null) {
            logger.error("处理SUBSCRIBE请求  未获取到消息体{}", evt.getRequest());
            return;
         }
         String cmd = XmlUtil.getText(rootElement, "CmdType");
         if (CmdType.MOBILE_POSITION.equals(cmd)) {
            processNotifyMobilePosition(evt, rootElement);
            processNotifyMobilePosition(serverTransaction, rootElement);
//         } else if (CmdType.ALARM.equals(cmd)) {
//            logger.info("接收到Alarm订阅");
//            processNotifyAlarm(evt, rootElement);
//            processNotifyAlarm(serverTransaction, rootElement);
         } else if (CmdType.CATALOG.equals(cmd)) {
            processNotifyCatalogList(evt, rootElement);
            processNotifyCatalogList(serverTransaction, rootElement);
         } else {
            logger.info("接收到消息:" + cmd);
@@ -105,7 +95,6 @@
            ServerTransaction transaction = getServerTransaction(evt);
            if (transaction != null) {
               transaction.sendResponse(response);
               transaction.getDialog().delete();
               transaction.terminate();
            } else {
               logger.info("processRequest serverTransactionId is null.");
@@ -120,24 +109,20 @@
   /**
    * 处理移动位置订阅消息
    */
   private void processNotifyMobilePosition(RequestEvent evt, Element rootElement) throws SipException {
      String platformId = SipUtils.getUserIdFromFromHeader(evt.getRequest());
   private void processNotifyMobilePosition(ServerTransaction serverTransaction, Element rootElement) throws SipException {
      if (serverTransaction == null) {
         return;
      }
      String platformId = SipUtils.getUserIdFromFromHeader(serverTransaction.getRequest());
      String deviceId = XmlUtil.getText(rootElement, "DeviceID");
      ParentPlatform platform = storager.queryParentPlatByServerGBId(platformId);
      SubscribeInfo subscribeInfo = new SubscribeInfo(evt, platformId);
      SubscribeInfo subscribeInfo = new SubscribeInfo(serverTransaction, platformId);
      if (platform == null) {
         return;
      }
      if (evt.getServerTransaction() == null) {
         ServerTransaction serverTransaction = "TCP".equals(platform.getTransport()) ? tcpSipProvider.getNewServerTransaction(evt.getRequest())
               : udpSipProvider.getNewServerTransaction(evt.getRequest());
         subscribeInfo.setTransaction(serverTransaction);
         Dialog dialog = serverTransaction.getDialog();
         dialog.terminateOnBye(false);
         subscribeInfo.setDialog(dialog);
      }
      String sn = XmlUtil.getText(rootElement, "SN");
      logger.info("[回复 移动位置订阅]: {}", platformId);
      logger.info("[回复上级的移动位置订阅请求]: {}", platformId);
      StringBuilder resultXml = new StringBuilder(200);
      resultXml.append("<?xml version=\"1.0\" ?>\r\n")
            .append("<Response>\r\n")
@@ -155,17 +140,19 @@
         }else {
            subscribeInfo.setGpsInterval(Integer.parseInt(interval));
         }
         subscribeInfo.setSn(sn);
         subscribeHolder.putMobilePositionSubscribe(platformId, subscribeInfo);
      }else if (subscribeInfo.getExpires() == 0) {
         subscribeHolder.removeMobilePositionSubscribe(platformId);
      }
      try {
         ParentPlatform parentPlatform = storager.queryParentPlatByServerGBId(platformId);
         responseXmlAck(evt, resultXml.toString(), parentPlatform);
         SIPResponse response = responseXmlAck(serverTransaction, resultXml.toString(), parentPlatform, subscribeInfo.getExpires());
         if (subscribeInfo.getExpires() == 0) {
            subscribeHolder.removeMobilePositionSubscribe(platformId);
         }else {
            subscribeInfo.setResponse(response);
            subscribeHolder.putMobilePositionSubscribe(platformId, subscribeInfo);
         }
      } catch (SipException | InvalidArgumentException | ParseException e) {
         e.printStackTrace();
      }
@@ -175,24 +162,20 @@
   }
   private void processNotifyCatalogList(RequestEvent evt, Element rootElement) throws SipException {
      String platformId = SipUtils.getUserIdFromFromHeader(evt.getRequest());
   private void processNotifyCatalogList(ServerTransaction serverTransaction, Element rootElement) throws SipException {
      if (serverTransaction == null) {
         return;
      }
      String platformId = SipUtils.getUserIdFromFromHeader(serverTransaction.getRequest());
      String deviceId = XmlUtil.getText(rootElement, "DeviceID");
      ParentPlatform platform = storager.queryParentPlatByServerGBId(platformId);
      if (platform == null){
         return;
      }
      SubscribeInfo subscribeInfo = new SubscribeInfo(evt, platformId);
      if (evt.getServerTransaction() == null) {
         ServerTransaction serverTransaction = "TCP".equals(platform.getTransport()) ? tcpSipProvider.getNewServerTransaction(evt.getRequest())
               : udpSipProvider.getNewServerTransaction(evt.getRequest());
         subscribeInfo.setTransaction(serverTransaction);
         Dialog dialog = serverTransaction.getDialog();
         dialog.terminateOnBye(false);
         subscribeInfo.setDialog(dialog);
      }
      SubscribeInfo subscribeInfo = new SubscribeInfo(serverTransaction, platformId);
      String sn = XmlUtil.getText(rootElement, "SN");
      logger.info("[回复 目录订阅]: {}/{}", platformId, deviceId);
      logger.info("[回复上级的目录订阅请求]: {}/{}", platformId, deviceId);
      StringBuilder resultXml = new StringBuilder(200);
      resultXml.append("<?xml version=\"1.0\" ?>\r\n")
            .append("<Response>\r\n")
@@ -207,13 +190,17 @@
      }else if (subscribeInfo.getExpires() == 0) {
         subscribeHolder.removeCatalogSubscribe(platformId);
      }
      try {
         ParentPlatform parentPlatform = storager.queryParentPlatByServerGBId(platformId);
         responseXmlAck(evt, resultXml.toString(), parentPlatform);
         SIPResponse response = responseXmlAck(serverTransaction, resultXml.toString(), parentPlatform, subscribeInfo.getExpires());
         if (subscribeInfo.getExpires() == 0) {
            subscribeHolder.removeCatalogSubscribe(platformId);
         }else {
            subscribeInfo.setResponse(response);
            subscribeHolder.putCatalogSubscribe(platformId, subscribeInfo);
         }
      } catch (SipException | InvalidArgumentException | ParseException e) {
         e.printStackTrace();
      }
   }
}