From e3668177e4cb989b596ed7b2eef71eb348860736 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 9 Mar 2019 15:01:08 -0800 Subject: Refactor motion event handler --- src/org/traccar/BasePipelineFactory.java | 4 +--- src/org/traccar/Context.java | 9 --------- src/org/traccar/MainModule.java | 8 ++++++++ src/org/traccar/database/ConnectionManager.java | 4 +++- .../traccar/handler/events/MotionEventHandler.java | 21 +++++++++++++-------- src/org/traccar/reports/ReportUtils.java | 5 ++++- src/org/traccar/reports/Stops.java | 8 +++++++- src/org/traccar/reports/Trips.java | 8 +++++++- 8 files changed, 43 insertions(+), 24 deletions(-) (limited to 'src/org') 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 { 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 { } if (eventsEnabled) { - motionEventHandler = Context.getMotionEventHandler(); geofenceEventHandler = new GeofenceEventHandler(); alertEventHandler = new AlertEventHandler(); ignitionEventHandler = new IgnitionEventHandler(); @@ -159,7 +157,7 @@ public abstract class BasePipelineFactory extends ChannelInitializer { 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 result = new HashMap<>(); - Map event = Context.getMotionEventHandler().updateMotionState(deviceState); + Map 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 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 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 Collection detectTripsAndStops( + IdentityManager identityManager, DeviceManager deviceManager, Collection positionCollection, TripsConfig tripsConfig, boolean ignoreOdometer, Class reportClass) { @@ -331,7 +334,7 @@ public final class ReportUtils { ArrayList 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); } -- cgit v1.2.3