设备注册 未完

This commit is contained in:
shikong 2023-08-12 17:26:30 +08:00
parent 5bd6e800c8
commit e485263d02
5 changed files with 51 additions and 10 deletions

View File

@ -0,0 +1,13 @@
package cn.skcks.docking.gb28181.core.sip.gb28181.cache;
import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
@UtilityClass
public class CacheUtil {
public final static String SEPARATOR = ":";
public static String getKey(String prefix,String... ids){
return StringUtils.joinWith(SEPARATOR, (Object[]) ArrayUtils.addFirst(ids,prefix));
}
}

View File

@ -0,0 +1,18 @@
package cn.skcks.docking.gb28181.core.sip.gb28181.constant;
import lombok.experimental.UtilityClass;
import java.util.concurrent.TimeUnit;
@UtilityClass
public class DeviceConstant {
public final static int KEEP_ALIVE_INTERVAL = 60;
public final static TimeUnit UNIT = TimeUnit.SECONDS;
@UtilityClass
public class Cache {
public final static String DEVICE = "DEVICE";
public final static String ONLINE = "ONLINE";
}
}

View File

@ -1,5 +1,7 @@
package cn.skcks.docking.gb28181.service.docking.device;
import cn.hutool.core.date.DateUtil;
import cn.skcks.docking.gb28181.core.sip.gb28181.constant.DeviceConstant;
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;
@ -7,10 +9,10 @@ import cn.skcks.docking.gb28181.service.docking.device.cache.DeviceOnlineCacheSe
import cn.skcks.docking.gb28181.service.docking.device.cache.DockingDeviceCacheService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.math.NumberUtils;
import org.mybatis.dynamic.sql.SqlBuilder;
import org.springframework.stereotype.Service;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
@Slf4j
@ -42,7 +44,11 @@ public class DockingDeviceService {
public void online(DockingDevice device) {
String deviceId = device.getDeviceId();
log.info("[设备上线] deviceId => {}, {}://{}:{}", deviceId, device.getTransport(), device.getIp(), device.getPort());
device.setUpdateTime(DateUtil.now());
if (device.getKeepaliveIntervalTime() == null || device.getKeepaliveIntervalTime() == 0) {
// 默认心跳间隔60
device.setKeepaliveIntervalTime(DeviceConstant.KEEP_ALIVE_INTERVAL);
}
dockingDeviceMapper
.selectOne((s -> s.where(DockingDeviceDynamicSqlSupport.deviceId, SqlBuilder.isEqualTo(deviceId))))
.ifPresentOrElse((ignore -> {

View File

@ -2,6 +2,8 @@ package cn.skcks.docking.gb28181.service.docking.device.cache;
import cn.hutool.core.date.DateUtil;
import cn.skcks.docking.gb28181.common.redis.RedisUtil;
import cn.skcks.docking.gb28181.core.sip.gb28181.cache.CacheUtil;
import cn.skcks.docking.gb28181.core.sip.gb28181.constant.DeviceConstant;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
@ -13,10 +15,8 @@ import java.util.concurrent.TimeUnit;
@Service
@RequiredArgsConstructor
public class DeviceOnlineCacheService {
public final static String PREFIX = "ONLINE";
private String getKey(String deviceId) {
return StringUtils.joinWith(":", PREFIX, deviceId);
return CacheUtil.getKey(DeviceConstant.Cache.ONLINE,deviceId);
}
public boolean isOnline(String deviceId){
@ -26,7 +26,7 @@ public class DeviceOnlineCacheService {
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);
RedisUtil.KeyOps.expire(key, DeviceConstant.KEEP_ALIVE_INTERVAL * 3, DeviceConstant.UNIT);
}
public void setOffline(String deviceId){

View File

@ -2,6 +2,8 @@ package cn.skcks.docking.gb28181.service.docking.device.cache;
import cn.skcks.docking.gb28181.common.json.JsonUtils;
import cn.skcks.docking.gb28181.common.redis.RedisUtil;
import cn.skcks.docking.gb28181.core.sip.gb28181.cache.CacheUtil;
import cn.skcks.docking.gb28181.core.sip.gb28181.constant.DeviceConstant;
import cn.skcks.docking.gb28181.orm.mybatis.dynamic.model.DockingDevice;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@ -12,10 +14,12 @@ import org.springframework.stereotype.Service;
@RequiredArgsConstructor
@Service
public class DockingDeviceCacheService {
public static final String PREFIX = "DEVICE";
private String getKey(String deviceId) {
return CacheUtil.getKey(DeviceConstant.Cache.DEVICE,deviceId);
}
public DockingDevice getDevice(String deviceId) {
String key = StringUtils.joinWith(":", PREFIX, deviceId);
String key = getKey(deviceId);
String json = RedisUtil.StringOps.get(key);
if (json == null){
return null;
@ -24,12 +28,12 @@ public class DockingDeviceCacheService {
}
public void removeDevice(String deviceId) {
String key = StringUtils.joinWith(":", PREFIX, deviceId);
String key = getKey(deviceId);
RedisUtil.KeyOps.delete(key);
}
public void cacheDevice(String deviceId, DockingDevice device) {
String key = StringUtils.joinWith(":", PREFIX, deviceId);
String key = getKey(deviceId);
RedisUtil.StringOps.set(key,JsonUtils.toCompressJson(device));
}
}