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 --- debug.xml | 2 +- src/org/traccar/events/OverspeedEventHandler.java | 41 +++++++++++++++++++++-- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/debug.xml b/debug.xml index f078a7e9d..bd55b7ee1 100644 --- a/debug.xml +++ b/debug.xml @@ -44,7 +44,7 @@ true 60 true - 90 + true true true true 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