From 34a5823cb19d4b957da1c0bdda426eb8e76950d2 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 25 Feb 2019 23:50:42 -0800 Subject: Refactor copy attributes handler --- src/org/traccar/BasePipelineFactory.java | 7 +------ src/org/traccar/MainModule.java | 10 ++++++++++ src/org/traccar/config/Keys.java | 7 +++++++ src/org/traccar/database/DeviceManager.java | 4 ++++ src/org/traccar/handler/CopyAttributesHandler.java | 17 ++++++++--------- 5 files changed, 30 insertions(+), 15 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index 1b3be36fc..088689fe7 100644 --- a/src/org/traccar/BasePipelineFactory.java +++ b/src/org/traccar/BasePipelineFactory.java @@ -57,7 +57,6 @@ public abstract class BasePipelineFactory extends ChannelInitializer { private final TrackerServer server; private int timeout; - private CopyAttributesHandler copyAttributesHandler; private ComputedAttributesHandler computedAttributesHandler; private CommandResultEventHandler commandResultEventHandler; @@ -78,10 +77,6 @@ public abstract class BasePipelineFactory extends ChannelInitializer { timeout = Context.getConfig().getInteger(Keys.SERVER_TIMEOUT); } - if (Context.getConfig().getBoolean("processing.copyAttributes.enable")) { - copyAttributesHandler = new CopyAttributesHandler(); - } - if (Context.getConfig().getBoolean("processing.computedAttributes.enable")) { computedAttributesHandler = new ComputedAttributesHandler(); } @@ -161,7 +156,7 @@ public abstract class BasePipelineFactory extends ChannelInitializer { Main.getInjector().getInstance(GeocoderHandler.class), Main.getInjector().getInstance(MotionHandler.class), Main.getInjector().getInstance(EngineHoursHandler.class), - copyAttributesHandler, + Main.getInjector().getInstance(CopyAttributesHandler.class), computedAttributesHandler); if (Context.getDataManager() != null) { diff --git a/src/org/traccar/MainModule.java b/src/org/traccar/MainModule.java index cd5f2568e..34ece8658 100644 --- a/src/org/traccar/MainModule.java +++ b/src/org/traccar/MainModule.java @@ -43,6 +43,7 @@ import org.traccar.geolocation.GoogleGeolocationProvider; import org.traccar.geolocation.MozillaGeolocationProvider; import org.traccar.geolocation.OpenCellIdGeolocationProvider; import org.traccar.geolocation.UnwiredGeolocationProvider; +import org.traccar.handler.CopyAttributesHandler; import org.traccar.handler.DistanceHandler; import org.traccar.handler.EngineHoursHandler; import org.traccar.handler.FilterHandler; @@ -237,6 +238,15 @@ public class MainModule extends AbstractModule { return null; } + @Singleton + @Provides + public static CopyAttributesHandler provideCopyAttributesHandler(Config config, IdentityManager identityManager) { + if (config.getBoolean(Keys.PROCESSING_COPY_ATTRIBUTES_ENABLE)) { + return new CopyAttributesHandler(identityManager); + } + return null; + } + @Override protected void configure() { binder().requireExplicitBindings(); diff --git a/src/org/traccar/config/Keys.java b/src/org/traccar/config/Keys.java index 6c1225a9b..b9bc0c9dc 100644 --- a/src/org/traccar/config/Keys.java +++ b/src/org/traccar/config/Keys.java @@ -185,6 +185,13 @@ public final class Keys { public static final ConfigKey PROCESSING_ENGINE_HOURS_ENABLE = new ConfigKey( "processing.engineHours.enable", Boolean.class); + /** + * Enable copying of missing attributes from last position to the current one. Might be useful if device doesn't + * send some values in every message. + */ + public static final ConfigKey PROCESSING_COPY_ATTRIBUTES_ENABLE = new ConfigKey( + "processing.copyAttributes.enable", Boolean.class); + /** * Boolean flag to enable or disable reverse geocoder. */ diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index 9cc84b2a3..de4607d1f 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -310,6 +310,7 @@ public class DeviceManager extends BaseObjectManager implements Identity return result; } + @Override public boolean lookupAttributeBoolean( long deviceId, String attributeName, boolean defaultValue, boolean lookupConfig) { Object result = lookupAttribute(deviceId, attributeName, lookupConfig); @@ -319,12 +320,14 @@ public class DeviceManager extends BaseObjectManager implements Identity return defaultValue; } + @Override public String lookupAttributeString( long deviceId, String attributeName, String defaultValue, boolean lookupConfig) { Object result = lookupAttribute(deviceId, attributeName, lookupConfig); return result != null ? (String) result : defaultValue; } + @Override public int lookupAttributeInteger(long deviceId, String attributeName, int defaultValue, boolean lookupConfig) { Object result = lookupAttribute(deviceId, attributeName, lookupConfig); if (result != null) { @@ -333,6 +336,7 @@ public class DeviceManager extends BaseObjectManager implements Identity return defaultValue; } + @Override public long lookupAttributeLong( long deviceId, String attributeName, long defaultValue, boolean lookupConfig) { Object result = lookupAttribute(deviceId, attributeName, lookupConfig); diff --git a/src/org/traccar/handler/CopyAttributesHandler.java b/src/org/traccar/handler/CopyAttributesHandler.java index 73642d81b..6a0966d33 100644 --- a/src/org/traccar/handler/CopyAttributesHandler.java +++ b/src/org/traccar/handler/CopyAttributesHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2019 Anton Tananaev (anton@traccar.org) * Copyright 2016 - 2017 Andrey Kunitsyn (andrey@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,29 +18,28 @@ package org.traccar.handler; import io.netty.channel.ChannelHandler; import org.traccar.BaseDataHandler; -import org.traccar.Context; +import org.traccar.database.IdentityManager; import org.traccar.model.Position; @ChannelHandler.Sharable public class CopyAttributesHandler extends BaseDataHandler { - private Position getLastPosition(long deviceId) { - if (Context.getIdentityManager() != null) { - return Context.getIdentityManager().getLastPosition(deviceId); - } - return null; + private IdentityManager identityManager; + + public CopyAttributesHandler(IdentityManager identityManager) { + this.identityManager = identityManager; } @Override protected Position handlePosition(Position position) { - String attributesString = Context.getDeviceManager().lookupAttributeString( + String attributesString = identityManager.lookupAttributeString( position.getDeviceId(), "processing.copyAttributes", "", true); - Position last = getLastPosition(position.getDeviceId()); if (attributesString.isEmpty()) { attributesString = Position.KEY_DRIVER_UNIQUE_ID; } else { attributesString += "," + Position.KEY_DRIVER_UNIQUE_ID; } + Position last = identityManager.getLastPosition(position.getDeviceId()); if (last != null) { for (String attribute : attributesString.split("[ ,]")) { if (last.getAttributes().containsKey(attribute) && !position.getAttributes().containsKey(attribute)) { -- cgit v1.2.3