diff options
Diffstat (limited to 'src/org/traccar/events')
-rw-r--r-- | src/org/traccar/events/GeofenceEventHandler.java | 39 | ||||
-rw-r--r-- | src/org/traccar/events/MotionEventHandler.java | 48 | ||||
-rw-r--r-- | src/org/traccar/events/OverspeedEventHandler.java | 40 |
3 files changed, 43 insertions, 84 deletions
diff --git a/src/org/traccar/events/GeofenceEventHandler.java b/src/org/traccar/events/GeofenceEventHandler.java index 1ea7aee41..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 @@ -47,7 +40,7 @@ public class GeofenceEventHandler extends BaseEventHandler { if (device == null) { return null; } - if (position.getId() != device.getPositionId() || !position.getValid()) { + if (!Context.getDeviceManager().isLatestPosition(position) || !position.getValid()) { return null; } @@ -63,29 +56,15 @@ public class GeofenceEventHandler extends BaseEventHandler { device.setGeofenceIds(currentGeofences); Collection<Event> 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 4a3d2f0f0..ddb99185f 100644 --- a/src/org/traccar/events/MotionEventHandler.java +++ b/src/org/traccar/events/MotionEventHandler.java @@ -15,25 +15,18 @@ */ 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; 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); - } + private static final double SPEED_THRESHOLD = 0.01; @Override protected Collection<Event> analyzePosition(Position position) { @@ -42,40 +35,23 @@ public class MotionEventHandler extends BaseEventHandler { if (device == null) { return null; } - if (position.getId() != device.getPositionId() || !position.getValid()) { + if (!Context.getDeviceManager().isLatestPosition(position) || !position.getValid()) { return null; } Collection<Event> result = null; double speed = position.getSpeed(); - boolean valid = position.getValid(); - String motion = device.getMotion(); - if (motion == null) { - motion = Device.STATUS_STOPPED; + double oldSpeed = 0; + Position lastPosition = Context.getDeviceManager().getLastPosition(position.getDeviceId()); + if (lastPosition != null) { + oldSpeed = lastPosition.getSpeed(); } - try { - if (valid && speed > SPEED_THRESHOLD && !motion.equals(Device.STATUS_MOVING)) { - device.setMotion(Device.STATUS_MOVING); - Context.getDeviceManager().updateDeviceStatus(device); - result = new ArrayList<>(); - result.add(new Event(Event.TYPE_DEVICE_MOVING, position.getDeviceId(), position.getId())); - } else if (valid && speed < SPEED_THRESHOLD && motion.equals(Device.STATUS_MOVING)) { - device.setMotion(Device.STATUS_STOPPED); - Context.getDeviceManager().updateDeviceStatus(device); - 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 fd005e170..dbdb01ffb 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,17 +23,16 @@ import org.traccar.Context; import org.traccar.model.Device; import org.traccar.model.Event; import org.traccar.model.Position; -import org.traccar.helper.Log; import org.traccar.helper.UnitsConverter; public class OverspeedEventHandler extends BaseEventHandler { - private double globalSpeedLimit; - private int suppressRepeated; + public static final String ATTRIBUTE_SPEED_LIMIT = "speedLimit"; + + private boolean notRepeat; public OverspeedEventHandler() { - globalSpeedLimit = UnitsConverter.knotsFromKph(Context.getConfig().getInteger("event.globalSpeedLimit", 0)); - suppressRepeated = Context.getConfig().getInteger("event.suppressRepeated", 60); + notRepeat = Context.getConfig().getBoolean("event.overspeed.notRepeat"); } @Override @@ -44,24 +42,30 @@ public class OverspeedEventHandler extends BaseEventHandler { if (device == null) { return null; } - if (position.getId() != device.getPositionId() || !position.getValid()) { + if (!Context.getDeviceManager().isLatestPosition(position) || !position.getValid()) { return null; } Collection<Event> events = new ArrayList<>(); double speed = position.getSpeed(); - boolean valid = position.getValid(); - - if (valid && globalSpeedLimit != 0 && speed > globalSpeedLimit) { - 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); + double speedLimit = 0; + String speedLimitAttribute = Context.getDeviceManager().lookupAttribute(device.getId(), ATTRIBUTE_SPEED_LIMIT); + if (speedLimitAttribute != null) { + speedLimit = Double.parseDouble(speedLimitAttribute); + } + if (speedLimit == 0) { + return null; + } + double oldSpeed = 0; + if (notRepeat) { + Position lastPosition = Context.getDeviceManager().getLastPosition(position.getDeviceId()); + if (lastPosition != null) { + oldSpeed = lastPosition.getSpeed(); } - + } + speedLimit = UnitsConverter.knotsFromKph(speedLimit); + if (speed > speedLimit && oldSpeed <= speedLimit) { + events.add(new Event(Event.TYPE_DEVICE_OVERSPEED, position.getDeviceId(), position.getId())); } return events; } |