648540858
2022-05-06 5d901b5e3f033e8b04e53420d68626cbd87431c8
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/NotifyRequestProcessor.java
@@ -1,22 +1,23 @@
package com.genersoft.iot.vmp.gb28181.transmit.event.request.impl;
import com.alibaba.fastjson.JSONObject;
import com.genersoft.iot.vmp.common.VideoManagerConstants;
import com.genersoft.iot.vmp.conf.SipConfig;
import com.genersoft.iot.vmp.conf.UserSetup;
import com.genersoft.iot.vmp.conf.UserSetting;
import com.genersoft.iot.vmp.gb28181.bean.*;
import com.genersoft.iot.vmp.gb28181.event.DeviceOffLineDetector;
import com.genersoft.iot.vmp.gb28181.event.EventPublisher;
import com.genersoft.iot.vmp.gb28181.event.subscribe.catalog.CatalogEvent;
import com.genersoft.iot.vmp.gb28181.transmit.SIPProcessorObserver;
import com.genersoft.iot.vmp.gb28181.transmit.callback.DeferredResultHolder;
import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
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.Coordtransform;
import com.genersoft.iot.vmp.gb28181.utils.NumericUtil;
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.utils.GpsUtil;
import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
import com.genersoft.iot.vmp.utils.redis.RedisUtil;
import org.dom4j.DocumentException;
import org.dom4j.Element;
@@ -45,10 +46,13 @@
    private final static Logger logger = LoggerFactory.getLogger(NotifyRequestProcessor.class);
   @Autowired
   private UserSetup userSetup;
   private UserSetting userSetting;
   @Autowired
   private IVideoManagerStorager storager;
   private IVideoManagerStorage storager;
   @Autowired
   private EventPublisher eventPublisher;
   @Autowired
   private SipConfig sipConfig;
@@ -58,10 +62,6 @@
   @Autowired
   private EventPublisher publisher;
   @Autowired
   private DeviceOffLineDetector offLineDetector;
   private String method = "NOTIFY";
