aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2023-11-14 07:35:45 -0800
committerAnton Tananaev <anton@traccar.org>2023-11-14 07:35:45 -0800
commit3aa0729fba832e13da2fd0a6a1c600636fa235a9 (patch)
treed4fcdfa5fecfed8168c4399329246d03dab18a03 /src/main
parentcd5413ae082a55e94d46f38a2a73f72eddb92aca (diff)
downloadtrackermap-server-3aa0729fba832e13da2fd0a6a1c600636fa235a9.tar.gz
trackermap-server-3aa0729fba832e13da2fd0a6a1c600636fa235a9.tar.bz2
trackermap-server-3aa0729fba832e13da2fd0a6a1c600636fa235a9.zip
Prefer used fuel attribute
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/org/traccar/handler/DistanceHandler.java16
-rw-r--r--src/main/java/org/traccar/reports/common/ReportUtils.java21
2 files changed, 14 insertions, 23 deletions
diff --git a/src/main/java/org/traccar/handler/DistanceHandler.java b/src/main/java/org/traccar/handler/DistanceHandler.java
index 6bb774a15..db8c73779 100644
--- a/src/main/java/org/traccar/handler/DistanceHandler.java
+++ b/src/main/java/org/traccar/handler/DistanceHandler.java
@@ -17,6 +17,8 @@
package org.traccar.handler;
import io.netty.channel.ChannelHandler;
+import jakarta.inject.Inject;
+import jakarta.inject.Singleton;
import org.traccar.BaseDataHandler;
import org.traccar.config.Config;
import org.traccar.config.Keys;
@@ -24,11 +26,6 @@ import org.traccar.helper.DistanceCalculator;
import org.traccar.model.Position;
import org.traccar.session.cache.CacheManager;
-import jakarta.inject.Inject;
-import jakarta.inject.Singleton;
-import java.math.BigDecimal;
-import java.math.RoundingMode;
-
@Singleton
@ChannelHandler.Sharable
public class DistanceHandler extends BaseDataHandler {
@@ -54,8 +51,7 @@ public class DistanceHandler extends BaseDataHandler {
if (position.hasAttribute(Position.KEY_DISTANCE)) {
distance = position.getDouble(Position.KEY_DISTANCE);
}
- double totalDistance = 0.0;
-
+ double totalDistance;
Position last = cacheManager.getPosition(position.getDeviceId());
if (last != null) {
totalDistance = last.getDouble(Position.KEY_TOTAL_DISTANCE);
@@ -63,7 +59,6 @@ public class DistanceHandler extends BaseDataHandler {
distance = DistanceCalculator.distance(
position.getLatitude(), position.getLongitude(),
last.getLatitude(), last.getLongitude());
- distance = BigDecimal.valueOf(distance).setScale(2, RoundingMode.HALF_EVEN).doubleValue();
}
if (filter && last.getLatitude() != 0 && last.getLongitude() != 0) {
boolean satisfiesMin = minError == 0 || distance > minError;
@@ -75,10 +70,11 @@ public class DistanceHandler extends BaseDataHandler {
distance = 0;
}
}
+ } else {
+ totalDistance = 0.0;
}
position.set(Position.KEY_DISTANCE, distance);
- totalDistance = BigDecimal.valueOf(totalDistance + distance).setScale(2, RoundingMode.HALF_EVEN).doubleValue();
- position.set(Position.KEY_TOTAL_DISTANCE, totalDistance);
+ position.set(Position.KEY_TOTAL_DISTANCE, totalDistance + distance);
return position;
}
diff --git a/src/main/java/org/traccar/reports/common/ReportUtils.java b/src/main/java/org/traccar/reports/common/ReportUtils.java
index 43db82708..3b8e84887 100644
--- a/src/main/java/org/traccar/reports/common/ReportUtils.java
+++ b/src/main/java/org/traccar/reports/common/ReportUtils.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2016 - 2022 Anton Tananaev (anton@traccar.org)
+ * Copyright 2016 - 2023 Anton Tananaev (anton@traccar.org)
* Copyright 2016 - 2017 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,6 +16,8 @@
*/
package org.traccar.reports.common;
+import jakarta.annotation.Nullable;
+import jakarta.inject.Inject;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.tools.generic.DateTool;
import org.apache.velocity.tools.generic.NumberTool;
@@ -52,13 +54,9 @@ import org.traccar.storage.query.Condition;
import org.traccar.storage.query.Order;
import org.traccar.storage.query.Request;
-import jakarta.annotation.Nullable;
-import jakarta.inject.Inject;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-import java.math.BigDecimal;
-import java.math.RoundingMode;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Date;
@@ -102,14 +100,11 @@ public class ReportUtils {
}
}
- public double calculateFuel(Position firstPosition, Position lastPosition) {
-
- if (firstPosition.getAttributes().get(Position.KEY_FUEL_LEVEL) != null
- && lastPosition.getAttributes().get(Position.KEY_FUEL_LEVEL) != null) {
-
- BigDecimal value = BigDecimal.valueOf(firstPosition.getDouble(Position.KEY_FUEL_LEVEL)
- - lastPosition.getDouble(Position.KEY_FUEL_LEVEL));
- return value.setScale(1, RoundingMode.HALF_EVEN).doubleValue();
+ public double calculateFuel(Position first, Position last) {
+ if (first.hasAttribute(Position.KEY_FUEL_USED) && last.hasAttribute(Position.KEY_FUEL_USED)) {
+ return last.getDouble(Position.KEY_FUEL_USED) - first.getDouble(Position.KEY_FUEL_USED);
+ } else if (first.hasAttribute(Position.KEY_FUEL_LEVEL) && last.hasAttribute(Position.KEY_FUEL_LEVEL)) {
+ return first.getDouble(Position.KEY_FUEL_LEVEL) - last.getDouble(Position.KEY_FUEL_LEVEL);
}
return 0;
}