From a8566c997d27bf22ee0debad7b9b8c2db9325a43 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sun, 24 Feb 2019 12:12:19 -0800 Subject: Refactor hemisphere handler --- src/org/traccar/BasePipelineFactory.java | 9 +--- src/org/traccar/HemisphereHandler.java | 57 ------------------------ src/org/traccar/MainModule.java | 10 +++++ src/org/traccar/config/Keys.java | 14 ++++++ src/org/traccar/handler/HemisphereHandler.java | 60 ++++++++++++++++++++++++++ 5 files changed, 86 insertions(+), 64 deletions(-) delete mode 100644 src/org/traccar/HemisphereHandler.java create mode 100644 src/org/traccar/handler/HemisphereHandler.java (limited to 'src/org/traccar') diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index b993e0741..082d666ac 100644 --- a/src/org/traccar/BasePipelineFactory.java +++ b/src/org/traccar/BasePipelineFactory.java @@ -39,6 +39,7 @@ import org.traccar.handler.CopyAttributesHandler; import org.traccar.handler.DistanceHandler; import org.traccar.handler.FilterHandler; import org.traccar.handler.GeolocationHandler; +import org.traccar.handler.HemisphereHandler; 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 EngineHoursHandler engineHoursHandler; private MotionHandler motionHandler; private GeocoderHandler geocoderHandler; - private HemisphereHandler hemisphereHandler; private CopyAttributesHandler copyAttributesHandler; private ComputedAttributesHandler computedAttributesHandler; @@ -90,11 +90,6 @@ public abstract class BasePipelineFactory extends ChannelInitializer { engineHoursHandler = new EngineHoursHandler(); } - if (Context.getConfig().hasKey("location.latitudeHemisphere") - || Context.getConfig().hasKey("location.longitudeHemisphere")) { - hemisphereHandler = new HemisphereHandler(); - } - if (Context.getConfig().getBoolean("processing.copyAttributes.enable")) { copyAttributesHandler = new CopyAttributesHandler(); } @@ -166,7 +161,7 @@ public abstract class BasePipelineFactory extends ChannelInitializer { addHandlers( pipeline, Main.getInjector().getInstance(GeolocationHandler.class), - hemisphereHandler, + Main.getInjector().getInstance(HemisphereHandler.class), Main.getInjector().getInstance(DistanceHandler.class), Main.getInjector().getInstance(RemoteAddressHandler.class)); diff --git a/src/org/traccar/HemisphereHandler.java b/src/org/traccar/HemisphereHandler.java deleted file mode 100644 index d1bcd340b..000000000 --- a/src/org/traccar/HemisphereHandler.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2016 - 2018 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. - * 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 HemisphereHandler extends BaseDataHandler { - - private int latitudeFactor; - private int longitudeFactor; - - public HemisphereHandler() { - String latitudeHemisphere = Context.getConfig().getString("location.latitudeHemisphere"); - if (latitudeHemisphere != null) { - if (latitudeHemisphere.equalsIgnoreCase("N")) { - latitudeFactor = 1; - } else if (latitudeHemisphere.equalsIgnoreCase("S")) { - latitudeFactor = -1; - } - } - String longitudeHemisphere = Context.getConfig().getString("location.longitudeHemisphere"); - if (longitudeHemisphere != null) { - if (longitudeHemisphere.equalsIgnoreCase("E")) { - longitudeFactor = 1; - } else if (longitudeHemisphere.equalsIgnoreCase("W")) { - longitudeFactor = -1; - } - } - } - - @Override - protected Position handlePosition(Position position) { - if (latitudeFactor != 0) { - position.setLatitude(Math.abs(position.getLatitude()) * latitudeFactor); - } - if (longitudeFactor != 0) { - position.setLongitude(Math.abs(position.getLongitude()) * longitudeFactor); - } - return position; - } - -} diff --git a/src/org/traccar/MainModule.java b/src/org/traccar/MainModule.java index 3b9bbe4b6..0e5c7d858 100644 --- a/src/org/traccar/MainModule.java +++ b/src/org/traccar/MainModule.java @@ -32,6 +32,7 @@ import org.traccar.geolocation.UnwiredGeolocationProvider; import org.traccar.handler.DistanceHandler; import org.traccar.handler.FilterHandler; import org.traccar.handler.GeolocationHandler; +import org.traccar.handler.HemisphereHandler; import org.traccar.handler.RemoteAddressHandler; import javax.annotation.Nullable; @@ -106,6 +107,15 @@ public class MainModule extends AbstractModule { return null; } + @Singleton + @Provides + public static HemisphereHandler provideHemisphereHandler(Config config) { + if (config.hasKey(Keys.LOCATION_LATITUDE_HEMISPHERE) || config.hasKey(Keys.LOCATION_LONGITUDE_HEMISPHERE)) { + return new HemisphereHandler(config); + } + return null; + } + @Singleton @Provides public static RemoteAddressHandler provideRemoteAddressHandler(Config config) { diff --git a/src/org/traccar/config/Keys.java b/src/org/traccar/config/Keys.java index 9347341b0..fddc70bba 100644 --- a/src/org/traccar/config/Keys.java +++ b/src/org/traccar/config/Keys.java @@ -213,6 +213,20 @@ public final class Keys { public static final ConfigKey GEOLOCATION_PROCESS_INVALID_POSITIONS = new ConfigKey( "geolocation.processInvalidPositions", Boolean.class); + /** + * Override latitude sign / hemisphere. Useful in cases where value is incorrect because of device bug. Value can be + * N for North or S for South. + */ + public static final ConfigKey LOCATION_LATITUDE_HEMISPHERE = new ConfigKey( + "location.latitudeHemisphere", Boolean.class); + + /** + * Override longitude sign / hemisphere. Useful in cases where value is incorrect because of device bug. Value can + * be E for East or W for West. + */ + public static final ConfigKey LOCATION_LONGITUDE_HEMISPHERE = new ConfigKey( + "location.longitudeHemisphere", Boolean.class); + private Keys() { } diff --git a/src/org/traccar/handler/HemisphereHandler.java b/src/org/traccar/handler/HemisphereHandler.java new file mode 100644 index 000000000..aff3d8a64 --- /dev/null +++ b/src/org/traccar/handler/HemisphereHandler.java @@ -0,0 +1,60 @@ +/* + * Copyright 2016 - 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. + * 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.config.Config; +import org.traccar.config.Keys; +import org.traccar.model.Position; + +@ChannelHandler.Sharable +public class HemisphereHandler extends BaseDataHandler { + + private int latitudeFactor; + private int longitudeFactor; + + public HemisphereHandler(Config config) { + String latitudeHemisphere = config.getString(Keys.LOCATION_LATITUDE_HEMISPHERE); + if (latitudeHemisphere != null) { + if (latitudeHemisphere.equalsIgnoreCase("N")) { + latitudeFactor = 1; + } else if (latitudeHemisphere.equalsIgnoreCase("S")) { + latitudeFactor = -1; + } + } + String longitudeHemisphere = config.getString(Keys.LOCATION_LATITUDE_HEMISPHERE); + if (longitudeHemisphere != null) { + if (longitudeHemisphere.equalsIgnoreCase("E")) { + longitudeFactor = 1; + } else if (longitudeHemisphere.equalsIgnoreCase("W")) { + longitudeFactor = -1; + } + } + } + + @Override + protected Position handlePosition(Position position) { + if (latitudeFactor != 0) { + position.setLatitude(Math.abs(position.getLatitude()) * latitudeFactor); + } + if (longitudeFactor != 0) { + position.setLongitude(Math.abs(position.getLongitude()) * longitudeFactor); + } + return position; + } + +} -- cgit v1.2.3