From 45940f73c57b871711ee9fb41ebeba3c0ca9e9b6 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 18 Jun 2022 16:48:15 -0700 Subject: Fix offline device notifications --- .../org/traccar/database/NotificationManager.java | 13 +++++++++++-- .../java/org/traccar/session/ConnectionManager.java | 7 +++++-- .../java/org/traccar/session/cache/CacheManager.java | 19 ++++++++++++++++--- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/traccar/database/NotificationManager.java b/src/main/java/org/traccar/database/NotificationManager.java index 92f4c818a..1314a3d0a 100644 --- a/src/main/java/org/traccar/database/NotificationManager.java +++ b/src/main/java/org/traccar/database/NotificationManager.java @@ -118,8 +118,17 @@ public class NotificationManager { } public void updateEvents(Map events) { - for (Entry event : events.entrySet()) { - updateEvent(event.getKey(), event.getValue()); + for (Entry entry : events.entrySet()) { + Event event = entry.getKey(); + Position position = entry.getValue(); + try { + cacheManager.addDevice(event.getDeviceId()); + updateEvent(event, position); + } catch (StorageException e) { + throw new RuntimeException(e); + } finally { + cacheManager.removeDevice(event.getDeviceId()); + } } } } diff --git a/src/main/java/org/traccar/session/ConnectionManager.java b/src/main/java/org/traccar/session/ConnectionManager.java index 05c4893fd..fe6521d18 100644 --- a/src/main/java/org/traccar/session/ConnectionManager.java +++ b/src/main/java/org/traccar/session/ConnectionManager.java @@ -144,7 +144,10 @@ public class ConnectionManager { endpointSessions.put(device.getUniqueId(), deviceSession); sessionsByEndpoint.put(endpoint, endpointSessions); sessionsByDeviceId.put(device.getId(), deviceSession); - cacheManager.addDevice(device.getId()); + + if (oldSession == null) { + cacheManager.addDevice(device.getId()); + } return deviceSession; } else { @@ -190,8 +193,8 @@ public class ConnectionManager { public void deviceUnknown(long deviceId) { updateDevice(deviceId, Device.STATUS_UNKNOWN, null); DeviceSession deviceSession = sessionsByDeviceId.remove(deviceId); - cacheManager.removeDevice(deviceId); if (deviceSession != null) { + cacheManager.removeDevice(deviceId); Endpoint endpoint = new Endpoint(deviceSession.getChannel(), deviceSession.getRemoteAddress()); sessionsByEndpoint.computeIfPresent(endpoint, (e, sessions) -> { sessions.remove(deviceSession.getUniqueId()); diff --git a/src/main/java/org/traccar/session/cache/CacheManager.java b/src/main/java/org/traccar/session/cache/CacheManager.java index 87384f746..abc8ca4c9 100644 --- a/src/main/java/org/traccar/session/cache/CacheManager.java +++ b/src/main/java/org/traccar/session/cache/CacheManager.java @@ -60,6 +60,7 @@ public class CacheManager { private final ReadWriteLock lock = new ReentrantReadWriteLock(); private final Map deviceCache = new HashMap<>(); + private final Map deviceReferences = new HashMap<>(); private final Map, List>> deviceLinks = new HashMap<>(); private final Map devicePositions = new HashMap<>(); @@ -136,9 +137,14 @@ public class CacheManager { public void addDevice(long deviceId) throws StorageException { try { lock.writeLock().lock(); - if (!deviceLinks.containsKey(deviceId)) { + Integer references = deviceReferences.get(deviceId); + if (references != null) { + references += 1; + } else { unsafeAddDevice(deviceId); + references = 1; } + deviceReferences.put(deviceId, references); } finally { lock.writeLock().unlock(); } @@ -147,8 +153,15 @@ public class CacheManager { public void removeDevice(long deviceId) { try { lock.writeLock().lock(); - if (deviceLinks.containsKey(deviceId)) { - unsafeRemoveDevice(deviceId); + Integer references = deviceReferences.get(deviceId); + if (references != null) { + references -= 1; + if (references <= 0) { + unsafeRemoveDevice(deviceId); + deviceReferences.remove(deviceId); + } else { + deviceReferences.put(deviceId, references); + } } } finally { lock.writeLock().unlock(); -- cgit v1.2.3