diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-08-18 12:56:51 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-08-18 12:56:51 +1200 |
commit | a90ce59795690267a0924c2b911f14fe0340a6b4 (patch) | |
tree | 5de6442a5a6cc4ed44d7b62cc75f1300a988eb84 /src/org/traccar | |
parent | afaede42a0cc8170128219bd1dfa6277e4e1809a (diff) | |
parent | 21ed9dbbd8c7be191894b11e895c1c0094b4aac6 (diff) | |
download | trackermap-server-a90ce59795690267a0924c2b911f14fe0340a6b4.tar.gz trackermap-server-a90ce59795690267a0924c2b911f14fe0340a6b4.tar.bz2 trackermap-server-a90ce59795690267a0924c2b911f14fe0340a6b4.zip |
Merge from mungingLabs into odometer
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; } |