From 9129b6f4eb6ab58a83107e47c888bb4a3bbf0f90 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 20 Jun 2017 13:44:35 +0500 Subject: - Combine 'DistanceHandler' and 'CoordinatesHandler' - Move 'DistanceHandler' closer to beginning of pipeline - Use KEY_DISTANCE instead of distance recalculation --- src/org/traccar/DistanceHandler.java | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) (limited to 'src/org/traccar/DistanceHandler.java') 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); -- cgit v1.2.3