diff options
author | Abyss777 <abyss@fox5.ru> | 2016-08-19 10:45:33 +0500 |
---|---|---|
committer | Abyss777 <abyss@fox5.ru> | 2016-08-19 10:45:33 +0500 |
commit | 1c7437d6de01fffbe3f69853717808b1790413fd (patch) | |
tree | ae6fa7144b30321e754773af4a2d464c9e52c624 /src/org/traccar/reports/ReportUtils.java | |
parent | 3826a93f1009942e22ec80c1e13be71e8e917b01 (diff) | |
download | trackermap-server-1c7437d6de01fffbe3f69853717808b1790413fd.tar.gz trackermap-server-1c7437d6de01fffbe3f69853717808b1790413fd.tar.bz2 trackermap-server-1c7437d6de01fffbe3f69853717808b1790413fd.zip |
- Added trips report
- Refactored reports models
- Added tests for ReportUtils
- Added speed to test-generator.py
- Other changes
Diffstat (limited to 'src/org/traccar/reports/ReportUtils.java')
-rw-r--r-- | src/org/traccar/reports/ReportUtils.java | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/org/traccar/reports/ReportUtils.java b/src/org/traccar/reports/ReportUtils.java index 9f6d34860..03e23498a 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,46 @@ 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) { + BigDecimal distance = new BigDecimal("0.0"); + if (useOdometer && firstPosition.getAttributes().containsKey(Position.KEY_ODOMETER) + && lastPosition.getAttributes().containsKey(Position.KEY_ODOMETER)) { + distance = new BigDecimal(lastPosition.getAttributes().get(Position.KEY_ODOMETER).toString()) + .subtract(new BigDecimal(firstPosition.getAttributes().get(Position.KEY_ODOMETER).toString())) + .multiply(new BigDecimal("1000")); + } else if (firstPosition.getAttributes().containsKey(Position.KEY_TOTAL_DISTANCE) + && lastPosition.getAttributes().containsKey(Position.KEY_TOTAL_DISTANCE)) { + distance = new BigDecimal(lastPosition.getAttributes().get(Position.KEY_TOTAL_DISTANCE).toString()) + .subtract(new BigDecimal(firstPosition.getAttributes().get(Position.KEY_TOTAL_DISTANCE) + .toString())); + } + return distance.setScale(2, RoundingMode.HALF_EVEN).doubleValue(); + } + + public static String calculateSpentFuel(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 "-"; + } + } |