From 9a285e59e580994dc9c3f80935f766f3dafdcd46 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sun, 7 Apr 2024 07:17:09 -0700 Subject: Fix filtering handler --- src/main/java/org/traccar/ProcessingHandler.java | 44 +++++++++++++--------- .../org/traccar/handler/BasePositionHandler.java | 2 +- .../traccar/handler/ComputedAttributesHandler.java | 2 +- .../org/traccar/handler/CopyAttributesHandler.java | 2 +- .../java/org/traccar/handler/DatabaseHandler.java | 2 +- .../java/org/traccar/handler/DistanceHandler.java | 2 +- .../org/traccar/handler/EngineHoursHandler.java | 2 +- .../java/org/traccar/handler/FilterHandler.java | 6 +-- .../java/org/traccar/handler/GeocoderHandler.java | 10 ++--- .../java/org/traccar/handler/GeofenceHandler.java | 2 +- .../org/traccar/handler/GeolocationHandler.java | 8 ++-- .../org/traccar/handler/HemisphereHandler.java | 2 +- .../java/org/traccar/handler/MotionHandler.java | 2 +- .../java/org/traccar/handler/OutdatedHandler.java | 2 +- .../traccar/handler/PositionForwardingHandler.java | 2 +- .../org/traccar/handler/PostProcessHandler.java | 4 +- .../org/traccar/handler/SpeedLimitHandler.java | 4 +- src/main/java/org/traccar/handler/TimeHandler.java | 2 +- 18 files changed, 52 insertions(+), 48 deletions(-) diff --git a/src/main/java/org/traccar/ProcessingHandler.java b/src/main/java/org/traccar/ProcessingHandler.java index 76a698a0a..fd048d127 100644 --- a/src/main/java/org/traccar/ProcessingHandler.java +++ b/src/main/java/org/traccar/ProcessingHandler.java @@ -155,15 +155,15 @@ public class ProcessingHandler extends ChannelInboundHandlerAdapter implements B var iterator = positionHandlers.iterator(); iterator.next().handlePosition(position, new BasePositionHandler.Callback() { @Override - public void processed(Position position) { - if (position != null) { + public void processed(boolean filtered) { + if (!filtered) { if (iterator.hasNext()) { iterator.next().handlePosition(position, this); } else { processEventHandlers(ctx, position); } } else { - finishedProcessing(ctx, null); + finishedProcessing(ctx, position, true); } } }); @@ -172,24 +172,32 @@ public class ProcessingHandler extends ChannelInboundHandlerAdapter implements B private void processEventHandlers(ChannelHandlerContext ctx, Position position) { eventHandlers.forEach(handler -> handler.analyzePosition( position, (event) -> notificationManager.updateEvents(Map.of(event, position)))); - finishedProcessing(ctx, position); + finishedProcessing(ctx, position, false); } - private void finishedProcessing(ChannelHandlerContext ctx, Position position) { - postProcessHandler.handlePosition(position, p -> { - positionLogger.log(ctx, p); - ctx.writeAndFlush(new AcknowledgementHandler.EventHandled(p)); + private void finishedProcessing(ChannelHandlerContext ctx, Position position, boolean filtered) { + if (!filtered) { + postProcessHandler.handlePosition(position, ignore -> { + positionLogger.log(ctx, position); + ctx.writeAndFlush(new AcknowledgementHandler.EventHandled(position)); + processNextPosition(ctx, position.getDeviceId()); + }); + } else { + ctx.writeAndFlush(new AcknowledgementHandler.EventHandled(position)); + processNextPosition(ctx, position.getDeviceId()); + } + } - Queue queue = getQueue(position.getDeviceId()); - Position nextPosition; - synchronized (queue) { - queue.poll(); // remove current position - nextPosition = queue.peek(); - } - if (nextPosition != null) { - processPositionHandlers(ctx, nextPosition); - } - }); + private void processNextPosition(ChannelHandlerContext ctx, long deviceId) { + Queue queue = getQueue(deviceId); + Position nextPosition; + synchronized (queue) { + queue.poll(); // remove current position + nextPosition = queue.peek(); + } + if (nextPosition != null) { + processPositionHandlers(ctx, nextPosition); + } } } diff --git a/src/main/java/org/traccar/handler/BasePositionHandler.java b/src/main/java/org/traccar/handler/BasePositionHandler.java index 2fee5c652..0a82e96b7 100644 --- a/src/main/java/org/traccar/handler/BasePositionHandler.java +++ b/src/main/java/org/traccar/handler/BasePositionHandler.java @@ -20,7 +20,7 @@ import org.traccar.model.Position; public abstract class BasePositionHandler { public interface Callback { - void processed(Position position); + void processed(boolean filtered); } public abstract void handlePosition(Position position, Callback callback); diff --git a/src/main/java/org/traccar/handler/ComputedAttributesHandler.java b/src/main/java/org/traccar/handler/ComputedAttributesHandler.java index 8d6fb39c3..4293bd1fc 100644 --- a/src/main/java/org/traccar/handler/ComputedAttributesHandler.java +++ b/src/main/java/org/traccar/handler/ComputedAttributesHandler.java @@ -196,7 +196,7 @@ public class ComputedAttributesHandler extends BasePositionHandler { } } } - callback.processed(position); + callback.processed(false); } } diff --git a/src/main/java/org/traccar/handler/CopyAttributesHandler.java b/src/main/java/org/traccar/handler/CopyAttributesHandler.java index f682c99c9..c7452e58c 100644 --- a/src/main/java/org/traccar/handler/CopyAttributesHandler.java +++ b/src/main/java/org/traccar/handler/CopyAttributesHandler.java @@ -44,7 +44,7 @@ public class CopyAttributesHandler extends BasePositionHandler { } } } - callback.processed(position); + callback.processed(false); } } diff --git a/src/main/java/org/traccar/handler/DatabaseHandler.java b/src/main/java/org/traccar/handler/DatabaseHandler.java index 0c8d2717d..5d96ebb34 100644 --- a/src/main/java/org/traccar/handler/DatabaseHandler.java +++ b/src/main/java/org/traccar/handler/DatabaseHandler.java @@ -47,7 +47,7 @@ public class DatabaseHandler extends BasePositionHandler { LOGGER.warn("Failed to store position", error); } - callback.processed(position); + callback.processed(false); } } diff --git a/src/main/java/org/traccar/handler/DistanceHandler.java b/src/main/java/org/traccar/handler/DistanceHandler.java index ee5d64894..e8ae7753a 100644 --- a/src/main/java/org/traccar/handler/DistanceHandler.java +++ b/src/main/java/org/traccar/handler/DistanceHandler.java @@ -71,7 +71,7 @@ public class DistanceHandler extends BasePositionHandler { position.set(Position.KEY_DISTANCE, distance); position.set(Position.KEY_TOTAL_DISTANCE, totalDistance + distance); - callback.processed(position); + callback.processed(false); } } diff --git a/src/main/java/org/traccar/handler/EngineHoursHandler.java b/src/main/java/org/traccar/handler/EngineHoursHandler.java index ed5f9b509..5aae6f673 100644 --- a/src/main/java/org/traccar/handler/EngineHoursHandler.java +++ b/src/main/java/org/traccar/handler/EngineHoursHandler.java @@ -43,7 +43,7 @@ public class EngineHoursHandler extends BasePositionHandler { } } } - callback.processed(position); + callback.processed(false); } } diff --git a/src/main/java/org/traccar/handler/FilterHandler.java b/src/main/java/org/traccar/handler/FilterHandler.java index a9e6024c8..796c302fb 100644 --- a/src/main/java/org/traccar/handler/FilterHandler.java +++ b/src/main/java/org/traccar/handler/FilterHandler.java @@ -270,11 +270,7 @@ public class FilterHandler extends BasePositionHandler { @Override public void handlePosition(Position position, Callback callback) { - if (filter(position)) { - callback.processed(null); - } else { - callback.processed(position); - } + callback.processed(filter(position)); } } diff --git a/src/main/java/org/traccar/handler/GeocoderHandler.java b/src/main/java/org/traccar/handler/GeocoderHandler.java index c62bcb6f8..b84237856 100644 --- a/src/main/java/org/traccar/handler/GeocoderHandler.java +++ b/src/main/java/org/traccar/handler/GeocoderHandler.java @@ -44,7 +44,7 @@ public class GeocoderHandler extends BasePositionHandler { @Override public void handlePosition(Position position, Callback callback) { if (!ignorePositions) { - callback.processed(position); + callback.processed(false); } if (processInvalidPositions || position.getValid()) { @@ -53,7 +53,7 @@ public class GeocoderHandler extends BasePositionHandler { if (lastPosition != null && lastPosition.getAddress() != null && position.getDouble(Position.KEY_DISTANCE) <= reuseDistance) { position.setAddress(lastPosition.getAddress()); - callback.processed(position); + callback.processed(false); return; } } @@ -63,17 +63,17 @@ public class GeocoderHandler extends BasePositionHandler { @Override public void onSuccess(String address) { position.setAddress(address); - callback.processed(position); + callback.processed(false); } @Override public void onFailure(Throwable e) { LOGGER.warn("Geocoding failed", e); - callback.processed(position); + callback.processed(false); } }); } else { - callback.processed(position); + callback.processed(false); } } diff --git a/src/main/java/org/traccar/handler/GeofenceHandler.java b/src/main/java/org/traccar/handler/GeofenceHandler.java index 33b46f058..8b363057a 100644 --- a/src/main/java/org/traccar/handler/GeofenceHandler.java +++ b/src/main/java/org/traccar/handler/GeofenceHandler.java @@ -41,7 +41,7 @@ public class GeofenceHandler extends BasePositionHandler { if (!geofenceIds.isEmpty()) { position.setGeofenceIds(geofenceIds); } - callback.processed(position); + callback.processed(false); } } diff --git a/src/main/java/org/traccar/handler/GeolocationHandler.java b/src/main/java/org/traccar/handler/GeolocationHandler.java index cb9c04808..c46bd3250 100644 --- a/src/main/java/org/traccar/handler/GeolocationHandler.java +++ b/src/main/java/org/traccar/handler/GeolocationHandler.java @@ -57,7 +57,7 @@ public class GeolocationHandler extends BasePositionHandler { updatePosition( position, lastPosition.getLatitude(), lastPosition.getLongitude(), lastPosition.getAccuracy()); - callback.processed(position); + callback.processed(false); return; } } @@ -71,17 +71,17 @@ public class GeolocationHandler extends BasePositionHandler { @Override public void onSuccess(double latitude, double longitude, double accuracy) { updatePosition(position, latitude, longitude, accuracy); - callback.processed(position); + callback.processed(false); } @Override public void onFailure(Throwable e) { LOGGER.warn("Geolocation network error", e); - callback.processed(position); + callback.processed(false); } }); } else { - callback.processed(position); + callback.processed(false); } } diff --git a/src/main/java/org/traccar/handler/HemisphereHandler.java b/src/main/java/org/traccar/handler/HemisphereHandler.java index 6b64177e4..48929538f 100644 --- a/src/main/java/org/traccar/handler/HemisphereHandler.java +++ b/src/main/java/org/traccar/handler/HemisphereHandler.java @@ -53,7 +53,7 @@ public class HemisphereHandler extends BasePositionHandler { if (longitudeFactor != 0) { position.setLongitude(Math.abs(position.getLongitude()) * longitudeFactor); } - callback.processed(position); + callback.processed(false); } } diff --git a/src/main/java/org/traccar/handler/MotionHandler.java b/src/main/java/org/traccar/handler/MotionHandler.java index bb7ff2a65..804ffdc25 100644 --- a/src/main/java/org/traccar/handler/MotionHandler.java +++ b/src/main/java/org/traccar/handler/MotionHandler.java @@ -38,7 +38,7 @@ public class MotionHandler extends BasePositionHandler { cacheManager, Keys.EVENT_MOTION_SPEED_THRESHOLD, position.getDeviceId()); position.set(Position.KEY_MOTION, position.getSpeed() > threshold); } - callback.processed(position); + callback.processed(false); } } diff --git a/src/main/java/org/traccar/handler/OutdatedHandler.java b/src/main/java/org/traccar/handler/OutdatedHandler.java index 88f1c4a0c..536440f66 100644 --- a/src/main/java/org/traccar/handler/OutdatedHandler.java +++ b/src/main/java/org/traccar/handler/OutdatedHandler.java @@ -50,7 +50,7 @@ public class OutdatedHandler extends BasePositionHandler { position.setDeviceTime(position.getServerTime()); } } - callback.processed(position); + callback.processed(false); } } diff --git a/src/main/java/org/traccar/handler/PositionForwardingHandler.java b/src/main/java/org/traccar/handler/PositionForwardingHandler.java index be62fff37..8512d4552 100644 --- a/src/main/java/org/traccar/handler/PositionForwardingHandler.java +++ b/src/main/java/org/traccar/handler/PositionForwardingHandler.java @@ -131,7 +131,7 @@ public class PositionForwardingHandler extends BasePositionHandler { positionData.setDevice(cacheManager.getObject(Device.class, position.getDeviceId())); new AsyncRequestAndCallback(positionData).send(); } - callback.processed(position); + callback.processed(false); } } diff --git a/src/main/java/org/traccar/handler/PostProcessHandler.java b/src/main/java/org/traccar/handler/PostProcessHandler.java index e1d833e46..5b1b2ef86 100644 --- a/src/main/java/org/traccar/handler/PostProcessHandler.java +++ b/src/main/java/org/traccar/handler/PostProcessHandler.java @@ -47,7 +47,7 @@ public class PostProcessHandler extends BasePositionHandler { @Override public void handlePosition(Position position, Callback callback) { try { - if (position != null && PositionUtil.isLatest(cacheManager, position)) { + if (PositionUtil.isLatest(cacheManager, position)) { Device updatedDevice = new Device(); updatedDevice.setId(position.getDeviceId()); updatedDevice.setPositionId(position.getId()); @@ -61,7 +61,7 @@ public class PostProcessHandler extends BasePositionHandler { } catch (StorageException error) { LOGGER.warn("Failed to update device", error); } - callback.processed(position); + callback.processed(false); } } diff --git a/src/main/java/org/traccar/handler/SpeedLimitHandler.java b/src/main/java/org/traccar/handler/SpeedLimitHandler.java index 604c10ca7..4c0922d01 100644 --- a/src/main/java/org/traccar/handler/SpeedLimitHandler.java +++ b/src/main/java/org/traccar/handler/SpeedLimitHandler.java @@ -40,13 +40,13 @@ public class SpeedLimitHandler extends BasePositionHandler { @Override public void onSuccess(double speedLimit) { position.set(Position.KEY_SPEED_LIMIT, speedLimit); - callback.processed(position); + callback.processed(false); } @Override public void onFailure(Throwable e) { LOGGER.warn("Speed limit provider failed", e); - callback.processed(position); + callback.processed(false); } }); } diff --git a/src/main/java/org/traccar/handler/TimeHandler.java b/src/main/java/org/traccar/handler/TimeHandler.java index 052ad41c3..f6c67bb76 100644 --- a/src/main/java/org/traccar/handler/TimeHandler.java +++ b/src/main/java/org/traccar/handler/TimeHandler.java @@ -51,7 +51,7 @@ public class TimeHandler extends BasePositionHandler { position.setFixTime(position.getDeviceTime()); } } - callback.processed(position); + callback.processed(false); } } -- cgit v1.2.3