@@ -105,11 +105,15 @@
    */
   private void processNotifyMobilePosition(RequestEvent evt) {
      try {
         FromHeader fromHeader = (FromHeader) evt.getRequest().getHeader(FromHeader.NAME);
         String deviceId = SipUtils.getUserIdFromFromHeader(fromHeader);
         // 回复 200 OK
         Element rootElement = getRootElement(evt);
         MobilePosition mobilePosition = new MobilePosition();
         Element deviceIdElement = rootElement.element("DeviceID");
         String deviceId = deviceIdElement.getTextTrim().toString();
         String channelId = deviceIdElement.getTextTrim().toString();
         Device device = redisCatchStorage.getDevice(deviceId);
         if (device != null) {
            if (!StringUtils.isEmpty(device.getName())) {
@@ -117,7 +121,9 @@
            }
         }
         mobilePosition.setDeviceId(XmlUtil.getText(rootElement, "DeviceID"));
         mobilePosition.setTime(XmlUtil.getText(rootElement, "Time"));
         mobilePosition.setChannelId(channelId);
         String time = XmlUtil.getText(rootElement, "Time");
         mobilePosition.setTime(time);
         mobilePosition.setLongitude(Double.parseDouble(XmlUtil.getText(rootElement, "Longitude")));
         mobilePosition.setLatitude(Double.parseDouble(XmlUtil.getText(rootElement, "Latitude")));
         if (NumericUtil.isDouble(XmlUtil.getText(rootElement, "Speed"))) {
@@ -135,17 +141,31 @@
         } else {
            mobilePosition.setAltitude(0.0);
         }
         logger.info("[收到 移动位置订阅]:{}/{}->{}.{}", mobilePosition.getDeviceId(), mobilePosition.getChannelId(),
               mobilePosition.getLongitude(), mobilePosition.getLatitude());
         mobilePosition.setReportSource("Mobile Position");
         BaiduPoint bp = new BaiduPoint();
         bp = GpsUtil.Wgs84ToBd09(String.valueOf(mobilePosition.getLongitude()), String.valueOf(mobilePosition.getLatitude()));
         logger.info("百度坐标:" + bp.getBdLng() + ", " + bp.getBdLat());
         mobilePosition.setGeodeticSystem("BD-09");
         mobilePosition.setCnLng(bp.getBdLng());
         mobilePosition.setCnLat(bp.getBdLat());
         if (!userSetup.getSavePositionHistory()) {
         // 默认来源坐标系为WGS-84处理
         Double[] gcj02Point = Coordtransform.WGS84ToGCJ02(mobilePosition.getLongitude(), mobilePosition.getLatitude());
         logger.info("GCJ02坐标:" + gcj02Point[0] + ", " + gcj02Point[1]);
         mobilePosition.setGeodeticSystem("GCJ-02");
         mobilePosition.setCnLng(gcj02Point[0] + "");
         mobilePosition.setCnLat(gcj02Point[1] + "");
         if (!userSetting.getSavePositionHistory()) {
            storager.clearMobilePositionsByDeviceId(deviceId);
         }
         storager.insertMobilePosition(mobilePosition);
         storager.updateChannelPotion(deviceId, channelId, mobilePosition.getLongitude(), mobilePosition.getLatitude() );
         // 发送redis消息。 通知位置信息的变化
         JSONObject jsonObject = new JSONObject();
         jsonObject.put("time", time);
         jsonObject.put("serial", deviceId);
         jsonObject.put("code", channelId);
         jsonObject.put("longitude", mobilePosition.getLongitude());
         jsonObject.put("latitude", mobilePosition.getLatitude());
         jsonObject.put("altitude", mobilePosition.getAltitude());
         jsonObject.put("direction", mobilePosition.getDirection());
         jsonObject.put("speed", mobilePosition.getSpeed());
         redisCatchStorage.sendMobilePositionMsg(jsonObject);
         responseAck(evt, Response.OK);
      } catch (DocumentException | SipException | InvalidArgumentException | ParseException e) {
         e.printStackTrace();
@@ -191,7 +211,7 @@
         } else {
            deviceAlarm.setLatitude(0.00);
         }
         logger.info("[收到Notify-Alarm]:{}/{}", device.getDeviceId(), deviceAlarm.getChannelId());
         if (deviceAlarm.getAlarmMethod().equals("4")) {
            MobilePosition mobilePosition = new MobilePosition();
            mobilePosition.setDeviceId(deviceAlarm.getDeviceId());
@@ -199,13 +219,13 @@
            mobilePosition.setLongitude(deviceAlarm.getLongitude());
            mobilePosition.setLatitude(deviceAlarm.getLatitude());
            mobilePosition.setReportSource("GPS Alarm");
            BaiduPoint bp = new BaiduPoint();
            bp = GpsUtil.Wgs84ToBd09(String.valueOf(mobilePosition.getLongitude()), String.valueOf(mobilePosition.getLatitude()));
            logger.info("百度坐标:" + bp.getBdLng() + ", " + bp.getBdLat());
            mobilePosition.setGeodeticSystem("BD-09");
            mobilePosition.setCnLng(bp.getBdLng());
            mobilePosition.setCnLat(bp.getBdLat());
            if (!userSetup.getSavePositionHistory()) {
            // 默认来源坐标系为WGS-84处理
            Double[] gcj02Point = Coordtransform.WGS84ToGCJ02(mobilePosition.getLongitude(), mobilePosition.getLatitude());
            logger.info("GCJ02坐标:" + gcj02Point[0] + ", " + gcj02Point[1]);
            mobilePosition.setGeodeticSystem("GCJ-02");
            mobilePosition.setCnLng(gcj02Point[0] + "");
            mobilePosition.setCnLat(gcj02Point[1] + "");
            if (!userSetting.getSavePositionHistory()) {
               storager.clearMobilePositionsByDeviceId(deviceId);
            }
            storager.insertMobilePosition(mobilePosition);
@@ -214,7 +234,7 @@
         // 回复200 OK
         responseAck(evt, Response.OK);
         if (offLineDetector.isOnline(deviceId)) {
         if (redisCatchStorage.deviceIsOnline(deviceId)) {
            publisher.deviceAlarmEventPublish(deviceAlarm);
         }
      } catch (DocumentException | SipException | InvalidArgumentException | ParseException e) {
@@ -255,41 +275,43 @@
                  continue;
               }
               Element eventElement = itemDevice.element("Event");
               DeviceChannel channel = channelContentHander(itemDevice);
               DeviceChannel channel = XmlUtil.channelContentHander(itemDevice);
               channel.setDeviceId(device.getDeviceId());
               logger.info("[收到 目录订阅]:{}/{}", device.getDeviceId(), channel.getChannelId());
               switch (eventElement.getText().toUpperCase()) {
                  case "ON" : // 上线
                  case CatalogEvent.ON: // 上线
                     logger.info("收到来自设备【{}】的通道【{}】上线通知", device.getDeviceId(), channel.getChannelId());
                     storager.deviceChannelOnline(deviceId, channel.getChannelId());
                     // 回复200 OK
                     responseAck(evt, Response.OK);
                     break;
                  case "OFF" : // 离线
                  case CatalogEvent.OFF : // 离线
                     logger.info("收到来自设备【{}】的通道【{}】离线通知", device.getDeviceId(), channel.getChannelId());
                     storager.deviceChannelOffline(deviceId, channel.getChannelId());
                     // 回复200 OK
                     responseAck(evt, Response.OK);
                     break;
                  case "VLOST" : // 视频丢失
                  case CatalogEvent.VLOST: // 视频丢失
                     logger.info("收到来自设备【{}】的通道【{}】视频丢失通知", device.getDeviceId(), channel.getChannelId());
                     storager.deviceChannelOffline(deviceId, channel.getChannelId());
                     // 回复200 OK
                     responseAck(evt, Response.OK);
                     break;
                  case "DEFECT" : // 故障
                  case CatalogEvent.DEFECT: // 故障
                     // 回复200 OK
                     responseAck(evt, Response.OK);
                     break;
                  case "ADD" : // 增加
                  case CatalogEvent.ADD: // 增加
                     logger.info("收到来自设备【{}】的增加通道【{}】通知", device.getDeviceId(), channel.getChannelId());
                     storager.updateChannel(deviceId, channel);
                     responseAck(evt, Response.OK);
                     break;
                  case "DEL" : // 删除
                  case CatalogEvent.DEL: // 删除
                     logger.info("收到来自设备【{}】的删除通道【{}】通知", device.getDeviceId(), channel.getChannelId());
                     storager.delChannel(deviceId, channel.getChannelId());
                     responseAck(evt, Response.OK);
                     break;
                  case "UPDATE" : // 更新
                  case CatalogEvent.UPDATE: // 更新
                     logger.info("收到来自设备【{}】的更新通道【{}】通知", device.getDeviceId(), channel.getChannelId());
                     storager.updateChannel(deviceId, channel);
                     responseAck(evt, Response.OK);
@@ -298,16 +320,12 @@
                     responseAck(evt, Response.BAD_REQUEST, "event not found");
               }
               // 转发变化信息
               eventPublisher.catalogEventPublish(null, channel, eventElement.getText().toUpperCase());
            }
            // RequestMessage msg = new RequestMessage();
            // msg.setDeviceId(deviceId);
            // msg.setType(DeferredResultHolder.CALLBACK_CMD_CATALOG);
            // msg.setData(device);
            // deferredResultHolder.invokeResult(msg);
            if (offLineDetector.isOnline(deviceId)) {
            if (!redisCatchStorage.deviceIsOnline(deviceId)) {
               publisher.onlineEventPublish(device, VideoManagerConstants.EVENT_ONLINE_MESSAGE);
            }
         }
@@ -316,97 +334,10 @@
      }
   }
   public DeviceChannel channelContentHander(Element itemDevice){
      Element channdelNameElement = itemDevice.element("Name");
      String channelName = channdelNameElement != null ? channdelNameElement.getTextTrim().toString() : "";
      Element statusElement = itemDevice.element("Status");
      String status = statusElement != null ? statusElement.getTextTrim().toString() : "ON";
      DeviceChannel deviceChannel = new DeviceChannel();
      deviceChannel.setName(channelName);
      Element channdelIdElement = itemDevice.element("DeviceID");
      String channelId = channdelIdElement != null ? channdelIdElement.getTextTrim().toString() : "";
      deviceChannel.setChannelId(channelId);
      // ONLINE OFFLINE HIKVISION DS-7716N-E4 NVR的兼容性处理
      if (status.equals("ON") || status.equals("On") || status.equals("ONLINE")) {
         deviceChannel.setStatus(1);
      }
      if (status.equals("OFF") || status.equals("Off") || status.equals("OFFLINE")) {
         deviceChannel.setStatus(0);
      }
      deviceChannel.setManufacture(XmlUtil.getText(itemDevice, "Manufacturer"));
      deviceChannel.setModel(XmlUtil.getText(itemDevice, "Model"));
      deviceChannel.setOwner(XmlUtil.getText(itemDevice, "Owner"));
      deviceChannel.setCivilCode(XmlUtil.getText(itemDevice, "CivilCode"));
      deviceChannel.setBlock(XmlUtil.getText(itemDevice, "Block"));
      deviceChannel.setAddress(XmlUtil.getText(itemDevice, "Address"));
      if (XmlUtil.getText(itemDevice, "Parental") == null
            || XmlUtil.getText(itemDevice, "Parental") == "") {
         deviceChannel.setParental(0);
      } else {
         deviceChannel.setParental(Integer.parseInt(XmlUtil.getText(itemDevice, "Parental")));
      }
      deviceChannel.setParentId(XmlUtil.getText(itemDevice, "ParentID"));
      if (XmlUtil.getText(itemDevice, "SafetyWay") == null
            || XmlUtil.getText(itemDevice, "SafetyWay") == "") {
         deviceChannel.setSafetyWay(0);
      } else {
         deviceChannel.setSafetyWay(Integer.parseInt(XmlUtil.getText(itemDevice, "SafetyWay")));
      }
      if (XmlUtil.getText(itemDevice, "RegisterWay") == null
            || XmlUtil.getText(itemDevice, "RegisterWay") == "") {
         deviceChannel.setRegisterWay(1);
      } else {
         deviceChannel.setRegisterWay(Integer.parseInt(XmlUtil.getText(itemDevice, "RegisterWay")));
      }
      deviceChannel.setCertNum(XmlUtil.getText(itemDevice, "CertNum"));
      if (XmlUtil.getText(itemDevice, "Certifiable") == null
            || XmlUtil.getText(itemDevice, "Certifiable") == "") {
         deviceChannel.setCertifiable(0);
      } else {
         deviceChannel.setCertifiable(Integer.parseInt(XmlUtil.getText(itemDevice, "Certifiable")));
      }
      if (XmlUtil.getText(itemDevice, "ErrCode") == null
            || XmlUtil.getText(itemDevice, "ErrCode") == "") {
         deviceChannel.setErrCode(0);
      } else {
         deviceChannel.setErrCode(Integer.parseInt(XmlUtil.getText(itemDevice, "ErrCode")));
      }
      deviceChannel.setEndTime(XmlUtil.getText(itemDevice, "EndTime"));
      deviceChannel.setSecrecy(XmlUtil.getText(itemDevice, "Secrecy"));
      deviceChannel.setIpAddress(XmlUtil.getText(itemDevice, "IPAddress"));
      if (XmlUtil.getText(itemDevice, "Port") == null || XmlUtil.getText(itemDevice, "Port") == "") {
         deviceChannel.setPort(0);
      } else {
         deviceChannel.setPort(Integer.parseInt(XmlUtil.getText(itemDevice, "Port")));
      }
      deviceChannel.setPassword(XmlUtil.getText(itemDevice, "Password"));
      if (NumericUtil.isDouble(XmlUtil.getText(itemDevice, "Longitude"))) {
         deviceChannel.setLongitude(Double.parseDouble(XmlUtil.getText(itemDevice, "Longitude")));
      } else {
         deviceChannel.setLongitude(0.00);
      }
      if (NumericUtil.isDouble(XmlUtil.getText(itemDevice, "Latitude"))) {
         deviceChannel.setLatitude(Double.parseDouble(XmlUtil.getText(itemDevice, "Latitude")));
      } else {
         deviceChannel.setLatitude(0.00);
      }
      if (XmlUtil.getText(itemDevice, "PTZType") == null
            || XmlUtil.getText(itemDevice, "PTZType") == "") {
         deviceChannel.setPTZType(0);
      } else {
         deviceChannel.setPTZType(Integer.parseInt(XmlUtil.getText(itemDevice, "PTZType")));
      }
      deviceChannel.setHasAudio(true); // 默认含有音频,播放时再检查是否有音频及是否AAC
      return deviceChannel;
   }
   public void setCmder(SIPCommander cmder) {
   }
   public void setStorager(IVideoManagerStorager storager) {
   public void setStorager(IVideoManagerStorage storager) {
      this.storager = storager;
   }
@@ -418,10 +349,6 @@
   }
   public void setDeferredResultHolder(DeferredResultHolder deferredResultHolder) {
   }
   public void setOffLineDetector(DeviceOffLineDetector offLineDetector) {
      this.offLineDetector = offLineDetector;
   }
   public IRedisCatchStorage getRedisCatchStorage() {