From 2af6e4cc6255f59acaa17e6763063b775d7ba1ea Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 9 Mar 2019 12:19:46 -0800 Subject: Refactor fuel drop manager --- src/org/traccar/BasePipelineFactory.java | 4 +--- src/org/traccar/MainModule.java | 7 +++++++ src/org/traccar/database/IdentityManager.java | 2 ++ .../traccar/handler/events/FuelDropEventHandler.java | 18 ++++++++++++------ test/org/traccar/TestIdentityManager.java | 6 ++++++ 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index 0187e1d3f..5cadfb2a6 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 FuelDropEventHandler fuelDropEventHandler; private MotionEventHandler motionEventHandler; private GeofenceEventHandler geofenceEventHandler; private AlertEventHandler alertEventHandler; @@ -75,7 +74,6 @@ public abstract class BasePipelineFactory extends ChannelInitializer { } if (eventsEnabled) { - fuelDropEventHandler = new FuelDropEventHandler(); motionEventHandler = Context.getMotionEventHandler(); geofenceEventHandler = new GeofenceEventHandler(); alertEventHandler = new AlertEventHandler(); @@ -160,7 +158,7 @@ public abstract class BasePipelineFactory extends ChannelInitializer { pipeline, Main.getInjector().getInstance(CommandResultEventHandler.class), Main.getInjector().getInstance(OverspeedEventHandler.class), - fuelDropEventHandler, + Main.getInjector().getInstance(FuelDropEventHandler.class), motionEventHandler, geofenceEventHandler, alertEventHandler, diff --git a/src/org/traccar/MainModule.java b/src/org/traccar/MainModule.java index fe280d7a3..46af5670c 100644 --- a/src/org/traccar/MainModule.java +++ b/src/org/traccar/MainModule.java @@ -57,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.FuelDropEventHandler; import org.traccar.handler.events.OverspeedEventHandler; import org.traccar.reports.model.TripsConfig; @@ -291,6 +292,12 @@ public class MainModule extends AbstractModule { return new OverspeedEventHandler(config, deviceManager, geofenceManager); } + @Singleton + @Provides + public static FuelDropEventHandler provideFuelDropEventHandler(IdentityManager identityManager) { + return new FuelDropEventHandler(identityManager); + } + @Override protected void configure() { binder().requireExplicitBindings(); diff --git a/src/org/traccar/database/IdentityManager.java b/src/org/traccar/database/IdentityManager.java index 59d2f9362..6228a0f75 100644 --- a/src/org/traccar/database/IdentityManager.java +++ b/src/org/traccar/database/IdentityManager.java @@ -38,4 +38,6 @@ public interface IdentityManager { long lookupAttributeLong(long deviceId, String attributeName, long defaultValue, boolean lookupConfig); + double lookupAttributeDouble(long deviceId, String attributeName, double defaultValue, boolean lookupConfig); + } diff --git a/src/org/traccar/handler/events/FuelDropEventHandler.java b/src/org/traccar/handler/events/FuelDropEventHandler.java index f13b8c7bb..59de61bba 100644 --- a/src/org/traccar/handler/events/FuelDropEventHandler.java +++ b/src/org/traccar/handler/events/FuelDropEventHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 - 2019 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,7 @@ package org.traccar.handler.events; 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; @@ -29,22 +29,28 @@ public class FuelDropEventHandler extends BaseEventHandler { public static final String ATTRIBUTE_FUEL_DROP_THRESHOLD = "fuelDropThreshold"; + private final IdentityManager identityManager; + + public FuelDropEventHandler(IdentityManager identityManager) { + this.identityManager = identityManager; + } + @Override protected Map analyzePosition(Position position) { - Device device = Context.getIdentityManager().getById(position.getDeviceId()); + Device device = identityManager.getById(position.getDeviceId()); if (device == null) { return null; } - if (!Context.getIdentityManager().isLatestPosition(position)) { + if (!identityManager.isLatestPosition(position)) { return null; } - double fuelDropThreshold = Context.getDeviceManager() + double fuelDropThreshold = identityManager .lookupAttributeDouble(device.getId(), ATTRIBUTE_FUEL_DROP_THRESHOLD, 0, false); if (fuelDropThreshold > 0) { - Position lastPosition = Context.getIdentityManager().getLastPosition(position.getDeviceId()); + Position lastPosition = identityManager.getLastPosition(position.getDeviceId()); if (position.getAttributes().containsKey(Position.KEY_FUEL_LEVEL) && lastPosition != null && lastPosition.getAttributes().containsKey(Position.KEY_FUEL_LEVEL)) { diff --git a/test/org/traccar/TestIdentityManager.java b/test/org/traccar/TestIdentityManager.java index 1b3f95ad8..0f7405dbd 100644 --- a/test/org/traccar/TestIdentityManager.java +++ b/test/org/traccar/TestIdentityManager.java @@ -63,4 +63,10 @@ public final class TestIdentityManager implements IdentityManager { return defaultValue; } + @Override + public double lookupAttributeDouble( + long deviceId, String attributeName, double defaultValue, boolean lookupConfig) { + return defaultValue; + } + } -- cgit v1.2.3