old mode 100644
new mode 100755
 |  |  | 
 |  |  | package com.genersoft.iot.vmp.gb28181.transmit.event.request.impl; | 
 |  |  |  | 
 |  |  | import com.genersoft.iot.vmp.conf.CivilCodeFileConf; | 
 |  |  | 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; | 
 |  |  | 
 |  |  | 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 javax.sip.RequestEvent; | 
 |  |  | import javax.sip.header.FromHeader; | 
 |  |  | import java.util.*; | 
 |  |  | import java.util.ArrayList; | 
 |  |  | import java.util.Iterator; | 
 |  |  | import java.util.List; | 
 |  |  | import java.util.Map; | 
 |  |  | import java.util.concurrent.ConcurrentHashMap; | 
 |  |  | import java.util.concurrent.CopyOnWriteArrayList; | 
 |  |  |  | 
 |  |  | 
 |  |  |  | 
 |  |  |    @Autowired | 
 |  |  |    private DynamicTask dynamicTask; | 
 |  |  |  | 
 |  |  |    @Autowired | 
 |  |  |    private CivilCodeFileConf civilCodeFileConf; | 
 |  |  |  | 
 |  |  |    @Autowired | 
 |  |  |    private SipConfig sipConfig; | 
 |  |  |  | 
 |  |  |    private final static String talkKey = "notify-request-for-catalog-task"; | 
 |  |  |  | 
 |  |  | 
 |  |  |                }else { | 
 |  |  |                   event = eventElement.getText().toUpperCase(); | 
 |  |  |                } | 
 |  |  |                DeviceChannel channel = XmlUtil.channelContentHander(itemDevice, device, event); | 
 |  |  |  | 
 |  |  |                DeviceChannel channel = XmlUtil.channelContentHandler(itemDevice, device, event, civilCodeFileConf); | 
 |  |  |                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("[收到目录订阅]:{}/{}", device.getDeviceId(), channel.getChannelId()); | 
 |  |  |                switch (event) { | 
 |  |  | 
 |  |  |                      // 判断此通道是否存在 | 
 |  |  |                      DeviceChannel deviceChannel = deviceChannelService.getOne(deviceId, channel.getChannelId()); | 
 |  |  |                      if (deviceChannel != null) { | 
 |  |  |                         logger.info("[增加通道] 已存在,不发送通知只更新,设备: {}, 通道 {}", device.getDeviceId(), channel.getChannelId()); | 
 |  |  |                         channel.setId(deviceChannel.getId()); | 
 |  |  |                         updateChannelMap.put(channel.getChannelId(), channel); | 
 |  |  |                         if (updateChannelMap.keySet().size() > 300) { | 
 |  |  | 
 |  |  |                         } | 
 |  |  |                      }else { | 
 |  |  |                         addChannelMap.put(channel.getChannelId(), channel); | 
 |  |  |                         if (userSetting.getDeviceStatusNotify()) { | 
 |  |  |                            // 发送redis消息 | 
 |  |  |                            redisCatchStorage.sendChannelAddOrDelete(device.getDeviceId(), channel.getChannelId(), true); | 
 |  |  |                         } | 
 |  |  |  | 
 |  |  |                         if (addChannelMap.keySet().size() > 300) { | 
 |  |  |                            executeSaveForAdd(); | 
 |  |  |                         } | 
 |  |  | 
 |  |  |                      // 删除 | 
 |  |  |                      logger.info("[收到删除通道通知] 来自设备: {}, 通道 {}", device.getDeviceId(), channel.getChannelId()); | 
 |  |  |                      deleteChannelList.add(channel); | 
 |  |  |                      if (userSetting.getDeviceStatusNotify()) { | 
 |  |  |                         // 发送redis消息 | 
 |  |  |                         redisCatchStorage.sendChannelAddOrDelete(device.getDeviceId(), channel.getChannelId(), false); | 
 |  |  |                      } | 
 |  |  |                      if (deleteChannelList.size() > 300) { | 
 |  |  |                         executeSaveForDelete(); | 
 |  |  |                      } | 
 |  |  | 
 |  |  |                      DeviceChannel deviceChannelForUpdate = deviceChannelService.getOne(deviceId, channel.getChannelId()); | 
 |  |  |                      if (deviceChannelForUpdate != null) { | 
 |  |  |                         channel.setId(deviceChannelForUpdate.getId()); | 
 |  |  |                         channel.setUpdateTime(DateUtil.getNow()); | 
 |  |  |                         updateChannelMap.put(channel.getChannelId(), channel); | 
 |  |  |                         if (updateChannelMap.keySet().size() > 300) { | 
 |  |  |                            executeSaveForUpdate(); | 
 |  |  | 
 |  |  |                         addChannelMap.put(channel.getChannelId(), channel); | 
 |  |  |                         if (addChannelMap.keySet().size() > 300) { | 
 |  |  |                            executeSaveForAdd(); | 
 |  |  |                         } | 
 |  |  |                         if (userSetting.getDeviceStatusNotify()) { | 
 |  |  |                            // 发送redis消息 | 
 |  |  |                            redisCatchStorage.sendChannelAddOrDelete(device.getDeviceId(), channel.getChannelId(), true); | 
 |  |  |                         } | 
 |  |  |                      } | 
 |  |  |                      break; | 
 |  |  | 
 |  |  |                // 转发变化信息 | 
 |  |  |                eventPublisher.catalogEventPublish(null, channel, event); | 
 |  |  |  | 
 |  |  |                if (updateChannelMap.keySet().size() > 0 | 
 |  |  |                      || addChannelMap.keySet().size() > 0 | 
 |  |  |                      || updateChannelOnlineList.size() > 0 | 
 |  |  |                      || updateChannelOfflineList.size() > 0 | 
 |  |  |                      || deleteChannelList.size() > 0) { | 
 |  |  |                if (!updateChannelMap.keySet().isEmpty() | 
 |  |  |                      || !addChannelMap.keySet().isEmpty() | 
 |  |  |                      || !updateChannelOnlineList.isEmpty() | 
 |  |  |                      || !updateChannelOfflineList.isEmpty() | 
 |  |  |                      || !deleteChannelList.isEmpty()) { | 
 |  |  |  | 
 |  |  |                   if (!dynamicTask.contains(talkKey)) { | 
 |  |  |                      dynamicTask.startDelay(talkKey, this::executeSave, 1000); | 
 |  |  | 
 |  |  |    } | 
 |  |  |  | 
 |  |  |    private void executeSave(){ | 
 |  |  |       System.out.println("定时存储数据"); | 
 |  |  |       executeSaveForUpdate(); | 
 |  |  |       executeSaveForDelete(); | 
 |  |  |       executeSaveForOnline(); | 
 |  |  |       executeSaveForOffline(); | 
 |  |  |       try { | 
 |  |  |          executeSaveForAdd(); | 
 |  |  |       } catch (Exception e) { | 
 |  |  |          logger.error("[存储收到的增加通道] 异常: ", e ); | 
 |  |  |       } | 
 |  |  |       try { | 
 |  |  |          executeSaveForUpdate(); | 
 |  |  |       } catch (Exception e) { | 
 |  |  |          logger.error("[存储收到的更新通道] 异常: ", e ); | 
 |  |  |       } | 
 |  |  |       try { | 
 |  |  |          executeSaveForDelete(); | 
 |  |  |       } catch (Exception e) { | 
 |  |  |          logger.error("[存储收到的删除通道] 异常: ", e ); | 
 |  |  |       } | 
 |  |  |       try { | 
 |  |  |          executeSaveForOnline(); | 
 |  |  |       } catch (Exception e) { | 
 |  |  |          logger.error("[存储收到的通道上线] 异常: ", e ); | 
 |  |  |       } | 
 |  |  |       try { | 
 |  |  |          executeSaveForOffline(); | 
 |  |  |       } catch (Exception e) { | 
 |  |  |          logger.error("[存储收到的通道离线] 异常: ", e ); | 
 |  |  |       } | 
 |  |  |       dynamicTask.stop(talkKey); | 
 |  |  |    } | 
 |  |  |  | 
 |  |  |    private void executeSaveForUpdate(){ | 
 |  |  |       if (updateChannelMap.values().size() > 0) { | 
 |  |  |       if (!updateChannelMap.values().isEmpty()) { | 
 |  |  |          ArrayList<DeviceChannel> deviceChannels = new ArrayList<>(updateChannelMap.values()); | 
 |  |  |          updateChannelMap.clear(); | 
 |  |  |          deviceChannelService.batchUpdateChannel(deviceChannels); | 
 |  |  | 
 |  |  |    } | 
 |  |  |  | 
 |  |  |    private void executeSaveForAdd(){ | 
 |  |  |       if (addChannelMap.values().size() > 0) { | 
 |  |  |       if (!addChannelMap.values().isEmpty()) { | 
 |  |  |          ArrayList<DeviceChannel> deviceChannels = new ArrayList<>(addChannelMap.values()); | 
 |  |  |          addChannelMap.clear(); | 
 |  |  |          deviceChannelService.batchAddChannel(deviceChannels); | 
 |  |  | 
 |  |  |    } | 
 |  |  |  | 
 |  |  |    private void executeSaveForDelete(){ | 
 |  |  |       if (deleteChannelList.size() > 0) { | 
 |  |  |       if (!deleteChannelList.isEmpty()) { | 
 |  |  |          deviceChannelService.deleteChannels(deleteChannelList); | 
 |  |  |          deleteChannelList.clear(); | 
 |  |  |       } | 
 |  |  |    } | 
 |  |  |  | 
 |  |  |    private void executeSaveForOnline(){ | 
 |  |  |       if (updateChannelOnlineList.size() > 0) { | 
 |  |  |       if (!updateChannelOnlineList.isEmpty()) { | 
 |  |  |          deviceChannelService.channelsOnline(updateChannelOnlineList); | 
 |  |  |          updateChannelOnlineList.clear(); | 
 |  |  |       } | 
 |  |  |    } | 
 |  |  |  | 
 |  |  |    private void executeSaveForOffline(){ | 
 |  |  |       if (updateChannelOfflineList.size() > 0) { | 
 |  |  |       if (!updateChannelOfflineList.isEmpty()) { | 
 |  |  |          deviceChannelService.channelsOffline(updateChannelOfflineList); | 
 |  |  |          updateChannelOfflineList.clear(); | 
 |  |  |       } |