diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2017-08-11 23:37:16 +1200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-11 23:37:16 +1200 |
commit | 30e8409375f9992a5248f7e2597691c0ee08188c (patch) | |
tree | ea51b7d111cfae849d23941ad82045139e87520b /src/org/traccar/events/MotionEventHandler.java | |
parent | 3562e0d927d539a1e9730c48ed376ee84ebc2b25 (diff) | |
parent | 26db3ccc2ed5d43a0e791a29d5cc4afca192e4b7 (diff) | |
download | trackermap-server-30e8409375f9992a5248f7e2597691c0ee08188c.tar.gz trackermap-server-30e8409375f9992a5248f7e2597691c0ee08188c.tar.bz2 trackermap-server-30e8409375f9992a5248f7e2597691c0ee08188c.zip |
Merge pull request #3431 from Abyss777/delayed_overspeed
Implement delayed overspeed events
Diffstat (limited to 'src/org/traccar/events/MotionEventHandler.java')
-rw-r--r-- | src/org/traccar/events/MotionEventHandler.java | 52 |
1 files changed, 33 insertions, 19 deletions
diff --git a/src/org/traccar/events/MotionEventHandler.java b/src/org/traccar/events/MotionEventHandler.java index 228b43c0f..b20a11999 100644 --- a/src/org/traccar/events/MotionEventHandler.java +++ b/src/org/traccar/events/MotionEventHandler.java @@ -32,11 +32,35 @@ public class MotionEventHandler extends BaseEventHandler { private TripsConfig tripsConfig; - public MotionEventHandler() { - tripsConfig = ReportUtils.initTripsConfig(); + public MotionEventHandler(TripsConfig tripsConfig) { + this.tripsConfig = tripsConfig; } - public static Event updateMotionState(DeviceState deviceState, Position position, TripsConfig tripsConfig) { + private Event newEvent(DeviceState deviceState, boolean newMotion) { + String eventType = newMotion ? Event.TYPE_DEVICE_MOVING : Event.TYPE_DEVICE_STOPPED; + Event event = new Event(eventType, deviceState.getMotionPosition().getDeviceId(), + deviceState.getMotionPosition().getId()); + deviceState.setMotionState(newMotion); + deviceState.setMotionPosition(null); + return event; + } + + public Event updateMotionState(DeviceState deviceState) { + Event result = null; + if (deviceState.getMotionState() != null && deviceState.getMotionPosition() != null) { + boolean newMotion = !deviceState.getMotionState(); + Position motionPosition = deviceState.getMotionPosition(); + long currentTime = System.currentTimeMillis(); + long motionTime = motionPosition.getFixTime().getTime() + + (newMotion ? tripsConfig.getMinimalTripDuration() : tripsConfig.getMinimalParkingDuration()); + if (motionTime <= currentTime) { + result = newEvent(deviceState, newMotion); + } + } + return result; + } + + public Event updateMotionState(DeviceState deviceState, Position position) { Event result = null; Boolean oldMotion = deviceState.getMotionState(); @@ -57,17 +81,11 @@ public class MotionEventHandler extends BaseEventHandler { if (newMotion) { if (motionTime + tripsConfig.getMinimalTripDuration() <= currentTime || distance >= tripsConfig.getMinimalTripDistance()) { - result = new Event(Event.TYPE_DEVICE_MOVING, motionPosition.getDeviceId(), - motionPosition.getId()); - deviceState.setMotionState(true); - deviceState.setMotionPosition(null); + result = newEvent(deviceState, newMotion); } } else { if (motionTime + tripsConfig.getMinimalParkingDuration() <= currentTime) { - result = new Event(Event.TYPE_DEVICE_STOPPED, motionPosition.getDeviceId(), - motionPosition.getId()); - deviceState.setMotionState(false); - deviceState.setMotionPosition(null); + result = newEvent(deviceState, newMotion); } } } @@ -77,7 +95,8 @@ public class MotionEventHandler extends BaseEventHandler { @Override protected Collection<Event> analyzePosition(Position position) { - Device device = Context.getIdentityManager().getById(position.getDeviceId()); + long deviceId = position.getDeviceId(); + Device device = Context.getIdentityManager().getById(deviceId); if (device == null) { return null; } @@ -86,17 +105,12 @@ public class MotionEventHandler extends BaseEventHandler { } Event result = null; - - long deviceId = position.getDeviceId(); DeviceState deviceState = Context.getDeviceManager().getDeviceState(deviceId); - if (deviceState == null) { - deviceState = new DeviceState(); - deviceState.setMotionState(position.getBoolean(Position.KEY_MOTION)); - } else if (deviceState.getMotionState() == null) { + if (deviceState.getMotionState() == null) { deviceState.setMotionState(position.getBoolean(Position.KEY_MOTION)); } else { - result = updateMotionState(deviceState, position, tripsConfig); + result = updateMotionState(deviceState, position); } Context.getDeviceManager().setDeviceState(deviceId, deviceState); if (result != null) { |