From f458cbe473bdc49157415a8bfc6441afd86b670e Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 18 Apr 2019 22:30:15 -0700 Subject: Option to force time override --- src/main/java/org/traccar/BasePipelineFactory.java | 2 + src/main/java/org/traccar/MainEventHandler.java | 4 +- src/main/java/org/traccar/MainModule.java | 10 ++++ src/main/java/org/traccar/config/Keys.java | 13 +++++ src/main/java/org/traccar/handler/TimeHandler.java | 63 ++++++++++++++++++++++ src/main/java/org/traccar/model/Position.java | 2 +- 6 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/traccar/handler/TimeHandler.java (limited to 'src/main/java') diff --git a/src/main/java/org/traccar/BasePipelineFactory.java b/src/main/java/org/traccar/BasePipelineFactory.java index ac008cdd1..a92dba7fa 100644 --- a/src/main/java/org/traccar/BasePipelineFactory.java +++ b/src/main/java/org/traccar/BasePipelineFactory.java @@ -26,6 +26,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.traccar.config.Keys; import org.traccar.handler.DefaultDataHandler; +import org.traccar.handler.TimeHandler; import org.traccar.handler.events.AlertEventHandler; import org.traccar.handler.events.CommandResultEventHandler; import org.traccar.handler.events.DriverEventHandler; @@ -120,6 +121,7 @@ public abstract class BasePipelineFactory extends ChannelInitializer { addHandlers( pipeline, + TimeHandler.class, GeolocationHandler.class, HemisphereHandler.class, DistanceHandler.class, diff --git a/src/main/java/org/traccar/MainEventHandler.java b/src/main/java/org/traccar/MainEventHandler.java index a8b53ff60..2309b1e70 100644 --- a/src/main/java/org/traccar/MainEventHandler.java +++ b/src/main/java/org/traccar/MainEventHandler.java @@ -45,10 +45,10 @@ public class MainEventHandler extends ChannelInboundHandlerAdapter { public MainEventHandler() { String connectionlessProtocolList = Context.getConfig().getString("status.ignoreOffline"); if (connectionlessProtocolList != null) { - connectionlessProtocols.addAll(Arrays.asList(connectionlessProtocolList.split(","))); + connectionlessProtocols.addAll(Arrays.asList(connectionlessProtocolList.split("[, ]"))); } logAttributes.addAll(Arrays.asList( - Context.getConfig().getString("logger.attributes", DEFAULT_LOGGER_ATTRIBUTES).split(","))); + Context.getConfig().getString("logger.attributes", DEFAULT_LOGGER_ATTRIBUTES).split("[, ]"))); } @Override diff --git a/src/main/java/org/traccar/MainModule.java b/src/main/java/org/traccar/MainModule.java index 6fe8bad1c..3acd19b6a 100644 --- a/src/main/java/org/traccar/MainModule.java +++ b/src/main/java/org/traccar/MainModule.java @@ -59,6 +59,7 @@ import org.traccar.handler.GeolocationHandler; import org.traccar.handler.HemisphereHandler; import org.traccar.handler.MotionHandler; import org.traccar.handler.RemoteAddressHandler; +import org.traccar.handler.TimeHandler; import org.traccar.handler.events.AlertEventHandler; import org.traccar.handler.events.CommandResultEventHandler; import org.traccar.handler.events.DriverEventHandler; @@ -298,6 +299,15 @@ public class MainModule extends AbstractModule { return null; } + @Singleton + @Provides + public static TimeHandler provideTimeHandler(Config config) { + if (config.hasKey(Keys.TIME_OVERRIDE)) { + return new TimeHandler(config); + } + return null; + } + @Singleton @Provides public static DefaultDataHandler provideDefaultDataHandler(@Nullable DataManager dataManager) { diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java index 48cf3e558..a000329e2 100644 --- a/src/main/java/org/traccar/config/Keys.java +++ b/src/main/java/org/traccar/config/Keys.java @@ -183,6 +183,19 @@ public final class Keys { public static final ConfigKey FILTER_SKIP_ATTRIBUTES_ENABLE = new ConfigKey( "filter.skipAttributes.enable", Boolean.class); + /** + * Override device time. Possible values are 'deviceTime' and 'serverTime' + */ + public static final ConfigKey TIME_OVERRIDE = new ConfigKey( + "time.override", String.class); + + /** + * List of protocols for overriding time. If not specified override is applied globally. List consist of protocol + * names that can be separated by comma or single space character. + */ + public static final ConfigKey TIME_PROTOCOLS = new ConfigKey( + "time.protocols", String.class); + /** * Replaces coordinates with last known if change is less than a 'coordinates.error' meters. Helps to avoid * coordinates jumps during parking period. diff --git a/src/main/java/org/traccar/handler/TimeHandler.java b/src/main/java/org/traccar/handler/TimeHandler.java new file mode 100644 index 000000000..d8039518c --- /dev/null +++ b/src/main/java/org/traccar/handler/TimeHandler.java @@ -0,0 +1,63 @@ +/* + * Copyright 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 io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; +import org.traccar.BaseProtocolDecoder; +import org.traccar.Context; +import org.traccar.config.Config; +import org.traccar.config.Keys; +import org.traccar.model.Position; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +@ChannelHandler.Sharable +public class TimeHandler extends ChannelInboundHandlerAdapter { + + private final boolean useServerTime; + private final Set protocols; + + public TimeHandler(Config config) { + useServerTime = config.getString(Keys.TIME_OVERRIDE).equalsIgnoreCase("serverTime"); + String protocolList = Context.getConfig().getString(Keys.TIME_PROTOCOLS); + if (protocolList != null) { + protocols = new HashSet<>(Arrays.asList(protocolList.split("[, ]"))); + } else { + protocols = null; + } + } + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) { + + if (msg instanceof Position && (protocols == null + || protocols.contains(ctx.pipeline().get(BaseProtocolDecoder.class).getProtocolName()))) { + + Position position = (Position) msg; + if (useServerTime) { + position.setDeviceTime(position.getServerTime()); + } + position.setFixTime(position.getDeviceTime()); + + } + ctx.fireChannelRead(msg); + } + +} diff --git a/src/main/java/org/traccar/model/Position.java b/src/main/java/org/traccar/model/Position.java index 4b327cbd2..6032dc588 100644 --- a/src/main/java/org/traccar/model/Position.java +++ b/src/main/java/org/traccar/model/Position.java @@ -157,7 +157,7 @@ public class Position extends Message { this.protocol = protocol; } - private Date serverTime; + private Date serverTime = new Date(); public Date getServerTime() { return serverTime; -- cgit v1.2.3