|  |  | 
 |  |  | package com.genersoft.iot.vmp.gb28181.event.online;
 | 
 |  |  | 
 | 
 |  |  | 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.event.EventPublisher;
 | 
 |  |  | import com.genersoft.iot.vmp.gb28181.event.subscribe.catalog.CatalogEvent;
 | 
 |  |  | import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
 | 
 |  |  | import com.genersoft.iot.vmp.service.IDeviceService;
 | 
 |  |  | import org.slf4j.Logger;
 | 
 |  |  | import org.slf4j.LoggerFactory;
 | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
 |  |  | 
 |  |  | import org.springframework.stereotype.Component;
 | 
 |  |  | 
 | 
 |  |  | import com.genersoft.iot.vmp.common.VideoManagerConstants;
 | 
 |  |  | import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
 | 
 |  |  | import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
 | 
 |  |  | import com.genersoft.iot.vmp.utils.redis.RedisUtil;
 | 
 |  |  | 
 | 
 |  |  | import java.text.SimpleDateFormat;
 | 
 |  |  | import java.util.List;
 | 
 |  |  | 
 | 
 |  |  | /**
 | 
 |  |  |  * @description: 在线事件监听器,监听到离线后,修改设备离在线状态。 设备在线有两个来源:
 | 
 |  |  |  *               1、设备主动注销,发送注销指令,{@link com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.RegisterRequestProcessor}
 | 
 |  |  |  *               2、设备未知原因离线,心跳超时,{@link com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.MessageRequestProcessor}
 | 
 |  |  |  *               1、设备主动注销,发送注销指令
 | 
 |  |  |  *               2、设备未知原因离线,心跳超时
 | 
 |  |  |  * @author: swwheihei
 | 
 |  |  |  * @date: 2020年5月6日 下午1:51:23
 | 
 |  |  |  */
 | 
 |  |  | 
 |  |  |    private final static Logger logger = LoggerFactory.getLogger(OnlineEventListener.class);
 | 
 |  |  | 
 | 
 |  |  |    @Autowired
 | 
 |  |  |    private IVideoManagerStorager storager;
 | 
 |  |  |    private IVideoManagerStorage storager;
 | 
 |  |  | 
 | 
 |  |  |    @Autowired
 | 
 |  |  |    private IDeviceService deviceService;
 | 
 |  |  |    
 | 
 |  |  |    @Autowired
 | 
 |  |  |     private RedisUtil redis;
 | 
 |  |  | 
 |  |  |    @Autowired
 | 
 |  |  |     private SipConfig sipConfig;
 | 
 |  |  | 
 | 
 |  |  |    @Autowired
 | 
 |  |  |     private UserSetting userSetting;
 | 
 |  |  | 
 | 
 |  |  |    @Autowired
 | 
 |  |  |     private EventPublisher eventPublisher;
 | 
 |  |  | 
 | 
 |  |  |    @Autowired
 | 
 |  |  |    private SIPCommander cmder;
 | 
 |  |  | 
 | 
 |  |  | 
 | 
 |  |  |    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 | 
 |  |  | 
 | 
 |  |  |    @Override
 | 
 |  |  |    public void onApplicationEvent(OnlineEvent event) {
 | 
 |  |  | 		 | 
 |  |  |       if (logger.isDebugEnabled()) {
 | 
 |  |  |          logger.debug("设备上线事件触发,deviceId:" + event.getDevice().getDeviceId() + ",from:" + event.getFrom());
 | 
 |  |  |       }
 | 
 |  |  |       Device device = event.getDevice();
 | 
 |  |  |       String key = VideoManagerConstants.KEEPLIVEKEY_PREFIX + event.getDevice().getDeviceId();
 | 
 |  |  | 
 | 
 |  |  |       logger.info("设备上线事件触发,deviceId:" + event.getDevice().getDeviceId() + ",from:" + event.getFrom());
 | 
 |  |  |       Device device = event.getDevice();
 | 
 |  |  |       if (device == null) return;
 | 
 |  |  |       String key = VideoManagerConstants.KEEPLIVEKEY_PREFIX + userSetting.getServerId() + "_" + event.getDevice().getDeviceId();
 | 
 |  |  |       Device deviceInStore = storager.queryVideoDevice(device.getDeviceId());
 | 
 |  |  |       device.setOnline(1);
 | 
 |  |  |       switch (event.getFrom()) {
 | 
 |  |  |       // 注册时触发的在线事件,先在redis中增加超时超时监听
 | 
 |  |  |       case VideoManagerConstants.EVENT_ONLINE_REGISTER:
 | 
 |  |  |          // 超时时间
 | 
 |  |  |          redis.set(key, event.getDevice().getDeviceId(), sipConfig.getKeepaliveTimeOut());
 | 
 |  |  |          device.setRegisterTime(format.format(System.currentTimeMillis()));
 | 
 |  |  |          if (deviceInStore == null) { //第一次上线
 | 
 |  |  |             logger.info("[{}] 首次注册,查询设备信息以及通道信息", device.getDeviceId());
 | 
 |  |  |             cmder.deviceInfoQuery(device);
 | 
 |  |  |             deviceService.sync(device);
 | 
 |  |  |          }
 | 
 |  |  |          break;
 | 
 |  |  |       // 设备主动发送心跳触发的在线事件
 | 
 |  |  |       case VideoManagerConstants.EVENT_ONLINE_KEEPLIVE:
 | 
 |  |  | 
 |  |  | 
 | 
 |  |  |          break;
 | 
 |  |  |       }
 | 
 |  |  | 
 | 
 |  |  |       device.setOnline(1);
 | 
 |  |  |       // 处理上线监听
 | 
 |  |  |       storager.updateDevice(device);
 | 
 |  |  | 
 | 
 |  |  |       // TODO 上线添加订阅
 | 
 |  |  | 
 | 
 |  |  |       // 上线添加订阅
 | 
 |  |  |       if (device.getSubscribeCycleForCatalog() > 0) {
 | 
 |  |  |          // 查询在线设备那些开启了订阅,为设备开启定时的目录订阅
 | 
 |  |  |          deviceService.addCatalogSubscribe(device);
 | 
 |  |  |       }
 | 
 |  |  |       if (device.getSubscribeCycleForMobilePosition() > 0) {
 | 
 |  |  |          deviceService.addMobilePositionSubscribe(device);
 | 
 |  |  |       }
 | 
 |  |  |    }
 | 
 |  |  | }
 |