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/main/java | |
parent | 2fc6343fc770bd09ba76e4511f0f94ac786d2c6a (diff) | |
download | trackermap-server-1ca3ab609cc77706178a9784287272555d577382.tar.gz trackermap-server-1ca3ab609cc77706178a9784287272555d577382.tar.bz2 trackermap-server-1ca3ab609cc77706178a9784287272555d577382.zip |
Fix average speed (fix #4616)
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/org/traccar/reports/ReportUtils.java | 9 | ||||
-rw-r--r-- | src/main/java/org/traccar/reports/Summary.java | 20 | ||||
-rw-r--r-- | src/main/java/org/traccar/reports/model/BaseReport.java | 2 |
3 files changed, 20 insertions, 11 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; } |