From 43e7373576c2137af86febad8d55f948838a900a Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 14 Oct 2023 18:36:41 -0700 Subject: Last position computed attributes --- .../traccar/handler/ComputedAttributesHandler.java | 24 +++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'src/main/java/org/traccar/handler') diff --git a/src/main/java/org/traccar/handler/ComputedAttributesHandler.java b/src/main/java/org/traccar/handler/ComputedAttributesHandler.java index 042747359..3f8a8c0bd 100644 --- a/src/main/java/org/traccar/handler/ComputedAttributesHandler.java +++ b/src/main/java/org/traccar/handler/ComputedAttributesHandler.java @@ -59,6 +59,7 @@ public class ComputedAttributesHandler extends BaseDataHandler { private final JexlFeatures features; private final boolean includeDeviceAttributes; + private final boolean includeLastAttributes; @Inject public ComputedAttributesHandler(Config config, CacheManager cacheManager) { @@ -81,6 +82,7 @@ public class ComputedAttributesHandler extends BaseDataHandler { .sandbox(sandbox) .create(); includeDeviceAttributes = config.getBoolean(Keys.PROCESSING_COMPUTED_ATTRIBUTES_DEVICE_ATTRIBUTES); + includeLastAttributes = config.getBoolean(Keys.PROCESSING_COMPUTED_ATTRIBUTES_LAST_ATTRIBUTES); } private MapContext prepareContext(Position position) { @@ -93,6 +95,10 @@ public class ComputedAttributesHandler extends BaseDataHandler { } } } + Position last = null; + if (includeLastAttributes) { + last = cacheManager.getPosition(position.getDeviceId()); + } Set methods = new HashSet<>(Arrays.asList(position.getClass().getMethods())); Arrays.asList(Object.class.getMethods()).forEach(methods::remove); for (Method method : methods) { @@ -102,9 +108,17 @@ public class ComputedAttributesHandler extends BaseDataHandler { try { if (!method.getReturnType().equals(Map.class)) { result.set(name, method.invoke(position)); + if (last != null) { + result.set(prefixAttribute("last", name), method.invoke(last)); + } } else { - for (Object key : ((Map) method.invoke(position)).keySet()) { - result.set((String) key, ((Map) method.invoke(position)).get(key)); + for (Map.Entry entry : ((Map) method.invoke(position)).entrySet()) { + result.set((String) entry.getKey(), entry.getValue()); + } + if (last != null) { + for (Map.Entry entry : ((Map) method.invoke(last)).entrySet()) { + result.set(prefixAttribute("last", (String) entry.getKey()), entry.getValue()); + } } } } catch (IllegalAccessException | InvocationTargetException error) { @@ -112,7 +126,11 @@ public class ComputedAttributesHandler extends BaseDataHandler { } } } - return result; + return result;//Character.toUpperCase(column.charAt(0)) + column.substring(1) + } + + private String prefixAttribute(String prefix, String key) { + return prefix + Character.toUpperCase(key.charAt(0)) + key.substring(1); } /** -- cgit v1.2.3