old mode 100644
new mode 100755
 |  |  | 
 |  |  | 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.UserSetup; | 
 |  |  | 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.GPSSubscribeTask; | 
 |  |  | import com.genersoft.iot.vmp.gb28181.transmit.SIPProcessorObserver; | 
 |  |  | import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommanderForPlatform; | 
 |  |  | import com.genersoft.iot.vmp.gb28181.transmit.SIPSender; | 
 |  |  | 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.IVideoManagerStorager; | 
 |  |  | import com.genersoft.iot.vmp.storager.IVideoManagerStorage; | 
 |  |  | import gov.nist.javax.sip.message.SIPRequest; | 
 |  |  | import gov.nist.javax.sip.message.SIPResponse; | 
 |  |  | import org.dom4j.DocumentException; | 
 |  |  | import org.dom4j.Element; | 
 |  |  | import org.slf4j.Logger; | 
 |  |  | 
 |  |  |  | 
 |  |  | import javax.sip.InvalidArgumentException; | 
 |  |  | import javax.sip.RequestEvent; | 
 |  |  | import javax.sip.ServerTransaction; | 
 |  |  | import javax.sip.SipException; | 
 |  |  | import javax.sip.header.ExpiresHeader; | 
 |  |  | import javax.sip.header.ToHeader; | 
 |  |  | import javax.sip.message.Request; | 
 |  |  | import javax.sip.message.Response; | 
 |  |  | import java.text.ParseException; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * SIP命令类型: SUBSCRIBE请求 | 
 |  |  |  * @author lin | 
 |  |  |  */ | 
 |  |  | @Component | 
 |  |  | public class SubscribeRequestProcessor extends SIPRequestProcessorParent implements InitializingBean, ISIPRequestProcessor { | 
 |  |  |  | 
 |  |  |    private Logger logger = LoggerFactory.getLogger(SubscribeRequestProcessor.class); | 
 |  |  |    private String method = "SUBSCRIBE"; | 
 |  |  |    private final Logger logger = LoggerFactory.getLogger(SubscribeRequestProcessor.class); | 
 |  |  |    private final String method = "SUBSCRIBE"; | 
 |  |  |  | 
 |  |  |    @Autowired | 
 |  |  |    private SIPProcessorObserver sipProcessorObserver; | 
 |  |  |  | 
 |  |  |    @Autowired | 
 |  |  |    private IRedisCatchStorage redisCatchStorage; | 
 |  |  |    private IVideoManagerStorage storager; | 
 |  |  |  | 
 |  |  |    @Autowired | 
 |  |  |    private ISIPCommanderForPlatform sipCommanderForPlatform; | 
 |  |  |    private SubscribeHolder subscribeHolder; | 
 |  |  |  | 
 |  |  |    @Autowired | 
 |  |  |    private IVideoManagerStorager storager; | 
 |  |  |  | 
 |  |  |    @Autowired | 
 |  |  |    private DynamicTask dynamicTask; | 
 |  |  |  | 
 |  |  |    @Autowired | 
 |  |  |    private UserSetup userSetup; | 
 |  |  |    private SIPSender sipSender; | 
 |  |  |  | 
 |  |  |    @Override | 
 |  |  |    public void afterPropertiesSet() throws Exception { | 
 |  |  | 
 |  |  |    /**    | 
 |  |  |     * 处理SUBSCRIBE请求   | 
 |  |  |     *  | 
 |  |  |     * @param evt | 
 |  |  |     * @param evt 事件 | 
 |  |  |     */ | 
 |  |  |    @Override | 
 |  |  |    public void process(RequestEvent evt) { | 
 |  |  |       Request request = evt.getRequest(); | 
 |  |  |  | 
 |  |  |       SIPRequest request = (SIPRequest) 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(request, 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(request, rootElement); | 
 |  |  |          } else { | 
 |  |  |             logger.info("接收到消息:" + cmd); | 
 |  |  |  | 
 |  |  |             Response response = null; | 
 |  |  |             response = getMessageFactory().createResponse(200, request); | 
 |  |  |             Response response = getMessageFactory().createResponse(200, request); | 
 |  |  |             if (response != null) { | 
 |  |  |                ExpiresHeader expireHeader = getHeaderFactory().createExpiresHeader(30); | 
 |  |  |                response.setExpires(expireHeader); | 
 |  |  |             } | 
 |  |  |             logger.info("response : " + response.toString()); | 
 |  |  |             ServerTransaction transaction = getServerTransaction(evt); | 
 |  |  |             if (transaction != null) { | 
 |  |  |                transaction.sendResponse(response); | 
 |  |  |                transaction.getDialog().delete(); | 
 |  |  |                transaction.terminate(); | 
 |  |  |             } else { | 
 |  |  |                logger.info("processRequest serverTransactionId is null."); | 
 |  |  |             } | 
 |  |  |             logger.info("response : " + response); | 
 |  |  |             sipSender.transmitRequest(request.getLocalAddress().getHostAddress(), response); | 
 |  |  |          } | 
 |  |  |       } catch (ParseException e) { | 
 |  |  |          e.printStackTrace(); | 
 |  |  |       } catch (SipException e) { | 
 |  |  |          e.printStackTrace(); | 
 |  |  |       } catch (InvalidArgumentException e) { | 
 |  |  |          e.printStackTrace(); | 
 |  |  |       } catch (DocumentException e) { | 
 |  |  |          e.printStackTrace(); | 
 |  |  |       } catch (ParseException | SipException | InvalidArgumentException | DocumentException e) { | 
 |  |  |          logger.error("未处理的异常 ", e); | 
 |  |  |       } | 
 |  |  |  | 
 |  |  |    } | 
 |  |  | 
 |  |  |    /** | 
 |  |  |     * 处理移动位置订阅消息 | 
 |  |  |     */ | 
 |  |  |    private void processNotifyMobilePosition(RequestEvent evt, Element rootElement) { | 
 |  |  |       String platformId = SipUtils.getUserIdFromFromHeader(evt.getRequest()); | 
 |  |  |       String deviceID = XmlUtil.getText(rootElement, "DeviceID"); | 
 |  |  |       SubscribeInfo subscribeInfo = new SubscribeInfo(evt, platformId); | 
 |  |  |    private void processNotifyMobilePosition(SIPRequest request, Element rootElement) throws SipException { | 
 |  |  |       if (request == null) { | 
 |  |  |          return; | 
 |  |  |       } | 
 |  |  |       String platformId = SipUtils.getUserIdFromFromHeader(request); | 
 |  |  |       String deviceId = XmlUtil.getText(rootElement, "DeviceID"); | 
 |  |  |       ParentPlatform platform = storager.queryParentPlatByServerGBId(platformId); | 
 |  |  |       SubscribeInfo subscribeInfo = new SubscribeInfo(request, platformId); | 
 |  |  |       if (platform == null) { | 
 |  |  |          return; | 
 |  |  |       } | 
 |  |  |  | 
 |  |  |       String sn = XmlUtil.getText(rootElement, "SN"); | 
 |  |  |       String key = VideoManagerConstants.SIP_SUBSCRIBE_PREFIX + userSetup.getServerId() +  "_MobilePosition_" + platformId; | 
 |  |  |       logger.info("接收到{}的MobilePosition订阅", platformId); | 
 |  |  |       logger.info("[回复上级的移动位置订阅请求]: {}", platformId); | 
 |  |  |       StringBuilder resultXml = new StringBuilder(200); | 
 |  |  |       resultXml.append("<?xml version=\"1.0\" ?>\r\n") | 
 |  |  |             .append("<Response>\r\n") | 
 |  |  |             .append("<CmdType>MobilePosition</CmdType>\r\n") | 
 |  |  |             .append("<SN>" + sn + "</SN>\r\n") | 
 |  |  |             .append("<DeviceID>" + deviceID + "</DeviceID>\r\n") | 
 |  |  |             .append("<SN>").append(sn).append("</SN>\r\n") | 
 |  |  |             .append("<DeviceID>").append(deviceId).append("</DeviceID>\r\n") | 
 |  |  |             .append("<Result>OK</Result>\r\n") | 
 |  |  |             .append("</Response>\r\n"); | 
 |  |  |  | 
 |  |  |       if (subscribeInfo.getExpires() > 0) { | 
 |  |  |          if (redisCatchStorage.getSubscribe(key) != null) { | 
 |  |  |             dynamicTask.stop(key); | 
 |  |  |          // GPS上报时间间隔 | 
 |  |  |          String interval = XmlUtil.getText(rootElement, "Interval"); | 
 |  |  |          if (interval == null) { | 
 |  |  |             subscribeInfo.setGpsInterval(5); | 
 |  |  |          }else { | 
 |  |  |             subscribeInfo.setGpsInterval(Integer.parseInt(interval)); | 
 |  |  |          } | 
 |  |  |          String interval = XmlUtil.getText(rootElement, "Interval"); // GPS上报时间间隔 | 
 |  |  |          dynamicTask.startCron(key, new GPSSubscribeTask(redisCatchStorage, sipCommanderForPlatform, storager,  platformId, sn, key), Integer.parseInt(interval)); | 
 |  |  |  | 
 |  |  |          redisCatchStorage.updateSubscribe(key, subscribeInfo); | 
 |  |  |       }else if (subscribeInfo.getExpires() == 0) { | 
 |  |  |          dynamicTask.stop(key); | 
 |  |  |          redisCatchStorage.delSubscribe(key); | 
 |  |  |          subscribeInfo.setSn(sn); | 
 |  |  |       } | 
 |  |  |  | 
 |  |  |       try { | 
 |  |  |          ParentPlatform parentPlatform = storager.queryParentPlatByServerGBId(platformId); | 
 |  |  |          Response response = responseXmlAck(evt, resultXml.toString(), parentPlatform); | 
 |  |  |          ToHeader toHeader = (ToHeader)response.getHeader(ToHeader.NAME); | 
 |  |  |          subscribeInfo.setToTag(toHeader.getTag()); | 
 |  |  |          redisCatchStorage.updateSubscribe(key, subscribeInfo); | 
 |  |  |          SIPResponse response = responseXmlAck(request, resultXml.toString(), parentPlatform, subscribeInfo.getExpires()); | 
 |  |  |          if (subscribeInfo.getExpires() == 0) { | 
 |  |  |             subscribeHolder.removeMobilePositionSubscribe(platformId); | 
 |  |  |          }else { | 
 |  |  |             subscribeInfo.setResponse(response); | 
 |  |  |             subscribeHolder.putMobilePositionSubscribe(platformId, subscribeInfo); | 
 |  |  |          } | 
 |  |  |  | 
 |  |  |       } catch (SipException e) { | 
 |  |  |          e.printStackTrace(); | 
 |  |  |       } catch (InvalidArgumentException e) { | 
 |  |  |          e.printStackTrace(); | 
 |  |  |       } catch (ParseException e) { | 
 |  |  |          e.printStackTrace(); | 
 |  |  |       } catch (SipException | InvalidArgumentException | ParseException e) { | 
 |  |  |          logger.error("未处理的异常 ", e); | 
 |  |  |       } | 
 |  |  |    } | 
 |  |  |  | 
 |  |  | 
 |  |  |  | 
 |  |  |    } | 
 |  |  |  | 
 |  |  |    private void processNotifyCatalogList(RequestEvent evt, Element rootElement) { | 
 |  |  |       String platformId = SipUtils.getUserIdFromFromHeader(evt.getRequest()); | 
 |  |  |       String deviceID = XmlUtil.getText(rootElement, "DeviceID"); | 
 |  |  |       SubscribeInfo subscribeInfo = new SubscribeInfo(evt, platformId); | 
 |  |  |    private void processNotifyCatalogList(SIPRequest request, Element rootElement) throws SipException { | 
 |  |  |       if (request == null) { | 
 |  |  |          return; | 
 |  |  |       } | 
 |  |  |       String platformId = SipUtils.getUserIdFromFromHeader(request); | 
 |  |  |       String deviceId = XmlUtil.getText(rootElement, "DeviceID"); | 
 |  |  |       ParentPlatform platform = storager.queryParentPlatByServerGBId(platformId); | 
 |  |  |       if (platform == null){ | 
 |  |  |          return; | 
 |  |  |       } | 
 |  |  |       SubscribeInfo subscribeInfo = new SubscribeInfo(request, platformId); | 
 |  |  |  | 
 |  |  |       String sn = XmlUtil.getText(rootElement, "SN"); | 
 |  |  |       String key = VideoManagerConstants.SIP_SUBSCRIBE_PREFIX + userSetup.getServerId() +  "_Catalog_" + platformId; | 
 |  |  |       logger.info("接收到{}的Catalog订阅", platformId); | 
 |  |  |       logger.info("[回复上级的目录订阅请求]: {}/{}", platformId, deviceId); | 
 |  |  |       StringBuilder resultXml = new StringBuilder(200); | 
 |  |  |       resultXml.append("<?xml version=\"1.0\" ?>\r\n") | 
 |  |  |             .append("<Response>\r\n") | 
 |  |  |             .append("<CmdType>Catalog</CmdType>\r\n") | 
 |  |  |             .append("<SN>" + sn + "</SN>\r\n") | 
 |  |  |             .append("<DeviceID>" + deviceID + "</DeviceID>\r\n") | 
 |  |  |             .append("<SN>").append(sn).append("</SN>\r\n") | 
 |  |  |             .append("<DeviceID>").append(deviceId).append("</DeviceID>\r\n") | 
 |  |  |             .append("<Result>OK</Result>\r\n") | 
 |  |  |             .append("</Response>\r\n"); | 
 |  |  |  | 
 |  |  |       if (subscribeInfo.getExpires() > 0) { | 
 |  |  |          redisCatchStorage.updateSubscribe(key, subscribeInfo); | 
 |  |  |          subscribeHolder.putCatalogSubscribe(platformId, subscribeInfo); | 
 |  |  |       }else if (subscribeInfo.getExpires() == 0) { | 
 |  |  |          redisCatchStorage.delSubscribe(key); | 
 |  |  |          subscribeHolder.removeCatalogSubscribe(platformId); | 
 |  |  |       } | 
 |  |  |  | 
 |  |  |       try { | 
 |  |  |          ParentPlatform parentPlatform = storager.queryParentPlatByServerGBId(platformId); | 
 |  |  |          Response response = responseXmlAck(evt, resultXml.toString(), parentPlatform); | 
 |  |  |          ToHeader toHeader = (ToHeader)response.getHeader(ToHeader.NAME); | 
 |  |  |          subscribeInfo.setToTag(toHeader.getTag()); | 
 |  |  |          redisCatchStorage.updateSubscribe(key, subscribeInfo); | 
 |  |  |  | 
 |  |  |       } catch (SipException e) { | 
 |  |  |          e.printStackTrace(); | 
 |  |  |       } catch (InvalidArgumentException e) { | 
 |  |  |          e.printStackTrace(); | 
 |  |  |       } catch (ParseException e) { | 
 |  |  |          e.printStackTrace(); | 
 |  |  |          SIPResponse response = responseXmlAck(request, 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) { | 
 |  |  |          logger.error("未处理的异常 ", e); | 
 |  |  |       } | 
 |  |  |    } | 
 |  |  |  | 
 |  |  | } |