648540858
2024-04-24 c21d973977a9f1d00d26179de764687ddd0ec56c
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/NotifyRequestForCatalogProcessor.java
old mode 100644 new mode 100755
@@ -1,6 +1,6 @@
package com.genersoft.iot.vmp.gb28181.transmit.event.request.impl;
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.DeviceChannel;
@@ -11,16 +11,20 @@
import com.genersoft.iot.vmp.gb28181.utils.XmlUtil;
import com.genersoft.iot.vmp.service.IDeviceChannelService;
import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
import com.genersoft.iot.vmp.utils.DateUtil;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.sip.RequestEvent;
import javax.sip.header.FromHeader;
import java.util.*;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
@@ -33,8 +37,6 @@
    private final static Logger logger = LoggerFactory.getLogger(NotifyRequestForCatalogProcessor.class);
   private final List<DeviceChannel> updateChannelOnlineList = new CopyOnWriteArrayList<>();
   private final List<DeviceChannel> updateChannelOfflineList = new CopyOnWriteArrayList<>();
   private final Map<String, DeviceChannel> updateChannelMap = new ConcurrentHashMap<>();
   private final Map<String, DeviceChannel> addChannelMap = new ConcurrentHashMap<>();
@@ -54,228 +56,111 @@
   private IDeviceChannelService deviceChannelService;
   @Autowired
   private DynamicTask dynamicTask;
   private SipConfig sipConfig;
   private final static String talkKey = "notify-request-for-catalog-task";
   @Transactional
   public void process(List<RequestEvent> evtList) {
      if (evtList.isEmpty()) {
         return;
      }
      for (RequestEvent evt : evtList) {
         try {
            long start = System.currentTimeMillis();
            FromHeader fromHeader = (FromHeader) evt.getRequest().getHeader(FromHeader.NAME);
            String deviceId = SipUtils.getUserIdFromFromHeader(fromHeader);
   public void process(RequestEvent evt) {
      try {
         long start = System.currentTimeMillis();
         FromHeader fromHeader = (FromHeader) evt.getRequest().getHeader(FromHeader.NAME);
         String deviceId = SipUtils.getUserIdFromFromHeader(fromHeader);
            Device device = redisCatchStorage.getDevice(deviceId);
            if (device == null || !device.isOnLine()) {
               logger.warn("[收到目录订阅]:{}, 但是设备已经离线", (device != null ? device.getDeviceId():"" ));
               return;
            }
            Element rootElement = getRootElement(evt, device.getCharset());
            if (rootElement == null) {
               logger.warn("[ 收到目录订阅 ] content cannot be null, {}", evt.getRequest());
               return;
            }
            Element deviceListElement = rootElement.element("DeviceList");
            if (deviceListElement == null) {
               return;
            }
            Iterator<Element> deviceListIterator = deviceListElement.elementIterator();
            if (deviceListIterator != null) {
         Device device = redisCatchStorage.getDevice(deviceId);
         if (device == null || !device.isOnLine()) {
            logger.warn("[收到目录订阅]:{}, 但是设备已经离线", (device != null ? device.getDeviceId():"" ));
            return;
         }
         Element rootElement = getRootElement(evt, device.getCharset());
         if (rootElement == null) {
            logger.warn("[ 收到目录订阅 ] content cannot be null, {}", evt.getRequest());
            return;
         }
         Element deviceListElement = rootElement.element("DeviceList");
         if (deviceListElement == null) {
            return;
         }
         Iterator<Element> deviceListIterator = deviceListElement.elementIterator();
         if (deviceListIterator != null) {
            // 遍历DeviceList
            while (deviceListIterator.hasNext()) {
               Element itemDevice = deviceListIterator.next();
               Element channelDeviceElement = itemDevice.element("DeviceID");
               if (channelDeviceElement == null) {
                  continue;
               }
               Element eventElement = itemDevice.element("Event");
               String event;
               if (eventElement == null) {
                  logger.warn("[收到目录订阅]:{}, 但是Event为空, 设为默认值 ADD", (device != null ? device.getDeviceId():"" ));
                  event = CatalogEvent.ADD;
               }else {
                  event = eventElement.getText().toUpperCase();
               }
               DeviceChannel channel = XmlUtil.channelContentHander(itemDevice, device, event);
               channel.setDeviceId(device.getDeviceId());
               logger.info("[收到目录订阅]:{}/{}", device.getDeviceId(), channel.getChannelId());
               switch (event) {
                  case CatalogEvent.ON:
                     // 上线
                     logger.info("[收到通道上线通知] 来自设备: {}, 通道 {}", device.getDeviceId(), channel.getChannelId());
                     updateChannelOnlineList.add(channel);
                     if (updateChannelOnlineList.size() > 300) {
                        executeSaveForOnline();
                     }
                     if (userSetting.getDeviceStatusNotify()) {
                        // 发送redis消息
                        redisCatchStorage.sendDeviceOrChannelStatus(device.getDeviceId(), channel.getChannelId(), true);
                     }
                     break;
                  case CatalogEvent.OFF :
                     // 离线
                     logger.info("[收到通道离线通知] 来自设备: {}, 通道 {}", device.getDeviceId(), channel.getChannelId());
                     if (userSetting.getRefuseChannelStatusChannelFormNotify()) {
                        logger.info("[收到通道离线通知] 但是平台已配置拒绝此消息,来自设备: {}, 通道 {}", device.getDeviceId(), channel.getChannelId());
                     }else {
                        updateChannelOfflineList.add(channel);
                        if (updateChannelOfflineList.size() > 300) {
                           executeSaveForOffline();
                        }
                        if (userSetting.getDeviceStatusNotify()) {
                           // 发送redis消息
                           redisCatchStorage.sendDeviceOrChannelStatus(device.getDeviceId(), channel.getChannelId(), false);
                        }
                     }
                     break;
                  case CatalogEvent.VLOST:
                     // 视频丢失
                     logger.info("[收到通道视频丢失通知] 来自设备: {}, 通道 {}", device.getDeviceId(), channel.getChannelId());
                     if (userSetting.getRefuseChannelStatusChannelFormNotify()) {
                        logger.info("[收到通道视频丢失通知] 但是平台已配置拒绝此消息,来自设备: {}, 通道 {}", device.getDeviceId(), channel.getChannelId());
                     }else {
                        updateChannelOfflineList.add(channel);
                        if (updateChannelOfflineList.size() > 300) {
                           executeSaveForOffline();
                        }
                        if (userSetting.getDeviceStatusNotify()) {
                           // 发送redis消息
                           redisCatchStorage.sendDeviceOrChannelStatus(device.getDeviceId(), channel.getChannelId(), false);
                        }
                     }
                     break;
                  case CatalogEvent.DEFECT:
                     // 故障
                     logger.info("[收到通道视频故障通知] 来自设备: {}, 通道 {}", device.getDeviceId(), channel.getChannelId());
                     if (userSetting.getRefuseChannelStatusChannelFormNotify()) {
                        logger.info("[收到通道视频故障通知] 但是平台已配置拒绝此消息,来自设备: {}, 通道 {}", device.getDeviceId(), channel.getChannelId());
                     }else {
                        updateChannelOfflineList.add(channel);
                        if (updateChannelOfflineList.size() > 300) {
                           executeSaveForOffline();
                        }
                        if (userSetting.getDeviceStatusNotify()) {
                           // 发送redis消息
                           redisCatchStorage.sendDeviceOrChannelStatus(device.getDeviceId(), channel.getChannelId(), false);
                        }
                     }
                     break;
                  case CatalogEvent.ADD:
                     // 增加
                     logger.info("[收到增加通道通知] 来自设备: {}, 通道 {}", device.getDeviceId(), channel.getChannelId());
                     // 判断此通道是否存在
                     DeviceChannel deviceChannel = deviceChannelService.getOne(deviceId, channel.getChannelId());
                     if (deviceChannel != null) {
                        channel.setId(deviceChannel.getId());
                        updateChannelMap.put(channel.getChannelId(), channel);
                        if (updateChannelMap.keySet().size() > 300) {
                           executeSaveForUpdate();
                        }
                     }else {
                        addChannelMap.put(channel.getChannelId(), channel);
                        if (addChannelMap.keySet().size() > 300) {
                           executeSaveForAdd();
                        }
                     }
                     break;
                  case CatalogEvent.DEL:
                     // 删除
                     logger.info("[收到删除通道通知] 来自设备: {}, 通道 {}", device.getDeviceId(), channel.getChannelId());
                     deleteChannelList.add(channel);
                     if (deleteChannelList.size() > 300) {
                        executeSaveForDelete();
                     }
                     break;
                  case CatalogEvent.UPDATE:
                     // 更新
                     logger.info("[收到更新通道通知] 来自设备: {}, 通道 {}", device.getDeviceId(), channel.getChannelId());
                     // 判断此通道是否存在
                     DeviceChannel deviceChannelForUpdate = deviceChannelService.getOne(deviceId, channel.getChannelId());
                     if (deviceChannelForUpdate != null) {
                        channel.setId(deviceChannelForUpdate.getId());
                        updateChannelMap.put(channel.getChannelId(), channel);
                        if (updateChannelMap.keySet().size() > 300) {
                           executeSaveForUpdate();
                        }
                     }else {
                        addChannelMap.put(channel.getChannelId(), channel);
                        if (addChannelMap.keySet().size() > 300) {
                           executeSaveForAdd();
                        }
                     }
                     break;
                  default:
                     logger.warn("[ NotifyCatalog ] event not found : {}", event );
               }
               // 转发变化信息
               eventPublisher.catalogEventPublish(null, channel, event);
               if (updateChannelMap.keySet().size() > 0
                     || addChannelMap.keySet().size() > 0
                     || updateChannelOnlineList.size() > 0
                     || updateChannelOfflineList.size() > 0
                     || deleteChannelList.size() > 0) {
                  if (!dynamicTask.contains(talkKey)) {
                     dynamicTask.startDelay(talkKey, this::executeSave, 1000);
               // 遍历DeviceList
               while (deviceListIterator.hasNext()) {
                  Element itemDevice = deviceListIterator.next();
                  Element eventElement = itemDevice.element("Event");
                  String event;
                  if (eventElement == null) {
                     logger.warn("[收到目录订阅]:{}, 但是Event为空, 设为默认值 ADD", (device != null ? device.getDeviceId():"" ));
                     event = CatalogEvent.ADD;
                  }else {
                     event = eventElement.getText().toUpperCase();
                  }
                  DeviceChannel channel = XmlUtil.channelContentHandler(itemDevice, device, event);
                  if (channel == null) {
                     logger.info("[收到目录订阅]:但是解析失败 {}", new String(evt.getRequest().getRawContent()));
                     continue;
                  }
                  if (channel.getParentId() != null && channel.getParentId().equals(sipConfig.getId())) {
                     channel.setParentId(null);
                  }
                  channel.setDeviceId(device.getDeviceId());
                  logger.info("[收到目录订阅]:{}, {}/{}",event, device.getDeviceId(), channel.getChannelId());
                  switch (event) {
                     case CatalogEvent.ON:
                        // 上线
                        deviceChannelService.online(channel);
                        if (userSetting.getDeviceStatusNotify()) {
                           // 发送redis消息
                           redisCatchStorage.sendDeviceOrChannelStatus(device.getDeviceId(), channel.getChannelId(), true);
                        }
                        break;
                     case CatalogEvent.OFF :
                     case CatalogEvent.VLOST:
                     case CatalogEvent.DEFECT:
                        // 离线
                        if (userSetting.getRefuseChannelStatusChannelFormNotify()) {
                           logger.info("[目录订阅] 离线 但是平台已配置拒绝此消息,来自设备: {}, 通道 {}", device.getDeviceId(), channel.getChannelId());
                        }else {
                           deviceChannelService.offline(channel);
                           if (userSetting.getDeviceStatusNotify()) {
                              // 发送redis消息
                              redisCatchStorage.sendDeviceOrChannelStatus(device.getDeviceId(), channel.getChannelId(), false);
                           }
                        }
                        break;
                     case CatalogEvent.DEL:
                        // 删除
                        deviceChannelService.delete(channel);
                        if (userSetting.getDeviceStatusNotify()) {
                           // 发送redis消息
                           redisCatchStorage.sendChannelAddOrDelete(device.getDeviceId(), channel.getChannelId(), false);
                        }
                        break;
                     case CatalogEvent.ADD:
                     case CatalogEvent.UPDATE:
                        // 更新
                        channel.setUpdateTime(DateUtil.getNow());
                        channel.setHasAudio(null);
                        deviceChannelService.updateChannel(deviceId,channel);
                        if (userSetting.getDeviceStatusNotify()) {
                           // 发送redis消息
                           redisCatchStorage.sendChannelAddOrDelete(device.getDeviceId(), channel.getChannelId(), true);
                        }
                        break;
                     default:
                        logger.warn("[ NotifyCatalog ] event not found : {}", event );
                  }
                  // 转发变化信息
                  eventPublisher.catalogEventPublish(null, channel, event);
               }
            }
         } catch (DocumentException e) {
            logger.error("未处理的异常 ", e);
         }
      } catch (DocumentException e) {
         logger.error("未处理的异常 ", e);
      }
   }
   private void executeSave(){
      System.out.println("定时存储数据");
      executeSaveForUpdate();
      executeSaveForDelete();
      executeSaveForOnline();
      executeSaveForOffline();
      dynamicTask.stop(talkKey);
   }
   private void executeSaveForUpdate(){
      if (updateChannelMap.values().size() > 0) {
         ArrayList<DeviceChannel> deviceChannels = new ArrayList<>(updateChannelMap.values());
         updateChannelMap.clear();
         deviceChannelService.batchUpdateChannel(deviceChannels);
      }
   }
   private void executeSaveForAdd(){
      if (addChannelMap.values().size() > 0) {
         ArrayList<DeviceChannel> deviceChannels = new ArrayList<>(addChannelMap.values());
         addChannelMap.clear();
         deviceChannelService.batchAddChannel(deviceChannels);
      }
   }
   private void executeSaveForDelete(){
      if (deleteChannelList.size() > 0) {
         deviceChannelService.deleteChannels(deleteChannelList);
         deleteChannelList.clear();
      }
   }
   private void executeSaveForOnline(){
      if (updateChannelOnlineList.size() > 0) {
         deviceChannelService.channelsOnline(updateChannelOnlineList);
         updateChannelOnlineList.clear();
      }
   }
   private void executeSaveForOffline(){
      if (updateChannelOfflineList.size() > 0) {
         deviceChannelService.channelsOffline(updateChannelOfflineList);
         updateChannelOfflineList.clear();
      }
   }
}