diff options
author | Abyss777 <abyss@fox5.ru> | 2017-06-20 13:44:35 +0500 |
---|---|---|
committer | Abyss777 <abyss@fox5.ru> | 2017-06-20 13:46:21 +0500 |
commit | 9129b6f4eb6ab58a83107e47c888bb4a3bbf0f90 (patch) | |
tree | 825c03638ad5f3fb73016035c35c97523edc8784 /src/org/traccar/DistanceHandler.java | |
parent | 6f569b6d62b110c2679772c4a56122db6a614ed9 (diff) | |
download | trackermap-server-9129b6f4eb6ab58a83107e47c888bb4a3bbf0f90.tar.gz trackermap-server-9129b6f4eb6ab58a83107e47c888bb4a3bbf0f90.tar.bz2 trackermap-server-9129b6f4eb6ab58a83107e47c888bb4a3bbf0f90.zip |
- Combine 'DistanceHandler' and 'CoordinatesHandler'
- Move 'DistanceHandler' closer to beginning of pipeline
- Use KEY_DISTANCE instead of distance recalculation
Diffstat (limited to 'src/org/traccar/DistanceHandler.java')
-rw-r--r-- | src/org/traccar/DistanceHandler.java | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/src/org/traccar/DistanceHandler.java b/src/org/traccar/DistanceHandler.java index 0ab2d9048..295bc3b29 100644 --- a/src/org/traccar/DistanceHandler.java +++ b/src/org/traccar/DistanceHandler.java @@ -1,6 +1,6 @@ /* * Copyright 2015 Amila Silva - * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2017 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,6 +24,16 @@ import java.math.RoundingMode; public class DistanceHandler extends BaseDataHandler { + private final boolean filter; + private final int coordinatesMinError; + private final int coordinatesMaxError; + + public DistanceHandler(boolean filter, int coordinatesMinError, int coordinatesMaxError) { + this.filter = filter; + this.coordinatesMinError = coordinatesMinError; + this.coordinatesMaxError = coordinatesMaxError; + } + private Position getLastPosition(long deviceId) { if (Context.getIdentityManager() != null) { return Context.getIdentityManager().getLastPosition(deviceId); @@ -35,25 +45,32 @@ public class DistanceHandler extends BaseDataHandler { protected Position handlePosition(Position position) { double distance = 0.0; + if (position.getAttributes().containsKey(Position.KEY_DISTANCE)) { + distance = position.getDouble(Position.KEY_DISTANCE); + } double totalDistance = 0.0; Position last = getLastPosition(position.getDeviceId()); if (last != null) { totalDistance = last.getDouble(Position.KEY_TOTAL_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 = position.getDouble(Position.KEY_DISTANCE); + } + if (filter && last.getValid() && last.getLatitude() != 0 && last.getLongitude() != 0) { + boolean satisfiesMin = coordinatesMinError == 0 || distance > coordinatesMinError; + boolean satisfiesMax = coordinatesMaxError == 0 + || distance < coordinatesMaxError || position.getValid(); + if (!satisfiesMin || !satisfiesMax) { + position.setLatitude(last.getLatitude()); + position.setLongitude(last.getLongitude()); + distance = 0; + } } } - if (!position.getAttributes().containsKey(Position.KEY_DISTANCE)) { - position.set(Position.KEY_DISTANCE, 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); |