aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbyss777 <abyss@fox5.ru>2017-08-11 09:22:53 +0500
committerAbyss777 <abyss@fox5.ru>2017-08-11 09:27:41 +0500
commit2e459d8d591ee9d3578a38d981e7c1c13eb3c389 (patch)
treed5d69d81dc796b54d67946b753e5f801fcb07913
parent532e0a98d469573a575dc595554792cbbd4cd953 (diff)
downloadtrackermap-server-2e459d8d591ee9d3578a38d981e7c1c13eb3c389.tar.gz
trackermap-server-2e459d8d591ee9d3578a38d981e7c1c13eb3c389.tar.bz2
trackermap-server-2e459d8d591ee9d3578a38d981e7c1c13eb3c389.zip
Move state updates from ConnectionManager to proper event handlers
-rw-r--r--src/org/traccar/database/ConnectionManager.java40
-rw-r--r--src/org/traccar/events/MotionEventHandler.java18
-rw-r--r--src/org/traccar/events/OverspeedEventHandler.java20
-rw-r--r--test/org/traccar/events/MotionEventHandlerTest.java21
-rw-r--r--test/org/traccar/events/OverspeedEventHandlerTest.java30
5 files changed, 93 insertions, 36 deletions
diff --git a/src/org/traccar/database/ConnectionManager.java b/src/org/traccar/database/ConnectionManager.java
index ee2a7bb47..b1dd1b726 100644
--- a/src/org/traccar/database/ConnectionManager.java
+++ b/src/org/traccar/database/ConnectionManager.java
@@ -21,6 +21,7 @@ import org.jboss.netty.util.TimerTask;
import org.traccar.Context;
import org.traccar.GlobalTimer;
import org.traccar.Protocol;
+import org.traccar.events.MotionEventHandler;
import org.traccar.events.OverspeedEventHandler;
import org.traccar.helper.Log;
import org.traccar.model.Device;
@@ -148,36 +149,17 @@ public class ConnectionManager {
public Set<Event> updateDeviceState(long deviceId) {
DeviceState deviceState = Context.getDeviceManager().getDeviceState(deviceId);
Set<Event> result = new HashSet<>();
- long currentTime = System.currentTimeMillis();
- if (deviceState.getMotionState() != null && deviceState.getMotionPosition() != null) {
- boolean newMotion = !deviceState.getMotionState();
- Position motionPosition = deviceState.getMotionPosition();
- long motionTime = motionPosition.getFixTime().getTime()
- + (newMotion ? tripsConfig.getMinimalTripDuration() : tripsConfig.getMinimalParkingDuration());
- if (motionTime <= currentTime) {
- String eventType = newMotion ? Event.TYPE_DEVICE_MOVING : Event.TYPE_DEVICE_STOPPED;
- result.add(new Event(eventType, motionPosition.getDeviceId(), motionPosition.getId()));
- deviceState.setMotionState(newMotion);
- deviceState.setMotionPosition(null);
- }
+
+ Event event = MotionEventHandler.updateMotionState(deviceState, tripsConfig);
+ if (event != null) {
+ result.add(event);
}
- if (deviceState.getOverspeedState() != null && !deviceState.getOverspeedState()
- && deviceState.getOverspeedPosition() != null) {
- double speedLimit = Context.getDeviceManager().lookupAttributeDouble(deviceId,
- OverspeedEventHandler.ATTRIBUTE_SPEED_LIMIT, 0, false);
- if (speedLimit != 0) {
- Position overspeedPosition = deviceState.getOverspeedPosition();
- long overspeedTime = overspeedPosition.getFixTime().getTime();
- if (overspeedTime + minimalOverspeedDuration <= currentTime) {
- Event event = new Event(Event.TYPE_DEVICE_OVERSPEED, overspeedPosition.getDeviceId(),
- overspeedPosition.getId());
- event.set("speed", overspeedPosition.getSpeed());
- event.set(OverspeedEventHandler.ATTRIBUTE_SPEED_LIMIT, speedLimit);
- result.add(event);
- deviceState.setOverspeedState(overspeedNotRepeat);
- deviceState.setOverspeedPosition(null);
- }
- }
+
+ event = OverspeedEventHandler.updateOverspeedState(deviceState, Context.getDeviceManager().
+ lookupAttributeDouble(deviceId, OverspeedEventHandler.ATTRIBUTE_SPEED_LIMIT, 0, false),
+ minimalOverspeedDuration, overspeedNotRepeat);
+ if (event != null) {
+ result.add(event);
}
return result;
diff --git a/src/org/traccar/events/MotionEventHandler.java b/src/org/traccar/events/MotionEventHandler.java
index ed21d7b83..1a8cb0ef8 100644
--- a/src/org/traccar/events/MotionEventHandler.java
+++ b/src/org/traccar/events/MotionEventHandler.java
@@ -36,6 +36,24 @@ public class MotionEventHandler extends BaseEventHandler {
tripsConfig = ReportUtils.initTripsConfig();
}
+ public static Event updateMotionState(DeviceState deviceState, TripsConfig tripsConfig) {
+ Event result = null;
+ if (deviceState.getMotionState() != null && deviceState.getMotionPosition() != null) {
+ boolean newMotion = !deviceState.getMotionState();
+ Position motionPosition = deviceState.getMotionPosition();
+ long currentTime = System.currentTimeMillis();
+ long motionTime = motionPosition.getFixTime().getTime()
+ + (newMotion ? tripsConfig.getMinimalTripDuration() : tripsConfig.getMinimalParkingDuration());
+ if (motionTime <= currentTime) {
+ String eventType = newMotion ? Event.TYPE_DEVICE_MOVING : Event.TYPE_DEVICE_STOPPED;
+ result = new Event(eventType, motionPosition.getDeviceId(), motionPosition.getId());
+ deviceState.setMotionState(newMotion);
+ deviceState.setMotionPosition(null);
+ }
+ }
+ return result;
+ }
+
public static Event updateMotionState(DeviceState deviceState, Position position, TripsConfig tripsConfig) {
Event result = null;
Boolean oldMotion = deviceState.getMotionState();
diff --git a/src/org/traccar/events/OverspeedEventHandler.java b/src/org/traccar/events/OverspeedEventHandler.java
index 3b91fed4d..f0bf8a032 100644
--- a/src/org/traccar/events/OverspeedEventHandler.java
+++ b/src/org/traccar/events/OverspeedEventHandler.java
@@ -37,6 +37,26 @@ public class OverspeedEventHandler extends BaseEventHandler {
minimalDuration = Context.getConfig().getLong("event.overspeed.minimalDuration") * 1000;
}
+ public static Event updateOverspeedState(DeviceState deviceState, double speedLimit,
+ long minimalDuration, boolean notRepeat) {
+ Event result = null;
+ if (deviceState.getOverspeedState() != null && !deviceState.getOverspeedState()
+ && deviceState.getOverspeedPosition() != null && speedLimit != 0) {
+ long currentTime = System.currentTimeMillis();
+ Position overspeedPosition = deviceState.getOverspeedPosition();
+ long overspeedTime = overspeedPosition.getFixTime().getTime();
+ if (overspeedTime + minimalDuration <= currentTime) {
+ result = new Event(Event.TYPE_DEVICE_OVERSPEED, overspeedPosition.getDeviceId(),
+ overspeedPosition.getId());
+ result.set("speed", overspeedPosition.getSpeed());
+ result.set(ATTRIBUTE_SPEED_LIMIT, speedLimit);
+ deviceState.setOverspeedState(notRepeat);
+ deviceState.setOverspeedPosition(null);
+ }
+ }
+ return result;
+ }
+
public static Event updateOverspeedState(DeviceState deviceState, Position position, double speedLimit,
long minimalOverspeedDuration, boolean notRepeat) {
Event result = null;
diff --git a/test/org/traccar/events/MotionEventHandlerTest.java b/test/org/traccar/events/MotionEventHandlerTest.java
index c44f3f4eb..9df573244 100644
--- a/test/org/traccar/events/MotionEventHandlerTest.java
+++ b/test/org/traccar/events/MotionEventHandlerTest.java
@@ -27,7 +27,7 @@ public class MotionEventHandlerTest extends BaseTest {
}
@Test
- public void testMotionEventHandler() throws Exception {
+ public void testMotionWithPosition() throws Exception {
TripsConfig tripsConfig = new TripsConfig(500, 300 * 1000, 300 * 1000, false, 0);
Position position = new Position();
@@ -64,4 +64,23 @@ public class MotionEventHandlerTest extends BaseTest {
assertNull(deviceState.getMotionPosition());
}
+ @Test
+ public void testMotionWithStatus() throws Exception {
+ TripsConfig tripsConfig = new TripsConfig(500, 300 * 1000, 300 * 1000, false, 0);
+
+ Position position = new Position();
+ position.setTime(new Date(System.currentTimeMillis() - 360000));
+ position.set(Position.KEY_MOTION, true);
+ DeviceState deviceState = new DeviceState();
+ deviceState.setMotionState(false);
+ deviceState.setMotionPosition(position);
+
+ Event event = MotionEventHandler.updateMotionState(deviceState, tripsConfig);
+
+ assertNotNull(event);
+ assertEquals(Event.TYPE_DEVICE_MOVING, event.getType());
+ assertTrue(deviceState.getMotionState());
+ assertNull(deviceState.getMotionPosition());
+ }
+
}
diff --git a/test/org/traccar/events/OverspeedEventHandlerTest.java b/test/org/traccar/events/OverspeedEventHandlerTest.java
index 25bbb4319..eae0917c0 100644
--- a/test/org/traccar/events/OverspeedEventHandlerTest.java
+++ b/test/org/traccar/events/OverspeedEventHandlerTest.java
@@ -2,7 +2,6 @@ package org.traccar.events;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
@@ -26,7 +25,7 @@ public class OverspeedEventHandlerTest extends BaseTest {
return dateFormat.parse(time);
}
- private void testOverspeed(boolean notRepeat) throws Exception {
+ private void testOverspeedWithPosition(boolean notRepeat) throws Exception {
Position position = new Position();
position.setTime(date("2017-01-01 00:00:00"));
position.setSpeed(50);
@@ -55,12 +54,12 @@ public class OverspeedEventHandlerTest extends BaseTest {
assertEquals(notRepeat, deviceState.getOverspeedState());
assertNull(deviceState.getOverspeedPosition());
-
+
nextPosition.setTime(date("2017-01-01 00:00:30"));
event = OverspeedEventHandler.updateOverspeedState(deviceState, nextPosition, 40, 15000, notRepeat);
assertNull(event);
assertEquals(notRepeat, deviceState.getOverspeedState());
-
+
if (notRepeat) {
assertNull(deviceState.getOverspeedPosition());
} else {
@@ -76,10 +75,29 @@ public class OverspeedEventHandlerTest extends BaseTest {
assertNull(deviceState.getOverspeedPosition());
}
+ private void testOverspeedWithStatus(boolean notRepeat) throws Exception {
+ Position position = new Position();
+ position.setTime(new Date(System.currentTimeMillis() - 30000));
+ position.setSpeed(50);
+ DeviceState deviceState = new DeviceState();
+ deviceState.setOverspeedState(false);
+ deviceState.setOverspeedPosition(position);
+
+ Event event = OverspeedEventHandler.updateOverspeedState(deviceState, 40, 15000, notRepeat);
+
+ assertNotNull(event);
+ assertEquals(Event.TYPE_DEVICE_OVERSPEED, event.getType());
+ assertEquals(notRepeat, deviceState.getOverspeedState());
+
+ }
+
@Test
public void testOverspeedEventHandler() throws Exception {
- testOverspeed(false);
- testOverspeed(true);
+ testOverspeedWithPosition(false);
+ testOverspeedWithPosition(true);
+
+ testOverspeedWithStatus(false);
+ testOverspeedWithStatus(true);
}
}