aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2016-08-14 16:31:24 +0300
committerGitHub <noreply@github.com>2016-08-14 16:31:24 +0300
commit15b5ac0768ee7236d1af025460a3bdd4f8fa6994 (patch)
tree778506714b81ffb4fece27a4e6fa2eb5bf17e824 /src/org/traccar
parentd9777620508a7b197f0e5752869eef91d83e20a0 (diff)
parent6c8609aa3151246ab171995ef08c161e5d3b8242 (diff)
downloadtraccar-server-15b5ac0768ee7236d1af025460a3bdd4f8fa6994.tar.gz
traccar-server-15b5ac0768ee7236d1af025460a3bdd4f8fa6994.tar.bz2
traccar-server-15b5ac0768ee7236d1af025460a3bdd4f8fa6994.zip
Merge pull request #2211 from Abyss777/distance
Changed way of summary distance calculation
Diffstat (limited to 'src/org/traccar')
-rw-r--r--src/org/traccar/BasePipelineFactory.java11
-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.java48
4 files changed, 49 insertions, 28 deletions
diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java
index ed7aa0a69..615251d5f 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")) {
@@ -181,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);
}
@@ -202,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..7537e2867 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 = BigDecimal.valueOf(totalDistance + distance).setScale(2, RoundingMode.HALF_EVEN).doubleValue();
+ 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 68733ded6..402e6d432 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;
@@ -37,33 +36,48 @@ 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());
Collection<Position> 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 (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
- && Boolean.parseBoolean(previousPosition.getAttributes()
- .get(Position.KEY_IGNITION).toString())) {
- result.addEngineHours(position.getFixTime().getTime()
- - previousPosition.getFixTime().getTime());
- }
+ if (firstPosition == null) {
+ firstPosition = position;
+ }
+ 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();
result.setMaxSpeed(position.getSpeed());
}
- result.setAverageSpeed(speedSum / positions.size());
- result.setDistance(new BigDecimal(result.getDistance()).setScale(2, RoundingMode.HALF_UP).doubleValue());
+ if (firstPosition.getAttributes().containsKey(Position.KEY_ODOMETER)
+ && previousPosition.getAttributes().containsKey(Position.KEY_ODOMETER)) {
+ 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(((Number) previousPosition.getAttributes().get(Position.KEY_TOTAL_DISTANCE))
+ .doubleValue()
+ - ((Number) firstPosition.getAttributes().get(Position.KEY_TOTAL_DISTANCE)).doubleValue());
+ }
+ 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;
}
@@ -73,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();
}
@@ -84,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();
}