Search in sources :

Example 1 with Attribute

use of org.traccar.model.Attribute in project traccar by tananaev.

the class ComputedAttributesHandler method handlePosition.

@Override
protected Position handlePosition(Position position) {
    Collection<Attribute> attributes = Context.getAttributesManager().getItems(Context.getAttributesManager().getAllDeviceItems(position.getDeviceId()));
    for (Attribute attribute : attributes) {
        if (attribute.getAttribute() != null) {
            Object result = null;
            try {
                result = computeAttribute(attribute, position);
            } catch (JexlException error) {
                Log.warning(error);
            }
            if (result != null) {
                try {
                    switch(attribute.getType()) {
                        case "number":
                            Number numberValue = (Number) result;
                            position.getAttributes().put(attribute.getAttribute(), numberValue);
                            break;
                        case "boolean":
                            Boolean booleanValue = (Boolean) result;
                            position.getAttributes().put(attribute.getAttribute(), booleanValue);
                            break;
                        default:
                            position.getAttributes().put(attribute.getAttribute(), result.toString());
                    }
                } catch (ClassCastException error) {
                    Log.warning(error);
                }
            }
        }
    }
    return position;
}
Also used : Attribute(org.traccar.model.Attribute) JexlException(org.apache.commons.jexl2.JexlException)

Example 2 with Attribute

use of org.traccar.model.Attribute in project traccar by tananaev.

the class AttributesManager method updateCachedItem.

@Override
public void updateCachedItem(Attribute attribute) {
    Attribute cachedAttribute = getById(attribute.getId());
    cachedAttribute.setDescription(attribute.getDescription());
    cachedAttribute.setAttribute(attribute.getAttribute());
    cachedAttribute.setExpression(attribute.getExpression());
    cachedAttribute.setType(attribute.getType());
}
Also used : Attribute(org.traccar.model.Attribute)

Example 3 with Attribute

use of org.traccar.model.Attribute in project traccar by tananaev.

the class ComputedAttributesTest method testComputedAttributes.

@Test
public void testComputedAttributes() {
    Position position = new Position();
    ComputedAttributesHandler computedAttributesHandler = new ComputedAttributesHandler();
    Date date = new Date();
    position.setTime(date);
    position.setSpeed(42);
    position.setValid(false);
    position.set("adc1", 128);
    position.set("booleanFlag", true);
    position.set("adc2", 100);
    position.set("bitFlag", 7);
    position.set("event", 42);
    position.set("result", "success");
    Attribute attribute = new Attribute();
    attribute.setExpression("adc1");
    assertEquals(128, computedAttributesHandler.computeAttribute(attribute, position));
    attribute.setExpression("!booleanFlag");
    assertEquals(false, computedAttributesHandler.computeAttribute(attribute, position));
    attribute.setExpression("adc2 * 2 + 50");
    assertEquals(250, computedAttributesHandler.computeAttribute(attribute, position));
    attribute.setExpression("(bitFlag & 4) != 0");
    assertEquals(true, computedAttributesHandler.computeAttribute(attribute, position));
    attribute.setExpression("if (event == 42) \"lowBattery\"");
    assertEquals("lowBattery", computedAttributesHandler.computeAttribute(attribute, position));
    attribute.setExpression("speed > 5 && valid");
    assertEquals(false, computedAttributesHandler.computeAttribute(attribute, position));
    attribute.setExpression("fixTime");
    assertEquals(date, computedAttributesHandler.computeAttribute(attribute, position));
    attribute.setExpression("math:pow(adc1, 2)");
    assertEquals(16384.0, computedAttributesHandler.computeAttribute(attribute, position));
    // modification tests
    attribute.setExpression("adc1 = 256");
    computedAttributesHandler.computeAttribute(attribute, position);
    assertEquals(128, position.getInteger("adc1"));
    attribute.setExpression("result = \"fail\"");
    computedAttributesHandler.computeAttribute(attribute, position);
    assertEquals("success", position.getString("result"));
    attribute.setExpression("fixTime = \"2017-10-18 10:00:01\"");
    computedAttributesHandler.computeAttribute(attribute, position);
    assertEquals(date, position.getFixTime());
}
Also used : Position(org.traccar.model.Position) Attribute(org.traccar.model.Attribute) Date(java.util.Date) Test(org.junit.Test)

Example 4 with Attribute

use of org.traccar.model.Attribute in project traccar by traccar.

the class AttributesManager method updateCachedItem.

@Override
public void updateCachedItem(Attribute attribute) {
    Attribute cachedAttribute = getById(attribute.getId());
    cachedAttribute.setDescription(attribute.getDescription());
    cachedAttribute.setAttribute(attribute.getAttribute());
    cachedAttribute.setExpression(attribute.getExpression());
    cachedAttribute.setType(attribute.getType());
}
Also used : Attribute(org.traccar.model.Attribute)

Example 5 with Attribute

use of org.traccar.model.Attribute in project traccar by traccar.

the class ComputedAttributesHandler method handlePosition.

@Override
protected Position handlePosition(Position position) {
    Collection<Attribute> attributes = Context.getAttributesManager().getItems(Context.getAttributesManager().getAllDeviceItems(position.getDeviceId()));
    for (Attribute attribute : attributes) {
        if (attribute.getAttribute() != null) {
            Object result = null;
            try {
                result = computeAttribute(attribute, position);
            } catch (JexlException error) {
                Log.warning(error);
            }
            if (result != null) {
                try {
                    switch(attribute.getType()) {
                        case "number":
                            Number numberValue = (Number) result;
                            position.getAttributes().put(attribute.getAttribute(), numberValue);
                            break;
                        case "boolean":
                            Boolean booleanValue = (Boolean) result;
                            position.getAttributes().put(attribute.getAttribute(), booleanValue);
                            break;
                        default:
                            position.getAttributes().put(attribute.getAttribute(), result.toString());
                    }
                } catch (ClassCastException error) {
                    Log.warning(error);
                }
            }
        }
    }
    return position;
}
Also used : Attribute(org.traccar.model.Attribute) JexlException(org.apache.commons.jexl2.JexlException)

Aggregations

Attribute (org.traccar.model.Attribute)6 Date (java.util.Date)2 JexlException (org.apache.commons.jexl2.JexlException)2 Test (org.junit.Test)2 Position (org.traccar.model.Position)2