From b588b3c723cad4629dcecbce8983933f7ff2a255 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 14 Jun 2016 18:05:05 +0500 Subject: - Overlapping geofences - Simplified user-device link --- src/org/traccar/database/GeofenceManager.java | 65 ++++++++++++++++++--------- 1 file changed, 43 insertions(+), 22 deletions(-) (limited to 'src/org/traccar/database/GeofenceManager.java') diff --git a/src/org/traccar/database/GeofenceManager.java b/src/org/traccar/database/GeofenceManager.java index 64afc876c..b551bc467 100644 --- a/src/org/traccar/database/GeofenceManager.java +++ b/src/org/traccar/database/GeofenceManager.java @@ -1,20 +1,24 @@ package org.traccar.database; import java.sql.SQLException; +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; +import org.traccar.Context; import org.traccar.helper.Log; import org.traccar.model.Device; import org.traccar.model.Geofence; import org.traccar.model.GroupGeofence; -import org.traccar.model.UserDeviceGeofence; +import org.traccar.model.Position; +import org.traccar.model.DeviceGeofence; import org.traccar.model.GeofencePermission; public class GeofenceManager { @@ -25,8 +29,8 @@ public class GeofenceManager { private final Map> userGeofences = new HashMap<>(); private final Map> groupGeofences = new HashMap<>(); + private final Map> deviceGeofencesWithGroups = new HashMap<>(); private final Map> deviceGeofences = new HashMap<>(); - private final Map>> userDeviceGeofences = new HashMap<>(); private final ReadWriteLock deviceGeofencesLock = new ReentrantReadWriteLock(); private final ReadWriteLock geofencesLock = new ReentrantReadWriteLock(); @@ -54,17 +58,17 @@ public class GeofenceManager { public Set getAllDeviceGeofences(long deviceId) { deviceGeofencesLock.readLock().lock(); try { - return getDeviceGeofences(deviceGeofences, deviceId); + return getDeviceGeofences(deviceGeofencesWithGroups, deviceId); } finally { deviceGeofencesLock.readLock().unlock(); } } - public Set getUserDeviceGeofences(long userId, long deviceId) { + public Set getDeviceGeofences(long deviceId) { deviceGeofencesLock.readLock().lock(); try { - return getUserDeviceGeofencesUnlocked(userId, deviceId); + return getDeviceGeofences(deviceGeofences, deviceId); } finally { deviceGeofencesLock.readLock().unlock(); } @@ -77,13 +81,6 @@ public class GeofenceManager { return deviceGeofences.get(deviceId); } - private Set getUserDeviceGeofencesUnlocked(long userId, long deviceId) { - if (!userDeviceGeofences.containsKey(userId)) { - userDeviceGeofences.put(userId, new HashMap>()); - } - return getDeviceGeofences(userDeviceGeofences.get(userId), deviceId); - } - public final void refresh() { if (dataManager != null) { try { @@ -107,24 +104,38 @@ public class GeofenceManager { } deviceGeofences.clear(); + deviceGeofencesWithGroups.clear(); - for (Map.Entry>> deviceGeofence : userDeviceGeofences.entrySet()) { - deviceGeofence.getValue().clear(); + for (DeviceGeofence deviceGeofence : dataManager.getDeviceGeofences()) { + getDeviceGeofences(deviceGeofences, deviceGeofence.getDeviceId()) + .add(deviceGeofence.getGeofenceId()); + getDeviceGeofences(deviceGeofencesWithGroups, deviceGeofence.getDeviceId()) + .add(deviceGeofence.getGeofenceId()); } - userDeviceGeofences.clear(); - for (UserDeviceGeofence userDeviceGeofence : dataManager.getUserDeviceGeofences()) { - getDeviceGeofences(deviceGeofences, userDeviceGeofence.getDeviceId()) - .add(userDeviceGeofence.getGeofenceId()); - getUserDeviceGeofencesUnlocked(userDeviceGeofence.getUserId(), userDeviceGeofence.getDeviceId()) - .add(userDeviceGeofence.getGeofenceId()); - } for (Device device : dataManager.getAllDevices()) { long groupId = device.getGroupId(); while (groupId != 0) { - getDeviceGeofences(deviceGeofences, device.getId()).addAll(getGroupGeofences(groupId)); + getDeviceGeofences(deviceGeofencesWithGroups, + device.getId()).addAll(getGroupGeofences(groupId)); groupId = dataManager.getGroupById(groupId).getGroupId(); } + List deviceGeofenceIds = device.getGeofenceIds(); + if (deviceGeofenceIds == null) { + deviceGeofenceIds = new ArrayList(); + } else { + deviceGeofenceIds.clear(); + } + Position lastPosition = Context.getConnectionManager().getLastPosition(device.getId()); + if (lastPosition != null) { + for (Long geofenceId : deviceGeofencesWithGroups.get(device.getId())) { + if (getGeofence(geofenceId).getGeometry() + .containsPoint(lastPosition.getLatitude(), lastPosition.getLongitude())) { + deviceGeofenceIds.add(geofenceId); + } + } + } + device.setGeofenceIds(deviceGeofenceIds); } } finally { @@ -188,4 +199,14 @@ public class GeofenceManager { return getUserGeofencesIds(userId).contains(geofenceId); } + public List getCurrentDeviceGeofences(Position position) { + List result = new ArrayList(); + for (Long geofenceId : getAllDeviceGeofences(position.getDeviceId())) { + if (getGeofence(geofenceId).getGeometry().containsPoint(position.getLatitude(), position.getLongitude())) { + result.add(geofenceId); + } + } + return result; + } + } -- cgit v1.2.3