diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2020-12-22 21:22:06 -0800 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2020-12-22 21:22:06 -0800 |
commit | 1ca3ab609cc77706178a9784287272555d577382 (patch) | |
tree | fe3b11ce140666e6f0febe51748aefb4fc986ecc /src | |
parent | 2fc6343fc770bd09ba76e4511f0f94ac786d2c6a (diff) | |
download | traccar-server-1ca3ab609cc77706178a9784287272555d577382.tar.gz traccar-server-1ca3ab609cc77706178a9784287272555d577382.tar.bz2 traccar-server-1ca3ab609cc77706178a9784287272555d577382.zip |
Fix average speed (fix #4616)
Diffstat (limited to 'src')
4 files changed, 25 insertions, 16 deletions
diff --git a/src/main/java/org/traccar/reports/ReportUtils.java b/src/main/java/org/traccar/reports/ReportUtils.java index 870264fad..8dd77606c 100644 --- a/src/main/java/org/traccar/reports/ReportUtils.java +++ b/src/main/java/org/traccar/reports/ReportUtils.java @@ -29,6 +29,7 @@ import org.traccar.Context; import org.traccar.database.DeviceManager; import org.traccar.database.IdentityManager; import org.traccar.handler.events.MotionEventHandler; +import org.traccar.helper.UnitsConverter; import org.traccar.model.DeviceState; import org.traccar.model.Driver; import org.traccar.model.Event; @@ -173,11 +174,9 @@ public final class ReportUtils { Position startTrip = positions.get(startIndex); Position endTrip = positions.get(endIndex); - double speedMax = 0.0; - double speedSum = 0.0; + double speedMax = 0; for (int i = startIndex; i <= endIndex; i++) { double speed = positions.get(i).getSpeed(); - speedSum += speed; if (speed > speedMax) { speedMax = speed; } @@ -214,7 +213,9 @@ public final class ReportUtils { trip.setDistance(calculateDistance(startTrip, endTrip, !ignoreOdometer)); trip.setDuration(tripDuration); - trip.setAverageSpeed(speedSum / (endIndex - startIndex)); + if (tripDuration > 0) { + trip.setAverageSpeed(UnitsConverter.knotsFromMps(trip.getDistance() * 1000 / tripDuration)); + } trip.setMaxSpeed(speedMax); trip.setSpentFuel(calculateFuel(startTrip, endTrip)); diff --git a/src/main/java/org/traccar/reports/Summary.java b/src/main/java/org/traccar/reports/Summary.java index 83fe9caf7..d576ac1e6 100644 --- a/src/main/java/org/traccar/reports/Summary.java +++ b/src/main/java/org/traccar/reports/Summary.java @@ -28,6 +28,7 @@ import java.util.Date; import org.jxls.util.JxlsHelper; import org.traccar.Context; +import org.traccar.helper.UnitsConverter; import org.traccar.model.Position; import org.traccar.reports.model.SummaryReport; @@ -43,13 +44,11 @@ public final class Summary { if (positions != null && !positions.isEmpty()) { Position firstPosition = null; Position previousPosition = null; - double speedSum = 0; for (Position position : positions) { if (firstPosition == null) { firstPosition = position; } previousPosition = position; - speedSum += position.getSpeed(); if (position.getSpeed() > result.getMaxSpeed()) { result.setMaxSpeed(position.getSpeed()); } @@ -57,13 +56,22 @@ public final class Summary { boolean ignoreOdometer = Context.getDeviceManager() .lookupAttributeBoolean(deviceId, "report.ignoreOdometer", false, false, true); result.setDistance(ReportUtils.calculateDistance(firstPosition, previousPosition, !ignoreOdometer)); - result.setAverageSpeed(speedSum / positions.size()); result.setSpentFuel(ReportUtils.calculateFuel(firstPosition, previousPosition)); + long durationMilliseconds; if (firstPosition.getAttributes().containsKey(Position.KEY_HOURS) && previousPosition.getAttributes().containsKey(Position.KEY_HOURS)) { - result.setEngineHours( - previousPosition.getLong(Position.KEY_HOURS) - firstPosition.getLong(Position.KEY_HOURS)); + durationMilliseconds = + previousPosition.getLong(Position.KEY_HOURS) - firstPosition.getLong(Position.KEY_HOURS); + result.setEngineHours(durationMilliseconds); + } else { + durationMilliseconds = + previousPosition.getFixTime().getTime() - firstPosition.getFixTime().getTime(); + } + + if (durationMilliseconds > 0) { + result.setAverageSpeed( + UnitsConverter.knotsFromMps(result.getDistance() * 1000 / durationMilliseconds)); } if (!ignoreOdometer @@ -77,7 +85,7 @@ public final class Summary { } result.setStartTime(firstPosition.getFixTime()); - result.setEndTime(previousPosition.getServerTime()); + result.setEndTime(previousPosition.getFixTime()); } return result; } diff --git a/src/main/java/org/traccar/reports/model/BaseReport.java b/src/main/java/org/traccar/reports/model/BaseReport.java index d0719576e..928c0557d 100644 --- a/src/main/java/org/traccar/reports/model/BaseReport.java +++ b/src/main/java/org/traccar/reports/model/BaseReport.java @@ -60,7 +60,7 @@ public class BaseReport { return averageSpeed; } - public void setAverageSpeed(Double averageSpeed) { + public void setAverageSpeed(double averageSpeed) { this.averageSpeed = averageSpeed; } diff --git a/src/test/java/org/traccar/reports/ReportUtilsTest.java b/src/test/java/org/traccar/reports/ReportUtilsTest.java index 01b9c276f..deb17ddd2 100644 --- a/src/test/java/org/traccar/reports/ReportUtilsTest.java +++ b/src/test/java/org/traccar/reports/ReportUtilsTest.java @@ -91,7 +91,7 @@ public class ReportUtilsTest extends BaseTest { assertEquals(date("2016-01-01 00:02:00.000"), itemTrip.getStartTime()); assertEquals(date("2016-01-01 00:05:00.000"), itemTrip.getEndTime()); assertEquals(180000, itemTrip.getDuration()); - assertEquals(10, itemTrip.getAverageSpeed(), 0.01); + assertEquals(32.4, itemTrip.getAverageSpeed(), 0.01); assertEquals(10, itemTrip.getMaxSpeed(), 0.01); assertEquals(3000, itemTrip.getDistance(), 0.01); @@ -145,7 +145,7 @@ public class ReportUtilsTest extends BaseTest { assertEquals(date("2016-01-01 00:02:00.000"), itemTrip.getStartTime()); assertEquals(date("2016-01-01 00:05:00.000"), itemTrip.getEndTime()); assertEquals(180000, itemTrip.getDuration()); - assertEquals(10, itemTrip.getAverageSpeed(), 0.01); + assertEquals(32.4, itemTrip.getAverageSpeed(), 0.01); assertEquals(10, itemTrip.getMaxSpeed(), 0.01); assertEquals(3000, itemTrip.getDistance(), 0.01); @@ -160,7 +160,7 @@ public class ReportUtilsTest extends BaseTest { assertEquals(date("2016-01-01 00:02:00.000"), itemTrip.getStartTime()); assertEquals(date("2016-01-01 00:05:00.000"), itemTrip.getEndTime()); assertEquals(180000, itemTrip.getDuration()); - assertEquals(10, itemTrip.getAverageSpeed(), 0.01); + assertEquals(32.4, itemTrip.getAverageSpeed(), 0.01); assertEquals(10, itemTrip.getMaxSpeed(), 0.01); assertEquals(3000, itemTrip.getDistance(), 0.01); @@ -216,7 +216,7 @@ public class ReportUtilsTest extends BaseTest { assertEquals(date("2016-01-01 00:02:00.000"), itemTrip.getStartTime()); assertEquals(date("2016-01-01 00:09:00.000"), itemTrip.getEndTime()); assertEquals(420000, itemTrip.getDuration()); - assertEquals(8.57, itemTrip.getAverageSpeed(), 0.01); + assertEquals(32.4, itemTrip.getAverageSpeed(), 0.01); assertEquals(10, itemTrip.getMaxSpeed(), 0.01); assertEquals(7000, itemTrip.getDistance(), 0.01); @@ -370,7 +370,7 @@ public class ReportUtilsTest extends BaseTest { assertEquals(date("2016-01-01 00:00:00.000"), itemTrip.getStartTime()); assertEquals(date("2016-01-01 00:04:00.000"), itemTrip.getEndTime()); assertEquals(240000, itemTrip.getDuration()); - assertEquals(6.75, itemTrip.getAverageSpeed(), 0.01); + assertEquals(4.86, itemTrip.getAverageSpeed(), 0.01); assertEquals(7, itemTrip.getMaxSpeed(), 0.01); assertEquals(600, itemTrip.getDistance(), 0.01); |