From bf51c1994531da91f364b81d6d8c0778ed9c3570 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 7 Mar 2019 21:21:22 -0800 Subject: Refactor overspeed handler --- src/org/traccar/BasePipelineFactory.java | 4 +- src/org/traccar/Context.java | 11 ------ src/org/traccar/MainModule.java | 20 ++++++++++ src/org/traccar/config/Keys.java | 18 +++++++++ src/org/traccar/database/ConnectionManager.java | 6 ++- .../handler/events/OverspeedEventHandler.java | 43 +++++++++++++--------- 6 files changed, 69 insertions(+), 33 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index 1366850c0..0187e1d3f 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 OverspeedEventHandler overspeedEventHandler; private FuelDropEventHandler fuelDropEventHandler; private MotionEventHandler motionEventHandler; private GeofenceEventHandler geofenceEventHandler; @@ -76,7 +75,6 @@ public abstract class BasePipelineFactory extends ChannelInitializer { } if (eventsEnabled) { - overspeedEventHandler = Context.getOverspeedEventHandler(); fuelDropEventHandler = new FuelDropEventHandler(); motionEventHandler = Context.getMotionEventHandler(); geofenceEventHandler = new GeofenceEventHandler(); @@ -161,7 +159,7 @@ public abstract class BasePipelineFactory extends ChannelInitializer { addHandlers( pipeline, Main.getInjector().getInstance(CommandResultEventHandler.class), - overspeedEventHandler, + Main.getInjector().getInstance(OverspeedEventHandler.class), fuelDropEventHandler, motionEventHandler, geofenceEventHandler, diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 627299c01..845dae3c6 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -42,7 +42,6 @@ import org.traccar.database.NotificationManager; import org.traccar.database.PermissionsManager; import org.traccar.database.UsersManager; import org.traccar.handler.events.MotionEventHandler; -import org.traccar.handler.events.OverspeedEventHandler; import org.traccar.geocoder.Geocoder; import org.traccar.helper.Log; import org.traccar.helper.SanitizerModule; @@ -245,12 +244,6 @@ public final class Context { return motionEventHandler; } - private static OverspeedEventHandler overspeedEventHandler; - - public static OverspeedEventHandler getOverspeedEventHandler() { - return overspeedEventHandler; - } - private static TripsConfig tripsConfig; public static TripsConfig getTripsConfig() { @@ -387,10 +380,6 @@ public final class Context { velocityEngine.init(velocityProperties); motionEventHandler = new MotionEventHandler(tripsConfig); - overspeedEventHandler = new OverspeedEventHandler( - Context.getConfig().getLong("event.overspeed.minimalDuration") * 1000, - Context.getConfig().getBoolean("event.overspeed.notRepeat"), - Context.getConfig().getBoolean("event.overspeed.preferLowest")); } public static void init(IdentityManager testIdentityManager, MediaManager testMediaManager) { diff --git a/src/org/traccar/MainModule.java b/src/org/traccar/MainModule.java index cfd280c9f..fe280d7a3 100644 --- a/src/org/traccar/MainModule.java +++ b/src/org/traccar/MainModule.java @@ -23,6 +23,8 @@ import org.traccar.config.Config; import org.traccar.config.Keys; import org.traccar.database.AttributesManager; import org.traccar.database.DataManager; +import org.traccar.database.DeviceManager; +import org.traccar.database.GeofenceManager; import org.traccar.database.IdentityManager; import org.traccar.database.StatisticsManager; import org.traccar.geocoder.AddressFormat; @@ -55,6 +57,7 @@ import org.traccar.handler.HemisphereHandler; import org.traccar.handler.MotionHandler; import org.traccar.handler.RemoteAddressHandler; import org.traccar.handler.events.CommandResultEventHandler; +import org.traccar.handler.events.OverspeedEventHandler; import org.traccar.reports.model.TripsConfig; import javax.annotation.Nullable; @@ -92,6 +95,16 @@ public class MainModule extends AbstractModule { return Context.getTripsConfig(); } + @Provides + public static DeviceManager provideDeviceManager() { + return Context.getDeviceManager(); + } + + @Provides + public static GeofenceManager provideGeofenceManager() { + return Context.getGeofenceManager(); + } + @Provides public static AttributesManager provideAttributesManager() { return Context.getAttributesManager(); @@ -271,6 +284,13 @@ public class MainModule extends AbstractModule { return new CommandResultEventHandler(); } + @Singleton + @Provides + public static OverspeedEventHandler provideOverspeedEventHandler( + Config config, DeviceManager deviceManager, GeofenceManager geofenceManager) { + return new OverspeedEventHandler(config, deviceManager, geofenceManager); + } + @Override protected void configure() { binder().requireExplicitBindings(); diff --git a/src/org/traccar/config/Keys.java b/src/org/traccar/config/Keys.java index dc5560daf..c7c5d16c6 100644 --- a/src/org/traccar/config/Keys.java +++ b/src/org/traccar/config/Keys.java @@ -45,6 +45,24 @@ public final class Keys { public static final ConfigKey EVENT_ENABLE = new ConfigKey( "event.enable", Boolean.class); + /** + * If true, the event is generated once at the beginning of overspeeding period. + */ + public static final ConfigKey EVENT_OVERSPEED_NOT_REPEAT = new ConfigKey( + "event.overspeed.notRepeat", Boolean.class); + + /** + * Minimal over speed duration to trigger the event. Value in seconds. + */ + public static final ConfigKey EVENT_OVERSPEED_MINIMAL_DURATION = new ConfigKey( + "event.overspeed.minimalDuration", Long.class); + + /** + * Relevant only for geofence speed limits. Use lowest speed limits from all geofences. + */ + public static final ConfigKey EVENT_OVERSPEED_PREFER_LOWEST = new ConfigKey( + "event.overspeed.preferLowest", Boolean.class); + /** * List of external handler classes to use in Netty pipeline. */ diff --git a/src/org/traccar/database/ConnectionManager.java b/src/org/traccar/database/ConnectionManager.java index fc52fc375..157f2d0a8 100644 --- a/src/org/traccar/database/ConnectionManager.java +++ b/src/org/traccar/database/ConnectionManager.java @@ -22,6 +22,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.traccar.Context; import org.traccar.GlobalTimer; +import org.traccar.Main; import org.traccar.Protocol; import org.traccar.handler.events.OverspeedEventHandler; import org.traccar.model.Device; @@ -152,8 +153,9 @@ public class ConnectionManager { result.putAll(event); } - event = Context.getOverspeedEventHandler().updateOverspeedState(deviceState, Context.getDeviceManager(). - lookupAttributeDouble(deviceId, OverspeedEventHandler.ATTRIBUTE_SPEED_LIMIT, 0, false)); + event = Main.getInjector().getInstance(OverspeedEventHandler.class) + .updateOverspeedState(deviceState, Context.getDeviceManager(). + lookupAttributeDouble(deviceId, OverspeedEventHandler.ATTRIBUTE_SPEED_LIMIT, 0, false)); if (event != null) { result.putAll(event); } diff --git a/src/org/traccar/handler/events/OverspeedEventHandler.java b/src/org/traccar/handler/events/OverspeedEventHandler.java index 34344fb47..157bb64e0 100644 --- a/src/org/traccar/handler/events/OverspeedEventHandler.java +++ b/src/org/traccar/handler/events/OverspeedEventHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 - 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2019 Anton Tananaev (anton@traccar.org) * Copyright 2018 Andrey Kunitsyn (andrey@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -20,7 +20,10 @@ import java.util.Collections; import java.util.Map; import io.netty.channel.ChannelHandler; -import org.traccar.Context; +import org.traccar.config.Config; +import org.traccar.config.Keys; +import org.traccar.database.DeviceManager; +import org.traccar.database.GeofenceManager; import org.traccar.model.Device; import org.traccar.model.DeviceState; import org.traccar.model.Event; @@ -30,22 +33,28 @@ import org.traccar.model.Position; @ChannelHandler.Sharable public class OverspeedEventHandler extends BaseEventHandler { + public static final String ATTRIBUTE_SPEED = "speed"; public static final String ATTRIBUTE_SPEED_LIMIT = "speedLimit"; - private boolean notRepeat; - private boolean preferLowest; - private long minimalDuration; + private final DeviceManager deviceManager; + private final GeofenceManager geofenceManager; - public OverspeedEventHandler(long minimalDuration, boolean notRepeat, boolean preferLowest) { - this.notRepeat = notRepeat; - this.minimalDuration = minimalDuration; - this.preferLowest = preferLowest; + private final boolean notRepeat; + private final long minimalDuration; + private final boolean preferLowest; + + public OverspeedEventHandler(Config config, DeviceManager deviceManager, GeofenceManager geofenceManager) { + this.deviceManager = deviceManager; + this.geofenceManager = geofenceManager; + notRepeat = config.getBoolean(Keys.EVENT_OVERSPEED_NOT_REPEAT); + minimalDuration = config.getLong(Keys.EVENT_OVERSPEED_MINIMAL_DURATION) * 1000; + preferLowest = config.getBoolean(Keys.EVENT_OVERSPEED_PREFER_LOWEST); } private Map newEvent(DeviceState deviceState, double speedLimit) { Position position = deviceState.getOverspeedPosition(); Event event = new Event(Event.TYPE_DEVICE_OVERSPEED, position.getDeviceId(), position.getId()); - event.set("speed", deviceState.getOverspeedPosition().getSpeed()); + event.set(ATTRIBUTE_SPEED, deviceState.getOverspeedPosition().getSpeed()); event.set(ATTRIBUTE_SPEED_LIMIT, speedLimit); event.setGeofenceId(deviceState.getOverspeedGeofenceId()); deviceState.setOverspeedState(notRepeat); @@ -103,22 +112,22 @@ public class OverspeedEventHandler extends BaseEventHandler { protected Map analyzePosition(Position position) { long deviceId = position.getDeviceId(); - Device device = Context.getIdentityManager().getById(deviceId); + Device device = deviceManager.getById(deviceId); if (device == null) { return null; } - if (!Context.getIdentityManager().isLatestPosition(position) || !position.getValid()) { + if (!deviceManager.isLatestPosition(position) || !position.getValid()) { return null; } - double speedLimit = Context.getDeviceManager().lookupAttributeDouble(deviceId, ATTRIBUTE_SPEED_LIMIT, 0, false); + double speedLimit = deviceManager.lookupAttributeDouble(deviceId, ATTRIBUTE_SPEED_LIMIT, 0, false); double geofenceSpeedLimit = 0; long overspeedGeofenceId = 0; - if (Context.getGeofenceManager() != null && device.getGeofenceIds() != null) { + if (geofenceManager != null && device.getGeofenceIds() != null) { for (long geofenceId : device.getGeofenceIds()) { - Geofence geofence = Context.getGeofenceManager().getById(geofenceId); + Geofence geofence = geofenceManager.getById(geofenceId); if (geofence != null) { double currentSpeedLimit = geofence.getDouble(ATTRIBUTE_SPEED_LIMIT); if (currentSpeedLimit > 0 && geofenceSpeedLimit == 0 @@ -139,7 +148,7 @@ public class OverspeedEventHandler extends BaseEventHandler { } Map result = null; - DeviceState deviceState = Context.getDeviceManager().getDeviceState(deviceId); + DeviceState deviceState = deviceManager.getDeviceState(deviceId); if (deviceState.getOverspeedState() == null) { deviceState.setOverspeedState(position.getSpeed() > speedLimit); @@ -148,7 +157,7 @@ public class OverspeedEventHandler extends BaseEventHandler { result = updateOverspeedState(deviceState, position, speedLimit, overspeedGeofenceId); } - Context.getDeviceManager().setDeviceState(deviceId, deviceState); + deviceManager.setDeviceState(deviceId, deviceState); return result; } -- cgit v1.2.3