diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2019-02-26 19:05:25 -0800 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2019-02-26 19:05:25 -0800 |
commit | 728e55fc00a4b7b980d6e59f2ce8664859a5c848 (patch) | |
tree | af71d95acbd58370dd7c0ac4463275513360f610 /src/org/traccar/handler | |
parent | 03539bca46f9242ff62db9a6c9ab722ae2bb6a4d (diff) | |
download | trackermap-server-728e55fc00a4b7b980d6e59f2ce8664859a5c848.tar.gz trackermap-server-728e55fc00a4b7b980d6e59f2ce8664859a5c848.tar.bz2 trackermap-server-728e55fc00a4b7b980d6e59f2ce8664859a5c848.zip |
Refactor computed attributes
Diffstat (limited to 'src/org/traccar/handler')
-rw-r--r-- | src/org/traccar/handler/ComputedAttributesHandler.java | 37 |
1 files changed, 24 insertions, 13 deletions
diff --git a/src/org/traccar/handler/ComputedAttributesHandler.java b/src/org/traccar/handler/ComputedAttributesHandler.java index 5ceadafdd..153da29b9 100644 --- a/src/org/traccar/handler/ComputedAttributesHandler.java +++ b/src/org/traccar/handler/ComputedAttributesHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 - 2019 Anton Tananaev (anton@traccar.org) * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -32,7 +32,10 @@ import org.apache.commons.jexl2.MapContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.traccar.BaseDataHandler; -import org.traccar.Context; +import org.traccar.config.Config; +import org.traccar.config.Keys; +import org.traccar.database.AttributesManager; +import org.traccar.database.IdentityManager; import org.traccar.model.Attribute; import org.traccar.model.Device; import org.traccar.model.Position; @@ -42,23 +45,27 @@ public class ComputedAttributesHandler extends BaseDataHandler { private static final Logger LOGGER = LoggerFactory.getLogger(ComputedAttributesHandler.class); - private JexlEngine engine; + private final IdentityManager identityManager; + private final AttributesManager attributesManager; - private boolean mapDeviceAttributes; + private final JexlEngine engine; - public ComputedAttributesHandler() { + private final boolean includeDeviceAttributes; + + public ComputedAttributesHandler( + Config config, IdentityManager identityManager, AttributesManager attributesManager) { + this.identityManager = identityManager; + this.attributesManager = attributesManager; engine = new JexlEngine(); engine.setStrict(true); - engine.setFunctions(Collections.singletonMap("math", (Object) Math.class)); - if (Context.getConfig() != null) { - mapDeviceAttributes = Context.getConfig().getBoolean("processing.computedAttributes.deviceAttributes"); - } + engine.setFunctions(Collections.singletonMap("math", Math.class)); + includeDeviceAttributes = config.getBoolean(Keys.PROCESSING_COMPUTED_ATTRIBUTES_DEVICE_ATTRIBUTES); } private MapContext prepareContext(Position position) { MapContext result = new MapContext(); - if (mapDeviceAttributes) { - Device device = Context.getIdentityManager().getById(position.getDeviceId()); + if (includeDeviceAttributes) { + Device device = identityManager.getById(position.getDeviceId()); if (device != null) { for (Object key : device.getAttributes().keySet()) { result.set((String) key, device.getAttributes().get(key)); @@ -87,14 +94,18 @@ public class ComputedAttributesHandler extends BaseDataHandler { return result; } + /** + * @deprecated logic needs to be extracted to be used in API resource + */ + @Deprecated public Object computeAttribute(Attribute attribute, Position position) throws JexlException { return engine.createExpression(attribute.getExpression()).evaluate(prepareContext(position)); } @Override protected Position handlePosition(Position position) { - Collection<Attribute> attributes = Context.getAttributesManager().getItems( - Context.getAttributesManager().getAllDeviceItems(position.getDeviceId())); + Collection<Attribute> attributes = attributesManager.getItems( + attributesManager.getAllDeviceItems(position.getDeviceId())); for (Attribute attribute : attributes) { if (attribute.getAttribute() != null) { Object result = null; |