aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/handler
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-07-14 17:00:57 -0700
committerAnton Tananaev <anton@traccar.org>2022-07-14 17:00:57 -0700
commit0bd4c493e9003449457e87ebad6c7016ded71c8d (patch)
tree4beefd16bdc7665154d73fc07967ac45b1a06a49 /src/main/java/org/traccar/handler
parent34fefbffa49925a5d337ff388fb9db400d707f8b (diff)
downloadtrackermap-server-0bd4c493e9003449457e87ebad6c7016ded71c8d.tar.gz
trackermap-server-0bd4c493e9003449457e87ebad6c7016ded71c8d.tar.bz2
trackermap-server-0bd4c493e9003449457e87ebad6c7016ded71c8d.zip
Add LBS location caching (fix #3790)
Diffstat (limited to 'src/main/java/org/traccar/handler')
-rw-r--r--src/main/java/org/traccar/handler/GeocoderHandler.java8
-rw-r--r--src/main/java/org/traccar/handler/GeolocationHandler.java45
2 files changed, 37 insertions, 16 deletions
diff --git a/src/main/java/org/traccar/handler/GeocoderHandler.java b/src/main/java/org/traccar/handler/GeocoderHandler.java
index 0248fca05..e4f240a90 100644
--- a/src/main/java/org/traccar/handler/GeocoderHandler.java
+++ b/src/main/java/org/traccar/handler/GeocoderHandler.java
@@ -35,14 +35,14 @@ public class GeocoderHandler extends ChannelInboundHandlerAdapter {
private final CacheManager cacheManager;
private final boolean ignorePositions;
private final boolean processInvalidPositions;
- private final int geocoderReuseDistance;
+ private final int reuseDistance;
public GeocoderHandler(Config config, Geocoder geocoder, CacheManager cacheManager) {
this.geocoder = geocoder;
this.cacheManager = cacheManager;
ignorePositions = config.getBoolean(Keys.GEOCODER_IGNORE_POSITIONS);
processInvalidPositions = config.getBoolean(Keys.GEOCODER_PROCESS_INVALID_POSITIONS);
- geocoderReuseDistance = config.getInteger(Keys.GEOCODER_REUSE_DISTANCE, 0);
+ reuseDistance = config.getInteger(Keys.GEOCODER_REUSE_DISTANCE, 0);
}
@Override
@@ -50,10 +50,10 @@ public class GeocoderHandler extends ChannelInboundHandlerAdapter {
if (message instanceof Position && !ignorePositions) {
final Position position = (Position) message;
if (processInvalidPositions || position.getValid()) {
- if (geocoderReuseDistance != 0) {
+ if (reuseDistance != 0) {
Position lastPosition = cacheManager.getPosition(position.getDeviceId());
if (lastPosition != null && lastPosition.getAddress() != null
- && position.getDouble(Position.KEY_DISTANCE) <= geocoderReuseDistance) {
+ && position.getDouble(Position.KEY_DISTANCE) <= reuseDistance) {
position.setAddress(lastPosition.getAddress());
ctx.fireChannelRead(position);
return;
diff --git a/src/main/java/org/traccar/handler/GeolocationHandler.java b/src/main/java/org/traccar/handler/GeolocationHandler.java
index 0e78322c8..e7389f22d 100644
--- a/src/main/java/org/traccar/handler/GeolocationHandler.java
+++ b/src/main/java/org/traccar/handler/GeolocationHandler.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2015 - 2018 Anton Tananaev (anton@traccar.org)
+ * Copyright 2015 - 2022 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.
@@ -25,6 +25,7 @@ import org.traccar.config.Keys;
import org.traccar.database.StatisticsManager;
import org.traccar.geolocation.GeolocationProvider;
import org.traccar.model.Position;
+import org.traccar.session.cache.CacheManager;
@ChannelHandler.Sharable
public class GeolocationHandler extends ChannelInboundHandlerAdapter {
@@ -32,14 +33,19 @@ public class GeolocationHandler extends ChannelInboundHandlerAdapter {
private static final Logger LOGGER = LoggerFactory.getLogger(GeolocationHandler.class);
private final GeolocationProvider geolocationProvider;
+ private final CacheManager cacheManager;
private final StatisticsManager statisticsManager;
private final boolean processInvalidPositions;
+ private final boolean reuse;
public GeolocationHandler(
- Config config, GeolocationProvider geolocationProvider, StatisticsManager statisticsManager) {
+ Config config, GeolocationProvider geolocationProvider, CacheManager cacheManager,
+ StatisticsManager statisticsManager) {
this.geolocationProvider = geolocationProvider;
+ this.cacheManager = cacheManager;
this.statisticsManager = statisticsManager;
- this.processInvalidPositions = config.getBoolean(Keys.GEOLOCATION_PROCESS_INVALID_POSITIONS);
+ processInvalidPositions = config.getBoolean(Keys.GEOLOCATION_PROCESS_INVALID_POSITIONS);
+ reuse = config.getBoolean(Keys.GEOLOCATION_REUSE);
}
@Override
@@ -48,6 +54,17 @@ public class GeolocationHandler extends ChannelInboundHandlerAdapter {
final Position position = (Position) message;
if ((position.getOutdated() || processInvalidPositions && !position.getValid())
&& position.getNetwork() != null) {
+ if (reuse) {
+ Position lastPosition = cacheManager.getPosition(position.getDeviceId());
+ if (lastPosition != null && position.getNetwork().equals(lastPosition.getNetwork())) {
+ updatePosition(
+ position, lastPosition.getLatitude(), lastPosition.getLongitude(),
+ lastPosition.getAccuracy());
+ ctx.fireChannelRead(position);
+ return;
+ }
+ }
+
if (statisticsManager != null) {
statisticsManager.registerGeolocationRequest();
}
@@ -56,15 +73,7 @@ public class GeolocationHandler extends ChannelInboundHandlerAdapter {
new GeolocationProvider.LocationProviderCallback() {
@Override
public void onSuccess(double latitude, double longitude, double accuracy) {
- position.set(Position.KEY_APPROXIMATE, true);
- position.setValid(true);
- position.setFixTime(position.getDeviceTime());
- position.setLatitude(latitude);
- position.setLongitude(longitude);
- position.setAccuracy(accuracy);
- position.setAltitude(0);
- position.setSpeed(0);
- position.setCourse(0);
+ updatePosition(position, latitude, longitude, accuracy);
ctx.fireChannelRead(position);
}
@@ -82,4 +91,16 @@ public class GeolocationHandler extends ChannelInboundHandlerAdapter {
}
}
+ private void updatePosition(Position position, double latitude, double longitude, double accuracy) {
+ position.set(Position.KEY_APPROXIMATE, true);
+ position.setValid(true);
+ position.setFixTime(position.getDeviceTime());
+ position.setLatitude(latitude);
+ position.setLongitude(longitude);
+ position.setAccuracy(accuracy);
+ position.setAltitude(0);
+ position.setSpeed(0);
+ position.setCourse(0);
+ }
+
}