From 45c076da2d5be1cad89a9a7e1c0dc2d3755bd7b5 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 30 Jun 2022 09:15:19 -0700 Subject: Fix online device removal --- .../org/traccar/session/cache/CacheManager.java | 56 +++++++++++----------- 1 file changed, 29 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/main/java/org/traccar/session/cache/CacheManager.java b/src/main/java/org/traccar/session/cache/CacheManager.java index bc5fc357f..9d1db7d14 100644 --- a/src/main/java/org/traccar/session/cache/CacheManager.java +++ b/src/main/java/org/traccar/session/cache/CacheManager.java @@ -268,38 +268,40 @@ public class CacheManager implements BroadcastInterface { Device device = storage.getObject(Device.class, new Request( new Columns.All(), new Condition.Equals("id", "id", deviceId))); - addObject(deviceId, device); + if (device != null) { + addObject(deviceId, device); + + for (Class clazz : CLASSES) { + var objects = storage.getObjects(clazz, new Request( + new Columns.All(), new Condition.Permission(Device.class, deviceId, clazz))); + links.put(clazz, objects.stream().map(BaseModel::getId).collect(Collectors.toList())); + objects.forEach(object -> addObject(deviceId, object)); + } - for (Class clazz : CLASSES) { - var objects = storage.getObjects(clazz, new Request( - new Columns.All(), new Condition.Permission(Device.class, deviceId, clazz))); - links.put(clazz, objects.stream().map(BaseModel::getId).collect(Collectors.toList())); - objects.forEach(object -> addObject(deviceId, object)); - } + var users = storage.getObjects(User.class, new Request( + new Columns.All(), new Condition.Permission(User.class, Device.class, deviceId))); + links.put(User.class, users.stream().map(BaseModel::getId).collect(Collectors.toList())); + for (var user : users) { + addObject(deviceId, user); + var notifications = storage.getObjects(Notification.class, new Request( + new Columns.All(), new Condition.Permission(User.class, user.getId(), Notification.class))); + notifications.stream() + .filter(Notification::getAlways) + .forEach(object -> { + links.computeIfAbsent(Notification.class, k -> new LinkedList<>()).add(object.getId()); + addObject(deviceId, object); + }); + } - var users = storage.getObjects(User.class, new Request( - new Columns.All(), new Condition.Permission(User.class, Device.class, deviceId))); - links.put(User.class, users.stream().map(BaseModel::getId).collect(Collectors.toList())); - for (var user : users) { - addObject(deviceId, user); - var notifications = storage.getObjects(Notification.class, new Request( - new Columns.All(), new Condition.Permission(User.class, user.getId(), Notification.class))); - notifications.stream() - .filter(Notification::getAlways) - .forEach(object -> { - links.computeIfAbsent(Notification.class, k -> new LinkedList<>()).add(object.getId()); - addObject(deviceId, object); - }); - } + deviceLinks.put(deviceId, links); - deviceLinks.put(deviceId, links); + if (device.getPositionId() > 0) { + devicePositions.put(deviceId, storage.getObject(Position.class, new Request( + new Columns.All(), new Condition.Equals("id", "id", device.getPositionId())))); + } - if (device.getPositionId() > 0) { - devicePositions.put(deviceId, storage.getObject(Position.class, new Request( - new Columns.All(), new Condition.Equals("id", "id", device.getPositionId())))); + invalidateDeviceGeofences(device); } - - invalidateDeviceGeofences(device); } private void unsafeRemoveDevice(long deviceId) { -- cgit v1.2.3