From 1ca3ab609cc77706178a9784287272555d577382 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 22 Dec 2020 21:22:06 -0800 Subject: Fix average speed (fix #4616) --- src/main/java/org/traccar/reports/ReportUtils.java | 9 +++++---- src/main/java/org/traccar/reports/Summary.java | 20 ++++++++++++++------ .../java/org/traccar/reports/model/BaseReport.java | 2 +- 3 files changed, 20 insertions(+), 11 deletions(-) (limited to 'src/main/java') 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; } -- cgit v1.2.3