|  |  | 
 |  |  | package com.genersoft.iot.vmp.gb28181.event.online;
 | 
 |  |  | 
 | 
 |  |  | import com.genersoft.iot.vmp.conf.SipConfig;
 | 
 |  |  | import com.genersoft.iot.vmp.gb28181.bean.Device;
 | 
 |  |  | import org.slf4j.Logger;
 | 
 |  |  | import org.slf4j.LoggerFactory;
 | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
 |  |  | 
 |  |  | import com.genersoft.iot.vmp.common.VideoManagerConstants;
 | 
 |  |  | import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
 | 
 |  |  | import com.genersoft.iot.vmp.utils.redis.RedisUtil;
 | 
 |  |  | 
 | 
 |  |  | import java.text.SimpleDateFormat;
 | 
 |  |  | import java.util.Date;
 | 
 |  |  | 
 | 
 |  |  | /**
 | 
 |  |  |  * @Description: 在线事件监听器,监听到离线后,修改设备离在线状态。 设备在线有两个来源:
 | 
 |  |  | 
 |  |  |    @Autowired
 | 
 |  |  |     private RedisUtil redis;
 | 
 |  |  | 
 | 
 |  |  |    @Autowired
 | 
 |  |  |     private SipConfig sipConfig;
 | 
 |  |  | 
 | 
 |  |  |    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 | 
 |  |  | 
 | 
 |  |  |    @Override
 | 
 |  |  |    public void onApplicationEvent(OnlineEvent event) {
 | 
 |  |  |       
 | 
 |  |  |       if (logger.isDebugEnabled()) {
 | 
 |  |  |          logger.debug("设备离线事件触发,deviceId:" + event.getDeviceId() + ",from:" + event.getFrom());
 | 
 |  |  |          logger.debug("设备上线事件触发,deviceId:" + event.getDevice().getDeviceId() + ",from:" + event.getFrom());
 | 
 |  |  |       }
 | 
 |  |  | 		 | 
 |  |  |       String key = VideoManagerConstants.KEEPLIVEKEY_PREFIX + event.getDeviceId();
 | 
 |  |  |       boolean needUpdateStorager = false;
 | 
 |  |  |       Device device = event.getDevice();
 | 
 |  |  |       String key = VideoManagerConstants.KEEPLIVEKEY_PREFIX + event.getDevice().getDeviceId();
 | 
 |  |  | 
 | 
 |  |  |       switch (event.getFrom()) {
 | 
 |  |  |       // 注册时触发的在线事件,先在redis中增加超时超时监听
 | 
 |  |  |       case VideoManagerConstants.EVENT_ONLINE_REGISTER:
 | 
 |  |  |          // TODO 超时时间暂时写死为180秒
 | 
 |  |  |          redis.set(key, event.getDeviceId(), 180);
 | 
 |  |  |          needUpdateStorager = true;
 | 
 |  |  |          // 超时时间
 | 
 |  |  |          redis.set(key, event.getDevice().getDeviceId(), sipConfig.getKeepaliveTimeOut());
 | 
 |  |  |          device.setRegisterTime(format.format(new Date(System.currentTimeMillis())));
 | 
 |  |  |          break;
 | 
 |  |  |       // 设备主动发送心跳触发的离线事件
 | 
 |  |  |       // 设备主动发送心跳触发的在线事件
 | 
 |  |  |       case VideoManagerConstants.EVENT_ONLINE_KEEPLIVE:
 | 
 |  |  |          boolean exist = redis.hasKey(key);
 | 
 |  |  |          // 先判断是否还存在,当设备先心跳超时后又发送心跳时,redis没有监听,需要增加
 | 
 |  |  |          if (!exist) {
 | 
 |  |  |             needUpdateStorager = true;
 | 
 |  |  |             redis.set(key, event.getDeviceId(), 180);
 | 
 |  |  |             redis.set(key, event.getDevice().getDeviceId(), sipConfig.getKeepaliveTimeOut());
 | 
 |  |  |          } else {
 | 
 |  |  |             redis.expire(key, 180);
 | 
 |  |  |             redis.expire(key, sipConfig.getKeepaliveTimeOut());
 | 
 |  |  |          }
 | 
 |  |  |          device.setKeepaliveTime(format.format(new Date(System.currentTimeMillis())));
 | 
 |  |  |          break;
 | 
 |  |  |       // 设备主动发送消息触发的在线事件
 | 
 |  |  |       case VideoManagerConstants.EVENT_ONLINE_MESSAGE:
 | 
 |  |  | 
 | 
 |  |  |          break;
 | 
 |  |  |       }
 | 
 |  |  | 		 | 
 |  |  |       if (needUpdateStorager) {
 | 
 |  |  |          // 处理离线监听
 | 
 |  |  |          storager.online(event.getDeviceId());
 | 
 |  |  |       }
 | 
 |  |  | 
 | 
 |  |  |       device.setOnline(1);
 | 
 |  |  |       // 处理上线监听
 | 
 |  |  |       storager.updateDevice(device);
 | 
 |  |  |    }
 | 
 |  |  | }
 |