diff options
Diffstat (limited to 'src/org/traccar')
-rw-r--r-- | src/org/traccar/BasePipelineFactory.java | 5 | ||||
-rw-r--r-- | src/org/traccar/OdometerHandler.java | 55 | ||||
-rw-r--r-- | src/org/traccar/model/Position.java | 4 |
3 files changed, 63 insertions, 1 deletions
diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index 4d8b0ffc4..6c4a6b5bd 100644 --- a/src/org/traccar/BasePipelineFactory.java +++ b/src/org/traccar/BasePipelineFactory.java @@ -25,7 +25,6 @@ import org.jboss.netty.channel.ChannelPipelineFactory; import org.jboss.netty.channel.ChannelStateEvent; import org.jboss.netty.channel.Channels; import org.jboss.netty.channel.DownstreamMessageEvent; -import org.jboss.netty.channel.ExceptionEvent; import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.channel.SimpleChannelHandler; import org.jboss.netty.handler.logging.LoggingHandler; @@ -38,6 +37,7 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { private int resetDelay; private FilterHandler filterHandler; + private OdometerHandler odometerHandler; private ReverseGeocoderHandler reverseGeocoderHandler; protected class OpenChannelHandler extends SimpleChannelHandler { @@ -114,6 +114,9 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { if (filterHandler != null) { pipeline.addLast("filter", filterHandler); } + if (odometerHandler != null) { + pipeline.addLast("odometer", odometerHandler); + } if (reverseGeocoderHandler != null) { pipeline.addLast("geocoder", reverseGeocoderHandler); } diff --git a/src/org/traccar/OdometerHandler.java b/src/org/traccar/OdometerHandler.java new file mode 100644 index 000000000..d5860c02e --- /dev/null +++ b/src/org/traccar/OdometerHandler.java @@ -0,0 +1,55 @@ +/** + * + */ +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) { + // set odometer only on movement + double odometer = 0.0; + if (last.getSpeed() == 0 && position.getSpeed() == 0) { + odometer = last.getOdometer(); + } else { + double distance = DistanceCalculator.distance( + position.getLatitude(), position.getLongitude(), + last.getLatitude(), last.getLongitude()); + distance = Math.round((distance) * 100.0) / 100.0; + odometer = distance + last.getOdometer(); + } + position.setOdometer(odometer); + } + return position; + } + + @Override + protected Position handlePosition(Position position) { + return calculateDistance(position); + } + +} diff --git a/src/org/traccar/model/Position.java b/src/org/traccar/model/Position.java index 62c999d94..0c928439c 100644 --- a/src/org/traccar/model/Position.java +++ b/src/org/traccar/model/Position.java @@ -56,6 +56,10 @@ public class Position extends Event implements Factory { private double course; public double getCourse() { return course; } public void setCourse(double course) { this.course = course; } + + private double odometer; // value in meters + public double getOdometer() { return odometer; } + public void setOdometer(double odometer) { this.odometer = odometer; } private String address; public String getAddress() { return address; } |