aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/reports/ReportUtils.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/traccar/reports/ReportUtils.java')
-rw-r--r--src/org/traccar/reports/ReportUtils.java45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/org/traccar/reports/ReportUtils.java b/src/org/traccar/reports/ReportUtils.java
index 9f6d34860..34f04ac87 100644
--- a/src/org/traccar/reports/ReportUtils.java
+++ b/src/org/traccar/reports/ReportUtils.java
@@ -16,10 +16,14 @@
*/
package org.traccar.reports;
+import java.math.BigDecimal;
+import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collection;
import org.traccar.Context;
+import org.traccar.helper.Log;
+import org.traccar.model.Position;
public final class ReportUtils {
@@ -35,4 +39,45 @@ public final class ReportUtils {
return result;
}
+ public static double calculateDistance(Position firstPosition, Position lastPosition) {
+ return calculateDistance(firstPosition, lastPosition, true);
+ }
+
+ public static double calculateDistance(Position firstPosition, Position lastPosition, boolean useOdometer) {
+ double distance = 0.0;
+ if (useOdometer && firstPosition.getAttributes().containsKey(Position.KEY_ODOMETER)
+ && lastPosition.getAttributes().containsKey(Position.KEY_ODOMETER)) {
+ distance = (((Number) lastPosition.getAttributes().get(Position.KEY_ODOMETER)).doubleValue()
+ - ((Number) firstPosition.getAttributes().get(Position.KEY_ODOMETER)).doubleValue())
+ * 1000;
+ } else if (firstPosition.getAttributes().containsKey(Position.KEY_TOTAL_DISTANCE)
+ && lastPosition.getAttributes().containsKey(Position.KEY_TOTAL_DISTANCE)) {
+ distance = ((Number) lastPosition.getAttributes().get(Position.KEY_TOTAL_DISTANCE)).doubleValue()
+ - ((Number) firstPosition.getAttributes().get(Position.KEY_TOTAL_DISTANCE)).doubleValue();
+ }
+ return distance;
+ }
+
+ public static String calculateFuel(Position firstPosition, Position lastPosition) {
+
+ if (firstPosition.getAttributes().get(Position.KEY_FUEL) != null
+ && lastPosition.getAttributes().get(Position.KEY_FUEL) != null) {
+ try {
+ switch (firstPosition.getProtocol()) {
+ case "meitrack":
+ case "galileo":
+ case "noran":
+ return new BigDecimal(firstPosition.getAttributes().get(Position.KEY_FUEL).toString())
+ .subtract(new BigDecimal(lastPosition.getAttributes().get(Position.KEY_FUEL).toString()))
+ .setScale(2, RoundingMode.HALF_EVEN).toString() + " %";
+ default:
+ break;
+ }
+ } catch (Exception error) {
+ Log.warning(error);
+ }
+ }
+ return "-";
+ }
+
}