aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/org/traccar/BasePipelineFactory.java4
-rw-r--r--src/org/traccar/Context.java20
-rw-r--r--src/org/traccar/database/ConnectionManager.java18
-rw-r--r--src/org/traccar/events/MotionEventHandler.java34
-rw-r--r--src/org/traccar/events/OverspeedEventHandler.java40
-rw-r--r--test/org/traccar/events/MotionEventHandlerTest.java14
-rw-r--r--test/org/traccar/events/OverspeedEventHandlerTest.java17
7 files changed, 79 insertions, 68 deletions
diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java
index b368c800d..da8060071 100644
--- a/src/org/traccar/BasePipelineFactory.java
+++ b/src/org/traccar/BasePipelineFactory.java
@@ -165,9 +165,9 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory {
if (Context.getConfig().getBoolean("event.enable")) {
commandResultEventHandler = new CommandResultEventHandler();
- overspeedEventHandler = new OverspeedEventHandler();
+ overspeedEventHandler = Context.getOverspeedEventHandler();
fuelDropEventHandler = new FuelDropEventHandler();
- motionEventHandler = new MotionEventHandler();
+ motionEventHandler = Context.getMotionEventHandler();
geofenceEventHandler = new GeofenceEventHandler();
alertEventHandler = new AlertEventHandler();
ignitionEventHandler = new IgnitionEventHandler();
diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java
index 306a37e83..4bcc1468b 100644
--- a/src/org/traccar/Context.java
+++ b/src/org/traccar/Context.java
@@ -41,6 +41,8 @@ import org.traccar.database.GeofenceManager;
import org.traccar.database.GroupsManager;
import org.traccar.database.StatisticsManager;
import org.traccar.database.UsersManager;
+import org.traccar.events.MotionEventHandler;
+import org.traccar.events.OverspeedEventHandler;
import org.traccar.geocoder.BingMapsGeocoder;
import org.traccar.geocoder.FactualGeocoder;
import org.traccar.geocoder.GeocodeFarmGeocoder;
@@ -65,6 +67,7 @@ import org.traccar.geolocation.GeolocationProvider;
import org.traccar.geolocation.MozillaGeolocationProvider;
import org.traccar.geolocation.OpenCellIdGeolocationProvider;
import org.traccar.notification.EventForwarder;
+import org.traccar.reports.ReportUtils;
import org.traccar.smpp.SmppClient;
import org.traccar.web.WebServer;
@@ -229,6 +232,18 @@ public final class Context {
return smppClient;
}
+ private static MotionEventHandler motionEventHandler;
+
+ public static MotionEventHandler getMotionEventHandler() {
+ return motionEventHandler;
+ }
+
+ private static OverspeedEventHandler overspeedEventHandler;
+
+ public static OverspeedEventHandler getOverspeedEventHandler() {
+ return overspeedEventHandler;
+ }
+
public static void init(String[] arguments) throws Exception {
config = new Config();
@@ -350,6 +365,11 @@ public final class Context {
velocityEngine = new VelocityEngine();
velocityEngine.init(velocityProperties);
+
+ motionEventHandler = new MotionEventHandler(ReportUtils.initTripsConfig());
+ overspeedEventHandler = new OverspeedEventHandler(
+ Context.getConfig().getLong("event.overspeed.minimalDuration") * 1000,
+ Context.getConfig().getBoolean("event.overspeed.notRepeat"));
}
serverManager = new ServerManager();
diff --git a/src/org/traccar/database/ConnectionManager.java b/src/org/traccar/database/ConnectionManager.java
index b1dd1b726..7a0a6d30d 100644
--- a/src/org/traccar/database/ConnectionManager.java
+++ b/src/org/traccar/database/ConnectionManager.java
@@ -21,15 +21,12 @@ 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;
import org.traccar.model.DeviceState;
import org.traccar.model.Event;
import org.traccar.model.Position;
-import org.traccar.reports.ReportUtils;
-import org.traccar.reports.model.TripsConfig;
import java.net.SocketAddress;
import java.sql.SQLException;
@@ -47,9 +44,6 @@ public class ConnectionManager {
private final long deviceTimeout;
private final boolean enableStatusEvents;
private final boolean updateDeviceState;
- private TripsConfig tripsConfig = null;
- private long minimalOverspeedDuration;
- private boolean overspeedNotRepeat;
private final Map<Long, ActiveDevice> activeDevices = new ConcurrentHashMap<>();
private final Map<Long, Set<UpdateListener>> listeners = new ConcurrentHashMap<>();
@@ -59,11 +53,6 @@ public class ConnectionManager {
deviceTimeout = Context.getConfig().getLong("status.timeout", DEFAULT_TIMEOUT) * 1000;
enableStatusEvents = Context.getConfig().getBoolean("event.enable");
updateDeviceState = Context.getConfig().getBoolean("status.updateDeviceState");
- if (updateDeviceState) {
- tripsConfig = ReportUtils.initTripsConfig();
- minimalOverspeedDuration = Context.getConfig().getLong("event.overspeed.minimalDuration") * 1000;
- overspeedNotRepeat = Context.getConfig().getBoolean("event.overspeed.notRepeat");
- }
}
public void addActiveDevice(long deviceId, Protocol protocol, Channel channel, SocketAddress remoteAddress) {
@@ -150,14 +139,13 @@ public class ConnectionManager {
DeviceState deviceState = Context.getDeviceManager().getDeviceState(deviceId);
Set<Event> result = new HashSet<>();
- Event event = MotionEventHandler.updateMotionState(deviceState, tripsConfig);
+ Event event = Context.getMotionEventHandler().updateMotionState(deviceState);
if (event != null) {
result.add(event);
}
- event = OverspeedEventHandler.updateOverspeedState(deviceState, Context.getDeviceManager().
- lookupAttributeDouble(deviceId, OverspeedEventHandler.ATTRIBUTE_SPEED_LIMIT, 0, false),
- minimalOverspeedDuration, overspeedNotRepeat);
+ event = Context.getOverspeedEventHandler().updateOverspeedState(deviceState, Context.getDeviceManager().
+ lookupAttributeDouble(deviceId, OverspeedEventHandler.ATTRIBUTE_SPEED_LIMIT, 0, false));
if (event != null) {
result.add(event);
}
diff --git a/src/org/traccar/events/MotionEventHandler.java b/src/org/traccar/events/MotionEventHandler.java
index 1a8cb0ef8..b20a11999 100644
--- a/src/org/traccar/events/MotionEventHandler.java
+++ b/src/org/traccar/events/MotionEventHandler.java
@@ -32,11 +32,20 @@ public class MotionEventHandler extends BaseEventHandler {
private TripsConfig tripsConfig;
- public MotionEventHandler() {
- tripsConfig = ReportUtils.initTripsConfig();
+ public MotionEventHandler(TripsConfig tripsConfig) {
+ this.tripsConfig = tripsConfig;
}
- public static Event updateMotionState(DeviceState deviceState, TripsConfig tripsConfig) {
+ private Event newEvent(DeviceState deviceState, boolean newMotion) {
+ String eventType = newMotion ? Event.TYPE_DEVICE_MOVING : Event.TYPE_DEVICE_STOPPED;
+ Event event = new Event(eventType, deviceState.getMotionPosition().getDeviceId(),
+ deviceState.getMotionPosition().getId());
+ deviceState.setMotionState(newMotion);
+ deviceState.setMotionPosition(null);
+ return event;
+ }
+
+ public Event updateMotionState(DeviceState deviceState) {
Event result = null;
if (deviceState.getMotionState() != null && deviceState.getMotionPosition() != null) {
boolean newMotion = !deviceState.getMotionState();
@@ -45,16 +54,13 @@ public class MotionEventHandler extends BaseEventHandler {
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);
+ result = newEvent(deviceState, newMotion);
}
}
return result;
}
- public static Event updateMotionState(DeviceState deviceState, Position position, TripsConfig tripsConfig) {
+ public Event updateMotionState(DeviceState deviceState, Position position) {
Event result = null;
Boolean oldMotion = deviceState.getMotionState();
@@ -75,17 +81,11 @@ public class MotionEventHandler extends BaseEventHandler {
if (newMotion) {
if (motionTime + tripsConfig.getMinimalTripDuration() <= currentTime
|| distance >= tripsConfig.getMinimalTripDistance()) {
- result = new Event(Event.TYPE_DEVICE_MOVING, motionPosition.getDeviceId(),
- motionPosition.getId());
- deviceState.setMotionState(true);
- deviceState.setMotionPosition(null);
+ result = newEvent(deviceState, newMotion);
}
} else {
if (motionTime + tripsConfig.getMinimalParkingDuration() <= currentTime) {
- result = new Event(Event.TYPE_DEVICE_STOPPED, motionPosition.getDeviceId(),
- motionPosition.getId());
- deviceState.setMotionState(false);
- deviceState.setMotionPosition(null);
+ result = newEvent(deviceState, newMotion);
}
}
}
@@ -110,7 +110,7 @@ public class MotionEventHandler extends BaseEventHandler {
if (deviceState.getMotionState() == null) {
deviceState.setMotionState(position.getBoolean(Position.KEY_MOTION));
} else {
- result = updateMotionState(deviceState, position, tripsConfig);
+ result = updateMotionState(deviceState, position);
}
Context.getDeviceManager().setDeviceState(deviceId, deviceState);
if (result != null) {
diff --git a/src/org/traccar/events/OverspeedEventHandler.java b/src/org/traccar/events/OverspeedEventHandler.java
index f0bf8a032..953af6b33 100644
--- a/src/org/traccar/events/OverspeedEventHandler.java
+++ b/src/org/traccar/events/OverspeedEventHandler.java
@@ -32,13 +32,22 @@ public class OverspeedEventHandler extends BaseEventHandler {
private boolean notRepeat;
private long minimalDuration;
- public OverspeedEventHandler() {
- notRepeat = Context.getConfig().getBoolean("event.overspeed.notRepeat");
- minimalDuration = Context.getConfig().getLong("event.overspeed.minimalDuration") * 1000;
+ public OverspeedEventHandler(long minimalDuration, boolean notRepeat) {
+ this.notRepeat = notRepeat;
+ this.minimalDuration = minimalDuration;
}
- public static Event updateOverspeedState(DeviceState deviceState, double speedLimit,
- long minimalDuration, boolean notRepeat) {
+ private Event newEvent(DeviceState deviceState, double speedLimit) {
+ Event event = new Event(Event.TYPE_DEVICE_OVERSPEED, deviceState.getOverspeedPosition().getDeviceId(),
+ deviceState.getOverspeedPosition().getId());
+ event.set("speed", deviceState.getOverspeedPosition().getSpeed());
+ event.set(ATTRIBUTE_SPEED_LIMIT, speedLimit);
+ deviceState.setOverspeedState(notRepeat);
+ deviceState.setOverspeedPosition(null);
+ return event;
+ }
+
+ public Event updateOverspeedState(DeviceState deviceState, double speedLimit) {
Event result = null;
if (deviceState.getOverspeedState() != null && !deviceState.getOverspeedState()
&& deviceState.getOverspeedPosition() != null && speedLimit != 0) {
@@ -46,19 +55,13 @@ public class OverspeedEventHandler extends BaseEventHandler {
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);
+ result = newEvent(deviceState, speedLimit);
}
}
return result;
}
- public static Event updateOverspeedState(DeviceState deviceState, Position position, double speedLimit,
- long minimalOverspeedDuration, boolean notRepeat) {
+ public Event updateOverspeedState(DeviceState deviceState, Position position, double speedLimit) {
Event result = null;
Boolean oldOverspeed = deviceState.getOverspeedState();
@@ -78,13 +81,8 @@ public class OverspeedEventHandler extends BaseEventHandler {
Position overspeedPosition = deviceState.getOverspeedPosition();
if (overspeedPosition != null) {
long overspeedTime = overspeedPosition.getFixTime().getTime();
- if (newOverspeed && overspeedTime + minimalOverspeedDuration <= 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);
+ if (newOverspeed && overspeedTime + minimalDuration <= currentTime) {
+ result = newEvent(deviceState, speedLimit);
}
}
return result;
@@ -113,7 +111,7 @@ public class OverspeedEventHandler extends BaseEventHandler {
if (deviceState.getOverspeedState() == null) {
deviceState.setOverspeedState(position.getSpeed() > speedLimit);
} else {
- result = updateOverspeedState(deviceState, position, speedLimit, minimalDuration, notRepeat);
+ result = updateOverspeedState(deviceState, position, speedLimit);
}
Context.getDeviceManager().setDeviceState(deviceId, deviceState);
diff --git a/test/org/traccar/events/MotionEventHandlerTest.java b/test/org/traccar/events/MotionEventHandlerTest.java
index 9df573244..6b7b9daee 100644
--- a/test/org/traccar/events/MotionEventHandlerTest.java
+++ b/test/org/traccar/events/MotionEventHandlerTest.java
@@ -28,7 +28,8 @@ public class MotionEventHandlerTest extends BaseTest {
@Test
public void testMotionWithPosition() throws Exception {
- TripsConfig tripsConfig = new TripsConfig(500, 300 * 1000, 300 * 1000, false, 0);
+ MotionEventHandler motionEventHandler = new MotionEventHandler(
+ new TripsConfig(500, 300 * 1000, 300 * 1000, false, 0));
Position position = new Position();
position.setTime(date("2017-01-01 00:00:00"));
@@ -43,11 +44,11 @@ public class MotionEventHandlerTest extends BaseTest {
nextPosition.set(Position.KEY_MOTION, true);
nextPosition.set(Position.KEY_TOTAL_DISTANCE, 200);
- Event event = MotionEventHandler.updateMotionState(deviceState, nextPosition, tripsConfig);
+ Event event = motionEventHandler.updateMotionState(deviceState, nextPosition);
assertNull(event);
nextPosition.set(Position.KEY_TOTAL_DISTANCE, 600);
- event = MotionEventHandler.updateMotionState(deviceState, nextPosition, tripsConfig);
+ event = motionEventHandler.updateMotionState(deviceState, nextPosition);
assertNotNull(event);
assertEquals(Event.TYPE_DEVICE_MOVING, event.getType());
assertTrue(deviceState.getMotionState());
@@ -57,7 +58,7 @@ public class MotionEventHandlerTest extends BaseTest {
deviceState.setMotionPosition(position);
nextPosition.setTime(date("2017-01-01 00:06:00"));
nextPosition.set(Position.KEY_TOTAL_DISTANCE, 200);
- event = MotionEventHandler.updateMotionState(deviceState, nextPosition, tripsConfig);
+ event = motionEventHandler.updateMotionState(deviceState, nextPosition);
assertNotNull(event);
assertEquals(Event.TYPE_DEVICE_MOVING, event.getType());
assertTrue(deviceState.getMotionState());
@@ -66,7 +67,8 @@ public class MotionEventHandlerTest extends BaseTest {
@Test
public void testMotionWithStatus() throws Exception {
- TripsConfig tripsConfig = new TripsConfig(500, 300 * 1000, 300 * 1000, false, 0);
+ MotionEventHandler motionEventHandler = new MotionEventHandler(
+ new TripsConfig(500, 300 * 1000, 300 * 1000, false, 0));
Position position = new Position();
position.setTime(new Date(System.currentTimeMillis() - 360000));
@@ -75,7 +77,7 @@ public class MotionEventHandlerTest extends BaseTest {
deviceState.setMotionState(false);
deviceState.setMotionPosition(position);
- Event event = MotionEventHandler.updateMotionState(deviceState, tripsConfig);
+ Event event = motionEventHandler.updateMotionState(deviceState);
assertNotNull(event);
assertEquals(Event.TYPE_DEVICE_MOVING, event.getType());
diff --git a/test/org/traccar/events/OverspeedEventHandlerTest.java b/test/org/traccar/events/OverspeedEventHandlerTest.java
index eae0917c0..48d7445ff 100644
--- a/test/org/traccar/events/OverspeedEventHandlerTest.java
+++ b/test/org/traccar/events/OverspeedEventHandlerTest.java
@@ -26,13 +26,15 @@ public class OverspeedEventHandlerTest extends BaseTest {
}
private void testOverspeedWithPosition(boolean notRepeat) throws Exception {
+ OverspeedEventHandler overspeedEventHandler = new OverspeedEventHandler(15000, notRepeat);
+
Position position = new Position();
position.setTime(date("2017-01-01 00:00:00"));
position.setSpeed(50);
DeviceState deviceState = new DeviceState();
deviceState.setOverspeedState(false);
- Event event = OverspeedEventHandler.updateOverspeedState(deviceState, position, 40, 15000, notRepeat);
+ Event event = overspeedEventHandler.updateOverspeedState(deviceState, position, 40);
assertNull(event);
assertFalse(deviceState.getOverspeedState());
assertEquals(position, deviceState.getOverspeedPosition());
@@ -41,12 +43,12 @@ public class OverspeedEventHandlerTest extends BaseTest {
nextPosition.setTime(date("2017-01-01 00:00:10"));
nextPosition.setSpeed(55);
- event = OverspeedEventHandler.updateOverspeedState(deviceState, nextPosition, 40, 15000, notRepeat);
+ event = overspeedEventHandler.updateOverspeedState(deviceState, nextPosition, 40);
assertNull(event);
nextPosition.setTime(date("2017-01-01 00:00:20"));
- event = OverspeedEventHandler.updateOverspeedState(deviceState, nextPosition, 40, 15000, notRepeat);
+ event = overspeedEventHandler.updateOverspeedState(deviceState, nextPosition, 40);
assertNotNull(event);
assertEquals(Event.TYPE_DEVICE_OVERSPEED, event.getType());
assertEquals(50, event.getDouble("speed"), 0.1);
@@ -56,7 +58,7 @@ public class OverspeedEventHandlerTest extends BaseTest {
assertNull(deviceState.getOverspeedPosition());
nextPosition.setTime(date("2017-01-01 00:00:30"));
- event = OverspeedEventHandler.updateOverspeedState(deviceState, nextPosition, 40, 15000, notRepeat);
+ event = overspeedEventHandler.updateOverspeedState(deviceState, nextPosition, 40);
assertNull(event);
assertEquals(notRepeat, deviceState.getOverspeedState());
@@ -69,13 +71,15 @@ public class OverspeedEventHandlerTest extends BaseTest {
nextPosition.setTime(date("2017-01-01 00:00:40"));
nextPosition.setSpeed(30);
- event = OverspeedEventHandler.updateOverspeedState(deviceState, nextPosition, 40, 15000, notRepeat);
+ event = overspeedEventHandler.updateOverspeedState(deviceState, nextPosition, 40);
assertNull(event);
assertFalse(deviceState.getOverspeedState());
assertNull(deviceState.getOverspeedPosition());
}
private void testOverspeedWithStatus(boolean notRepeat) throws Exception {
+ OverspeedEventHandler overspeedEventHandler = new OverspeedEventHandler(15000, notRepeat);
+
Position position = new Position();
position.setTime(new Date(System.currentTimeMillis() - 30000));
position.setSpeed(50);
@@ -83,12 +87,11 @@ public class OverspeedEventHandlerTest extends BaseTest {
deviceState.setOverspeedState(false);
deviceState.setOverspeedPosition(position);
- Event event = OverspeedEventHandler.updateOverspeedState(deviceState, 40, 15000, notRepeat);
+ Event event = overspeedEventHandler.updateOverspeedState(deviceState, 40);
assertNotNull(event);
assertEquals(Event.TYPE_DEVICE_OVERSPEED, event.getType());
assertEquals(notRepeat, deviceState.getOverspeedState());
-
}
@Test