Commit bdc0f83e29281bb46c5fe7a6eb6562f7f0e616af
1 parent
81da03d9
启动时清理无效的设备缓存数据,避免设备无法注册
Showing
6 changed files
with
59 additions
and
0 deletions
src/main/java/com/genersoft/iot/vmp/gb28181/task/SipRunner.java
| @@ -69,6 +69,26 @@ public class SipRunner implements CommandLineRunner { | @@ -69,6 +69,26 @@ public class SipRunner implements CommandLineRunner { | ||
| 69 | // 重置cseq计数 | 69 | // 重置cseq计数 |
| 70 | redisCatchStorage.resetAllCSEQ(); | 70 | redisCatchStorage.resetAllCSEQ(); |
| 71 | // 清理redis | 71 | // 清理redis |
| 72 | + // 清理数据库不存在但是redis中存在的数据 | ||
| 73 | + List<Device> devicesInDb = deviceService.getAll(); | ||
| 74 | + if (devicesInDb.size() == 0) { | ||
| 75 | + redisCatchStorage.removeAllDevice(); | ||
| 76 | + }else { | ||
| 77 | + List<Device> devicesInRedis = redisCatchStorage.getAllDevices(); | ||
| 78 | + if (devicesInRedis.size() > 0) { | ||
| 79 | + Map<String, Device> deviceMapInDb = new HashMap<>(); | ||
| 80 | + devicesInDb.parallelStream().forEach(device -> { | ||
| 81 | + deviceMapInDb.put(device.getDeviceId(), device); | ||
| 82 | + }); | ||
| 83 | + devicesInRedis.parallelStream().forEach(device -> { | ||
| 84 | + if (deviceMapInDb.get(device.getDeviceId()) == null) { | ||
| 85 | + redisCatchStorage.removeDevice(device.getDeviceId()); | ||
| 86 | + } | ||
| 87 | + }); | ||
| 88 | + } | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + | ||
| 72 | // 查找国标推流 | 92 | // 查找国标推流 |
| 73 | List<SendRtpItem> sendRtpItems = redisCatchStorage.queryAllSendRTPServer(); | 93 | List<SendRtpItem> sendRtpItems = redisCatchStorage.queryAllSendRTPServer(); |
| 74 | if (sendRtpItems.size() > 0) { | 94 | if (sendRtpItems.size() > 0) { |
src/main/java/com/genersoft/iot/vmp/service/IDeviceService.java
src/main/java/com/genersoft/iot/vmp/service/impl/DeviceServiceImpl.java
| @@ -631,4 +631,9 @@ public class DeviceServiceImpl implements IDeviceService { | @@ -631,4 +631,9 @@ public class DeviceServiceImpl implements IDeviceService { | ||
| 631 | public ResourceBaceInfo getOverview() { | 631 | public ResourceBaceInfo getOverview() { |
| 632 | return deviceMapper.getOverview(); | 632 | return deviceMapper.getOverview(); |
| 633 | } | 633 | } |
| 634 | + | ||
| 635 | + @Override | ||
| 636 | + public List<Device> getAll() { | ||
| 637 | + return deviceMapper.getAll(); | ||
| 638 | + } | ||
| 634 | } | 639 | } |
src/main/java/com/genersoft/iot/vmp/storager/IRedisCatchStorage.java
| @@ -258,4 +258,7 @@ public interface IRedisCatchStorage { | @@ -258,4 +258,7 @@ public interface IRedisCatchStorage { | ||
| 258 | 258 | ||
| 259 | List<SendRtpItem> queryAllSendRTPServer(); | 259 | List<SendRtpItem> queryAllSendRTPServer(); |
| 260 | 260 | ||
| 261 | + List<Device> getAllDevices(); | ||
| 262 | + | ||
| 263 | + void removeAllDevice(); | ||
| 261 | } | 264 | } |
src/main/java/com/genersoft/iot/vmp/storager/dao/DeviceMapper.java
| @@ -280,4 +280,6 @@ public interface DeviceMapper { | @@ -280,4 +280,6 @@ public interface DeviceMapper { | ||
| 280 | @Select("select count(1) as total, sum(online) as online from device") | 280 | @Select("select count(1) as total, sum(online) as online from device") |
| 281 | ResourceBaceInfo getOverview(); | 281 | ResourceBaceInfo getOverview(); |
| 282 | 282 | ||
| 283 | + @Select("select * from device") | ||
| 284 | + List<Device> getAll(); | ||
| 283 | } | 285 | } |
src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java
| @@ -665,6 +665,31 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { | @@ -665,6 +665,31 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { | ||
| 665 | } | 665 | } |
| 666 | 666 | ||
| 667 | @Override | 667 | @Override |
| 668 | + public void removeAllDevice() { | ||
| 669 | + String scanKey = VideoManagerConstants.DEVICE_PREFIX + userSetting.getServerId() + "_*"; | ||
| 670 | + List<Object> keys = RedisUtil.scan(scanKey); | ||
| 671 | + for (Object key : keys) { | ||
| 672 | + RedisUtil.del((String) key); | ||
| 673 | + } | ||
| 674 | + } | ||
| 675 | + | ||
| 676 | + @Override | ||
| 677 | + public List<Device> getAllDevices() { | ||
| 678 | + String scanKey = VideoManagerConstants.DEVICE_PREFIX + userSetting.getServerId() + "_*"; | ||
| 679 | + List<Device> result = new ArrayList<>(); | ||
| 680 | + List<Object> keys = RedisUtil.scan(scanKey); | ||
| 681 | + for (Object o : keys) { | ||
| 682 | + String key = (String) o; | ||
| 683 | + Device device = JsonUtil.redisJsonToObject(key, Device.class); | ||
| 684 | + if (Objects.nonNull(device)) { // 只取没有存过得 | ||
| 685 | + result.add(JsonUtil.redisJsonToObject(key, Device.class)); | ||
| 686 | + } | ||
| 687 | + } | ||
| 688 | + | ||
| 689 | + return result; | ||
| 690 | + } | ||
| 691 | + | ||
| 692 | + @Override | ||
| 668 | public Device getDevice(String deviceId) { | 693 | public Device getDevice(String deviceId) { |
| 669 | String key = VideoManagerConstants.DEVICE_PREFIX + userSetting.getServerId() + "_" + deviceId; | 694 | String key = VideoManagerConstants.DEVICE_PREFIX + userSetting.getServerId() + "_" + deviceId; |
| 670 | return JsonUtil.redisJsonToObject(key, Device.class); | 695 | return JsonUtil.redisJsonToObject(key, Device.class); |