diff options
author | Abyss777 <abyss@fox5.ru> | 2016-06-14 18:05:05 +0500 |
---|---|---|
committer | Abyss777 <abyss@fox5.ru> | 2016-06-14 18:05:05 +0500 |
commit | b588b3c723cad4629dcecbce8983933f7ff2a255 (patch) | |
tree | ee1ed23c7c02ded8ca92c904e6f4f21aacfda8d8 /src/org/traccar/database | |
parent | 185c0830e17b6969977026d4be27e34878bb3db9 (diff) | |
download | trackermap-server-b588b3c723cad4629dcecbce8983933f7ff2a255.tar.gz trackermap-server-b588b3c723cad4629dcecbce8983933f7ff2a255.tar.bz2 trackermap-server-b588b3c723cad4629dcecbce8983933f7ff2a255.zip |
- Overlapping geofences
- Simplified user-device link
Diffstat (limited to 'src/org/traccar/database')
-rw-r--r-- | src/org/traccar/database/ConnectionManager.java | 19 | ||||
-rw-r--r-- | src/org/traccar/database/DataManager.java | 19 | ||||
-rw-r--r-- | src/org/traccar/database/GeofenceManager.java | 65 | ||||
-rw-r--r-- | src/org/traccar/database/NotificationManager.java | 42 |
4 files changed, 98 insertions, 47 deletions
diff --git a/src/org/traccar/database/ConnectionManager.java b/src/org/traccar/database/ConnectionManager.java index 6e47dfad3..4f12c44d4 100644 --- a/src/org/traccar/database/ConnectionManager.java +++ b/src/org/traccar/database/ConnectionManager.java @@ -94,7 +94,7 @@ public class ConnectionManager { if (status.equals(Device.STATUS_ONLINE)) { event.setType(Event.TYPE_DEVICE_ONLINE); } - updateEvent(event, null); + Context.getNotificationManager().updateEvent(event, null); } device.setStatus(status); @@ -147,19 +147,10 @@ public class ConnectionManager { } } - public synchronized void updateEvent(Event event, Position position) { - long deviceId = event.getDeviceId(); - try { - Context.getDataManager().addEvent(event); - } catch (SQLException error) { - Log.warning(error); - } - for (long userId : Context.getPermissionsManager().getDeviceUsers(deviceId)) { - if (listeners.containsKey(userId) && (event.getGeofenceId() == 0 - || Context.getGeofenceManager().checkGeofence(userId, event.getGeofenceId()))) { - for (UpdateListener listener : listeners.get(userId)) { - listener.onUpdateEvent(event, position); - } + public synchronized void updateEvent(long userId, Event event, Position position) { + if (listeners.containsKey(userId)) { + for (UpdateListener listener : listeners.get(userId)) { + listener.onUpdateEvent(event, position); } } } diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 30334b78b..2b09e2fb0 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -55,7 +55,7 @@ import org.traccar.model.GroupPermission; import org.traccar.model.Position; import org.traccar.model.Server; import org.traccar.model.User; -import org.traccar.model.UserDeviceGeofence; +import org.traccar.model.DeviceGeofence; import org.traccar.model.GeofencePermission; import com.zaxxer.hikari.HikariConfig; @@ -372,7 +372,6 @@ public class DataManager implements IdentityManager { Device cachedDevice = getDeviceById(device.getId()); cachedDevice.setStatus(device.getStatus()); cachedDevice.setMotion(device.getMotion()); - cachedDevice.setGeofenceId(device.getGeofenceId()); } public void removeDevice(long deviceId) throws SQLException { @@ -596,22 +595,20 @@ public class DataManager implements IdentityManager { .executeUpdate(); } - public Collection<UserDeviceGeofence> getUserDeviceGeofences() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectUserDeviceGeofences")) - .executeQuery(UserDeviceGeofence.class); + public Collection<DeviceGeofence> getDeviceGeofences() throws SQLException { + return QueryBuilder.create(dataSource, getQuery("database.selectDeviceGeofences")) + .executeQuery(DeviceGeofence.class); } - public void linkUserDeviceGeofence(long userId, long deviceId, long geofenceId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.linkUserDeviceGeofence")) - .setLong("userId", userId) + public void linkDeviceGeofence(long deviceId, long geofenceId) throws SQLException { + QueryBuilder.create(dataSource, getQuery("database.linkDeviceGeofence")) .setLong("deviceId", deviceId) .setLong("geofenceId", geofenceId) .executeUpdate(); } - public void unlinkUserDeviceGeofence(long userId, long deviceId, long geofenceId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.unlinkUserDeviceGeofence")) - .setLong("userId", userId) + public void unlinkDeviceGeofence(long deviceId, long geofenceId) throws SQLException { + QueryBuilder.create(dataSource, getQuery("database.unlinkDeviceGeofence")) .setLong("deviceId", deviceId) .setLong("geofenceId", geofenceId) .executeUpdate(); 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<Long, Set<Long>> userGeofences = new HashMap<>(); private final Map<Long, Set<Long>> groupGeofences = new HashMap<>(); + private final Map<Long, Set<Long>> deviceGeofencesWithGroups = new HashMap<>(); private final Map<Long, Set<Long>> deviceGeofences = new HashMap<>(); - private final Map<Long, Map<Long, Set<Long>>> userDeviceGeofences = new HashMap<>(); private final ReadWriteLock deviceGeofencesLock = new ReentrantReadWriteLock(); private final ReadWriteLock geofencesLock = new ReentrantReadWriteLock(); @@ -54,17 +58,17 @@ public class GeofenceManager { public Set<Long> getAllDeviceGeofences(long deviceId) { deviceGeofencesLock.readLock().lock(); try { - return getDeviceGeofences(deviceGeofences, deviceId); + return getDeviceGeofences(deviceGeofencesWithGroups, deviceId); } finally { deviceGeofencesLock.readLock().unlock(); } } - public Set<Long> getUserDeviceGeofences(long userId, long deviceId) { + public Set<Long> 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<Long> getUserDeviceGeofencesUnlocked(long userId, long deviceId) { - if (!userDeviceGeofences.containsKey(userId)) { - userDeviceGeofences.put(userId, new HashMap<Long, Set<Long>>()); - } - 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<Long, Map<Long, Set<Long>>> 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<Long> deviceGeofenceIds = device.getGeofenceIds(); + if (deviceGeofenceIds == null) { + deviceGeofenceIds = new ArrayList<Long>(); + } 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<Long> getCurrentDeviceGeofences(Position position) { + List<Long> result = new ArrayList<Long>(); + for (Long geofenceId : getAllDeviceGeofences(position.getDeviceId())) { + if (getGeofence(geofenceId).getGeometry().containsPoint(position.getLatitude(), position.getLongitude())) { + result.add(geofenceId); + } + } + return result; + } + } diff --git a/src/org/traccar/database/NotificationManager.java b/src/org/traccar/database/NotificationManager.java new file mode 100644 index 000000000..8c8e958c8 --- /dev/null +++ b/src/org/traccar/database/NotificationManager.java @@ -0,0 +1,42 @@ +package org.traccar.database; + +import java.sql.SQLException; +import java.util.Collection; +import java.util.Set; + +import org.traccar.Context; +import org.traccar.helper.Log; +import org.traccar.model.Event; +import org.traccar.model.Position; + +public class NotificationManager { + + private final DataManager dataManager; + + public NotificationManager(DataManager dataManager) { + this.dataManager = dataManager; + } + + public void updateEvent(Event event, Position position) { + try { + dataManager.addEvent(event); + } catch (SQLException error) { + Log.warning(error); + } + + Set<Long> users = Context.getPermissionsManager().getDeviceUsers(event.getDeviceId()); + for (Long userId : users) { + if (event.getGeofenceId() == 0 + || Context.getGeofenceManager().checkGeofence(userId, event.getGeofenceId())) { + Context.getConnectionManager().updateEvent(userId, event, position); + } + } + } + + public void updateEvents(Collection<Event> events, Position position) { + + for (Event event : events) { + updateEvent(event, position); + } + } +} |