From 1fd120a7fb74134a8ed7e12e685137f77a872567 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Fri, 12 Aug 2016 15:16:10 +0500 Subject: Always calculate distance --- src/org/traccar/BasePipelineFactory.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index ed7aa0a69..e2b991afa 100644 --- a/src/org/traccar/BasePipelineFactory.java +++ b/src/org/traccar/BasePipelineFactory.java @@ -132,9 +132,7 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { Context.getLocationProvider(), Context.getConfig().getBoolean("location.processInvalidPositions")); } - if (Context.getConfig().getBoolean("distance.enable")) { - distanceHandler = new DistanceHandler(); - } + distanceHandler = new DistanceHandler(); if (Context.getConfig().hasKey("location.latitudeHemisphere") || Context.getConfig().hasKey("location.longitudeHemisphere")) { -- cgit v1.2.3 From 89af2b67e7b7d3404204658f44d1fa48626ae6de Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Fri, 12 Aug 2016 15:57:52 +0500 Subject: Calculate summary distance with help of "odometer" or "distance" attribute --- src/org/traccar/reports/Summary.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/org/traccar/reports/Summary.java b/src/org/traccar/reports/Summary.java index 68733ded6..870d3ea29 100644 --- a/src/org/traccar/reports/Summary.java +++ b/src/org/traccar/reports/Summary.java @@ -26,7 +26,6 @@ import javax.json.Json; import javax.json.JsonArrayBuilder; import org.traccar.Context; -import org.traccar.helper.DistanceCalculator; import org.traccar.model.Position; import org.traccar.reports.model.SummaryReport; import org.traccar.web.CsvBuilder; @@ -43,12 +42,14 @@ public final class Summary { result.setDeviceName(Context.getDeviceManager().getDeviceById(deviceId).getName()); Collection positions = Context.getDataManager().getPositions(deviceId, from, to); if (positions != null && !positions.isEmpty()) { + Position firstPosition = null; Position previousPosition = null; double speedSum = 0; for (Position position : positions) { + if (firstPosition == null) { + firstPosition = position; + } if (previousPosition != null) { - result.addDistance(DistanceCalculator.distance(previousPosition.getLatitude(), - previousPosition.getLongitude(), position.getLatitude(), position.getLongitude())); if (position.getAttributes().get(Position.KEY_IGNITION) != null && Boolean.parseBoolean(position.getAttributes().get(Position.KEY_IGNITION).toString()) && previousPosition.getAttributes().get(Position.KEY_IGNITION) != null @@ -62,6 +63,16 @@ public final class Summary { speedSum += position.getSpeed(); result.setMaxSpeed(position.getSpeed()); } + if (firstPosition.getAttributes().containsKey(Position.KEY_ODOMETER) + && previousPosition.getAttributes().containsKey(Position.KEY_ODOMETER)) { + result.setDistance(((Double) previousPosition.getAttributes().get(Position.KEY_ODOMETER) + - (Double) firstPosition.getAttributes().get(Position.KEY_ODOMETER)) * 1000); + } else if (firstPosition.getAttributes().containsKey(Position.KEY_DISTANCE) + && previousPosition.getAttributes().containsKey(Position.KEY_DISTANCE)) { + result.setDistance((Double) previousPosition.getAttributes().get(Position.KEY_DISTANCE) + - (Double) firstPosition.getAttributes().get(Position.KEY_DISTANCE)); + } + result.setAverageSpeed(speedSum / positions.size()); result.setDistance(new BigDecimal(result.getDistance()).setScale(2, RoundingMode.HALF_UP).doubleValue()); } -- cgit v1.2.3 From a678177768cba7aaf0912e2c0765af7a8bd075c3 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Fri, 12 Aug 2016 16:05:43 +0500 Subject: Removed unnecessary rounding --- src/org/traccar/reports/Summary.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/org/traccar/reports/Summary.java b/src/org/traccar/reports/Summary.java index 870d3ea29..35fd80791 100644 --- a/src/org/traccar/reports/Summary.java +++ b/src/org/traccar/reports/Summary.java @@ -74,7 +74,6 @@ public final class Summary { } result.setAverageSpeed(speedSum / positions.size()); - result.setDistance(new BigDecimal(result.getDistance()).setScale(2, RoundingMode.HALF_UP).doubleValue()); } return result; } -- cgit v1.2.3 From b47795087d138ccd6d9634aadcfd29cbca629218 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Fri, 12 Aug 2016 16:21:51 +0500 Subject: Removed unused imports --- src/org/traccar/reports/Summary.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/org/traccar/reports/Summary.java b/src/org/traccar/reports/Summary.java index 35fd80791..dac8afe67 100644 --- a/src/org/traccar/reports/Summary.java +++ b/src/org/traccar/reports/Summary.java @@ -16,8 +16,6 @@ */ package org.traccar.reports; -import java.math.BigDecimal; -import java.math.RoundingMode; import java.sql.SQLException; import java.util.Collection; import java.util.Date; -- cgit v1.2.3 From 82c2d713d4da71f8eeef3e134b06dbb0abf37633 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Sat, 13 Aug 2016 20:36:46 +0500 Subject: - Split distance to distance and total distance - Switch summary report to total distance - Move DistanceHandler after filters in pipeline - Other accompanying changes --- src/org/traccar/BasePipelineFactory.java | 7 ++++--- src/org/traccar/DistanceHandler.java | 17 ++++++++++++----- src/org/traccar/model/Position.java | 1 + src/org/traccar/reports/Summary.java | 25 ++++++++++++------------- test/org/traccar/DistanceHandlerTest.java | 1 + web/app/AttributeFormatter.js | 2 +- 6 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index e2b991afa..615251d5f 100644 --- a/src/org/traccar/BasePipelineFactory.java +++ b/src/org/traccar/BasePipelineFactory.java @@ -179,9 +179,6 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { if (hemisphereHandler != null) { pipeline.addLast("hemisphere", hemisphereHandler); } - if (distanceHandler != null) { - pipeline.addLast("distance", distanceHandler); - } if (reverseGeocoderHandler != null) { pipeline.addLast("geocoder", reverseGeocoderHandler); } @@ -200,6 +197,10 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { pipeline.addLast("coordinatesHandler", coordinatesHandler); } + if (distanceHandler != null) { + pipeline.addLast("distance", distanceHandler); + } + if (Context.getDataManager() != null) { pipeline.addLast("dataHandler", new DefaultDataHandler()); } diff --git a/src/org/traccar/DistanceHandler.java b/src/org/traccar/DistanceHandler.java index 9f174a6c7..5cfb86ea0 100644 --- a/src/org/traccar/DistanceHandler.java +++ b/src/org/traccar/DistanceHandler.java @@ -34,23 +34,30 @@ public class DistanceHandler extends BaseDataHandler { public Position calculateDistance(Position position) { double distance = 0.0; + double totalDistance = 0.0; Position last = getLastPosition(position.getDeviceId()); if (last != null) { - if (last.getAttributes().containsKey(Position.KEY_DISTANCE)) { - distance = ((Number) last.getAttributes().get(Position.KEY_DISTANCE)).doubleValue(); + if (last.getAttributes().containsKey(Position.KEY_TOTAL_DISTANCE)) { + totalDistance = ((Number) last.getAttributes().get(Position.KEY_TOTAL_DISTANCE)).doubleValue(); } - if (position.getValid()) { - distance += DistanceCalculator.distance( + if (!position.getAttributes().containsKey(Position.KEY_DISTANCE)) { + distance = DistanceCalculator.distance( position.getLatitude(), position.getLongitude(), last.getLatitude(), last.getLongitude()); distance = BigDecimal.valueOf(distance).setScale(2, RoundingMode.HALF_EVEN).doubleValue(); + } else { + distance = ((Number) position.getAttributes().get(Position.KEY_DISTANCE)).doubleValue(); } } + if (!position.getAttributes().containsKey(Position.KEY_DISTANCE)) { + position.set(Position.KEY_DISTANCE, distance); + } + totalDistance += distance; + position.set(Position.KEY_TOTAL_DISTANCE, totalDistance); - position.set(Position.KEY_DISTANCE, distance); return position; } diff --git a/src/org/traccar/model/Position.java b/src/org/traccar/model/Position.java index d2409a75d..680a6ae8f 100644 --- a/src/org/traccar/model/Position.java +++ b/src/org/traccar/model/Position.java @@ -49,6 +49,7 @@ public class Position extends Message { public static final String KEY_IP = "ip"; public static final String KEY_ARCHIVE = "archive"; public static final String KEY_DISTANCE = "distance"; + public static final String KEY_TOTAL_DISTANCE = "totalDistance"; public static final String KEY_RPM = "rpm"; public static final String KEY_VIN = "vin"; public static final String KEY_APPROXIMATE = "approximate"; diff --git a/src/org/traccar/reports/Summary.java b/src/org/traccar/reports/Summary.java index dac8afe67..e2c11e7b1 100644 --- a/src/org/traccar/reports/Summary.java +++ b/src/org/traccar/reports/Summary.java @@ -47,15 +47,14 @@ public final class Summary { if (firstPosition == null) { firstPosition = position; } - if (previousPosition != null) { - if (position.getAttributes().get(Position.KEY_IGNITION) != null - && Boolean.parseBoolean(position.getAttributes().get(Position.KEY_IGNITION).toString()) - && previousPosition.getAttributes().get(Position.KEY_IGNITION) != null - && Boolean.parseBoolean(previousPosition.getAttributes() - .get(Position.KEY_IGNITION).toString())) { - result.addEngineHours(position.getFixTime().getTime() - - previousPosition.getFixTime().getTime()); - } + if (previousPosition != null + && position.getAttributes().get(Position.KEY_IGNITION) != null + && Boolean.parseBoolean(position.getAttributes().get(Position.KEY_IGNITION).toString()) + && previousPosition.getAttributes().get(Position.KEY_IGNITION) != null + && Boolean.parseBoolean(previousPosition.getAttributes() + .get(Position.KEY_IGNITION).toString())) { + result.addEngineHours(position.getFixTime().getTime() + - previousPosition.getFixTime().getTime()); } previousPosition = position; speedSum += position.getSpeed(); @@ -65,10 +64,10 @@ public final class Summary { && previousPosition.getAttributes().containsKey(Position.KEY_ODOMETER)) { result.setDistance(((Double) previousPosition.getAttributes().get(Position.KEY_ODOMETER) - (Double) firstPosition.getAttributes().get(Position.KEY_ODOMETER)) * 1000); - } else if (firstPosition.getAttributes().containsKey(Position.KEY_DISTANCE) - && previousPosition.getAttributes().containsKey(Position.KEY_DISTANCE)) { - result.setDistance((Double) previousPosition.getAttributes().get(Position.KEY_DISTANCE) - - (Double) firstPosition.getAttributes().get(Position.KEY_DISTANCE)); + } else if (firstPosition.getAttributes().containsKey(Position.KEY_TOTAL_DISTANCE) + && previousPosition.getAttributes().containsKey(Position.KEY_TOTAL_DISTANCE)) { + result.setDistance((Double) previousPosition.getAttributes().get(Position.KEY_TOTAL_DISTANCE) + - (Double) firstPosition.getAttributes().get(Position.KEY_TOTAL_DISTANCE)); } result.setAverageSpeed(speedSum / positions.size()); diff --git a/test/org/traccar/DistanceHandlerTest.java b/test/org/traccar/DistanceHandlerTest.java index 853f2c645..b14caaec5 100644 --- a/test/org/traccar/DistanceHandlerTest.java +++ b/test/org/traccar/DistanceHandlerTest.java @@ -15,6 +15,7 @@ public class DistanceHandlerTest { Position position = distanceHandler.calculateDistance(new Position()); assertEquals(0.0, position.getAttributes().get(Position.KEY_DISTANCE)); + assertEquals(0.0, position.getAttributes().get(Position.KEY_TOTAL_DISTANCE)); } diff --git a/web/app/AttributeFormatter.js b/web/app/AttributeFormatter.js index 274b8d028..dcf50c580 100644 --- a/web/app/AttributeFormatter.js +++ b/web/app/AttributeFormatter.js @@ -61,7 +61,7 @@ Ext.define('Traccar.AttributeFormatter', { return this.speedFormatter; } else if (key === 'course') { return this.courseFormatter; - } else if (key === 'distance' || key === 'odometer') { + } else if (key === 'distance' || key === 'odometer' || key === 'totalDistance') { return this.distanceFormatter; } else if (key === 'hours') { return this.hoursFormatter; -- cgit v1.2.3 From fec39779b9d84290fc8bcdc3581347d7082103f5 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Sat, 13 Aug 2016 21:57:28 +0500 Subject: Added rounding for total distance --- src/org/traccar/DistanceHandler.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/org/traccar/DistanceHandler.java b/src/org/traccar/DistanceHandler.java index 5cfb86ea0..99b240150 100644 --- a/src/org/traccar/DistanceHandler.java +++ b/src/org/traccar/DistanceHandler.java @@ -56,6 +56,7 @@ public class DistanceHandler extends BaseDataHandler { position.set(Position.KEY_DISTANCE, distance); } totalDistance += distance; + totalDistance = BigDecimal.valueOf(totalDistance).setScale(2, RoundingMode.HALF_EVEN).doubleValue(); position.set(Position.KEY_TOTAL_DISTANCE, totalDistance); return position; -- cgit v1.2.3 From 6c8609aa3151246ab171995ef08c161e5d3b8242 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Sun, 14 Aug 2016 17:51:48 +0500 Subject: - Added rounding for some values - Some changes in casting objects - Renamed General to Summary --- src/org/traccar/DistanceHandler.java | 3 +-- src/org/traccar/reports/Summary.java | 25 ++++++++++++++++--------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/org/traccar/DistanceHandler.java b/src/org/traccar/DistanceHandler.java index 99b240150..7537e2867 100644 --- a/src/org/traccar/DistanceHandler.java +++ b/src/org/traccar/DistanceHandler.java @@ -55,8 +55,7 @@ public class DistanceHandler extends BaseDataHandler { if (!position.getAttributes().containsKey(Position.KEY_DISTANCE)) { position.set(Position.KEY_DISTANCE, distance); } - totalDistance += distance; - totalDistance = BigDecimal.valueOf(totalDistance).setScale(2, RoundingMode.HALF_EVEN).doubleValue(); + totalDistance = BigDecimal.valueOf(totalDistance + distance).setScale(2, RoundingMode.HALF_EVEN).doubleValue(); position.set(Position.KEY_TOTAL_DISTANCE, totalDistance); return position; diff --git a/src/org/traccar/reports/Summary.java b/src/org/traccar/reports/Summary.java index e2c11e7b1..402e6d432 100644 --- a/src/org/traccar/reports/Summary.java +++ b/src/org/traccar/reports/Summary.java @@ -16,6 +16,8 @@ */ package org.traccar.reports; +import java.math.BigDecimal; +import java.math.RoundingMode; import java.sql.SQLException; import java.util.Collection; import java.util.Date; @@ -34,7 +36,7 @@ public final class Summary { private Summary() { } - private static SummaryReport calculateGeneralResult(long deviceId, Date from, Date to) throws SQLException { + private static SummaryReport calculateSummaryResult(long deviceId, Date from, Date to) throws SQLException { SummaryReport result = new SummaryReport(); result.setDeviceId(deviceId); result.setDeviceName(Context.getDeviceManager().getDeviceById(deviceId).getName()); @@ -62,15 +64,20 @@ public final class Summary { } if (firstPosition.getAttributes().containsKey(Position.KEY_ODOMETER) && previousPosition.getAttributes().containsKey(Position.KEY_ODOMETER)) { - result.setDistance(((Double) previousPosition.getAttributes().get(Position.KEY_ODOMETER) - - (Double) firstPosition.getAttributes().get(Position.KEY_ODOMETER)) * 1000); + result.setDistance((Integer.parseInt(previousPosition.getAttributes().get(Position.KEY_ODOMETER) + .toString()) + - Integer.parseInt(firstPosition.getAttributes().get(Position.KEY_ODOMETER).toString())) + * 1000); } else if (firstPosition.getAttributes().containsKey(Position.KEY_TOTAL_DISTANCE) && previousPosition.getAttributes().containsKey(Position.KEY_TOTAL_DISTANCE)) { - result.setDistance((Double) previousPosition.getAttributes().get(Position.KEY_TOTAL_DISTANCE) - - (Double) firstPosition.getAttributes().get(Position.KEY_TOTAL_DISTANCE)); + result.setDistance(((Number) previousPosition.getAttributes().get(Position.KEY_TOTAL_DISTANCE)) + .doubleValue() + - ((Number) firstPosition.getAttributes().get(Position.KEY_TOTAL_DISTANCE)).doubleValue()); } - - result.setAverageSpeed(speedSum / positions.size()); + result.setDistance(new BigDecimal(result.getDistance()) + .setScale(2, RoundingMode.HALF_EVEN).doubleValue()); + result.setAverageSpeed(new BigDecimal(speedSum / positions.size()) + .setScale(3, RoundingMode.HALF_EVEN).doubleValue()); } return result; } @@ -80,7 +87,7 @@ public final class Summary { JsonArrayBuilder json = Json.createArrayBuilder(); for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { Context.getPermissionsManager().checkDevice(userId, deviceId); - json.add(JsonConverter.objectToJson(calculateGeneralResult(deviceId, from, to))); + json.add(JsonConverter.objectToJson(calculateSummaryResult(deviceId, from, to))); } return json.build().toString(); } @@ -91,7 +98,7 @@ public final class Summary { csv.addHeaderLine(new SummaryReport()); for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { Context.getPermissionsManager().checkDevice(userId, deviceId); - csv.addLine(calculateGeneralResult(deviceId, from, to)); + csv.addLine(calculateSummaryResult(deviceId, from, to)); } return csv.build(); } -- cgit v1.2.3