diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2017-08-10 16:30:37 +1200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-10 16:30:37 +1200 |
commit | b82be4f9f3b4d1d6c345bc41d5c26aca03e369cb (patch) | |
tree | d0765e885e4d3af1cc5b58011df5574c6ad82ea6 /src/org/traccar/events | |
parent | ea1b94b524c880927f85fd0c94a26bd3ec17c898 (diff) | |
parent | 34faf7e1d024596b388d1dd3a062f19adde0027c (diff) | |
download | trackermap-server-b82be4f9f3b4d1d6c345bc41d5c26aca03e369cb.tar.gz trackermap-server-b82be4f9f3b4d1d6c345bc41d5c26aca03e369cb.tar.bz2 trackermap-server-b82be4f9f3b4d1d6c345bc41d5c26aca03e369cb.zip |
Merge pull request #3426 from Abyss777/delayed_motion_events
Implement motion detection similar to trips detection
Diffstat (limited to 'src/org/traccar/events')
-rw-r--r-- | src/org/traccar/events/MotionEventHandler.java | 76 |
1 files changed, 64 insertions, 12 deletions
diff --git a/src/org/traccar/events/MotionEventHandler.java b/src/org/traccar/events/MotionEventHandler.java index ed81176a8..228b43c0f 100644 --- a/src/org/traccar/events/MotionEventHandler.java +++ b/src/org/traccar/events/MotionEventHandler.java @@ -1,5 +1,6 @@ /* - * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,11 +22,58 @@ import java.util.Collections; import org.traccar.BaseEventHandler; import org.traccar.Context; import org.traccar.model.Device; +import org.traccar.model.DeviceState; import org.traccar.model.Event; import org.traccar.model.Position; +import org.traccar.reports.ReportUtils; +import org.traccar.reports.model.TripsConfig; public class MotionEventHandler extends BaseEventHandler { + private TripsConfig tripsConfig; + + public MotionEventHandler() { + tripsConfig = ReportUtils.initTripsConfig(); + } + + public static Event updateMotionState(DeviceState deviceState, Position position, TripsConfig tripsConfig) { + Event result = null; + Boolean oldMotion = deviceState.getMotionState(); + + long currentTime = position.getFixTime().getTime(); + boolean newMotion = position.getBoolean(Position.KEY_MOTION); + if (newMotion != oldMotion) { + if (deviceState.getMotionPosition() == null) { + deviceState.setMotionPosition(position); + } + } else { + deviceState.setMotionPosition(null); + } + + Position motionPosition = deviceState.getMotionPosition(); + if (motionPosition != null) { + long motionTime = motionPosition.getFixTime().getTime(); + double distance = ReportUtils.calculateDistance(motionPosition, position, false); + 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); + } + } else { + if (motionTime + tripsConfig.getMinimalParkingDuration() <= currentTime) { + result = new Event(Event.TYPE_DEVICE_STOPPED, motionPosition.getDeviceId(), + motionPosition.getId()); + deviceState.setMotionState(false); + deviceState.setMotionPosition(null); + } + } + } + return result; + } + @Override protected Collection<Event> analyzePosition(Position position) { @@ -37,18 +85,22 @@ public class MotionEventHandler extends BaseEventHandler { return null; } - boolean motion = position.getBoolean(Position.KEY_MOTION); - boolean oldMotion = false; - Position lastPosition = Context.getIdentityManager().getLastPosition(position.getDeviceId()); - if (lastPosition != null) { - oldMotion = lastPosition.getBoolean(Position.KEY_MOTION); + 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) { + deviceState.setMotionState(position.getBoolean(Position.KEY_MOTION)); + } else { + result = updateMotionState(deviceState, position, tripsConfig); } - if (motion && !oldMotion) { - return Collections.singleton( - new Event(Event.TYPE_DEVICE_MOVING, position.getDeviceId(), position.getId())); - } else if (!motion && oldMotion) { - return Collections.singleton( - new Event(Event.TYPE_DEVICE_STOPPED, position.getDeviceId(), position.getId())); + Context.getDeviceManager().setDeviceState(deviceId, deviceState); + if (result != null) { + return Collections.singleton(result); } return null; } |