/**
 * 
 */
package org.traccar;

import org.traccar.helper.DistanceCalculator;
import org.traccar.helper.Log;
import org.traccar.model.Position;

/**
 * <p>
 * Odometer - total mileage calculation handler
 * </p>
 * 
 * @author Amila Silva
 *
 */
public class OdometerHandler extends BaseDataHandler {

    public OdometerHandler() {
        Log.debug("System based odometer calculation enabled for all devices");
    }

    private Position getLastPosition(long deviceId) {
        if (Context.getConnectionManager() != null) {
            return Context.getConnectionManager().getLastPosition(deviceId);
        }
        return null;
    }

    private Position calculateDistance(Position position) {
        Position last = getLastPosition(position.getDeviceId());
        if (last != null) {
            double distance = DistanceCalculator.distance(
                    position.getLatitude(), position.getLongitude(),
                    last.getLatitude(), last.getLongitude());
            distance = Math.round((distance) * 100.0) / 100.0;
            double odometer = distance + last.getOdometer();
            position.setOdometer(odometer);
        }
        return position;
    }

    @Override
    protected Position handlePosition(Position position) {
        return calculateDistance(position);
    }

}

@@ -783,32 +783,252 @@
 last != null) %7B%0A
+            // set odometer only on movement%0A            double odometer = 0.0;%0A            if (last.getSpeed() == 0 && position.getSpeed() == 0) %7B%0A                odometer = last.getOdometer();%0A            %7D else %7B%0A    
             doub
@@ -1070,16 +1070,20 @@
 stance(%0A
+    
         
@@ -1163,16 +1163,20 @@
         
+    
 last.get
@@ -1205,24 +1205,28 @@
 ngitude());%0A
+    
             
@@ -1280,38 +1280,35 @@
 .0;%0A            
-double
+   
  odometer = dist
@@ -1326,32 +1326,46 @@
 .getOdometer();%0A
+            %7D%0A
             posi

Set Odometer only if there is a movement