设备注册 未完

This commit is contained in:
shikong 2023-08-12 16:14:36 +08:00
parent b5cb55c7ee
commit 5bd6e800c8
4 changed files with 78 additions and 7 deletions

View File

@ -132,7 +132,9 @@ public class RegisterRequestProcessor implements MessageProcessor {
device.setExpires(expires);
// expires == 0 注销
if (expires == 0) {
device.setOnLine(false);
log.info("设备注销 deviceId => {}", deviceId);
dockingDeviceService.offline(device);
} else {
device.setRegisterTime(DateUtil.now());
dockingDeviceService.online(device);

View File

@ -3,27 +3,37 @@ package cn.skcks.docking.gb28181.service.docking.device;
import cn.skcks.docking.gb28181.orm.mybatis.dynamic.mapper.DockingDeviceDynamicSqlSupport;
import cn.skcks.docking.gb28181.orm.mybatis.dynamic.mapper.DockingDeviceMapper;
import cn.skcks.docking.gb28181.orm.mybatis.dynamic.model.DockingDevice;
import cn.skcks.docking.gb28181.service.docking.device.cache.DeviceOnlineCacheService;
import cn.skcks.docking.gb28181.service.docking.device.cache.DockingDeviceCacheService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.dynamic.sql.SqlBuilder;
import org.springframework.stereotype.Service;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
@Slf4j
@Service
@RequiredArgsConstructor
public class DockingDeviceService {
private final DockingDeviceMapper dockingDeviceMapper;
private final DockingDeviceCacheService deviceCacheService;
private final DeviceOnlineCacheService onlineCacheService;
/**
* 根据设备Id 获取设备信息 并缓存
* @param deviceId 设备id
* @return 设备信息
*/
public DockingDevice getDeviceInfo(String deviceId) {
DockingDevice device = deviceCacheService.getDeviceInfo(deviceId);
DockingDevice device = deviceCacheService.getDevice(deviceId);
if (device == null) {
device = dockingDeviceMapper
.selectOne((s -> s.where(DockingDeviceDynamicSqlSupport.deviceId, SqlBuilder.isEqualTo(deviceId))))
.orElse(null);
if (device != null) {
deviceCacheService.cacheDeviceInfo(deviceId, device);
deviceCacheService.cacheDevice(deviceId, device);
}
}
return device;
@ -32,7 +42,25 @@ public class DockingDeviceService {
public void online(DockingDevice device) {
String deviceId = device.getDeviceId();
log.info("[设备上线] deviceId => {}, {}://{}:{}", deviceId, device.getTransport(), device.getIp(), device.getPort());
dockingDeviceMapper.insert(device);
deviceCacheService.cacheDeviceInfo(deviceId, device);
dockingDeviceMapper
.selectOne((s -> s.where(DockingDeviceDynamicSqlSupport.deviceId, SqlBuilder.isEqualTo(deviceId))))
.ifPresentOrElse((ignore -> {
dockingDeviceMapper.updateByPrimaryKey(device);
}),()->{
dockingDeviceMapper.insert(device);
});
getDeviceInfo(deviceId);
onlineCacheService.setOnline(deviceId, 180, TimeUnit.SECONDS);
}
public void offline(DockingDevice device){
String deviceId = device.getDeviceId();
dockingDeviceMapper.updateByPrimaryKey(device);
log.info("[设备离线] deviceId => {}", deviceId);
deviceCacheService.removeDevice(deviceId);
onlineCacheService.setOffline(deviceId);
}
}

View File

@ -0,0 +1,36 @@
package cn.skcks.docking.gb28181.service.docking.device.cache;
import cn.hutool.core.date.DateUtil;
import cn.skcks.docking.gb28181.common.redis.RedisUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Slf4j
@Service
@RequiredArgsConstructor
public class DeviceOnlineCacheService {
public final static String PREFIX = "ONLINE";
private String getKey(String deviceId) {
return StringUtils.joinWith(":", PREFIX, deviceId);
}
public boolean isOnline(String deviceId){
return RedisUtil.KeyOps.hasKey(getKey(deviceId));
}
public void setOnline(String deviceId, long time, TimeUnit unit){
String key = getKey(deviceId);
RedisUtil.StringOps.set(key, DateUtil.now());
RedisUtil.KeyOps.expire(key, time,unit);
}
public void setOffline(String deviceId){
String key = getKey(deviceId);
RedisUtil.KeyOps.delete(key);
}
}

View File

@ -12,9 +12,9 @@ import org.springframework.stereotype.Service;
@RequiredArgsConstructor
@Service
public class DockingDeviceCacheService {
public static final String PREFIX = "DockingDevice";
public static final String PREFIX = "DEVICE";
public DockingDevice getDeviceInfo(String deviceId) {
public DockingDevice getDevice(String deviceId) {
String key = StringUtils.joinWith(":", PREFIX, deviceId);
String json = RedisUtil.StringOps.get(key);
if (json == null){
@ -23,7 +23,12 @@ public class DockingDeviceCacheService {
return JsonUtils.parse(json, DockingDevice.class);
}
public void cacheDeviceInfo(String deviceId,DockingDevice device) {
public void removeDevice(String deviceId) {
String key = StringUtils.joinWith(":", PREFIX, deviceId);
RedisUtil.KeyOps.delete(key);
}
public void cacheDevice(String deviceId, DockingDevice device) {
String key = StringUtils.joinWith(":", PREFIX, deviceId);
RedisUtil.StringOps.set(key,JsonUtils.toCompressJson(device));
}