From 1c7437d6de01fffbe3f69853717808b1790413fd Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Fri, 19 Aug 2016 10:45:33 +0500 Subject: - Added trips report - Refactored reports models - Added tests for ReportUtils - Added speed to test-generator.py - Other changes --- test/org/traccar/reports/ReportUtilsTest.java | 34 +++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 test/org/traccar/reports/ReportUtilsTest.java (limited to 'test/org/traccar') diff --git a/test/org/traccar/reports/ReportUtilsTest.java b/test/org/traccar/reports/ReportUtilsTest.java new file mode 100644 index 000000000..e7f762b28 --- /dev/null +++ b/test/org/traccar/reports/ReportUtilsTest.java @@ -0,0 +1,34 @@ +package org.traccar.reports; + +import org.junit.Assert; +import org.junit.Test; +import org.traccar.model.Position; + +public class ReportUtilsTest { + + @Test + public void testCalculateDistance() { + Position startPosition = new Position(); + startPosition.set(Position.KEY_TOTAL_DISTANCE, 500); + Position endPosition = new Position(); + endPosition.set(Position.KEY_TOTAL_DISTANCE, 700); + Assert.assertEquals(ReportUtils.calculateDistance(startPosition, endPosition), 200.0, 10); + startPosition.set(Position.KEY_ODOMETER, 50000); + endPosition.set(Position.KEY_ODOMETER, 50001); + Assert.assertEquals(ReportUtils.calculateDistance(startPosition, endPosition), 1000.0, 10); + } + + @Test + public void testCalculateSpentFuel() { + Position startPosition = new Position(); + Position endPosition = new Position(); + Assert.assertEquals(ReportUtils.calculateSpentFuel(startPosition, endPosition), "-"); + startPosition.setProtocol("meitrack"); + startPosition.set(Position.KEY_FUEL, 0.07); + endPosition.set(Position.KEY_FUEL, 0.05); + Assert.assertEquals(ReportUtils.calculateSpentFuel(startPosition, endPosition), "0.02 %"); + startPosition.setProtocol("galileo"); + Assert.assertEquals(ReportUtils.calculateSpentFuel(startPosition, endPosition), "0.02 %"); + } + +} -- cgit v1.2.3 From 1ce8c1945e36acd56b7ceb87b900a0efa14e9ecd Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Sat, 20 Aug 2016 09:26:57 +0500 Subject: - Renamed calculateFuel - Used int in BigDecimal constructor - Added extra space --- src/org/traccar/reports/ReportUtils.java | 4 ++-- src/org/traccar/reports/Trips.java | 2 +- src/org/traccar/reports/model/BaseReport.java | 2 +- test/org/traccar/reports/ReportUtilsTest.java | 8 +++++--- 4 files changed, 9 insertions(+), 7 deletions(-) (limited to 'test/org/traccar') diff --git a/src/org/traccar/reports/ReportUtils.java b/src/org/traccar/reports/ReportUtils.java index 03e23498a..baa799e90 100644 --- a/src/org/traccar/reports/ReportUtils.java +++ b/src/org/traccar/reports/ReportUtils.java @@ -49,7 +49,7 @@ public final class ReportUtils { && 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")); + .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()) @@ -59,7 +59,7 @@ public final class ReportUtils { return distance.setScale(2, RoundingMode.HALF_EVEN).doubleValue(); } - public static String calculateSpentFuel(Position firstPosition, Position lastPosition) { + public static String calculateFuel(Position firstPosition, Position lastPosition) { if (firstPosition.getAttributes().get(Position.KEY_FUEL) != null && lastPosition.getAttributes().get(Position.KEY_FUEL) != null) { diff --git a/src/org/traccar/reports/Trips.java b/src/org/traccar/reports/Trips.java index 068c47b8f..b661ffffa 100644 --- a/src/org/traccar/reports/Trips.java +++ b/src/org/traccar/reports/Trips.java @@ -65,7 +65,7 @@ public final class Trips { trip.setDuration(tripDuration); trip.setAverageSpeed(speedSum / (endIndex - startIndex)); trip.setMaxSpeed(speedMax); - trip.setSpentFuel(ReportUtils.calculateSpentFuel(startTrip, endTrip)); + trip.setSpentFuel(ReportUtils.calculateFuel(startTrip, endTrip)); return trip; } diff --git a/src/org/traccar/reports/model/BaseReport.java b/src/org/traccar/reports/model/BaseReport.java index bcbed29fe..8c5807c9b 100644 --- a/src/org/traccar/reports/model/BaseReport.java +++ b/src/org/traccar/reports/model/BaseReport.java @@ -57,7 +57,7 @@ public class BaseReport { this.averageSpeed = new BigDecimal(averageSpeed.toString()).setScale(3, RoundingMode.HALF_EVEN).doubleValue(); } - private double maxSpeed; //knots + private double maxSpeed; // knots public double getMaxSpeed() { return maxSpeed; } diff --git a/test/org/traccar/reports/ReportUtilsTest.java b/test/org/traccar/reports/ReportUtilsTest.java index e7f762b28..80b0f6287 100644 --- a/test/org/traccar/reports/ReportUtilsTest.java +++ b/test/org/traccar/reports/ReportUtilsTest.java @@ -22,13 +22,15 @@ public class ReportUtilsTest { public void testCalculateSpentFuel() { Position startPosition = new Position(); Position endPosition = new Position(); - Assert.assertEquals(ReportUtils.calculateSpentFuel(startPosition, endPosition), "-"); + Assert.assertEquals(ReportUtils.calculateFuel(startPosition, endPosition), "-"); startPosition.setProtocol("meitrack"); startPosition.set(Position.KEY_FUEL, 0.07); endPosition.set(Position.KEY_FUEL, 0.05); - Assert.assertEquals(ReportUtils.calculateSpentFuel(startPosition, endPosition), "0.02 %"); + Assert.assertEquals(ReportUtils.calculateFuel(startPosition, endPosition), "0.02 %"); startPosition.setProtocol("galileo"); - Assert.assertEquals(ReportUtils.calculateSpentFuel(startPosition, endPosition), "0.02 %"); + Assert.assertEquals(ReportUtils.calculateFuel(startPosition, endPosition), "0.02 %"); + startPosition.setProtocol("noran"); + Assert.assertEquals(ReportUtils.calculateFuel(startPosition, endPosition), "0.02 %"); } } -- cgit v1.2.3 From 804c56a76dc8406e4ff072289a4a9f97322dfe83 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Sat, 20 Aug 2016 14:36:49 +0500 Subject: Removed rounding and BigDecimal for distance and averageSpeed --- src/org/traccar/reports/ReportUtils.java | 15 +++++++-------- src/org/traccar/reports/model/BaseReport.java | 5 +---- test/org/traccar/reports/ReportUtilsTest.java | 4 ++-- 3 files changed, 10 insertions(+), 14 deletions(-) (limited to 'test/org/traccar') diff --git a/src/org/traccar/reports/ReportUtils.java b/src/org/traccar/reports/ReportUtils.java index baa799e90..34f04ac87 100644 --- a/src/org/traccar/reports/ReportUtils.java +++ b/src/org/traccar/reports/ReportUtils.java @@ -44,19 +44,18 @@ public final class ReportUtils { } public static double calculateDistance(Position firstPosition, Position lastPosition, boolean useOdometer) { - BigDecimal distance = new BigDecimal("0.0"); + double distance = 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)); + 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 = new BigDecimal(lastPosition.getAttributes().get(Position.KEY_TOTAL_DISTANCE).toString()) - .subtract(new BigDecimal(firstPosition.getAttributes().get(Position.KEY_TOTAL_DISTANCE) - .toString())); + distance = ((Number) lastPosition.getAttributes().get(Position.KEY_TOTAL_DISTANCE)).doubleValue() + - ((Number) firstPosition.getAttributes().get(Position.KEY_TOTAL_DISTANCE)).doubleValue(); } - return distance.setScale(2, RoundingMode.HALF_EVEN).doubleValue(); + return distance; } public static String calculateFuel(Position firstPosition, Position lastPosition) { diff --git a/src/org/traccar/reports/model/BaseReport.java b/src/org/traccar/reports/model/BaseReport.java index 8c5807c9b..246cdede0 100644 --- a/src/org/traccar/reports/model/BaseReport.java +++ b/src/org/traccar/reports/model/BaseReport.java @@ -16,9 +16,6 @@ */ package org.traccar.reports.model; -import java.math.BigDecimal; -import java.math.RoundingMode; - public class BaseReport { private long deviceId; @@ -54,7 +51,7 @@ public class BaseReport { return averageSpeed; } public void setAverageSpeed(Double averageSpeed) { - this.averageSpeed = new BigDecimal(averageSpeed.toString()).setScale(3, RoundingMode.HALF_EVEN).doubleValue(); + this.averageSpeed = averageSpeed; } private double maxSpeed; // knots diff --git a/test/org/traccar/reports/ReportUtilsTest.java b/test/org/traccar/reports/ReportUtilsTest.java index 80b0f6287..2e01505db 100644 --- a/test/org/traccar/reports/ReportUtilsTest.java +++ b/test/org/traccar/reports/ReportUtilsTest.java @@ -9,9 +9,9 @@ public class ReportUtilsTest { @Test public void testCalculateDistance() { Position startPosition = new Position(); - startPosition.set(Position.KEY_TOTAL_DISTANCE, 500); + startPosition.set(Position.KEY_TOTAL_DISTANCE, 500.0); Position endPosition = new Position(); - endPosition.set(Position.KEY_TOTAL_DISTANCE, 700); + endPosition.set(Position.KEY_TOTAL_DISTANCE, 700.0); Assert.assertEquals(ReportUtils.calculateDistance(startPosition, endPosition), 200.0, 10); startPosition.set(Position.KEY_ODOMETER, 50000); endPosition.set(Position.KEY_ODOMETER, 50001); -- cgit v1.2.3