aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/handler
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-09-21 17:53:50 -0700
committerAnton Tananaev <anton@traccar.org>2022-09-21 17:53:58 -0700
commit388799edf2adc9c3070a83e72f074e523629574f (patch)
tree35809a83791475cece4f053457cafddc9d226b71 /src/main/java/org/traccar/handler
parentd5b1385596f79da2514ea7cccf27e9bdeebcacae (diff)
downloadtrackermap-server-388799edf2adc9c3070a83e72f074e523629574f.tar.gz
trackermap-server-388799edf2adc9c3070a83e72f074e523629574f.tar.bz2
trackermap-server-388799edf2adc9c3070a83e72f074e523629574f.zip
Refactor motion handling
Diffstat (limited to 'src/main/java/org/traccar/handler')
-rw-r--r--src/main/java/org/traccar/handler/events/MotionEventHandler.java88
-rw-r--r--src/main/java/org/traccar/handler/events/OverspeedEventHandler.java2
2 files changed, 41 insertions, 49 deletions
diff --git a/src/main/java/org/traccar/handler/events/MotionEventHandler.java b/src/main/java/org/traccar/handler/events/MotionEventHandler.java
index 7ef9ec21d..234899785 100644
--- a/src/main/java/org/traccar/handler/events/MotionEventHandler.java
+++ b/src/main/java/org/traccar/handler/events/MotionEventHandler.java
@@ -45,54 +45,52 @@ public class MotionEventHandler extends BaseEventHandler {
this.tripsConfig = tripsConfig;
}
- private Map<Event, Position> newEvent(DeviceState deviceState, boolean newMotion) {
- String eventType = newMotion ? Event.TYPE_DEVICE_MOVING : Event.TYPE_DEVICE_STOPPED;
- Position position = deviceState.getMotionPosition();
- Event event = new Event(eventType, position);
- deviceState.setMotionState(newMotion);
- deviceState.setMotionPosition(null);
- return Collections.singletonMap(event, position);
- }
-
- public Map<Event, Position> updateMotionState(DeviceState deviceState, Position position) {
- return updateMotionState(deviceState, position, position.getBoolean(Position.KEY_MOTION));
- }
+ public Map<Event, Position> updateMotionState(DeviceState deviceState, Position position, boolean newState) {
- public Map<Event, Position> updateMotionState(DeviceState deviceState, Position position, boolean newMotion) {
- Map<Event, Position> result = null;
- Boolean oldMotion = deviceState.getMotionState();
+ boolean oldState = deviceState.getMotionState();
+ if (oldState == newState) {
+ if (deviceState.getMotionTime() != null) {
+ long oldTime = deviceState.getMotionTime().getTime();
+ long newTime = position.getFixTime().getTime();
- long currentTime = position.getFixTime().getTime();
- if (newMotion != oldMotion) {
- if (deviceState.getMotionPosition() == null) {
- deviceState.setMotionPosition(position);
- }
- } else {
- deviceState.setMotionPosition(null);
- }
+ double distance = position.getDouble(Position.KEY_TOTAL_DISTANCE) - deviceState.getMotionDistance();
+ Boolean ignition = null;
+ if (tripsConfig.getUseIgnition() && position.hasAttribute(Position.KEY_IGNITION)) {
+ ignition = position.getBoolean(Position.KEY_IGNITION);
+ }
- Position motionPosition = deviceState.getMotionPosition();
- if (motionPosition != null) {
- long motionTime = motionPosition.getFixTime().getTime();
- double distance = PositionUtil.calculateDistance(motionPosition, position, false);
- Boolean ignition = null;
- if (tripsConfig.getUseIgnition()
- && position.hasAttribute(Position.KEY_IGNITION)) {
- ignition = position.getBoolean(Position.KEY_IGNITION);
- }
- if (newMotion) {
- if (motionTime + tripsConfig.getMinimalTripDuration() <= currentTime
- || distance >= tripsConfig.getMinimalTripDistance()) {
- result = newEvent(deviceState, newMotion);
+ boolean generateEvent = false;
+ if (newState) {
+ if (newTime - oldTime >= tripsConfig.getMinimalTripDuration()
+ || distance >= tripsConfig.getMinimalTripDistance()) {
+ generateEvent = true;
+ }
+ } else {
+ if (newTime - oldTime >= tripsConfig.getMinimalParkingDuration()
+ || ignition != null && !ignition) {
+ generateEvent = true;
+ }
}
- } else {
- if (motionTime + tripsConfig.getMinimalParkingDuration() <= currentTime
- || ignition != null && !ignition) {
- result = newEvent(deviceState, newMotion);
+
+ if (generateEvent) {
+
+ String eventType = newState ? Event.TYPE_DEVICE_MOVING : Event.TYPE_DEVICE_STOPPED;
+ Event event = new Event(eventType, position);
+
+ deviceState.setMotionTime(null);
+ deviceState.setMotionDistance(0);
+
+ return Collections.singletonMap(event, position);
+
}
}
+ } else {
+ deviceState.setMotionState(newState);
+ deviceState.setMotionTime(position.getFixTime());
+ deviceState.setMotionDistance(position.getDouble(Position.KEY_TOTAL_DISTANCE));
}
- return result;
+
+ return null;
}
@Override
@@ -108,14 +106,8 @@ public class MotionEventHandler extends BaseEventHandler {
return null;
}
- Map<Event, Position> result = null;
DeviceState deviceState = connectionManager.getDeviceState(deviceId);
-
- if (deviceState.getMotionState() == null) {
- deviceState.setMotionState(position.getBoolean(Position.KEY_MOTION));
- } else {
- result = updateMotionState(deviceState, position);
- }
+ var result = updateMotionState(deviceState, position, position.getBoolean(Position.KEY_MOTION));
connectionManager.setDeviceState(deviceId, deviceState);
return result;
}
diff --git a/src/main/java/org/traccar/handler/events/OverspeedEventHandler.java b/src/main/java/org/traccar/handler/events/OverspeedEventHandler.java
index cfba56a38..3984299d7 100644
--- a/src/main/java/org/traccar/handler/events/OverspeedEventHandler.java
+++ b/src/main/java/org/traccar/handler/events/OverspeedEventHandler.java
@@ -136,7 +136,7 @@ public class OverspeedEventHandler extends BaseEventHandler {
}
DeviceState deviceState = connectionManager.getDeviceState(deviceId);
- Map<Event, Position> result = updateOverspeedState(deviceState, position, speedLimit, overspeedGeofenceId);
+ var result = updateOverspeedState(deviceState, position, speedLimit, overspeedGeofenceId);
connectionManager.setDeviceState(deviceId, deviceState);
return result;
}