aboutsummaryrefslogtreecommitdiff
path: root/src/org
diff options
context:
space:
mode:
Diffstat (limited to 'src/org')
-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
4 files changed, 29 insertions, 21 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());