aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/org/traccar/events/MotionEventHandler.java5
-rw-r--r--src/org/traccar/reports/ReportUtils.java114
-rw-r--r--src/org/traccar/reports/Stops.java4
-rw-r--r--src/org/traccar/reports/Trips.java4
-rw-r--r--test/org/traccar/reports/ReportUtilsTest.java16
5 files changed, 68 insertions, 75 deletions
diff --git a/src/org/traccar/events/MotionEventHandler.java b/src/org/traccar/events/MotionEventHandler.java
index 8c2d4c56c..b96898fc1 100644
--- a/src/org/traccar/events/MotionEventHandler.java
+++ b/src/org/traccar/events/MotionEventHandler.java
@@ -61,11 +61,14 @@ public class MotionEventHandler extends BaseEventHandler {
}
public Event updateMotionState(DeviceState deviceState, Position position) {
+ return updateMotionState(deviceState, position, position.getBoolean(Position.KEY_MOTION));
+ }
+
+ public Event updateMotionState(DeviceState deviceState, Position position, boolean newMotion) {
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);
diff --git a/src/org/traccar/reports/ReportUtils.java b/src/org/traccar/reports/ReportUtils.java
index 75f300b1d..cd13b28e6 100644
--- a/src/org/traccar/reports/ReportUtils.java
+++ b/src/org/traccar/reports/ReportUtils.java
@@ -31,6 +31,7 @@ import org.traccar.model.DeviceState;
import org.traccar.model.Driver;
import org.traccar.model.Event;
import org.traccar.model.Position;
+import org.traccar.reports.model.BaseReport;
import org.traccar.reports.model.StopReport;
import org.traccar.reports.model.TripReport;
import org.traccar.reports.model.TripsConfig;
@@ -239,13 +240,16 @@ public final class ReportUtils {
private static boolean isMoving(ArrayList<Position> positions, int index,
TripsConfig tripsConfig, double speedThreshold) {
- if (tripsConfig.getMinimalNoDataDuration() > 0 && (index < positions.size() - 1
- && positions.get(index + 1).getFixTime().getTime() - positions.get(index).getFixTime().getTime()
- >= tripsConfig.getMinimalNoDataDuration())
- || index > 0
- && positions.get(index).getFixTime().getTime() - positions.get(index - 1).getFixTime().getTime()
- >= tripsConfig.getMinimalNoDataDuration()) {
- return false;
+ if (tripsConfig.getMinimalNoDataDuration() > 0) {
+ boolean beforeGap = index < positions.size() - 1
+ && positions.get(index + 1).getFixTime().getTime() - positions.get(index).getFixTime().getTime()
+ >= tripsConfig.getMinimalNoDataDuration();
+ boolean afterGap = index > 0
+ && positions.get(index).getFixTime().getTime() - positions.get(index - 1).getFixTime().getTime()
+ >= tripsConfig.getMinimalNoDataDuration();
+ if (beforeGap || afterGap) {
+ return false;
+ }
}
if (positions.get(index).getAttributes().containsKey(Position.KEY_MOTION)
&& positions.get(index).getAttributes().get(Position.KEY_MOTION) instanceof Boolean) {
@@ -255,76 +259,62 @@ public final class ReportUtils {
}
}
- public static Collection<TripReport> detectTrips(Collection<Position> positionCollection, TripsConfig tripsConfig,
- boolean ignoreOdometer, double speedThreshold) {
- Collection<TripReport> result = new ArrayList<>();
+ public static <T extends BaseReport> Collection<T> detectTripsAndStops(Collection<Position> positionCollection,
+ TripsConfig tripsConfig, boolean ignoreOdometer, double speedThreshold, Class<T> reportClass) {
+ Collection<T> result = new ArrayList<>();
ArrayList<Position> positions = new ArrayList<>(positionCollection);
if (positions != null && !positions.isEmpty()) {
+ boolean trips = reportClass.equals(TripReport.class);
MotionEventHandler motionHandler = new MotionEventHandler(tripsConfig);
DeviceState deviceState = new DeviceState();
deviceState.setMotionState(isMoving(positions, 0, tripsConfig, speedThreshold));
- int startTripIndex = deviceState.getMotionState() ? 0 : -1;
- int startParkingIndex = -1;
+ int startTripIndex = trips && deviceState.getMotionState() ? 0 : -1;
+ int startParkingIndex = !trips && !deviceState.getMotionState() ? 0 : -1;
for (int i = 0; i < positions.size(); i++) {
- positions.get(i).set(Position.KEY_MOTION, isMoving(positions, i, tripsConfig, speedThreshold));
- Event event = motionHandler.updateMotionState(deviceState, positions.get(i));
- if (startTripIndex == -1 && !deviceState.getMotionState() && deviceState.getMotionPosition() != null) {
- startTripIndex = i;
- startParkingIndex = -1;
- }
- if (deviceState.getMotionState()) {
- if (startParkingIndex == -1) {
- startParkingIndex = i;
- } else if (deviceState.getMotionPosition() == null) {
+ Event event = motionHandler.updateMotionState(deviceState, positions.get(i),
+ isMoving(positions, i, tripsConfig, speedThreshold));
+ if (deviceState.getMotionPosition() != null) {
+ if (trips && startTripIndex == -1 && !deviceState.getMotionState()) {
+ startTripIndex = i;
startParkingIndex = -1;
+ } else if (!trips && startParkingIndex == -1 && deviceState.getMotionState()) {
+ startParkingIndex = i;
+ startTripIndex = -1;
}
}
- if (startTripIndex != -1 && startParkingIndex != -1 && event != null && !deviceState.getMotionState()) {
- result.add(calculateTrip(positions, startTripIndex, startParkingIndex, ignoreOdometer));
- startTripIndex = -1;
- }
- }
- if (startTripIndex != -1 && startParkingIndex != -1) {
- result.add(calculateTrip(positions, startTripIndex, startParkingIndex, ignoreOdometer));
- }
- }
- return result;
- }
-
- public static Collection<StopReport> detectStops(Collection<Position> positionCollection, TripsConfig tripsConfig,
- boolean ignoreOdometer, double speedThreshold) {
- Collection<StopReport> result = new ArrayList<>();
-
- ArrayList<Position> positions = new ArrayList<>(positionCollection);
- if (positions != null && !positions.isEmpty()) {
- MotionEventHandler motionHandler = new MotionEventHandler(tripsConfig);
- DeviceState deviceState = new DeviceState();
- deviceState.setMotionState(isMoving(positions, 0, tripsConfig, speedThreshold));
- int startTripIndex = -1;
- int startParkingIndex = deviceState.getMotionState() ? -1 : 0;
- for (int i = 0; i < positions.size(); i++) {
- positions.get(i).set(Position.KEY_MOTION, isMoving(positions, i, tripsConfig, speedThreshold));
- Event event = motionHandler.updateMotionState(deviceState, positions.get(i));
- if (startParkingIndex == -1 && deviceState.getMotionState()
- && deviceState.getMotionPosition() != null) {
- startParkingIndex = i;
- startTripIndex = -1;
+ if (trips) {
+ if (deviceState.getMotionState()) {
+ if (startParkingIndex == -1) {
+ startParkingIndex = i;
+ } else if (deviceState.getMotionPosition() == null) {
+ startParkingIndex = -1;
+ }
+ }
+ } else {
+ if (!deviceState.getMotionState()) {
+ if (startTripIndex == -1) {
+ startTripIndex = i;
+ } else if (deviceState.getMotionPosition() == null) {
+ startTripIndex = -1;
+ }
+ }
}
- if (!deviceState.getMotionState()) {
- if (startTripIndex == -1) {
- startTripIndex = i;
- } else if (deviceState.getMotionPosition() == null) {
+ if (startTripIndex != -1 && startParkingIndex != -1 && event != null) {
+ if (trips && !deviceState.getMotionState()) {
+ result.add((T) calculateTrip(positions, startTripIndex, startParkingIndex, ignoreOdometer));
startTripIndex = -1;
+ } else if (!trips && deviceState.getMotionState()) {
+ result.add((T) calculateStop(positions, startParkingIndex, startTripIndex));
+ startParkingIndex = -1;
}
}
- if (startParkingIndex != -1 && startTripIndex != -1 && event != null && deviceState.getMotionState()) {
- result.add(calculateStop(positions, startParkingIndex, startTripIndex));
- startParkingIndex = -1;
- }
}
- if (startParkingIndex != -1) {
- result.add(calculateStop(positions, startParkingIndex,
+ if (trips && startTripIndex != -1 && startParkingIndex != -1) {
+ result.add((T) calculateTrip(positions, startTripIndex, startParkingIndex, ignoreOdometer));
+ }
+ if (!trips && startParkingIndex != -1) {
+ result.add((T) calculateStop(positions, startParkingIndex,
startTripIndex != -1 ? startTripIndex : positions.size() - 1));
}
}
diff --git a/src/org/traccar/reports/Stops.java b/src/org/traccar/reports/Stops.java
index 68fdef334..1e72cc927 100644
--- a/src/org/traccar/reports/Stops.java
+++ b/src/org/traccar/reports/Stops.java
@@ -44,9 +44,9 @@ public final class Stops {
boolean ignoreOdometer = Context.getDeviceManager()
.lookupAttributeBoolean(deviceId, "report.ignoreOdometer", false, true);
- Collection<StopReport> result = ReportUtils.detectStops(
+ Collection<StopReport> result = ReportUtils.detectTripsAndStops(
Context.getDataManager().getPositions(deviceId, from, to),
- Context.getTripsConfig(), ignoreOdometer, speedThreshold);
+ Context.getTripsConfig(), ignoreOdometer, speedThreshold, StopReport.class);
return result;
}
diff --git a/src/org/traccar/reports/Trips.java b/src/org/traccar/reports/Trips.java
index bb39cf493..1ee62e87c 100644
--- a/src/org/traccar/reports/Trips.java
+++ b/src/org/traccar/reports/Trips.java
@@ -43,9 +43,9 @@ public final class Trips {
boolean ignoreOdometer = Context.getDeviceManager()
.lookupAttributeBoolean(deviceId, "report.ignoreOdometer", false, true);
- Collection<TripReport> result = ReportUtils.detectTrips(
+ Collection<TripReport> result = ReportUtils.detectTripsAndStops(
Context.getDataManager().getPositions(deviceId, from, to),
- Context.getTripsConfig(), ignoreOdometer, speedThreshold);
+ Context.getTripsConfig(), ignoreOdometer, speedThreshold, TripReport.class);
return result;
}
diff --git a/test/org/traccar/reports/ReportUtilsTest.java b/test/org/traccar/reports/ReportUtilsTest.java
index c74109550..c82853161 100644
--- a/test/org/traccar/reports/ReportUtilsTest.java
+++ b/test/org/traccar/reports/ReportUtilsTest.java
@@ -81,7 +81,7 @@ public class ReportUtilsTest extends BaseTest {
TripsConfig tripsConfig = new TripsConfig(500, 300000, 180000, 900000, false);
- Collection<TripReport> trips = ReportUtils.detectTrips(data, tripsConfig, false, 0.01);
+ Collection<TripReport> trips = ReportUtils.detectTripsAndStops(data, tripsConfig, false, 0.01, TripReport.class);
assertNotNull(trips);
assertFalse(trips.isEmpty());
@@ -95,7 +95,7 @@ public class ReportUtilsTest extends BaseTest {
assertEquals(10, itemTrip.getMaxSpeed(), 0.01);
assertEquals(3000, itemTrip.getDistance(), 0.01);
- Collection<StopReport> stops = ReportUtils.detectStops(data, tripsConfig, false, 0.01);
+ Collection<StopReport> stops = ReportUtils.detectTripsAndStops(data, tripsConfig, false, 0.01, StopReport.class);
assertNotNull(stops);
assertFalse(stops.isEmpty());
@@ -129,7 +129,7 @@ public class ReportUtilsTest extends BaseTest {
TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, 900000, false);
- Collection<StopReport> result = ReportUtils.detectStops(data, tripsConfig, false, 0.01);
+ Collection<StopReport> result = ReportUtils.detectTripsAndStops(data, tripsConfig, false, 0.01, StopReport.class);
assertNotNull(result);
assertFalse(result.isEmpty());
@@ -155,7 +155,7 @@ public class ReportUtilsTest extends BaseTest {
TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, 900000, false);
- Collection<StopReport> result = ReportUtils.detectStops(data, tripsConfig, false, 0.01);
+ Collection<StopReport> result = ReportUtils.detectTripsAndStops(data, tripsConfig, false, 0.01, StopReport.class);
assertNotNull(result);
assertFalse(result.isEmpty());
@@ -181,7 +181,7 @@ public class ReportUtilsTest extends BaseTest {
TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, 900000, false);
- Collection<StopReport> result = ReportUtils.detectStops(data, tripsConfig, false, 0.01);
+ Collection<StopReport> result = ReportUtils.detectTripsAndStops(data, tripsConfig, false, 0.01, StopReport.class);
assertNotNull(result);
assertFalse(result.isEmpty());
@@ -207,7 +207,7 @@ public class ReportUtilsTest extends BaseTest {
TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, 900000, false);
- Collection<StopReport> result = ReportUtils.detectStops(data, tripsConfig, false, 0.01);
+ Collection<StopReport> result = ReportUtils.detectTripsAndStops(data, tripsConfig, false, 0.01, StopReport.class);
assertNotNull(result);
assertTrue(result.isEmpty());
@@ -229,7 +229,7 @@ public class ReportUtilsTest extends BaseTest {
TripsConfig tripsConfig = new TripsConfig(500, 200000, 200000, 900000, false);
- Collection<TripReport> trips = ReportUtils.detectTrips(data, tripsConfig, false, 0.01);
+ Collection<TripReport> trips = ReportUtils.detectTripsAndStops(data, tripsConfig, false, 0.01, TripReport.class);
assertNotNull(trips);
assertFalse(trips.isEmpty());
@@ -243,7 +243,7 @@ public class ReportUtilsTest extends BaseTest {
assertEquals(7, itemTrip.getMaxSpeed(), 0.01);
assertEquals(600, itemTrip.getDistance(), 0.01);
- Collection<StopReport> stops = ReportUtils.detectStops(data, tripsConfig, false, 0.01);
+ Collection<StopReport> stops = ReportUtils.detectTripsAndStops(data, tripsConfig, false, 0.01, StopReport.class);
assertNotNull(stops);
assertFalse(stops.isEmpty());