diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/task/SipRunner.java b/src/main/java/com/genersoft/iot/vmp/gb28181/task/SipRunner.java index 451c589c..2ffec099 100644 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/task/SipRunner.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/task/SipRunner.java @@ -69,6 +69,26 @@ public class SipRunner implements CommandLineRunner { // 重置cseq计数 redisCatchStorage.resetAllCSEQ(); // 清理redis + // 清理数据库不存在但是redis中存在的数据 + List devicesInDb = deviceService.getAll(); + if (devicesInDb.size() == 0) { + redisCatchStorage.removeAllDevice(); + }else { + List devicesInRedis = redisCatchStorage.getAllDevices(); + if (devicesInRedis.size() > 0) { + Map deviceMapInDb = new HashMap<>(); + devicesInDb.parallelStream().forEach(device -> { + deviceMapInDb.put(device.getDeviceId(), device); + }); + devicesInRedis.parallelStream().forEach(device -> { + if (deviceMapInDb.get(device.getDeviceId()) == null) { + redisCatchStorage.removeDevice(device.getDeviceId()); + } + }); + } + } + + // 查找国标推流 List sendRtpItems = redisCatchStorage.queryAllSendRTPServer(); if (sendRtpItems.size() > 0) { diff --git a/src/main/java/com/genersoft/iot/vmp/service/IDeviceService.java b/src/main/java/com/genersoft/iot/vmp/service/IDeviceService.java index b87c9a73..072519bd 100644 --- a/src/main/java/com/genersoft/iot/vmp/service/IDeviceService.java +++ b/src/main/java/com/genersoft/iot/vmp/service/IDeviceService.java @@ -163,4 +163,8 @@ public interface IDeviceService { */ ResourceBaceInfo getOverview(); + /** + * 获取所有设备 + */ + List getAll(); } diff --git a/src/main/java/com/genersoft/iot/vmp/service/impl/DeviceServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/service/impl/DeviceServiceImpl.java index 7bb9619b..d1e7c0b4 100644 --- a/src/main/java/com/genersoft/iot/vmp/service/impl/DeviceServiceImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/service/impl/DeviceServiceImpl.java @@ -631,4 +631,9 @@ public class DeviceServiceImpl implements IDeviceService { public ResourceBaceInfo getOverview() { return deviceMapper.getOverview(); } + + @Override + public List getAll() { + return deviceMapper.getAll(); + } } diff --git a/src/main/java/com/genersoft/iot/vmp/storager/IRedisCatchStorage.java b/src/main/java/com/genersoft/iot/vmp/storager/IRedisCatchStorage.java index 15b6d807..1e10469f 100644 --- a/src/main/java/com/genersoft/iot/vmp/storager/IRedisCatchStorage.java +++ b/src/main/java/com/genersoft/iot/vmp/storager/IRedisCatchStorage.java @@ -258,4 +258,7 @@ public interface IRedisCatchStorage { List queryAllSendRTPServer(); + List getAllDevices(); + + void removeAllDevice(); } diff --git a/src/main/java/com/genersoft/iot/vmp/storager/dao/DeviceMapper.java b/src/main/java/com/genersoft/iot/vmp/storager/dao/DeviceMapper.java index 8143d35a..0aed8207 100644 --- a/src/main/java/com/genersoft/iot/vmp/storager/dao/DeviceMapper.java +++ b/src/main/java/com/genersoft/iot/vmp/storager/dao/DeviceMapper.java @@ -280,4 +280,6 @@ public interface DeviceMapper { @Select("select count(1) as total, sum(online) as online from device") ResourceBaceInfo getOverview(); + @Select("select * from device") + List getAll(); } diff --git a/src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java b/src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java index e997e4d5..4f229d78 100644 --- a/src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java @@ -664,6 +664,31 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { RedisUtil.del(key); } + @Override + public void removeAllDevice() { + String scanKey = VideoManagerConstants.DEVICE_PREFIX + userSetting.getServerId() + "_*"; + List keys = RedisUtil.scan(scanKey); + for (Object key : keys) { + RedisUtil.del((String) key); + } + } + + @Override + public List getAllDevices() { + String scanKey = VideoManagerConstants.DEVICE_PREFIX + userSetting.getServerId() + "_*"; + List result = new ArrayList<>(); + List keys = RedisUtil.scan(scanKey); + for (Object o : keys) { + String key = (String) o; + Device device = JsonUtil.redisJsonToObject(key, Device.class); + if (Objects.nonNull(device)) { // 只取没有存过得 + result.add(JsonUtil.redisJsonToObject(key, Device.class)); + } + } + + return result; + } + @Override public Device getDevice(String deviceId) { String key = VideoManagerConstants.DEVICE_PREFIX + userSetting.getServerId() + "_" + deviceId;