From 041c2b897c28dc316e8613c9ea38dee48dd64482 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sun, 24 Feb 2019 18:06:39 -0800 Subject: Refactor motion handler --- src/org/traccar/BasePipelineFactory.java | 6 ++-- src/org/traccar/MainModule.java | 13 ++++++++ src/org/traccar/MotionHandler.java | 39 ------------------------ src/org/traccar/handler/MotionHandler.java | 40 +++++++++++++++++++++++++ test/org/traccar/MotionHandlerTest.java | 21 ------------- test/org/traccar/handler/MotionHandlerTest.java | 21 +++++++++++++ 6 files changed, 76 insertions(+), 64 deletions(-) delete mode 100644 src/org/traccar/MotionHandler.java create mode 100644 src/org/traccar/handler/MotionHandler.java delete mode 100644 test/org/traccar/MotionHandlerTest.java create mode 100644 test/org/traccar/handler/MotionHandlerTest.java diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index 8e6a62391..cb60b20a7 100644 --- a/src/org/traccar/BasePipelineFactory.java +++ b/src/org/traccar/BasePipelineFactory.java @@ -41,6 +41,7 @@ import org.traccar.handler.FilterHandler; import org.traccar.handler.GeocoderHandler; import org.traccar.handler.GeolocationHandler; import org.traccar.handler.HemisphereHandler; +import org.traccar.handler.MotionHandler; import org.traccar.handler.NetworkMessageHandler; import org.traccar.handler.OpenChannelHandler; import org.traccar.handler.RemoteAddressHandler; @@ -56,7 +57,6 @@ public abstract class BasePipelineFactory extends ChannelInitializer { private int timeout; private EngineHoursHandler engineHoursHandler; - private MotionHandler motionHandler; private CopyAttributesHandler copyAttributesHandler; private ComputedAttributesHandler computedAttributesHandler; @@ -78,8 +78,6 @@ public abstract class BasePipelineFactory extends ChannelInitializer { timeout = Context.getConfig().getInteger(Keys.SERVER_TIMEOUT); } - motionHandler = new MotionHandler(Context.getTripsConfig().getSpeedThreshold()); - if (Context.getConfig().getBoolean("processing.engineHours.enable")) { engineHoursHandler = new EngineHoursHandler(); } @@ -165,7 +163,7 @@ public abstract class BasePipelineFactory extends ChannelInitializer { pipeline, Main.getInjector().getInstance(FilterHandler.class), Main.getInjector().getInstance(GeocoderHandler.class), - motionHandler, + Main.getInjector().getInstance(MotionHandler.class), engineHoursHandler, copyAttributesHandler, computedAttributesHandler); diff --git a/src/org/traccar/MainModule.java b/src/org/traccar/MainModule.java index c6ca65088..0c3c5be51 100644 --- a/src/org/traccar/MainModule.java +++ b/src/org/traccar/MainModule.java @@ -47,7 +47,9 @@ import org.traccar.handler.DistanceHandler; import org.traccar.handler.FilterHandler; import org.traccar.handler.GeolocationHandler; import org.traccar.handler.HemisphereHandler; +import org.traccar.handler.MotionHandler; import org.traccar.handler.RemoteAddressHandler; +import org.traccar.reports.model.TripsConfig; import javax.annotation.Nullable; import javax.ws.rs.client.Client; @@ -79,6 +81,11 @@ public class MainModule extends AbstractModule { return Context.getClient(); } + @Provides + public static TripsConfig provideTripsConfig() { + return Context.getTripsConfig(); + } + @Singleton @Provides public static StatisticsManager provideStatisticsManager(Config config, DataManager dataManager, Client client) { @@ -202,6 +209,12 @@ public class MainModule extends AbstractModule { return null; } + @Singleton + @Provides + public static MotionHandler provideMotionHandler(TripsConfig tripsConfig) { + return new MotionHandler(tripsConfig.getSpeedThreshold()); + } + @Override protected void configure() { binder().requireExplicitBindings(); diff --git a/src/org/traccar/MotionHandler.java b/src/org/traccar/MotionHandler.java deleted file mode 100644 index ec9a5ffd7..000000000 --- a/src/org/traccar/MotionHandler.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar; - -import io.netty.channel.ChannelHandler; -import org.traccar.model.Position; - -@ChannelHandler.Sharable -public class MotionHandler extends BaseDataHandler { - - private double speedThreshold; - - public MotionHandler(double speedThreshold) { - this.speedThreshold = speedThreshold; - } - - @Override - protected Position handlePosition(Position position) { - if (!position.getAttributes().containsKey(Position.KEY_MOTION)) { - position.set(Position.KEY_MOTION, position.getSpeed() > speedThreshold); - } - return position; - } - -} diff --git a/src/org/traccar/handler/MotionHandler.java b/src/org/traccar/handler/MotionHandler.java new file mode 100644 index 000000000..e8051dd75 --- /dev/null +++ b/src/org/traccar/handler/MotionHandler.java @@ -0,0 +1,40 @@ +/* + * Copyright 2017 - 2019 Anton Tananaev (anton@traccar.org) + * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.handler; + +import io.netty.channel.ChannelHandler; +import org.traccar.BaseDataHandler; +import org.traccar.model.Position; + +@ChannelHandler.Sharable +public class MotionHandler extends BaseDataHandler { + + private double speedThreshold; + + public MotionHandler(double speedThreshold) { + this.speedThreshold = speedThreshold; + } + + @Override + protected Position handlePosition(Position position) { + if (!position.getAttributes().containsKey(Position.KEY_MOTION)) { + position.set(Position.KEY_MOTION, position.getSpeed() > speedThreshold); + } + return position; + } + +} diff --git a/test/org/traccar/MotionHandlerTest.java b/test/org/traccar/MotionHandlerTest.java deleted file mode 100644 index fc6dce033..000000000 --- a/test/org/traccar/MotionHandlerTest.java +++ /dev/null @@ -1,21 +0,0 @@ -package org.traccar; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; -import org.traccar.model.Position; - -public class MotionHandlerTest { - - @Test - public void testCalculateMotion() throws Exception { - - MotionHandler motionHandler = new MotionHandler(0.01); - - Position position = motionHandler.handlePosition(new Position()); - - assertEquals(false, position.getAttributes().get(Position.KEY_MOTION)); - - } - -} diff --git a/test/org/traccar/handler/MotionHandlerTest.java b/test/org/traccar/handler/MotionHandlerTest.java new file mode 100644 index 000000000..9e0859664 --- /dev/null +++ b/test/org/traccar/handler/MotionHandlerTest.java @@ -0,0 +1,21 @@ +package org.traccar.handler; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.traccar.model.Position; + +public class MotionHandlerTest { + + @Test + public void testCalculateMotion() { + + MotionHandler motionHandler = new MotionHandler(0.01); + + Position position = motionHandler.handlePosition(new Position()); + + assertEquals(false, position.getAttributes().get(Position.KEY_MOTION)); + + } + +} -- cgit v1.2.3