aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/org/traccar/BasePipelineFactory.java7
-rw-r--r--src/org/traccar/DistanceHandler.java17
-rw-r--r--src/org/traccar/model/Position.java1
-rw-r--r--src/org/traccar/reports/Summary.java25
-rw-r--r--test/org/traccar/DistanceHandlerTest.java1
-rw-r--r--web/app/AttributeFormatter.js2
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;