From 074cc52c3d645ac974a1489aa4e197d471093403 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 3 Aug 2022 19:06:12 -0700 Subject: Implement refuel events --- .../traccar/handler/events/FuelEventHandler.java | 28 +++++++++++++--------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'src/main/java/org/traccar/handler') diff --git a/src/main/java/org/traccar/handler/events/FuelEventHandler.java b/src/main/java/org/traccar/handler/events/FuelEventHandler.java index 9ec819a5f..d5d4ab9be 100644 --- a/src/main/java/org/traccar/handler/events/FuelEventHandler.java +++ b/src/main/java/org/traccar/handler/events/FuelEventHandler.java @@ -25,7 +25,6 @@ import org.traccar.model.Position; import org.traccar.session.cache.CacheManager; import javax.inject.Inject; -import java.util.Collections; import java.util.Map; @ChannelHandler.Sharable @@ -49,18 +48,25 @@ public class FuelEventHandler extends BaseEventHandler { return null; } - double fuelDropThreshold = AttributeUtil.lookup( - cacheManager, Keys.EVENT_FUEL_DROP_THRESHOLD, position.getDeviceId()); - if (fuelDropThreshold > 0) { + if (position.hasAttribute(Position.KEY_FUEL_LEVEL)) { Position lastPosition = cacheManager.getPosition(position.getDeviceId()); - if (position.hasAttribute(Position.KEY_FUEL_LEVEL) - && lastPosition != null && lastPosition.hasAttribute(Position.KEY_FUEL_LEVEL)) { + if (lastPosition != null && lastPosition.hasAttribute(Position.KEY_FUEL_LEVEL)) { + double before = lastPosition.getDouble(Position.KEY_FUEL_LEVEL); + double after = position.getDouble(Position.KEY_FUEL_LEVEL); + double change = after - before; - double drop = lastPosition.getDouble(Position.KEY_FUEL_LEVEL) - - position.getDouble(Position.KEY_FUEL_LEVEL); - if (drop >= fuelDropThreshold) { - Event event = new Event(Event.TYPE_DEVICE_FUEL_DROP, position); - return Collections.singletonMap(event, position); + if (change > 0) { + double threshold = AttributeUtil.lookup( + cacheManager, Keys.EVENT_FUEL_INCREASE_THRESHOLD, position.getDeviceId()); + if (change >= threshold) { + return Map.of(new Event(Event.TYPE_DEVICE_FUEL_INCREASE, position), position); + } + } else if (change < 0) { + double threshold = AttributeUtil.lookup( + cacheManager, Keys.EVENT_FUEL_DROP_THRESHOLD, position.getDeviceId()); + if (Math.abs(change) >= threshold) { + return Map.of(new Event(Event.TYPE_DEVICE_FUEL_DROP, position), position); + } } } } -- cgit v1.2.3