diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2019-03-09 15:01:08 -0800 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2019-03-09 15:01:08 -0800 |
commit | e3668177e4cb989b596ed7b2eef71eb348860736 (patch) | |
tree | 7c5a0f131471ce8a3b5206a42fb44bb310d6ccf5 | |
parent | 2af6e4cc6255f59acaa17e6763063b775d7ba1ea (diff) | |
download | trackermap-server-e3668177e4cb989b596ed7b2eef71eb348860736.tar.gz trackermap-server-e3668177e4cb989b596ed7b2eef71eb348860736.tar.bz2 trackermap-server-e3668177e4cb989b596ed7b2eef71eb348860736.zip |
Refactor motion event handler
-rw-r--r-- | src/org/traccar/BasePipelineFactory.java | 4 | ||||
-rw-r--r-- | src/org/traccar/Context.java | 9 | ||||
-rw-r--r-- | src/org/traccar/MainModule.java | 8 | ||||
-rw-r--r-- | src/org/traccar/database/ConnectionManager.java | 4 | ||||
-rw-r--r-- | src/org/traccar/handler/events/MotionEventHandler.java | 21 | ||||
-rw-r--r-- | src/org/traccar/reports/ReportUtils.java | 5 | ||||
-rw-r--r-- | src/org/traccar/reports/Stops.java | 8 | ||||
-rw-r--r-- | src/org/traccar/reports/Trips.java | 8 | ||||
-rw-r--r-- | test/org/traccar/handler/events/MotionEventHandlerTest.java | 6 | ||||
-rw-r--r-- | test/org/traccar/reports/ReportUtilsTest.java | 40 |
10 files changed, 73 insertions, 40 deletions
diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index 5cadfb2a6..d0136a6bb 100644 --- a/src/org/traccar/BasePipelineFactory.java +++ b/src/org/traccar/BasePipelineFactory.java @@ -58,7 +58,6 @@ public abstract class BasePipelineFactory extends ChannelInitializer<Channel> { private boolean eventsEnabled; private int timeout; - private MotionEventHandler motionEventHandler; private GeofenceEventHandler geofenceEventHandler; private AlertEventHandler alertEventHandler; private IgnitionEventHandler ignitionEventHandler; @@ -74,7 +73,6 @@ public abstract class BasePipelineFactory extends ChannelInitializer<Channel> { } if (eventsEnabled) { - motionEventHandler = Context.getMotionEventHandler(); geofenceEventHandler = new GeofenceEventHandler(); alertEventHandler = new AlertEventHandler(); ignitionEventHandler = new IgnitionEventHandler(); @@ -159,7 +157,7 @@ public abstract class BasePipelineFactory extends ChannelInitializer<Channel> { Main.getInjector().getInstance(CommandResultEventHandler.class), Main.getInjector().getInstance(OverspeedEventHandler.class), Main.getInjector().getInstance(FuelDropEventHandler.class), - motionEventHandler, + Main.getInjector().getInstance(MotionEventHandler.class), geofenceEventHandler, alertEventHandler, ignitionEventHandler, diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 845dae3c6..9c20db9e4 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -41,7 +41,6 @@ import org.traccar.database.MediaManager; import org.traccar.database.NotificationManager; import org.traccar.database.PermissionsManager; import org.traccar.database.UsersManager; -import org.traccar.handler.events.MotionEventHandler; import org.traccar.geocoder.Geocoder; import org.traccar.helper.Log; import org.traccar.helper.SanitizerModule; @@ -238,12 +237,6 @@ public final class Context { return smsManager; } - private static MotionEventHandler motionEventHandler; - - public static MotionEventHandler getMotionEventHandler() { - return motionEventHandler; - } - private static TripsConfig tripsConfig; public static TripsConfig getTripsConfig() { @@ -378,8 +371,6 @@ public final class Context { velocityEngine = new VelocityEngine(); velocityEngine.init(velocityProperties); - - motionEventHandler = new MotionEventHandler(tripsConfig); } public static void init(IdentityManager testIdentityManager, MediaManager testMediaManager) { diff --git a/src/org/traccar/MainModule.java b/src/org/traccar/MainModule.java index 46af5670c..10d35f72f 100644 --- a/src/org/traccar/MainModule.java +++ b/src/org/traccar/MainModule.java @@ -58,6 +58,7 @@ import org.traccar.handler.MotionHandler; import org.traccar.handler.RemoteAddressHandler; import org.traccar.handler.events.CommandResultEventHandler; import org.traccar.handler.events.FuelDropEventHandler; +import org.traccar.handler.events.MotionEventHandler; import org.traccar.handler.events.OverspeedEventHandler; import org.traccar.reports.model.TripsConfig; @@ -298,6 +299,13 @@ public class MainModule extends AbstractModule { return new FuelDropEventHandler(identityManager); } + @Singleton + @Provides + public static MotionEventHandler provideMotionEventHandler( + IdentityManager identityManager, DeviceManager deviceManager, TripsConfig tripsConfig) { + return new MotionEventHandler(identityManager, deviceManager, tripsConfig); + } + @Override protected void configure() { binder().requireExplicitBindings(); diff --git a/src/org/traccar/database/ConnectionManager.java b/src/org/traccar/database/ConnectionManager.java index 157f2d0a8..8bae1ea93 100644 --- a/src/org/traccar/database/ConnectionManager.java +++ b/src/org/traccar/database/ConnectionManager.java @@ -24,6 +24,7 @@ import org.traccar.Context; import org.traccar.GlobalTimer; import org.traccar.Main; import org.traccar.Protocol; +import org.traccar.handler.events.MotionEventHandler; import org.traccar.handler.events.OverspeedEventHandler; import org.traccar.model.Device; import org.traccar.model.DeviceState; @@ -148,7 +149,8 @@ public class ConnectionManager { DeviceState deviceState = Context.getDeviceManager().getDeviceState(deviceId); Map<Event, Position> result = new HashMap<>(); - Map<Event, Position> event = Context.getMotionEventHandler().updateMotionState(deviceState); + Map<Event, Position> event = Main.getInjector() + .getInstance(MotionEventHandler.class).updateMotionState(deviceState); if (event != null) { result.putAll(event); } diff --git a/src/org/traccar/handler/events/MotionEventHandler.java b/src/org/traccar/handler/events/MotionEventHandler.java index 5e88d34b5..9ec02ccfb 100644 --- a/src/org/traccar/handler/events/MotionEventHandler.java +++ b/src/org/traccar/handler/events/MotionEventHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2019 Anton Tananaev (anton@traccar.org) * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -20,7 +20,8 @@ import java.util.Collections; import java.util.Map; import io.netty.channel.ChannelHandler; -import org.traccar.Context; +import org.traccar.database.DeviceManager; +import org.traccar.database.IdentityManager; import org.traccar.model.Device; import org.traccar.model.DeviceState; import org.traccar.model.Event; @@ -31,9 +32,13 @@ import org.traccar.reports.model.TripsConfig; @ChannelHandler.Sharable public class MotionEventHandler extends BaseEventHandler { - private TripsConfig tripsConfig; + private final IdentityManager identityManager; + private final DeviceManager deviceManager; + private final TripsConfig tripsConfig; - public MotionEventHandler(TripsConfig tripsConfig) { + public MotionEventHandler(IdentityManager identityManager, DeviceManager deviceManager, TripsConfig tripsConfig) { + this.identityManager = identityManager; + this.deviceManager = deviceManager; this.tripsConfig = tripsConfig; } @@ -106,24 +111,24 @@ public class MotionEventHandler extends BaseEventHandler { protected Map<Event, Position> analyzePosition(Position position) { long deviceId = position.getDeviceId(); - Device device = Context.getIdentityManager().getById(deviceId); + Device device = identityManager.getById(deviceId); if (device == null) { return null; } - if (!Context.getIdentityManager().isLatestPosition(position) + if (!identityManager.isLatestPosition(position) || !tripsConfig.getProcessInvalidPositions() && !position.getValid()) { return null; } Map<Event, Position> result = null; - DeviceState deviceState = Context.getDeviceManager().getDeviceState(deviceId); + DeviceState deviceState = deviceManager.getDeviceState(deviceId); if (deviceState.getMotionState() == null) { deviceState.setMotionState(position.getBoolean(Position.KEY_MOTION)); } else { result = updateMotionState(deviceState, position); } - Context.getDeviceManager().setDeviceState(deviceId, deviceState); + deviceManager.setDeviceState(deviceId, deviceState); return result; } diff --git a/src/org/traccar/reports/ReportUtils.java b/src/org/traccar/reports/ReportUtils.java index 5e02afb64..3a631e0d9 100644 --- a/src/org/traccar/reports/ReportUtils.java +++ b/src/org/traccar/reports/ReportUtils.java @@ -26,6 +26,8 @@ import org.jxls.transform.Transformer; import org.jxls.transform.poi.PoiTransformer; import org.jxls.util.TransformerFactory; import org.traccar.Context; +import org.traccar.database.DeviceManager; +import org.traccar.database.IdentityManager; import org.traccar.handler.events.MotionEventHandler; import org.traccar.model.DeviceState; import org.traccar.model.Driver; @@ -323,6 +325,7 @@ public final class ReportUtils { } public static <T extends BaseReport> Collection<T> detectTripsAndStops( + IdentityManager identityManager, DeviceManager deviceManager, Collection<Position> positionCollection, TripsConfig tripsConfig, boolean ignoreOdometer, Class<T> reportClass) { @@ -331,7 +334,7 @@ public final class ReportUtils { ArrayList<Position> positions = new ArrayList<>(positionCollection); if (!positions.isEmpty()) { boolean trips = reportClass.equals(TripReport.class); - MotionEventHandler motionHandler = new MotionEventHandler(tripsConfig); + MotionEventHandler motionHandler = new MotionEventHandler(identityManager, deviceManager, tripsConfig); DeviceState deviceState = new DeviceState(); deviceState.setMotionState(isMoving(positions, 0, tripsConfig)); int startEventIndex = trips == deviceState.getMotionState() ? 0 : -1; diff --git a/src/org/traccar/reports/Stops.java b/src/org/traccar/reports/Stops.java index aa7a1ef20..98c9cef00 100644 --- a/src/org/traccar/reports/Stops.java +++ b/src/org/traccar/reports/Stops.java @@ -28,6 +28,9 @@ import java.util.Date; import org.apache.poi.ss.util.WorkbookUtil; import org.traccar.Context; +import org.traccar.Main; +import org.traccar.database.DeviceManager; +import org.traccar.database.IdentityManager; import org.traccar.model.Device; import org.traccar.model.Group; import org.traccar.reports.model.DeviceReport; @@ -42,8 +45,11 @@ public final class Stops { boolean ignoreOdometer = Context.getDeviceManager() .lookupAttributeBoolean(deviceId, "report.ignoreOdometer", false, true); + IdentityManager identityManager = Main.getInjector().getInstance(IdentityManager.class); + DeviceManager deviceManager = Main.getInjector().getInstance(DeviceManager.class); + return ReportUtils.detectTripsAndStops( - Context.getDataManager().getPositions(deviceId, from, to), + identityManager, deviceManager, Context.getDataManager().getPositions(deviceId, from, to), Context.getTripsConfig(), ignoreOdometer, StopReport.class); } diff --git a/src/org/traccar/reports/Trips.java b/src/org/traccar/reports/Trips.java index 792833c27..3cda65553 100644 --- a/src/org/traccar/reports/Trips.java +++ b/src/org/traccar/reports/Trips.java @@ -27,6 +27,9 @@ import java.util.Date; import org.apache.poi.ss.util.WorkbookUtil; import org.traccar.Context; +import org.traccar.Main; +import org.traccar.database.DeviceManager; +import org.traccar.database.IdentityManager; import org.traccar.model.Device; import org.traccar.model.Group; import org.traccar.reports.model.DeviceReport; @@ -41,8 +44,11 @@ public final class Trips { boolean ignoreOdometer = Context.getDeviceManager() .lookupAttributeBoolean(deviceId, "report.ignoreOdometer", false, true); + IdentityManager identityManager = Main.getInjector().getInstance(IdentityManager.class); + DeviceManager deviceManager = Main.getInjector().getInstance(DeviceManager.class); + return ReportUtils.detectTripsAndStops( - Context.getDataManager().getPositions(deviceId, from, to), + identityManager, deviceManager, Context.getDataManager().getPositions(deviceId, from, to), Context.getTripsConfig(), ignoreOdometer, TripReport.class); } diff --git a/test/org/traccar/handler/events/MotionEventHandlerTest.java b/test/org/traccar/handler/events/MotionEventHandlerTest.java index 8e643c476..f57c16635 100644 --- a/test/org/traccar/handler/events/MotionEventHandlerTest.java +++ b/test/org/traccar/handler/events/MotionEventHandlerTest.java @@ -31,7 +31,7 @@ public class MotionEventHandlerTest extends BaseTest { @Test public void testMotionWithPosition() throws Exception { MotionEventHandler motionEventHandler = new MotionEventHandler( - new TripsConfig(500, 300 * 1000, 300 * 1000, 0, false, false, 0.01)); + null, null, new TripsConfig(500, 300 * 1000, 300 * 1000, 0, false, false, 0.01)); Position position = new Position(); position.setTime(date("2017-01-01 00:00:00")); @@ -72,7 +72,7 @@ public class MotionEventHandlerTest extends BaseTest { @Test public void testMotionWithStatus() throws Exception { MotionEventHandler motionEventHandler = new MotionEventHandler( - new TripsConfig(500, 300 * 1000, 300 * 1000, 0, false, false, 0.01)); + null, null, new TripsConfig(500, 300 * 1000, 300 * 1000, 0, false, false, 0.01)); Position position = new Position(); position.setTime(new Date(System.currentTimeMillis() - 360000)); @@ -93,7 +93,7 @@ public class MotionEventHandlerTest extends BaseTest { @Test public void testStopWithPositionIgnition() throws Exception { MotionEventHandler motionEventHandler = new MotionEventHandler( - new TripsConfig(500, 300 * 1000, 300 * 1000, 0, true, false, 0.01)); + null, null, new TripsConfig(500, 300 * 1000, 300 * 1000, 0, true, false, 0.01)); Position position = new Position(); position.setTime(date("2017-01-01 00:00:00")); diff --git a/test/org/traccar/reports/ReportUtilsTest.java b/test/org/traccar/reports/ReportUtilsTest.java index a8ff653c9..01b9c276f 100644 --- a/test/org/traccar/reports/ReportUtilsTest.java +++ b/test/org/traccar/reports/ReportUtilsTest.java @@ -17,6 +17,7 @@ import java.util.TimeZone; import org.junit.Test; import org.traccar.BaseTest; +import org.traccar.TestIdentityManager; import org.traccar.model.Position; import org.traccar.reports.model.StopReport; import org.traccar.reports.model.TripReport; @@ -79,7 +80,8 @@ public class ReportUtilsTest extends BaseTest { TripsConfig tripsConfig = new TripsConfig(500, 300000, 180000, 900000, false, false, 0.01); - Collection<TripReport> trips = ReportUtils.detectTripsAndStops(data, tripsConfig, false, TripReport.class); + Collection<TripReport> trips = ReportUtils.detectTripsAndStops( + new TestIdentityManager(), null, data, tripsConfig, false, TripReport.class); assertNotNull(trips); assertFalse(trips.isEmpty()); @@ -93,7 +95,8 @@ public class ReportUtilsTest extends BaseTest { assertEquals(10, itemTrip.getMaxSpeed(), 0.01); assertEquals(3000, itemTrip.getDistance(), 0.01); - Collection<StopReport> stops = ReportUtils.detectTripsAndStops(data, tripsConfig, false, StopReport.class); + Collection<StopReport> stops = ReportUtils.detectTripsAndStops( + new TestIdentityManager(), null, data, tripsConfig, false, StopReport.class); assertNotNull(stops); assertFalse(stops.isEmpty()); @@ -131,7 +134,8 @@ public class ReportUtilsTest extends BaseTest { TripsConfig tripsConfig = new TripsConfig(500, 300000, 180000, 900000, true, false, 0.01); - Collection<TripReport> trips = ReportUtils.detectTripsAndStops(data, tripsConfig, false, TripReport.class); + Collection<TripReport> trips = ReportUtils.detectTripsAndStops( + new TestIdentityManager(), null, data, tripsConfig, false, TripReport.class); assertNotNull(trips); assertFalse(trips.isEmpty()); @@ -145,7 +149,8 @@ public class ReportUtilsTest extends BaseTest { assertEquals(10, itemTrip.getMaxSpeed(), 0.01); assertEquals(3000, itemTrip.getDistance(), 0.01); - trips = ReportUtils.detectTripsAndStops(data, tripsConfig, false, TripReport.class); + trips = ReportUtils.detectTripsAndStops( + new TestIdentityManager(), null, data, tripsConfig, false, TripReport.class); assertNotNull(trips); assertFalse(trips.isEmpty()); @@ -159,7 +164,8 @@ public class ReportUtilsTest extends BaseTest { assertEquals(10, itemTrip.getMaxSpeed(), 0.01); assertEquals(3000, itemTrip.getDistance(), 0.01); - Collection<StopReport> stops = ReportUtils.detectTripsAndStops(data, tripsConfig, false, StopReport.class); + Collection<StopReport> stops = ReportUtils.detectTripsAndStops( + new TestIdentityManager(), null, data, tripsConfig, false, StopReport.class); assertNotNull(stops); assertFalse(stops.isEmpty()); @@ -199,7 +205,8 @@ public class ReportUtilsTest extends BaseTest { TripsConfig tripsConfig = new TripsConfig(500, 300000, 180000, 900000, false, false, 0.01); - Collection<TripReport> trips = ReportUtils.detectTripsAndStops(data, tripsConfig, false, TripReport.class); + Collection<TripReport> trips = ReportUtils.detectTripsAndStops( + new TestIdentityManager(), null, data, tripsConfig, false, TripReport.class); assertNotNull(trips); assertFalse(trips.isEmpty()); @@ -213,7 +220,8 @@ public class ReportUtilsTest extends BaseTest { assertEquals(10, itemTrip.getMaxSpeed(), 0.01); assertEquals(7000, itemTrip.getDistance(), 0.01); - Collection<StopReport> stops = ReportUtils.detectTripsAndStops(data, tripsConfig, false, StopReport.class); + Collection<StopReport> stops = ReportUtils.detectTripsAndStops( + new TestIdentityManager(), null, data, tripsConfig, false, StopReport.class); assertNotNull(stops); assertFalse(stops.isEmpty()); @@ -247,7 +255,8 @@ public class ReportUtilsTest extends BaseTest { TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, 900000, false, false, 0.01); - Collection<StopReport> result = ReportUtils.detectTripsAndStops(data, tripsConfig, false, StopReport.class); + Collection<StopReport> result = ReportUtils.detectTripsAndStops( + new TestIdentityManager(), null, data, tripsConfig, false, StopReport.class); assertNotNull(result); assertFalse(result.isEmpty()); @@ -273,7 +282,8 @@ public class ReportUtilsTest extends BaseTest { TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, 900000, false, false, 0.01); - Collection<StopReport> result = ReportUtils.detectTripsAndStops(data, tripsConfig, false, StopReport.class); + Collection<StopReport> result = ReportUtils.detectTripsAndStops( + new TestIdentityManager(), null, data, tripsConfig, false, StopReport.class); assertNotNull(result); assertFalse(result.isEmpty()); @@ -299,7 +309,8 @@ public class ReportUtilsTest extends BaseTest { TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, 900000, false, false, 0.01); - Collection<StopReport> result = ReportUtils.detectTripsAndStops(data, tripsConfig, false, StopReport.class); + Collection<StopReport> result = ReportUtils.detectTripsAndStops( + new TestIdentityManager(), null, data, tripsConfig, false, StopReport.class); assertNotNull(result); assertFalse(result.isEmpty()); @@ -325,7 +336,8 @@ public class ReportUtilsTest extends BaseTest { TripsConfig tripsConfig = new TripsConfig(500, 300000, 200000, 900000, false, false, 0.01); - Collection<StopReport> result = ReportUtils.detectTripsAndStops(data, tripsConfig, false, StopReport.class); + Collection<StopReport> result = ReportUtils.detectTripsAndStops( + new TestIdentityManager(), null, data, tripsConfig, false, StopReport.class); assertNotNull(result); assertTrue(result.isEmpty()); @@ -347,7 +359,8 @@ public class ReportUtilsTest extends BaseTest { TripsConfig tripsConfig = new TripsConfig(500, 200000, 200000, 900000, false, false, 0.01); - Collection<TripReport> trips = ReportUtils.detectTripsAndStops(data, tripsConfig, false, TripReport.class); + Collection<TripReport> trips = ReportUtils.detectTripsAndStops( + new TestIdentityManager(), null, data, tripsConfig, false, TripReport.class); assertNotNull(trips); assertFalse(trips.isEmpty()); @@ -361,7 +374,8 @@ public class ReportUtilsTest extends BaseTest { assertEquals(7, itemTrip.getMaxSpeed(), 0.01); assertEquals(600, itemTrip.getDistance(), 0.01); - Collection<StopReport> stops = ReportUtils.detectTripsAndStops(data, tripsConfig, false, StopReport.class); + Collection<StopReport> stops = ReportUtils.detectTripsAndStops( + new TestIdentityManager(), null, data, tripsConfig, false, StopReport.class); assertNotNull(stops); assertFalse(stops.isEmpty()); |