From fc3a24148526b3d41ba40ed1f55cf40b54c6a255 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 3 Apr 2024 21:17:52 -0700 Subject: Handle outdated positions in buffer --- src/main/java/org/traccar/BaseProtocolDecoder.java | 19 +------- src/main/java/org/traccar/ProcessingHandler.java | 2 + .../org/traccar/database/BufferingManager.java | 26 +++++++--- .../java/org/traccar/handler/OutdatedHandler.java | 56 ++++++++++++++++++++++ 4 files changed, 78 insertions(+), 25 deletions(-) create mode 100644 src/main/java/org/traccar/handler/OutdatedHandler.java diff --git a/src/main/java/org/traccar/BaseProtocolDecoder.java b/src/main/java/org/traccar/BaseProtocolDecoder.java index 495a866c0..b764e5cdf 100644 --- a/src/main/java/org/traccar/BaseProtocolDecoder.java +++ b/src/main/java/org/traccar/BaseProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2022 Anton Tananaev (anton@traccar.org) + * Copyright 2012 - 2024 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. @@ -154,25 +154,8 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { public void getLastLocation(Position position, Date deviceTime) { if (position.getDeviceId() != 0) { position.setOutdated(true); - - Position last = cacheManager.getPosition(position.getDeviceId()); - if (last != null) { - position.setFixTime(last.getFixTime()); - position.setValid(last.getValid()); - position.setLatitude(last.getLatitude()); - position.setLongitude(last.getLongitude()); - position.setAltitude(last.getAltitude()); - position.setSpeed(last.getSpeed()); - position.setCourse(last.getCourse()); - position.setAccuracy(last.getAccuracy()); - } else { - position.setFixTime(new Date(0)); - } - if (deviceTime != null) { position.setDeviceTime(deviceTime); - } else { - position.setDeviceTime(new Date()); } } } diff --git a/src/main/java/org/traccar/ProcessingHandler.java b/src/main/java/org/traccar/ProcessingHandler.java index 6a97b9dea..76a698a0a 100644 --- a/src/main/java/org/traccar/ProcessingHandler.java +++ b/src/main/java/org/traccar/ProcessingHandler.java @@ -36,6 +36,7 @@ import org.traccar.handler.GeofenceHandler; import org.traccar.handler.GeolocationHandler; import org.traccar.handler.HemisphereHandler; import org.traccar.handler.MotionHandler; +import org.traccar.handler.OutdatedHandler; import org.traccar.handler.PositionForwardingHandler; import org.traccar.handler.PostProcessHandler; import org.traccar.handler.SpeedLimitHandler; @@ -90,6 +91,7 @@ public class ProcessingHandler extends ChannelInboundHandlerAdapter implements B bufferingManager = new BufferingManager(config, this); positionHandlers = Stream.of( + OutdatedHandler.class, TimeHandler.class, GeolocationHandler.class, HemisphereHandler.class, diff --git a/src/main/java/org/traccar/database/BufferingManager.java b/src/main/java/org/traccar/database/BufferingManager.java index 3e3cf587a..4d288c8d0 100644 --- a/src/main/java/org/traccar/database/BufferingManager.java +++ b/src/main/java/org/traccar/database/BufferingManager.java @@ -25,7 +25,7 @@ import org.traccar.config.Config; import org.traccar.config.Keys; import org.traccar.model.Position; -import java.util.Comparator; +import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.TreeSet; @@ -41,11 +41,6 @@ public class BufferingManager { private static final class Holder implements Comparable { - private static final Comparator COMPARATOR = Comparator - .comparing(Position::getFixTime) - .thenComparing(Position::getDeviceTime) - .thenComparing(Position::getServerTime); - private final ChannelHandlerContext context; private final Position position; private Timeout timeout; @@ -55,9 +50,26 @@ public class BufferingManager { this.position = position; } + private int compareTime(Date left, Date right) { + if (left != null && right != null) { + return left.compareTo(right); + } + return 0; + } + @Override public int compareTo(Holder other) { - return COMPARATOR.compare(position, other.position); + int fixTimeResult = compareTime(position.getFixTime(), other.position.getFixTime()); + if (fixTimeResult != 0) { + return fixTimeResult; + } + + int deviceTimeResult = compareTime(position.getDeviceTime(), other.position.getDeviceTime()); + if (deviceTimeResult != 0) { + return deviceTimeResult; + } + + return position.getServerTime().compareTo(other.position.getServerTime()); } } diff --git a/src/main/java/org/traccar/handler/OutdatedHandler.java b/src/main/java/org/traccar/handler/OutdatedHandler.java new file mode 100644 index 000000000..88f1c4a0c --- /dev/null +++ b/src/main/java/org/traccar/handler/OutdatedHandler.java @@ -0,0 +1,56 @@ +/* + * Copyright 2024 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 jakarta.inject.Inject; +import org.traccar.model.Position; +import org.traccar.session.cache.CacheManager; + +import java.util.Date; + +public class OutdatedHandler extends BasePositionHandler { + + private final CacheManager cacheManager; + + @Inject + public OutdatedHandler(CacheManager cacheManager) { + this.cacheManager = cacheManager; + } + + @Override + public void handlePosition(Position position, Callback callback) { + if (position.getOutdated()) { + Position last = cacheManager.getPosition(position.getDeviceId()); + if (last != null) { + position.setFixTime(last.getFixTime()); + position.setValid(last.getValid()); + position.setLatitude(last.getLatitude()); + position.setLongitude(last.getLongitude()); + position.setAltitude(last.getAltitude()); + position.setSpeed(last.getSpeed()); + position.setCourse(last.getCourse()); + position.setAccuracy(last.getAccuracy()); + } else { + position.setFixTime(new Date(315964819000L)); // gps epoch 1980-01-06 + } + if (position.getDeviceTime() == null) { + position.setDeviceTime(position.getServerTime()); + } + } + callback.processed(position); + } + +} -- cgit v1.2.3