diff options
author | Abyss777 <abyss@fox5.ru> | 2017-08-14 15:34:48 +0500 |
---|---|---|
committer | Abyss777 <abyss@fox5.ru> | 2017-08-15 15:01:47 +0500 |
commit | f1ee1017d62ec0d1b6b1ca56c297537870f1d32b (patch) | |
tree | 7a45608db55264fe7048c8097698717d235deabe | |
parent | 30e8409375f9992a5248f7e2597691c0ee08188c (diff) | |
download | trackermap-server-f1ee1017d62ec0d1b6b1ca56c297537870f1d32b.tar.gz trackermap-server-f1ee1017d62ec0d1b6b1ca56c297537870f1d32b.tar.bz2 trackermap-server-f1ee1017d62ec0d1b6b1ca56c297537870f1d32b.zip |
Reimplement trips detector using MotionEventHandler
-rw-r--r-- | src/org/traccar/reports/ReportUtils.java | 147 | ||||
-rw-r--r-- | src/org/traccar/reports/Stops.java | 9 | ||||
-rw-r--r-- | src/org/traccar/reports/Trips.java | 9 | ||||
-rw-r--r-- | test/org/traccar/reports/ReportUtilsTest.java | 75 |
4 files changed, 109 insertions, 131 deletions
diff --git a/src/org/traccar/reports/ReportUtils.java b/src/org/traccar/reports/ReportUtils.java index 8c39bb9dc..91dc2e019 100644 --- a/src/org/traccar/reports/ReportUtils.java +++ b/src/org/traccar/reports/ReportUtils.java @@ -26,9 +26,11 @@ import org.jxls.transform.Transformer; import org.jxls.transform.poi.PoiTransformer; import org.jxls.util.TransformerFactory; import org.traccar.Context; +import org.traccar.events.MotionEventHandler; +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; @@ -237,8 +239,11 @@ 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 + 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; } @@ -250,100 +255,80 @@ public final class ReportUtils { } } - public static Collection<BaseReport> detectTripsAndStops(TripsConfig tripsConfig, boolean ignoreOdometer, - double speedThreshold, Collection<Position> positionCollection, boolean trips) { - - Collection<BaseReport> result = new ArrayList<>(); + public static Collection<TripReport> detectTrips(Collection<Position> positionCollection, TripsConfig tripsConfig, + boolean ignoreOdometer, double speedThreshold) { + Collection<TripReport> result = new ArrayList<>(); ArrayList<Position> positions = new ArrayList<>(positionCollection); if (positions != null && !positions.isEmpty()) { - int previousStartParkingIndex = 0; + 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 previousEndParkingIndex = 0; - int endParkingIndex = 0; - - boolean isMoving = false; - boolean isLast = false; - boolean skipped = false; - boolean tripFiltered = false; - for (int i = 0; i < positions.size(); i++) { - isMoving = isMoving(positions, i, tripsConfig, speedThreshold); - isLast = i == positions.size() - 1; - - if ((isMoving || isLast) && startParkingIndex != -1) { - if (!skipped || previousEndParkingIndex == 0) { - previousEndParkingIndex = endParkingIndex; - } - endParkingIndex = 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 (!isMoving && startParkingIndex == -1) { - if (tripsConfig.getGreedyParking()) { - long tripDuration = positions.get(i).getFixTime().getTime() - - positions.get(endParkingIndex).getFixTime().getTime(); - double tripDistance = ReportUtils.calculateDistance(positions.get(endParkingIndex), - positions.get(i), false); - tripFiltered = tripDuration < tripsConfig.getMinimalTripDuration() - && tripDistance < tripsConfig.getMinimalTripDistance(); - if (tripFiltered) { - startParkingIndex = previousStartParkingIndex; - endParkingIndex = previousEndParkingIndex; - tripFiltered = false; - } else { - previousStartParkingIndex = i; - startParkingIndex = i; - } - } else { - long tripDuration = positions.get(i).getFixTime().getTime() - - positions.get(previousEndParkingIndex).getFixTime().getTime(); - double tripDistance = ReportUtils.calculateDistance(positions.get(previousEndParkingIndex), - positions.get(i), false); - tripFiltered = tripDuration < tripsConfig.getMinimalTripDuration() - && tripDistance < tripsConfig.getMinimalTripDistance(); + if (deviceState.getMotionState()) { + if (startParkingIndex == -1) { startParkingIndex = i; + } else if (deviceState.getMotionPosition() == null) { + startParkingIndex = -1; } } - if (startParkingIndex != -1 && (endParkingIndex > startParkingIndex || isLast)) { - long parkingDuration = positions.get(endParkingIndex).getFixTime().getTime() - - positions.get(startParkingIndex).getFixTime().getTime(); - if ((parkingDuration >= tripsConfig.getMinimalParkingDuration() || isLast) - && previousEndParkingIndex < startParkingIndex) { - if (!tripFiltered) { - if (trips) { - result.add(calculateTrip( - positions, previousEndParkingIndex, startParkingIndex, ignoreOdometer)); - } else { - if (result.isEmpty() && previousEndParkingIndex > previousStartParkingIndex) { - long previousParkingDuration = positions.get(previousEndParkingIndex) - .getFixTime().getTime() - positions.get(previousStartParkingIndex) - .getFixTime().getTime(); - if (previousParkingDuration >= tripsConfig.getMinimalParkingDuration()) { - result.add(calculateStop(positions, previousStartParkingIndex, - previousEndParkingIndex)); - } - } - result.add(calculateStop(positions, startParkingIndex, isLast ? i : endParkingIndex)); - } - } - previousEndParkingIndex = endParkingIndex; - skipped = false; - } else { - skipped = true; + 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++) { + boolean isMoving = isMoving(positions, i, tripsConfig, speedThreshold); + positions.get(i).set(Position.KEY_MOTION, isMoving); + Event event = motionHandler.updateMotionState(deviceState, positions.get(i)); + if (startParkingIndex == -1 && deviceState.getMotionState() + && deviceState.getMotionPosition() != null) { + startParkingIndex = i; + startTripIndex = -1; + } + if (!deviceState.getMotionState()) { + if (startTripIndex == -1) { + startTripIndex = i; + } else if (deviceState.getMotionPosition() == null) { + startTripIndex = -1; } + } + if (startParkingIndex != -1 && startTripIndex != -1 && event != null && deviceState.getMotionState()) { + result.add(calculateStop(positions, startParkingIndex, startTripIndex)); startParkingIndex = -1; } } - if (result.isEmpty() && !trips) { - int end = isMoving && !tripsConfig.getGreedyParking() - ? Math.max(endParkingIndex, previousEndParkingIndex) : positions.size() - 1; - long parkingDuration = positions.get(end).getFixTime().getTime() - - positions.get(previousStartParkingIndex).getFixTime().getTime(); - if (parkingDuration >= tripsConfig.getMinimalParkingDuration()) { - result.add(calculateStop(positions, previousStartParkingIndex, end)); - } + if (startParkingIndex != -1) { + result.add(calculateStop(positions, startParkingIndex, + startTripIndex != -1 ? startTripIndex : positions.size() - 1)); } } - return result; } } diff --git a/src/org/traccar/reports/Stops.java b/src/org/traccar/reports/Stops.java index 3f7f674b5..68fdef334 100644 --- a/src/org/traccar/reports/Stops.java +++ b/src/org/traccar/reports/Stops.java @@ -30,7 +30,6 @@ import org.apache.poi.ss.util.WorkbookUtil; import org.traccar.Context; import org.traccar.model.Device; import org.traccar.model.Group; -import org.traccar.reports.model.BaseReport; import org.traccar.reports.model.DeviceReport; import org.traccar.reports.model.StopReport; @@ -45,11 +44,11 @@ public final class Stops { boolean ignoreOdometer = Context.getDeviceManager() .lookupAttributeBoolean(deviceId, "report.ignoreOdometer", false, true); - Collection<? extends BaseReport> result = ReportUtils.detectTripsAndStops( - Context.getTripsConfig(), ignoreOdometer, speedThreshold, - Context.getDataManager().getPositions(deviceId, from, to), false); + Collection<StopReport> result = ReportUtils.detectStops( + Context.getDataManager().getPositions(deviceId, from, to), + Context.getTripsConfig(), ignoreOdometer, speedThreshold); - return (Collection<StopReport>) result; + return result; } public static Collection<StopReport> getObjects( diff --git a/src/org/traccar/reports/Trips.java b/src/org/traccar/reports/Trips.java index a4dcf00f1..bb39cf493 100644 --- a/src/org/traccar/reports/Trips.java +++ b/src/org/traccar/reports/Trips.java @@ -29,7 +29,6 @@ import org.apache.poi.ss.util.WorkbookUtil; import org.traccar.Context; import org.traccar.model.Device; import org.traccar.model.Group; -import org.traccar.reports.model.BaseReport; import org.traccar.reports.model.DeviceReport; import org.traccar.reports.model.TripReport; @@ -44,11 +43,11 @@ public final class Trips { boolean ignoreOdometer = Context.getDeviceManager() .lookupAttributeBoolean(deviceId, "report.ignoreOdometer", false, true); - Collection<? extends BaseReport> result = ReportUtils.detectTripsAndStops( - Context.getTripsConfig(), ignoreOdometer, speedThreshold, - Context.getDataManager().getPositions(deviceId, from, to), true); + Collection<TripReport> result = ReportUtils.detectTrips( + Context.getDataManager().getPositions(deviceId, from, to), + Context.getTripsConfig(), ignoreOdometer, speedThreshold); - return (Collection<TripReport>) result; + return result; } public static Collection<TripReport> getObjects(long userId, Collection<Long> deviceIds, Collection<Long> groupIds, diff --git a/test/org/traccar/reports/ReportUtilsTest.java b/test/org/traccar/reports/ReportUtilsTest.java index adcdf5875..1b552baa3 100644 --- a/test/org/traccar/reports/ReportUtilsTest.java +++ b/test/org/traccar/reports/ReportUtilsTest.java @@ -11,13 +11,13 @@ import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Collection; import java.util.Date; +import java.util.Iterator; import java.util.TimeZone; import org.junit.Assert; import org.junit.Test; import org.traccar.BaseTest; 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; @@ -81,12 +81,12 @@ public class ReportUtilsTest extends BaseTest { TripsConfig tripsConfig = new TripsConfig(500, 300000, 180000, false, 900000); - Collection<? extends BaseReport> result = ReportUtils.detectTripsAndStops(tripsConfig, false, 0.01, data, true); + Collection<TripReport> trips = ReportUtils.detectTrips(data, tripsConfig, false, 0.01); - assertNotNull(result); - assertFalse(result.isEmpty()); + assertNotNull(trips); + assertFalse(trips.isEmpty()); - TripReport itemTrip = (TripReport) result.iterator().next(); + TripReport itemTrip = trips.iterator().next(); assertEquals(date("2016-01-01 00:02:00.000"), itemTrip.getStartTime()); assertEquals(date("2016-01-01 00:05:00.000"), itemTrip.getEndTime()); @@ -95,12 +95,20 @@ public class ReportUtilsTest extends BaseTest { assertEquals(10, itemTrip.getMaxSpeed(), 0.01); assertEquals(3000, itemTrip.getDistance(), 0.01); - result = ReportUtils.detectTripsAndStops(tripsConfig, false, 0.01, data, false); + Collection<StopReport> stops = ReportUtils.detectStops(data, tripsConfig, false, 0.01); - assertNotNull(result); - assertFalse(result.isEmpty()); + assertNotNull(stops); + assertFalse(stops.isEmpty()); - StopReport itemStop = (StopReport) result.iterator().next(); + Iterator<StopReport> iterator = stops.iterator(); + + StopReport itemStop = iterator.next(); + + assertEquals(date("2016-01-01 00:00:00.000"), itemStop.getStartTime()); + assertEquals(date("2016-01-01 00:02:00.000"), itemStop.getEndTime()); + assertEquals(120000, itemStop.getDuration()); + + itemStop = iterator.next(); assertEquals(date("2016-01-01 00:05:00.000"), itemStop.getStartTime()); assertEquals(date("2016-01-01 00:07:00.000"), itemStop.getEndTime()); @@ -121,12 +129,12 @@ public class ReportUtilsTest extends BaseTest { TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, false, 900000); - Collection<? extends BaseReport> result = ReportUtils.detectTripsAndStops(tripsConfig, false, 0.01, data, false); + Collection<StopReport> result = ReportUtils.detectStops(data, tripsConfig, false, 0.01); assertNotNull(result); assertFalse(result.isEmpty()); - StopReport itemStop = (StopReport) result.iterator().next(); + StopReport itemStop = result.iterator().next(); assertEquals(date("2016-01-01 00:00:00.000"), itemStop.getStartTime()); assertEquals(date("2016-01-01 00:05:00.000"), itemStop.getEndTime()); @@ -147,32 +155,19 @@ public class ReportUtilsTest extends BaseTest { TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, false, 900000); - Collection<? extends BaseReport> result = ReportUtils.detectTripsAndStops(tripsConfig, false, 0.01, data, false); + Collection<StopReport> result = ReportUtils.detectStops(data, tripsConfig, false, 0.01); assertNotNull(result); assertFalse(result.isEmpty()); - StopReport itemStop = (StopReport) result.iterator().next(); + StopReport itemStop = result.iterator().next(); assertEquals(date("2016-01-01 00:00:00.000"), itemStop.getStartTime()); assertEquals(date("2016-01-01 00:04:00.000"), itemStop.getEndTime()); assertEquals(240000, itemStop.getDuration()); - tripsConfig.setGreedyParking(true); - - result = ReportUtils.detectTripsAndStops(tripsConfig, false, 0.01, data, false); - - assertNotNull(result); - assertFalse(result.isEmpty()); - - itemStop = (StopReport) result.iterator().next(); - - assertEquals(date("2016-01-01 00:00:00.000"), itemStop.getStartTime()); - assertEquals(date("2016-01-01 00:05:00.000"), itemStop.getEndTime()); - assertEquals(300000, itemStop.getDuration()); - } - + @Test public void testDetectStopsStartedFromTrip() throws ParseException { @@ -186,16 +181,16 @@ public class ReportUtilsTest extends BaseTest { TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, false, 900000); - Collection<? extends BaseReport> result = ReportUtils.detectTripsAndStops(tripsConfig, false, 0.01, data, false); + Collection<StopReport> result = ReportUtils.detectStops(data, tripsConfig, false, 0.01); assertNotNull(result); assertFalse(result.isEmpty()); - StopReport itemStop = (StopReport) result.iterator().next(); + StopReport itemStop = result.iterator().next(); - assertEquals(date("2016-01-01 00:00:00.000"), itemStop.getStartTime()); + assertEquals(date("2016-01-01 00:02:00.000"), itemStop.getStartTime()); assertEquals(date("2016-01-01 00:05:00.000"), itemStop.getEndTime()); - assertEquals(300000, itemStop.getDuration()); + assertEquals(180000, itemStop.getDuration()); } @@ -212,7 +207,7 @@ public class ReportUtilsTest extends BaseTest { TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, false, 900000); - Collection<? extends BaseReport> result = ReportUtils.detectTripsAndStops(tripsConfig, false, 0.01, data, false); + Collection<StopReport> result = ReportUtils.detectStops(data, tripsConfig, false, 0.01); assertNotNull(result); assertTrue(result.isEmpty()); @@ -234,12 +229,12 @@ public class ReportUtilsTest extends BaseTest { TripsConfig tripsConfig = new TripsConfig(500, 200000, 200000, false, 900000); - Collection<? extends BaseReport> result = ReportUtils.detectTripsAndStops(tripsConfig, false, 0.01, data, true); + Collection<TripReport> trips = ReportUtils.detectTrips(data, tripsConfig, false, 0.01); - assertNotNull(result); - assertFalse(result.isEmpty()); + assertNotNull(trips); + assertFalse(trips.isEmpty()); - TripReport itemTrip = (TripReport) result.iterator().next(); + TripReport itemTrip = trips.iterator().next(); assertEquals(date("2016-01-01 00:00:00.000"), itemTrip.getStartTime()); assertEquals(date("2016-01-01 00:04:00.000"), itemTrip.getEndTime()); @@ -248,12 +243,12 @@ public class ReportUtilsTest extends BaseTest { assertEquals(7, itemTrip.getMaxSpeed(), 0.01); assertEquals(600, itemTrip.getDistance(), 0.01); - result = ReportUtils.detectTripsAndStops(tripsConfig, false, 0.01, data, false); + Collection<StopReport> stops = ReportUtils.detectStops(data, tripsConfig, false, 0.01); - assertNotNull(result); - assertFalse(result.isEmpty()); + assertNotNull(stops); + assertFalse(stops.isEmpty()); - StopReport itemStop = (StopReport) result.iterator().next(); + StopReport itemStop = stops.iterator().next(); assertEquals(date("2016-01-01 00:04:00.000"), itemStop.getStartTime()); assertEquals(date("2016-01-01 00:23:00.000"), itemStop.getEndTime()); |