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 | |
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
-rw-r--r-- | src/org/traccar/BasePipelineFactory.java | 24 | ||||
-rw-r--r-- | src/org/traccar/CoordinatesHandler.java | 55 | ||||
-rw-r--r-- | src/org/traccar/DistanceHandler.java | 33 | ||||
-rw-r--r-- | src/org/traccar/FilterHandler.java | 10 | ||||
-rw-r--r-- | src/org/traccar/GeocoderHandler.java | 6 | ||||
-rw-r--r-- | test/org/traccar/DistanceHandlerTest.java | 9 |
6 files changed, 45 insertions, 92 deletions
diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index 45213c1a1..0997e6f11 100644 --- a/src/org/traccar/BasePipelineFactory.java +++ b/src/org/traccar/BasePipelineFactory.java @@ -49,7 +49,6 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { private int timeout; private FilterHandler filterHandler; - private CoordinatesHandler coordinatesHandler; private DistanceHandler distanceHandler; private MotionHandler motionHandler; private GeocoderHandler geocoderHandler; @@ -126,14 +125,14 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { } } + distanceHandler = new DistanceHandler(Context.getConfig().getBoolean("coordinates.filter"), + Context.getConfig().getInteger("coordinates.minError"), + Context.getConfig().getInteger("coordinates.maxError")); + if (Context.getConfig().getBoolean("filter.enable")) { filterHandler = new FilterHandler(); } - if (Context.getConfig().getBoolean("coordinates.filter")) { - coordinatesHandler = new CoordinatesHandler(); - } - if (Context.getGeocoder() != null) { geocoderHandler = new GeocoderHandler( Context.getGeocoder(), @@ -146,8 +145,6 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { Context.getConfig().getBoolean("geolocation.processInvalidPositions")); } - distanceHandler = new DistanceHandler(); - motionHandler = new MotionHandler(Context.getConfig().getDouble("event.motion.speedThreshold", 0.01)); if (Context.getConfig().hasKey("location.latitudeHemisphere") @@ -196,6 +193,11 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { if (hemisphereHandler != null) { pipeline.addLast("hemisphere", hemisphereHandler); } + + if (distanceHandler != null) { + pipeline.addLast("distance", distanceHandler); + } + if (geocoderHandler != null) { pipeline.addLast("geocoder", geocoderHandler); } @@ -207,14 +209,6 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { pipeline.addLast("filter", filterHandler); } - if (coordinatesHandler != null) { - pipeline.addLast("coordinatesHandler", coordinatesHandler); - } - - if (distanceHandler != null) { - pipeline.addLast("distance", distanceHandler); - } - if (motionHandler != null) { pipeline.addLast("motion", motionHandler); } diff --git a/src/org/traccar/CoordinatesHandler.java b/src/org/traccar/CoordinatesHandler.java deleted file mode 100644 index 80a423da1..000000000 --- a/src/org/traccar/CoordinatesHandler.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2016 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar; - -import org.traccar.helper.DistanceCalculator; -import org.traccar.model.Position; - -public class CoordinatesHandler extends BaseDataHandler { - - private final int coordinatesMinError; - private final int coordinatesMaxError; - - public CoordinatesHandler() { - Config config = Context.getConfig(); - coordinatesMinError = config.getInteger("coordinates.minError"); - coordinatesMaxError = config.getInteger("coordinates.maxError"); - } - - private Position getLastPosition(long deviceId) { - if (Context.getIdentityManager() != null) { - return Context.getIdentityManager().getLastPosition(deviceId); - } - return null; - } - - @Override - protected Position handlePosition(Position position) { - Position last = getLastPosition(position.getDeviceId()); - if (last != null && last.getValid() && last.getLatitude() != 0 && last.getLongitude() != 0) { - double distance = DistanceCalculator.distance( - position.getLatitude(), position.getLongitude(), last.getLatitude(), last.getLongitude()); - 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()); - } - } - return position; - } - -} 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); diff --git a/src/org/traccar/FilterHandler.java b/src/org/traccar/FilterHandler.java index f2a80a98f..973dfc205 100644 --- a/src/org/traccar/FilterHandler.java +++ b/src/org/traccar/FilterHandler.java @@ -15,7 +15,6 @@ */ package org.traccar; -import org.traccar.helper.DistanceCalculator; import org.traccar.helper.Log; import org.traccar.helper.UnitsConverter; import org.traccar.model.Position; @@ -111,19 +110,14 @@ public class FilterHandler extends BaseDataHandler { private boolean filterDistance(Position position, Position last) { if (filterDistance != 0 && last != null) { - double distance = DistanceCalculator.distance( - position.getLatitude(), position.getLongitude(), - last.getLatitude(), last.getLongitude()); - return distance < filterDistance; + return position.getDouble(Position.KEY_DISTANCE) < filterDistance; } return false; } private boolean filterMaxSpeed(Position position, Position last) { if (filterMaxSpeed != 0 && last != null) { - double distance = DistanceCalculator.distance( - position.getLatitude(), position.getLongitude(), - last.getLatitude(), last.getLongitude()); + double distance = position.getDouble(Position.KEY_DISTANCE); long time = position.getFixTime().getTime() - last.getFixTime().getTime(); return UnitsConverter.knotsFromMps(distance / time) > filterMaxSpeed; } diff --git a/src/org/traccar/GeocoderHandler.java b/src/org/traccar/GeocoderHandler.java index c5f4d2f9a..2742ff1a7 100644 --- a/src/org/traccar/GeocoderHandler.java +++ b/src/org/traccar/GeocoderHandler.java @@ -22,7 +22,6 @@ import org.jboss.netty.channel.Channels; import org.jboss.netty.channel.MessageEvent; import org.traccar.geocoder.AddressFormat; import org.traccar.geocoder.Geocoder; -import org.traccar.helper.DistanceCalculator; import org.traccar.helper.Log; import org.traccar.model.Position; @@ -62,10 +61,7 @@ public class GeocoderHandler implements ChannelUpstreamHandler { if (geocoderReuseDistance != 0) { Position lastPosition = Context.getIdentityManager().getLastPosition(position.getDeviceId()); if (lastPosition != null && lastPosition.getAddress() != null) { - double distance = DistanceCalculator.distance( - position.getLatitude(), position.getLongitude(), - lastPosition.getLatitude(), lastPosition.getLongitude()); - if (distance <= geocoderReuseDistance) { + if (position.getDouble(Position.KEY_DISTANCE) <= geocoderReuseDistance) { position.setAddress(lastPosition.getAddress()); Channels.fireMessageReceived(ctx, position, event.getRemoteAddress()); return; diff --git a/test/org/traccar/DistanceHandlerTest.java b/test/org/traccar/DistanceHandlerTest.java index 66b2f3d1c..c97135074 100644 --- a/test/org/traccar/DistanceHandlerTest.java +++ b/test/org/traccar/DistanceHandlerTest.java @@ -10,13 +10,20 @@ public class DistanceHandlerTest { @Test public void testCalculateDistance() throws Exception { - DistanceHandler distanceHandler = new DistanceHandler(); + DistanceHandler distanceHandler = new DistanceHandler(false, 0, 0); Position position = distanceHandler.handlePosition(new Position()); assertEquals(0.0, position.getAttributes().get(Position.KEY_DISTANCE)); assertEquals(0.0, position.getAttributes().get(Position.KEY_TOTAL_DISTANCE)); + position.set(Position.KEY_DISTANCE, 100); + + position = distanceHandler.handlePosition(position); + + assertEquals(100.0, position.getAttributes().get(Position.KEY_DISTANCE)); + assertEquals(100.0, position.getAttributes().get(Position.KEY_TOTAL_DISTANCE)); + } } |