aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbyss777 <abyss@fox5.ru>2017-06-16 09:30:32 +0500
committerAbyss777 <abyss@fox5.ru>2017-06-16 09:30:32 +0500
commitb5e180da135c48afc3d89665000b6de0e6a330e3 (patch)
tree1f9c604ac9898eb0c88a79c34b25f604037db55a
parentbf57beec25228d58df8604e23d8709f6d196f3c5 (diff)
downloadtraccar-server-b5e180da135c48afc3d89665000b6de0e6a330e3.tar.gz
traccar-server-b5e180da135c48afc3d89665000b6de0e6a330e3.tar.bz2
traccar-server-b5e180da135c48afc3d89665000b6de0e6a330e3.zip
Centralized motion detection
-rw-r--r--src/org/traccar/BasePipelineFactory.java7
-rw-r--r--src/org/traccar/MotionHandler.java41
-rw-r--r--src/org/traccar/events/MotionEventHandler.java16
-rw-r--r--src/org/traccar/protocol/Gl200ProtocolDecoder.java2
-rw-r--r--src/org/traccar/reports/ReportUtils.java7
-rw-r--r--test/org/traccar/MotionHandlerTest.java21
-rw-r--r--test/org/traccar/ProtocolTest.java4
-rw-r--r--test/org/traccar/events/MotionEventHandlerTest.java2
8 files changed, 86 insertions, 14 deletions
diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java
index 11457905d..45213c1a1 100644
--- a/src/org/traccar/BasePipelineFactory.java
+++ b/src/org/traccar/BasePipelineFactory.java
@@ -51,6 +51,7 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory {
private FilterHandler filterHandler;
private CoordinatesHandler coordinatesHandler;
private DistanceHandler distanceHandler;
+ private MotionHandler motionHandler;
private GeocoderHandler geocoderHandler;
private GeolocationHandler geolocationHandler;
private HemisphereHandler hemisphereHandler;
@@ -147,6 +148,8 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory {
distanceHandler = new DistanceHandler();
+ motionHandler = new MotionHandler(Context.getConfig().getDouble("event.motion.speedThreshold", 0.01));
+
if (Context.getConfig().hasKey("location.latitudeHemisphere")
|| Context.getConfig().hasKey("location.longitudeHemisphere")) {
hemisphereHandler = new HemisphereHandler();
@@ -212,6 +215,10 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory {
pipeline.addLast("distance", distanceHandler);
}
+ if (motionHandler != null) {
+ pipeline.addLast("motion", motionHandler);
+ }
+
if (copyAttributesHandler != null) {
pipeline.addLast("copyAttributes", copyAttributesHandler);
}
diff --git a/src/org/traccar/MotionHandler.java b/src/org/traccar/MotionHandler.java
new file mode 100644
index 000000000..fa3d51858
--- /dev/null
+++ b/src/org/traccar/MotionHandler.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2017 Anton Tananaev (anton@traccar.org)
+ * Copyright 2017 Andrey Kunitsyn (andrey@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.model.Position;
+
+public class MotionHandler extends BaseDataHandler {
+
+ private double speedThreshold;
+
+ public MotionHandler(double speedThreshold) {
+ this.speedThreshold = speedThreshold;
+ }
+
+ public Position calculateMotion(Position position) {
+ if (!position.getAttributes().containsKey(Position.KEY_MOTION)) {
+ position.set(Position.KEY_MOTION, position.getSpeed() > speedThreshold);
+ }
+ return position;
+ }
+
+ @Override
+ protected Position handlePosition(Position position) {
+ return calculateMotion(position);
+ }
+
+}
diff --git a/src/org/traccar/events/MotionEventHandler.java b/src/org/traccar/events/MotionEventHandler.java
index be8218985..e6fd10f3e 100644
--- a/src/org/traccar/events/MotionEventHandler.java
+++ b/src/org/traccar/events/MotionEventHandler.java
@@ -26,12 +26,6 @@ import org.traccar.model.Position;
public class MotionEventHandler extends BaseEventHandler {
- private double speedThreshold;
-
- public MotionEventHandler() {
- speedThreshold = Context.getConfig().getDouble("event.motion.speedThreshold", 0.01);
- }
-
@Override
protected Collection<Event> analyzePosition(Position position) {
@@ -43,16 +37,16 @@ public class MotionEventHandler extends BaseEventHandler {
return null;
}
- double speed = position.getSpeed();
- double oldSpeed = 0;
+ boolean motion = position.getBoolean(Position.KEY_MOTION);
+ boolean oldMotion = false;
Position lastPosition = Context.getIdentityManager().getLastPosition(position.getDeviceId());
if (lastPosition != null) {
- oldSpeed = lastPosition.getSpeed();
+ oldMotion = lastPosition.getBoolean(Position.KEY_MOTION);
}
- if (speed > speedThreshold && oldSpeed <= speedThreshold) {
+ if (motion && !oldMotion) {
return Collections.singleton(
new Event(Event.TYPE_DEVICE_MOVING, position.getDeviceId(), position.getId()));
- } else if (speed <= speedThreshold && oldSpeed > speedThreshold) {
+ } else if (!motion && oldMotion) {
return Collections.singleton(
new Event(Event.TYPE_DEVICE_STOPPED, position.getDeviceId(), position.getId()));
}
diff --git a/src/org/traccar/protocol/Gl200ProtocolDecoder.java b/src/org/traccar/protocol/Gl200ProtocolDecoder.java
index 0927f53fd..a3062c942 100644
--- a/src/org/traccar/protocol/Gl200ProtocolDecoder.java
+++ b/src/org/traccar/protocol/Gl200ProtocolDecoder.java
@@ -716,7 +716,7 @@ public class Gl200ProtocolDecoder extends BaseProtocolDecoder {
int reportType = parser.nextInt(0);
if (type.equals("NMR")) {
- position.set(Position.KEY_MOTION, reportType);
+ position.set(Position.KEY_MOTION, reportType == 1);
} else if (type.equals("SOS")) {
position.set(Position.KEY_ALARM, Position.ALARM_SOS);
}
diff --git a/src/org/traccar/reports/ReportUtils.java b/src/org/traccar/reports/ReportUtils.java
index 1e004beae..cd30a61c4 100644
--- a/src/org/traccar/reports/ReportUtils.java
+++ b/src/org/traccar/reports/ReportUtils.java
@@ -220,7 +220,12 @@ public final class ReportUtils {
>= tripsConfig.getMinimalNoDataDuration()) {
return false;
}
- return positions.get(index).getSpeed() > speedThreshold;
+ if (positions.get(index).getAttributes().containsKey(Position.KEY_MOTION)
+ && positions.get(index).getAttributes().get(Position.KEY_MOTION) instanceof Boolean) {
+ return positions.get(index).getBoolean(Position.KEY_MOTION);
+ } else {
+ return positions.get(index).getSpeed() > speedThreshold;
+ }
}
public static Collection<BaseReport> detectTripsAndStops(TripsConfig tripsConfig, boolean ignoreOdometer,
diff --git a/test/org/traccar/MotionHandlerTest.java b/test/org/traccar/MotionHandlerTest.java
new file mode 100644
index 000000000..d716a5ec7
--- /dev/null
+++ b/test/org/traccar/MotionHandlerTest.java
@@ -0,0 +1,21 @@
+package org.traccar;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.traccar.model.Position;
+
+public class MotionHandlerTest {
+
+ @Test
+ public void testCalculateMotion() throws Exception {
+
+ MotionHandler motionHandler = new MotionHandler(0.01);
+
+ Position position = motionHandler.calculateMotion(new Position());
+
+ assertEquals(false, position.getAttributes().get(Position.KEY_MOTION));
+
+ }
+
+}
diff --git a/test/org/traccar/ProtocolTest.java b/test/org/traccar/ProtocolTest.java
index ee5811e49..eaecfab0b 100644
--- a/test/org/traccar/ProtocolTest.java
+++ b/test/org/traccar/ProtocolTest.java
@@ -237,6 +237,10 @@ public class ProtocolTest extends BaseTest {
Assert.assertTrue(attributes.get(Position.KEY_CHARGE) instanceof Boolean);
}
+ if (attributes.containsKey(Position.KEY_MOTION)) {
+ Assert.assertTrue(attributes.get(Position.KEY_MOTION) instanceof Boolean);
+ }
+
if (position.getNetwork() != null && position.getNetwork().getCellTowers() != null) {
for (CellTower cellTower : position.getNetwork().getCellTowers()) {
checkInteger(cellTower.getMobileCountryCode(), 0, 999);
diff --git a/test/org/traccar/events/MotionEventHandlerTest.java b/test/org/traccar/events/MotionEventHandlerTest.java
index 622680d91..f05ef54d5 100644
--- a/test/org/traccar/events/MotionEventHandlerTest.java
+++ b/test/org/traccar/events/MotionEventHandlerTest.java
@@ -18,7 +18,7 @@ public class MotionEventHandlerTest extends BaseTest {
MotionEventHandler motionEventHandler = new MotionEventHandler();
Position position = new Position();
- position.setSpeed(10.0);
+ position.set(Position.KEY_MOTION, true);
position.setValid(true);
Collection<Event> events = motionEventHandler.analyzePosition(position);
assertNotNull(events);