aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbyss777 <abyss@fox5.ru>2017-10-30 13:27:24 +0500
committerAbyss777 <abyss@fox5.ru>2017-10-30 13:27:24 +0500
commitaa606dfd6159dc7e0d1ab35f656654db03c1cdec (patch)
tree259ef61225b55c87ae8dbf51bbc0a0514975c5c3
parenta23453cdeb6285db91af5544686192c966ad03c4 (diff)
downloadtraccar-server-aa606dfd6159dc7e0d1ab35f656654db03c1cdec.tar.gz
traccar-server-aa606dfd6159dc7e0d1ab35f656654db03c1cdec.tar.bz2
traccar-server-aa606dfd6159dc7e0d1ab35f656654db03c1cdec.zip
Map "Math" to Computed Attributes context
-rw-r--r--src/org/traccar/processing/ComputedAttributesHandler.java2
-rw-r--r--test/org/traccar/processing/ComputedAttributesTest.java17
2 files changed, 12 insertions, 7 deletions
diff --git a/src/org/traccar/processing/ComputedAttributesHandler.java b/src/org/traccar/processing/ComputedAttributesHandler.java
index f1f371475..1e702d17f 100644
--- a/src/org/traccar/processing/ComputedAttributesHandler.java
+++ b/src/org/traccar/processing/ComputedAttributesHandler.java
@@ -20,6 +20,7 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
+import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
@@ -43,6 +44,7 @@ public class ComputedAttributesHandler extends BaseDataHandler {
public ComputedAttributesHandler() {
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");
}
diff --git a/test/org/traccar/processing/ComputedAttributesTest.java b/test/org/traccar/processing/ComputedAttributesTest.java
index ac4331c6d..fe898ff54 100644
--- a/test/org/traccar/processing/ComputedAttributesTest.java
+++ b/test/org/traccar/processing/ComputedAttributesTest.java
@@ -8,7 +8,7 @@ import org.traccar.model.Attribute;
import org.traccar.model.Position;
public class ComputedAttributesTest {
-
+
@Test
public void testComputedAttributes() {
Position position = new Position();
@@ -39,26 +39,29 @@ public class ComputedAttributesTest {
attribute.setExpression("if (event == 42) \"lowBattery\"");
Assert.assertEquals("lowBattery", computedAttributesHandler.computeAttribute(attribute, position));
-
+
attribute.setExpression("speed > 5 && valid");
Assert.assertEquals(false, computedAttributesHandler.computeAttribute(attribute, position));
-
+
attribute.setExpression("fixTime");
Assert.assertEquals(date, computedAttributesHandler.computeAttribute(attribute, position));
-
+
+ attribute.setExpression("math:pow(adc1, 2)");
+ Assert.assertEquals(16384.0, computedAttributesHandler.computeAttribute(attribute, position));
+
// modification tests
attribute.setExpression("adc1 = 256");
computedAttributesHandler.computeAttribute(attribute, position);
Assert.assertEquals(128, position.getInteger("adc1"));
-
+
attribute.setExpression("result = \"fail\"");
computedAttributesHandler.computeAttribute(attribute, position);
Assert.assertEquals("success", position.getString("result"));
-
+
attribute.setExpression("fixTime = \"2017-10-18 10:00:01\"");
computedAttributesHandler.computeAttribute(attribute, position);
Assert.assertEquals(date, position.getFixTime());
-
+
}
}