设备信息增加最近注册时间和最近心跳时间,心跳超时时间变为可配置
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.genersoft.iot.vmp.gb28181.event;
|
||||
|
||||
import com.genersoft.iot.vmp.gb28181.bean.Device;
|
||||
import com.genersoft.iot.vmp.gb28181.event.platformKeepaliveExpire.PlatformKeepaliveExpireEvent;
|
||||
import com.genersoft.iot.vmp.gb28181.event.platformNotRegister.PlatformNotRegisterEvent;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -22,9 +23,9 @@ public class EventPublisher {
|
||||
@Autowired
|
||||
private ApplicationEventPublisher applicationEventPublisher;
|
||||
|
||||
public void onlineEventPublish(String deviceId, String from) {
|
||||
public void onlineEventPublish(Device device, String from) {
|
||||
OnlineEvent onEvent = new OnlineEvent(this);
|
||||
onEvent.setDeviceId(deviceId);
|
||||
onEvent.setDevice(device);
|
||||
onEvent.setFrom(from);
|
||||
applicationEventPublisher.publishEvent(onEvent);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.genersoft.iot.vmp.gb28181.event.online;
|
||||
|
||||
import com.genersoft.iot.vmp.gb28181.bean.Device;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
/**
|
||||
@@ -18,18 +19,18 @@ public class OnlineEvent extends ApplicationEvent {
|
||||
super(source);
|
||||
}
|
||||
|
||||
private String deviceId;
|
||||
private Device device;
|
||||
|
||||
private String from;
|
||||
|
||||
public String getDeviceId() {
|
||||
return deviceId;
|
||||
public Device getDevice() {
|
||||
return device;
|
||||
}
|
||||
|
||||
public void setDeviceId(String deviceId) {
|
||||
this.deviceId = deviceId;
|
||||
public void setDevice(Device device) {
|
||||
this.device = device;
|
||||
}
|
||||
|
||||
|
||||
public String getFrom() {
|
||||
return from;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
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;
|
||||
@@ -10,6 +12,9 @@ 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: 在线事件监听器,监听到离线后,修改设备离在线状态。 设备在线有两个来源:
|
||||
* 1、设备主动注销,发送注销指令,{@link com.genersoft.iot.vmp.gb28181.transmit.request.impl.RegisterRequestProcessor}
|
||||
@@ -28,39 +33,46 @@ public class OnlineEventListener implements ApplicationListener<OnlineEvent> {
|
||||
@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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user