aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/events/MotionEventHandler.java
diff options
context:
space:
mode:
authorAbyss777 <abyss@fox5.ru>2017-08-08 09:53:31 +0500
committerAbyss777 <abyss@fox5.ru>2017-08-09 11:04:29 +0500
commit94a922eaf167a73a0188255398fb1c6799767e18 (patch)
treed1f23e82db216a1c859a096a2c4f7f8676d50a6b /src/org/traccar/events/MotionEventHandler.java
parentc76acd69275ae2d6612bb731758df8a5b91e6508 (diff)
downloadtrackermap-server-94a922eaf167a73a0188255398fb1c6799767e18.tar.gz
trackermap-server-94a922eaf167a73a0188255398fb1c6799767e18.tar.bz2
trackermap-server-94a922eaf167a73a0188255398fb1c6799767e18.zip
Implement motion detection similar to trips detection
Diffstat (limited to 'src/org/traccar/events/MotionEventHandler.java')
-rw-r--r--src/org/traccar/events/MotionEventHandler.java75
1 files changed, 64 insertions, 11 deletions
diff --git a/src/org/traccar/events/MotionEventHandler.java b/src/org/traccar/events/MotionEventHandler.java
index ed81176a8..9168d0fd8 100644
--- a/src/org/traccar/events/MotionEventHandler.java
+++ b/src/org/traccar/events/MotionEventHandler.java
@@ -21,11 +21,60 @@ 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() {
+ if (Context.getConfig() != null) {
+ 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 potentialPosition = deviceState.getMotionPosition();
+ if (potentialPosition != null) {
+ long potentialTime = potentialPosition.getFixTime().getTime();
+ double distance = ReportUtils.calculateDistance(potentialPosition, position, false);
+ if (newMotion) {
+ if (potentialTime + tripsConfig.getMinimalTripDuration() <= currentTime
+ || distance >= tripsConfig.getMinimalTripDistance()) {
+ result = new Event(Event.TYPE_DEVICE_MOVING, potentialPosition.getDeviceId(),
+ potentialPosition.getId());
+ deviceState.setMotionState(true);
+ deviceState.setMotionPosition(null);
+ }
+ } else {
+ if (potentialTime + tripsConfig.getMinimalParkingDuration() <= currentTime) {
+ result = new Event(Event.TYPE_DEVICE_STOPPED, potentialPosition.getDeviceId(),
+ potentialPosition.getId());
+ deviceState.setMotionState(false);
+ deviceState.setMotionPosition(null);
+ }
+ }
+ }
+ return result;
+ }
+
@Override
protected Collection<Event> analyzePosition(Position position) {
@@ -37,18 +86,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;
}