From 60c442ac19776f0e07fafd1db8a0b75f971fd40e Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 26 Jul 2016 14:50:32 +0500 Subject: Overspeed handler enhanced to use attributes --- src/org/traccar/events/OverspeedEventHandler.java | 41 +++++++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) (limited to 'src/org/traccar/events/OverspeedEventHandler.java') diff --git a/src/org/traccar/events/OverspeedEventHandler.java b/src/org/traccar/events/OverspeedEventHandler.java index 6c9b523f4..b2b1aeb5f 100644 --- a/src/org/traccar/events/OverspeedEventHandler.java +++ b/src/org/traccar/events/OverspeedEventHandler.java @@ -24,16 +24,19 @@ import org.traccar.Context; import org.traccar.model.Device; import org.traccar.model.Event; import org.traccar.model.Position; +import org.traccar.model.Server; import org.traccar.helper.Log; import org.traccar.helper.UnitsConverter; public class OverspeedEventHandler extends BaseEventHandler { - private double globalSpeedLimit; + public static final String ATTRIBUTE_SPEED_LIMIT = "speedLimit"; + + private boolean checkGroups; private int suppressRepeated; public OverspeedEventHandler() { - globalSpeedLimit = UnitsConverter.knotsFromKph(Context.getConfig().getInteger("event.globalSpeedLimit", 0)); + checkGroups = Context.getConfig().getBoolean("event.overspeed.groupsEnabled"); suppressRepeated = Context.getConfig().getInteger("event.suppressRepeated", 60); } @@ -50,8 +53,40 @@ public class OverspeedEventHandler extends BaseEventHandler { Collection events = new ArrayList<>(); double speed = position.getSpeed(); + Double speedLimit = new Double(0); - if (globalSpeedLimit != 0 && speed > globalSpeedLimit) { + if (device.getAttributes().containsKey(ATTRIBUTE_SPEED_LIMIT)) { + speedLimit = Double.parseDouble((String) device.getAttributes().get(ATTRIBUTE_SPEED_LIMIT)); + } + if (speedLimit == 0 && checkGroups) { + long groupId = device.getGroupId(); + while (groupId != 0) { + if (Context.getDeviceManager().getGroupById(groupId).getAttributes() + .containsKey(ATTRIBUTE_SPEED_LIMIT)) { + speedLimit = Double.parseDouble((String) Context.getDeviceManager().getGroupById(groupId) + .getAttributes().get(ATTRIBUTE_SPEED_LIMIT)); + if (speedLimit != 0) { + break; + } + } + if (Context.getDeviceManager().getGroupById(groupId) != null) { + groupId = Context.getDeviceManager().getGroupById(groupId).getGroupId(); + } else { + groupId = 0; + } + } + } + if (speedLimit == 0) { + try { + Server server = Context.getDataManager().getServer(); + if (server.getAttributes().containsKey(ATTRIBUTE_SPEED_LIMIT)) { + speedLimit = Double.parseDouble((String) server.getAttributes().get(ATTRIBUTE_SPEED_LIMIT)); + } + } catch (SQLException error) { + Log.warning(error); + } + } + if (speedLimit != 0 && speed > UnitsConverter.knotsFromKph(speedLimit)) { try { if (Context.getDataManager().getLastEvents( position.getDeviceId(), Event.TYPE_DEVICE_OVERSPEED, suppressRepeated).isEmpty()) { -- cgit v1.2.3 From 387b774ae1b87db6b722e50576eb0b814501b101 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 26 Jul 2016 15:26:27 +0500 Subject: Use primitive --- src/org/traccar/events/OverspeedEventHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/org/traccar/events/OverspeedEventHandler.java') diff --git a/src/org/traccar/events/OverspeedEventHandler.java b/src/org/traccar/events/OverspeedEventHandler.java index b2b1aeb5f..e488b9df2 100644 --- a/src/org/traccar/events/OverspeedEventHandler.java +++ b/src/org/traccar/events/OverspeedEventHandler.java @@ -53,7 +53,7 @@ public class OverspeedEventHandler extends BaseEventHandler { Collection events = new ArrayList<>(); double speed = position.getSpeed(); - Double speedLimit = new Double(0); + double speedLimit = 0; if (device.getAttributes().containsKey(ATTRIBUTE_SPEED_LIMIT)) { speedLimit = Double.parseDouble((String) device.getAttributes().get(ATTRIBUTE_SPEED_LIMIT)); -- cgit v1.2.3 From b00b35b33095110cef46c8a255686cad87ac7150 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 27 Jul 2016 11:31:39 +0500 Subject: Added option to rise event only once during overspeed period. --- debug.xml | 1 + src/org/traccar/events/OverspeedEventHandler.java | 13 +++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) (limited to 'src/org/traccar/events/OverspeedEventHandler.java') diff --git a/debug.xml b/debug.xml index c08d46531..b33abc455 100644 --- a/debug.xml +++ b/debug.xml @@ -45,6 +45,7 @@ 60 true true + true true true true diff --git a/src/org/traccar/events/OverspeedEventHandler.java b/src/org/traccar/events/OverspeedEventHandler.java index e488b9df2..b61277548 100644 --- a/src/org/traccar/events/OverspeedEventHandler.java +++ b/src/org/traccar/events/OverspeedEventHandler.java @@ -33,10 +33,12 @@ public class OverspeedEventHandler extends BaseEventHandler { public static final String ATTRIBUTE_SPEED_LIMIT = "speedLimit"; private boolean checkGroups; + private boolean riseOnce; private int suppressRepeated; public OverspeedEventHandler() { checkGroups = Context.getConfig().getBoolean("event.overspeed.groupsEnabled"); + riseOnce = Context.getConfig().getBoolean("event.overspeed.riseOnce"); suppressRepeated = Context.getConfig().getInteger("event.suppressRepeated", 60); } @@ -86,7 +88,15 @@ public class OverspeedEventHandler extends BaseEventHandler { Log.warning(error); } } - if (speedLimit != 0 && speed > UnitsConverter.knotsFromKph(speedLimit)) { + double oldSpeed = 0; + if (riseOnce) { + Position lastPosition = Context.getDeviceManager().getLastPosition(position.getDeviceId()); + if (lastPosition != null) { + oldSpeed = lastPosition.getSpeed(); + } + } + speedLimit = UnitsConverter.knotsFromKph(speedLimit); + if (speedLimit != 0 && speed > speedLimit && oldSpeed <= speedLimit) { try { if (Context.getDataManager().getLastEvents( position.getDeviceId(), Event.TYPE_DEVICE_OVERSPEED, suppressRepeated).isEmpty()) { @@ -95,7 +105,6 @@ public class OverspeedEventHandler extends BaseEventHandler { } catch (SQLException error) { Log.warning(error); } - } return events; } -- cgit v1.2.3 From f7e82c4d9a66b8ab1ba90d4cae690f9c798d03a7 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 28 Jul 2016 11:42:10 +0500 Subject: Removed suppressing repeated events during analyzing. --- debug.xml | 1 - src/org/traccar/events/GeofenceEventHandler.java | 37 +++++------------------ src/org/traccar/events/MotionEventHandler.java | 32 ++++---------------- src/org/traccar/events/OverspeedEventHandler.java | 11 +------ 4 files changed, 15 insertions(+), 66 deletions(-) (limited to 'src/org/traccar/events/OverspeedEventHandler.java') diff --git a/debug.xml b/debug.xml index b33abc455..62e34d5d7 100644 --- a/debug.xml +++ b/debug.xml @@ -42,7 +42,6 @@ target/tracker-server.log true - 60 true true true diff --git a/src/org/traccar/events/GeofenceEventHandler.java b/src/org/traccar/events/GeofenceEventHandler.java index a9bed6f82..a0291dcfa 100644 --- a/src/org/traccar/events/GeofenceEventHandler.java +++ b/src/org/traccar/events/GeofenceEventHandler.java @@ -15,30 +15,23 @@ */ package org.traccar.events; -import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; import java.util.List; import org.traccar.BaseEventHandler; import org.traccar.Context; -import org.traccar.database.DataManager; import org.traccar.database.GeofenceManager; -import org.traccar.helper.Log; import org.traccar.model.Device; import org.traccar.model.Event; import org.traccar.model.Position; public class GeofenceEventHandler extends BaseEventHandler { - private int suppressRepeated; private GeofenceManager geofenceManager; - private DataManager dataManager; public GeofenceEventHandler() { - suppressRepeated = Context.getConfig().getInteger("event.suppressRepeated", 60); geofenceManager = Context.getGeofenceManager(); - dataManager = Context.getDataManager(); } @Override @@ -63,29 +56,15 @@ public class GeofenceEventHandler extends BaseEventHandler { device.setGeofenceIds(currentGeofences); Collection events = new ArrayList<>(); - try { - if (dataManager.getLastEvents(position.getDeviceId(), - Event.TYPE_GEOFENCE_ENTER, suppressRepeated).isEmpty()) { - for (long geofenceId : newGeofences) { - Event event = new Event(Event.TYPE_GEOFENCE_ENTER, position.getDeviceId(), position.getId()); - event.setGeofenceId(geofenceId); - events.add(event); - } - } - } catch (SQLException error) { - Log.warning(error); + for (long geofenceId : newGeofences) { + Event event = new Event(Event.TYPE_GEOFENCE_ENTER, position.getDeviceId(), position.getId()); + event.setGeofenceId(geofenceId); + events.add(event); } - try { - if (dataManager.getLastEvents(position.getDeviceId(), - Event.TYPE_GEOFENCE_EXIT, suppressRepeated).isEmpty()) { - for (long geofenceId : oldGeofences) { - Event event = new Event(Event.TYPE_GEOFENCE_EXIT, position.getDeviceId(), position.getId()); - event.setGeofenceId(geofenceId); - events.add(event); - } - } - } catch (SQLException error) { - Log.warning(error); + for (long geofenceId : oldGeofences) { + Event event = new Event(Event.TYPE_GEOFENCE_EXIT, position.getDeviceId(), position.getId()); + event.setGeofenceId(geofenceId); + events.add(event); } return events; } diff --git a/src/org/traccar/events/MotionEventHandler.java b/src/org/traccar/events/MotionEventHandler.java index 4b652a727..ddb99185f 100644 --- a/src/org/traccar/events/MotionEventHandler.java +++ b/src/org/traccar/events/MotionEventHandler.java @@ -15,13 +15,11 @@ */ package org.traccar.events; -import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; import org.traccar.BaseEventHandler; import org.traccar.Context; -import org.traccar.helper.Log; import org.traccar.model.Device; import org.traccar.model.Event; import org.traccar.model.Position; @@ -29,11 +27,6 @@ import org.traccar.model.Position; public class MotionEventHandler extends BaseEventHandler { private static final double SPEED_THRESHOLD = 0.01; - private int suppressRepeated; - - public MotionEventHandler() { - suppressRepeated = Context.getConfig().getInteger("event.suppressRepeated", 60); - } @Override protected Collection analyzePosition(Position position) { @@ -53,25 +46,12 @@ public class MotionEventHandler extends BaseEventHandler { if (lastPosition != null) { oldSpeed = lastPosition.getSpeed(); } - try { - if (speed > SPEED_THRESHOLD && oldSpeed <= SPEED_THRESHOLD) { - result = new ArrayList<>(); - result.add(new Event(Event.TYPE_DEVICE_MOVING, position.getDeviceId(), position.getId())); - } else if (speed <= SPEED_THRESHOLD && oldSpeed > SPEED_THRESHOLD) { - result = new ArrayList<>(); - result.add(new Event(Event.TYPE_DEVICE_STOPPED, position.getDeviceId(), position.getId())); - } - - if (result != null && !result.isEmpty()) { - for (Event event : result) { - if (!Context.getDataManager().getLastEvents(position.getDeviceId(), - event.getType(), suppressRepeated).isEmpty()) { - event = null; - } - } - } - } catch (SQLException error) { - Log.warning(error); + if (speed > SPEED_THRESHOLD && oldSpeed <= SPEED_THRESHOLD) { + result = new ArrayList<>(); + result.add(new Event(Event.TYPE_DEVICE_MOVING, position.getDeviceId(), position.getId())); + } else if (speed <= SPEED_THRESHOLD && oldSpeed > SPEED_THRESHOLD) { + result = new ArrayList<>(); + result.add(new Event(Event.TYPE_DEVICE_STOPPED, position.getDeviceId(), position.getId())); } return result; } diff --git a/src/org/traccar/events/OverspeedEventHandler.java b/src/org/traccar/events/OverspeedEventHandler.java index b61277548..0328b9109 100644 --- a/src/org/traccar/events/OverspeedEventHandler.java +++ b/src/org/traccar/events/OverspeedEventHandler.java @@ -34,12 +34,10 @@ public class OverspeedEventHandler extends BaseEventHandler { private boolean checkGroups; private boolean riseOnce; - private int suppressRepeated; public OverspeedEventHandler() { checkGroups = Context.getConfig().getBoolean("event.overspeed.groupsEnabled"); riseOnce = Context.getConfig().getBoolean("event.overspeed.riseOnce"); - suppressRepeated = Context.getConfig().getInteger("event.suppressRepeated", 60); } @Override @@ -97,14 +95,7 @@ public class OverspeedEventHandler extends BaseEventHandler { } speedLimit = UnitsConverter.knotsFromKph(speedLimit); if (speedLimit != 0 && speed > speedLimit && oldSpeed <= speedLimit) { - try { - if (Context.getDataManager().getLastEvents( - position.getDeviceId(), Event.TYPE_DEVICE_OVERSPEED, suppressRepeated).isEmpty()) { - events.add(new Event(Event.TYPE_DEVICE_OVERSPEED, position.getDeviceId(), position.getId())); - } - } catch (SQLException error) { - Log.warning(error); - } + events.add(new Event(Event.TYPE_DEVICE_OVERSPEED, position.getDeviceId(), position.getId())); } return events; } -- cgit v1.2.3 From 70f684945754b6bdadd2bf038ab955d93a95977d Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 28 Jul 2016 13:44:05 +0500 Subject: Organized server cache Moved lookupAttribute function to DeviceManager --- debug.xml | 3 +- src/org/traccar/api/resource/ServerResource.java | 5 ++- src/org/traccar/database/DeviceManager.java | 32 ++++++++++++++++++ src/org/traccar/database/PermissionsManager.java | 11 ++++++- src/org/traccar/events/OverspeedEventHandler.java | 40 +++-------------------- 5 files changed, 51 insertions(+), 40 deletions(-) (limited to 'src/org/traccar/events/OverspeedEventHandler.java') diff --git a/debug.xml b/debug.xml index 62e34d5d7..4c3b63d42 100644 --- a/debug.xml +++ b/debug.xml @@ -41,9 +41,10 @@ all target/tracker-server.log + true + true true - true true true true diff --git a/src/org/traccar/api/resource/ServerResource.java b/src/org/traccar/api/resource/ServerResource.java index 9e42687ab..0ca0d62aa 100644 --- a/src/org/traccar/api/resource/ServerResource.java +++ b/src/org/traccar/api/resource/ServerResource.java @@ -37,14 +37,13 @@ public class ServerResource extends BaseResource { @PermitAll @GET public Server get() throws SQLException { - return Context.getDataManager().getServer(); + return Context.getPermissionsManager().getServer(); } @PUT public Response update(Server entity) throws SQLException { Context.getPermissionsManager().checkAdmin(getUserId()); - Context.getDataManager().updateServer(entity); - Context.getPermissionsManager().refresh(); + Context.getPermissionsManager().updateServer(entity); return Response.ok(entity).build(); } diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index 5a0c87985..3476139f2 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -32,6 +32,7 @@ import org.traccar.helper.Log; import org.traccar.model.Device; import org.traccar.model.Group; import org.traccar.model.Position; +import org.traccar.model.Server; public class DeviceManager implements IdentityManager { @@ -40,6 +41,7 @@ public class DeviceManager implements IdentityManager { private final Config config; private final DataManager dataManager; private final long dataRefreshDelay; + private boolean lookupGroupsAttribute; private Map devicesById; private Map devicesByUniqueId; @@ -54,6 +56,7 @@ public class DeviceManager implements IdentityManager { this.dataManager = dataManager; this.config = Context.getConfig(); dataRefreshDelay = config.getLong("database.refreshDelay", DEFAULT_REFRESH_DELAY) * 1000; + lookupGroupsAttribute = config.getBoolean("deviceManager.lookupGroupsAttribute"); if (dataManager != null) { try { updateGroupCache(true); @@ -312,4 +315,33 @@ public class DeviceManager implements IdentityManager { dataManager.removeGroup(groupId); groupsById.remove(groupId); } + + public String lookupAttribute(long deviceId, String attributeName) { + String result = null; + Device device = getDeviceById(deviceId); + if (device != null) { + if (device.getAttributes().containsKey(attributeName)) { + result = (String) device.getAttributes().get(attributeName); + } + if (result == null && lookupGroupsAttribute) { + long groupId = device.getGroupId(); + while (groupId != 0) { + if (getGroupById(groupId) != null) { + result = (String) getGroupById(groupId).getAttributes().get(attributeName); + if (result != null) { + break; + } + groupId = getGroupById(groupId).getGroupId(); + } else { + groupId = 0; + } + } + } + if (result == null) { + Server server = Context.getPermissionsManager().getServer(); + result = (String) server.getAttributes().get(attributeName); + } + } + return result; + } } diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index d786dcc4e..92fcc3ebd 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -34,7 +34,7 @@ public class PermissionsManager { private final DataManager dataManager; - private Server server; + private volatile Server server; private final Map users = new HashMap<>(); @@ -153,4 +153,13 @@ public class PermissionsManager { } } + public Server getServer() { + return server; + } + + public void updateServer(Server server) throws SQLException { + dataManager.updateServer(server); + this.server = server; + } + } diff --git a/src/org/traccar/events/OverspeedEventHandler.java b/src/org/traccar/events/OverspeedEventHandler.java index 0328b9109..a1efbde48 100644 --- a/src/org/traccar/events/OverspeedEventHandler.java +++ b/src/org/traccar/events/OverspeedEventHandler.java @@ -15,7 +15,6 @@ */ package org.traccar.events; -import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; @@ -24,19 +23,15 @@ import org.traccar.Context; import org.traccar.model.Device; import org.traccar.model.Event; import org.traccar.model.Position; -import org.traccar.model.Server; -import org.traccar.helper.Log; import org.traccar.helper.UnitsConverter; public class OverspeedEventHandler extends BaseEventHandler { public static final String ATTRIBUTE_SPEED_LIMIT = "speedLimit"; - private boolean checkGroups; private boolean riseOnce; public OverspeedEventHandler() { - checkGroups = Context.getConfig().getBoolean("event.overspeed.groupsEnabled"); riseOnce = Context.getConfig().getBoolean("event.overspeed.riseOnce"); } @@ -54,37 +49,12 @@ public class OverspeedEventHandler extends BaseEventHandler { Collection events = new ArrayList<>(); double speed = position.getSpeed(); double speedLimit = 0; - - if (device.getAttributes().containsKey(ATTRIBUTE_SPEED_LIMIT)) { - speedLimit = Double.parseDouble((String) device.getAttributes().get(ATTRIBUTE_SPEED_LIMIT)); - } - if (speedLimit == 0 && checkGroups) { - long groupId = device.getGroupId(); - while (groupId != 0) { - if (Context.getDeviceManager().getGroupById(groupId).getAttributes() - .containsKey(ATTRIBUTE_SPEED_LIMIT)) { - speedLimit = Double.parseDouble((String) Context.getDeviceManager().getGroupById(groupId) - .getAttributes().get(ATTRIBUTE_SPEED_LIMIT)); - if (speedLimit != 0) { - break; - } - } - if (Context.getDeviceManager().getGroupById(groupId) != null) { - groupId = Context.getDeviceManager().getGroupById(groupId).getGroupId(); - } else { - groupId = 0; - } - } + String speedLimitAttribute = Context.getDeviceManager().lookupAttribute(device.getId(), ATTRIBUTE_SPEED_LIMIT); + if (speedLimitAttribute != null) { + speedLimit = Double.parseDouble(speedLimitAttribute); } if (speedLimit == 0) { - try { - Server server = Context.getDataManager().getServer(); - if (server.getAttributes().containsKey(ATTRIBUTE_SPEED_LIMIT)) { - speedLimit = Double.parseDouble((String) server.getAttributes().get(ATTRIBUTE_SPEED_LIMIT)); - } - } catch (SQLException error) { - Log.warning(error); - } + return null; } double oldSpeed = 0; if (riseOnce) { @@ -94,7 +64,7 @@ public class OverspeedEventHandler extends BaseEventHandler { } } speedLimit = UnitsConverter.knotsFromKph(speedLimit); - if (speedLimit != 0 && speed > speedLimit && oldSpeed <= speedLimit) { + if (speed > speedLimit && oldSpeed <= speedLimit) { events.add(new Event(Event.TYPE_DEVICE_OVERSPEED, position.getDeviceId(), position.getId())); } return events; -- cgit v1.2.3 From 6f89644fdcb3dbeb966cadbd810f9795922b8c4e Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 28 Jul 2016 17:57:24 +0500 Subject: Rename parameter --- debug.xml | 2 +- src/org/traccar/events/OverspeedEventHandler.java | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src/org/traccar/events/OverspeedEventHandler.java') diff --git a/debug.xml b/debug.xml index 4c3b63d42..223378eac 100644 --- a/debug.xml +++ b/debug.xml @@ -45,7 +45,7 @@ true true - true + true true true true diff --git a/src/org/traccar/events/OverspeedEventHandler.java b/src/org/traccar/events/OverspeedEventHandler.java index a1efbde48..dbdb01ffb 100644 --- a/src/org/traccar/events/OverspeedEventHandler.java +++ b/src/org/traccar/events/OverspeedEventHandler.java @@ -29,10 +29,10 @@ public class OverspeedEventHandler extends BaseEventHandler { public static final String ATTRIBUTE_SPEED_LIMIT = "speedLimit"; - private boolean riseOnce; + private boolean notRepeat; public OverspeedEventHandler() { - riseOnce = Context.getConfig().getBoolean("event.overspeed.riseOnce"); + notRepeat = Context.getConfig().getBoolean("event.overspeed.notRepeat"); } @Override @@ -57,7 +57,7 @@ public class OverspeedEventHandler extends BaseEventHandler { return null; } double oldSpeed = 0; - if (riseOnce) { + if (notRepeat) { Position lastPosition = Context.getDeviceManager().getLastPosition(position.getDeviceId()); if (lastPosition != null) { oldSpeed = lastPosition.getSpeed(); -- cgit v1.2.3