diff options
Diffstat (limited to 'src/main')
4 files changed, 84 insertions, 85 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; } diff --git a/src/main/java/org/traccar/reports/common/ReportUtils.java b/src/main/java/org/traccar/reports/common/ReportUtils.java index 7fff46f66..2bca00df7 100644 --- a/src/main/java/org/traccar/reports/common/ReportUtils.java +++ b/src/main/java/org/traccar/reports/common/ReportUtils.java @@ -37,7 +37,6 @@ import org.traccar.helper.model.UserUtil; import org.traccar.model.BaseModel; import org.traccar.model.Device; import org.traccar.model.Driver; -import org.traccar.model.Event; import org.traccar.model.Group; import org.traccar.model.Position; import org.traccar.model.User; @@ -65,7 +64,6 @@ import java.util.Date; import java.util.LinkedList; import java.util.List; import java.util.Locale; -import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; @@ -353,38 +351,39 @@ public class ReportUtils { if (!positions.isEmpty()) { boolean trips = reportClass.equals(TripReportItem.class); MotionEventHandler motionHandler = new MotionEventHandler(null, null, tripsConfig); + DeviceState deviceState = new DeviceState(); deviceState.setMotionState(isMoving(positions, 0, tripsConfig)); - int startEventIndex = trips == deviceState.getMotionState() ? 0 : -1; + + boolean detected = trips == deviceState.getMotionState(); + int startEventIndex = detected ? 0 : -1; int startNoEventIndex = -1; for (int i = 0; i < positions.size(); i++) { - Map<Event, Position> event = motionHandler.updateMotionState(deviceState, positions.get(i), - isMoving(positions, i, tripsConfig)); - if (startEventIndex == -1 - && (trips != deviceState.getMotionState() && deviceState.getMotionPosition() != null - || trips == deviceState.getMotionState() && event != null)) { - startEventIndex = i; - startNoEventIndex = -1; - } else if (trips != deviceState.getMotionState() && startEventIndex != -1 - && deviceState.getMotionPosition() == null && event == null) { - startEventIndex = -1; + boolean motion = isMoving(positions, i, tripsConfig); + if (deviceState.getMotionState() != motion) { + if (motion == trips) { + startEventIndex = detected ? startEventIndex : i; + startNoEventIndex = -1; + } else { + startNoEventIndex = i; + } } - if (startNoEventIndex == -1 - && (trips == deviceState.getMotionState() && deviceState.getMotionPosition() != null - || trips != deviceState.getMotionState() && event != null)) { - startNoEventIndex = i; - } else if (startNoEventIndex != -1 && deviceState.getMotionPosition() == null && event == null) { - startNoEventIndex = -1; - } - if (startEventIndex != -1 && startNoEventIndex != -1 && event != null - && trips != deviceState.getMotionState()) { - result.add(calculateTripOrStop( - device, positions, startEventIndex, startNoEventIndex, ignoreOdometer, reportClass)); - startEventIndex = -1; + + if (motionHandler.updateMotionState(deviceState, positions.get(i), motion) != null) { + if (motion == trips) { + detected = true; + startNoEventIndex = -1; + } else if (startEventIndex >= 0 && startNoEventIndex >= 0) { + result.add(calculateTripOrStop( + device, positions, startEventIndex, startNoEventIndex, ignoreOdometer, reportClass)); + detected = false; + startEventIndex = -1; + startNoEventIndex = -1; + } } } - if (startEventIndex != -1 && (startNoEventIndex != -1 || !trips)) { - int endIndex = startNoEventIndex != -1 ? startNoEventIndex : positions.size() - 1; + if (startEventIndex >= 0 && startEventIndex < positions.size() - 1) { + int endIndex = startNoEventIndex >= 0 ? startNoEventIndex : positions.size() - 1; result.add(calculateTripOrStop( device, positions, startEventIndex, endIndex, ignoreOdometer, reportClass)); } diff --git a/src/main/java/org/traccar/session/DeviceState.java b/src/main/java/org/traccar/session/DeviceState.java index f67b906c4..7bf2a62ac 100644 --- a/src/main/java/org/traccar/session/DeviceState.java +++ b/src/main/java/org/traccar/session/DeviceState.java @@ -16,30 +16,38 @@ */ package org.traccar.session; -import org.traccar.model.Position; - import java.util.Date; public class DeviceState { - private Boolean motionState; + private boolean motionState; public void setMotionState(boolean motionState) { this.motionState = motionState; } - public Boolean getMotionState() { + public boolean getMotionState() { return motionState; } - private Position motionPosition; + private Date motionTime; + + public Date getMotionTime() { + return motionTime; + } + + public void setMotionTime(Date motionTime) { + this.motionTime = motionTime; + } + + private double motionDistance; - public void setMotionPosition(Position motionPosition) { - this.motionPosition = motionPosition; + public double getMotionDistance() { + return motionDistance; } - public Position getMotionPosition() { - return motionPosition; + public void setMotionDistance(double motionDistance) { + this.motionDistance = motionDistance; } private boolean overspeedState; |