diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2019-03-09 17:04:08 -0800 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2019-03-09 17:04:08 -0800 |
commit | 5c084a0b2961dbfc3cd85a386fefe0b0da6c0c44 (patch) | |
tree | 637b9f89086bad578bbad04fd52e80bf379b8beb | |
parent | 2a584c67a1663897d98e7c64fe161c88b30ef314 (diff) | |
download | traccar-server-5c084a0b2961dbfc3cd85a386fefe0b0da6c0c44.tar.gz traccar-server-5c084a0b2961dbfc3cd85a386fefe0b0da6c0c44.tar.bz2 traccar-server-5c084a0b2961dbfc3cd85a386fefe0b0da6c0c44.zip |
Refactor ignition event handler
-rw-r--r-- | src/org/traccar/BasePipelineFactory.java | 4 | ||||
-rw-r--r-- | src/org/traccar/MainModule.java | 7 | ||||
-rw-r--r-- | src/org/traccar/handler/events/IgnitionEventHandler.java | 16 | ||||
-rw-r--r-- | test/org/traccar/handler/events/IgnitionEventHandlerTest.java | 9 |
4 files changed, 24 insertions, 12 deletions
diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index 5e194c856..550145860 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 IgnitionEventHandler ignitionEventHandler; private MaintenanceEventHandler maintenanceEventHandler; private DriverEventHandler driverEventHandler; @@ -71,7 +70,6 @@ public abstract class BasePipelineFactory extends ChannelInitializer<Channel> { } if (eventsEnabled) { - ignitionEventHandler = new IgnitionEventHandler(); maintenanceEventHandler = new MaintenanceEventHandler(); driverEventHandler = new DriverEventHandler(); } @@ -156,7 +154,7 @@ public abstract class BasePipelineFactory extends ChannelInitializer<Channel> { Main.getInjector().getInstance(MotionEventHandler.class), Main.getInjector().getInstance(GeofenceEventHandler.class), Main.getInjector().getInstance(AlertEventHandler.class), - ignitionEventHandler, + Main.getInjector().getInstance(IgnitionEventHandler.class), maintenanceEventHandler, driverEventHandler); } diff --git a/src/org/traccar/MainModule.java b/src/org/traccar/MainModule.java index 3f196972f..8ad89529f 100644 --- a/src/org/traccar/MainModule.java +++ b/src/org/traccar/MainModule.java @@ -61,6 +61,7 @@ import org.traccar.handler.events.AlertEventHandler; import org.traccar.handler.events.CommandResultEventHandler; import org.traccar.handler.events.FuelDropEventHandler; import org.traccar.handler.events.GeofenceEventHandler; +import org.traccar.handler.events.IgnitionEventHandler; import org.traccar.handler.events.MotionEventHandler; import org.traccar.handler.events.OverspeedEventHandler; import org.traccar.reports.model.TripsConfig; @@ -327,6 +328,12 @@ public class MainModule extends AbstractModule { return new AlertEventHandler(config, identityManager); } + @Singleton + @Provides + public static IgnitionEventHandler provideIgnitionEventHandler(IdentityManager identityManager) { + return new IgnitionEventHandler(identityManager); + } + @Override protected void configure() { binder().requireExplicitBindings(); diff --git a/src/org/traccar/handler/events/IgnitionEventHandler.java b/src/org/traccar/handler/events/IgnitionEventHandler.java index 7a83f45b7..ec133bafc 100644 --- a/src/org/traccar/handler/events/IgnitionEventHandler.java +++ b/src/org/traccar/handler/events/IgnitionEventHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2019 Anton Tananaev (anton@traccar.org) * Copyright 2016 Andrey Kunitsyn (andrey@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -20,7 +20,7 @@ import java.util.Collections; import java.util.Map; import io.netty.channel.ChannelHandler; -import org.traccar.Context; +import org.traccar.database.IdentityManager; import org.traccar.model.Device; import org.traccar.model.Event; import org.traccar.model.Position; @@ -28,10 +28,16 @@ import org.traccar.model.Position; @ChannelHandler.Sharable public class IgnitionEventHandler extends BaseEventHandler { + private final IdentityManager identityManager; + + public IgnitionEventHandler(IdentityManager identityManager) { + this.identityManager = identityManager; + } + @Override protected Map<Event, Position> analyzePosition(Position position) { - Device device = Context.getIdentityManager().getById(position.getDeviceId()); - if (device == null || !Context.getIdentityManager().isLatestPosition(position)) { + Device device = identityManager.getById(position.getDeviceId()); + if (device == null || !identityManager.isLatestPosition(position)) { return null; } @@ -40,7 +46,7 @@ public class IgnitionEventHandler extends BaseEventHandler { if (position.getAttributes().containsKey(Position.KEY_IGNITION)) { boolean ignition = position.getBoolean(Position.KEY_IGNITION); - Position lastPosition = Context.getIdentityManager().getLastPosition(position.getDeviceId()); + Position lastPosition = identityManager.getLastPosition(position.getDeviceId()); if (lastPosition != null && lastPosition.getAttributes().containsKey(Position.KEY_IGNITION)) { boolean oldIgnition = lastPosition.getBoolean(Position.KEY_IGNITION); diff --git a/test/org/traccar/handler/events/IgnitionEventHandlerTest.java b/test/org/traccar/handler/events/IgnitionEventHandlerTest.java index 709065c72..dade20fb8 100644 --- a/test/org/traccar/handler/events/IgnitionEventHandlerTest.java +++ b/test/org/traccar/handler/events/IgnitionEventHandlerTest.java @@ -1,26 +1,27 @@ package org.traccar.handler.events; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import java.util.Map; import org.junit.Test; import org.traccar.BaseTest; +import org.traccar.TestIdentityManager; import org.traccar.model.Event; import org.traccar.model.Position; public class IgnitionEventHandlerTest extends BaseTest { @Test - public void testIgnitionEventHandler() throws Exception { + public void testIgnitionEventHandler() { - IgnitionEventHandler ignitionEventHandler = new IgnitionEventHandler(); + IgnitionEventHandler ignitionEventHandler = new IgnitionEventHandler(new TestIdentityManager()); Position position = new Position(); position.set(Position.KEY_IGNITION, true); position.setValid(true); Map<Event, Position> events = ignitionEventHandler.analyzePosition(position); - assertEquals(events, null); + assertNull(events); } } |