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 --- src/main/java/org/traccar/config/Keys.java | 9 +++++++ .../traccar/handler/events/FuelEventHandler.java | 28 +++++++++++++--------- src/main/java/org/traccar/model/Event.java | 1 + templates/full/deviceFuelIncrease.vm | 11 +++++++++ templates/short/deviceFuelIncrease.vm | 2 ++ 5 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 templates/full/deviceFuelIncrease.vm create mode 100644 templates/short/deviceFuelIncrease.vm diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java index e97e104a1..a3b39581d 100644 --- a/src/main/java/org/traccar/config/Keys.java +++ b/src/main/java/org/traccar/config/Keys.java @@ -309,6 +309,15 @@ public final class Keys { List.of(KeyType.SERVER, KeyType.DEVICE), 0.0); + /** + * Fuel increase threshold value. When fuel level increases from one position to another for more the value, an + * event is generated. + */ + public static final ConfigKey EVENT_FUEL_INCREASE_THRESHOLD = new DoubleConfigKey( + "fuelIncreaseThreshold", + List.of(KeyType.SERVER, KeyType.DEVICE), + 0.0); + /** * Speed limit value in knots. */ 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); + } } } } diff --git a/src/main/java/org/traccar/model/Event.java b/src/main/java/org/traccar/model/Event.java index f00a0e5f1..0e851d748 100644 --- a/src/main/java/org/traccar/model/Event.java +++ b/src/main/java/org/traccar/model/Event.java @@ -52,6 +52,7 @@ public class Event extends Message { public static final String TYPE_DEVICE_OVERSPEED = "deviceOverspeed"; public static final String TYPE_DEVICE_FUEL_DROP = "deviceFuelDrop"; + public static final String TYPE_DEVICE_FUEL_INCREASE = "deviceFuelIncrease"; public static final String TYPE_GEOFENCE_ENTER = "geofenceEnter"; public static final String TYPE_GEOFENCE_EXIT = "geofenceExit"; diff --git a/templates/full/deviceFuelIncrease.vm b/templates/full/deviceFuelIncrease.vm new file mode 100644 index 000000000..9d4474e1a --- /dev/null +++ b/templates/full/deviceFuelIncrease.vm @@ -0,0 +1,11 @@ +#set($subject = "$device.name: fuel increase") + + + +Device: $device.name
+Time: $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.eventTime, $locale, $timezone)
+Point: #{if}($position.address)$position.address#{else}$position.latitude°, $position.longitude°#{end}
+
+Unsubscribe + + diff --git a/templates/short/deviceFuelIncrease.vm b/templates/short/deviceFuelIncrease.vm new file mode 100644 index 000000000..6a11418b1 --- /dev/null +++ b/templates/short/deviceFuelIncrease.vm @@ -0,0 +1,2 @@ +#set($subject = "$device.name: fuel increase") +$device.name fuel increase at $dateTool.format("YYYY-MM-dd HH:mm:ss", $event.eventTime, $locale, $timezone) -- cgit v1.2.3