From 5bd6e800c8e9c24dcaec5cbcf75b7cc808982717 Mon Sep 17 00:00:00 2001 From: shikong <919411476@qq.com> Date: Sat, 12 Aug 2023 16:14:36 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E5=A4=87=E6=B3=A8=E5=86=8C=20?= =?UTF-8?q?=E6=9C=AA=E5=AE=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../request/RegisterRequestProcessor.java | 2 ++ .../docking/device/DockingDeviceService.java | 36 ++++++++++++++++--- .../cache/DeviceOnlineCacheService.java | 36 +++++++++++++++++++ .../cache/DockingDeviceCacheService.java | 11 ++++-- 4 files changed, 78 insertions(+), 7 deletions(-) create mode 100644 gb28181-service/src/main/java/cn/skcks/docking/gb28181/service/docking/device/cache/DeviceOnlineCacheService.java diff --git a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/processor/register/request/RegisterRequestProcessor.java b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/processor/register/request/RegisterRequestProcessor.java index 1e68fd5..da6603a 100644 --- a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/processor/register/request/RegisterRequestProcessor.java +++ b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/core/sip/message/processor/register/request/RegisterRequestProcessor.java @@ -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); diff --git a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/service/docking/device/DockingDeviceService.java b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/service/docking/device/DockingDeviceService.java index c7c47f0..9f95d33 100644 --- a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/service/docking/device/DockingDeviceService.java +++ b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/service/docking/device/DockingDeviceService.java @@ -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); } } diff --git a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/service/docking/device/cache/DeviceOnlineCacheService.java b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/service/docking/device/cache/DeviceOnlineCacheService.java new file mode 100644 index 0000000..eaf2fe4 --- /dev/null +++ b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/service/docking/device/cache/DeviceOnlineCacheService.java @@ -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); + } +} diff --git a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/service/docking/device/cache/DockingDeviceCacheService.java b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/service/docking/device/cache/DockingDeviceCacheService.java index 0dee178..628edec 100644 --- a/gb28181-service/src/main/java/cn/skcks/docking/gb28181/service/docking/device/cache/DockingDeviceCacheService.java +++ b/gb28181-service/src/main/java/cn/skcks/docking/gb28181/service/docking/device/cache/DockingDeviceCacheService.java @@ -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)); }