Search in sources :

Example 1 with Config

use of org.traccar.config.Config in project traccar by tananaev.

the class ComputedAttributesTest method testComputedAttributes.

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

Example 2 with Config

use of org.traccar.config.Config in project traccar by tananaev.

the class OverspeedEventHandlerTest method testOverspeedWithPosition.

private void testOverspeedWithPosition(boolean notRepeat, long geofenceId) throws ParseException {
    Config config = new Config();
    config.setString(Keys.EVENT_OVERSPEED_NOT_REPEAT, String.valueOf(notRepeat));
    config.setString(Keys.EVENT_OVERSPEED_MINIMAL_DURATION, String.valueOf(15));
    config.setString(Keys.EVENT_OVERSPEED_PREFER_LOWEST, String.valueOf(false));
    OverspeedEventHandler overspeedEventHandler = new OverspeedEventHandler(config, null, null);
    Position position = new Position();
    position.setTime(date("2017-01-01 00:00:00"));
    position.setSpeed(50);
    DeviceState deviceState = new DeviceState();
    deviceState.setOverspeedState(false);
    Map<Event, Position> events = overspeedEventHandler.updateOverspeedState(deviceState, position, 40, geofenceId);
    assertNull(events);
    assertFalse(deviceState.getOverspeedState());
    assertEquals(position, deviceState.getOverspeedPosition());
    assertEquals(geofenceId, deviceState.getOverspeedGeofenceId());
    Position nextPosition = new Position();
    nextPosition.setTime(date("2017-01-01 00:00:10"));
    nextPosition.setSpeed(55);
    events = overspeedEventHandler.updateOverspeedState(deviceState, nextPosition, 40, geofenceId);
    assertNull(events);
    nextPosition.setTime(date("2017-01-01 00:00:20"));
    events = overspeedEventHandler.updateOverspeedState(deviceState, nextPosition, 40, geofenceId);
    assertNotNull(events);
    Event event = events.keySet().iterator().next();
    assertEquals(Event.TYPE_DEVICE_OVERSPEED, event.getType());
    assertEquals(50, event.getDouble("speed"), 0.1);
    assertEquals(40, event.getDouble(OverspeedEventHandler.ATTRIBUTE_SPEED_LIMIT), 0.1);
    assertEquals(geofenceId, event.getGeofenceId());
    assertEquals(notRepeat, deviceState.getOverspeedState());
    assertNull(deviceState.getOverspeedPosition());
    assertEquals(0, deviceState.getOverspeedGeofenceId());
    nextPosition.setTime(date("2017-01-01 00:00:30"));
    events = overspeedEventHandler.updateOverspeedState(deviceState, nextPosition, 40, geofenceId);
    assertNull(events);
    assertEquals(notRepeat, deviceState.getOverspeedState());
    if (notRepeat) {
        assertNull(deviceState.getOverspeedPosition());
        assertEquals(0, deviceState.getOverspeedGeofenceId());
    } else {
        assertNotNull(deviceState.getOverspeedPosition());
        assertEquals(geofenceId, deviceState.getOverspeedGeofenceId());
    }
    nextPosition.setTime(date("2017-01-01 00:00:40"));
    nextPosition.setSpeed(30);
    events = overspeedEventHandler.updateOverspeedState(deviceState, nextPosition, 40, geofenceId);
    assertNull(events);
    assertFalse(deviceState.getOverspeedState());
    assertNull(deviceState.getOverspeedPosition());
    assertEquals(0, deviceState.getOverspeedGeofenceId());
}
Also used : DeviceState(org.traccar.model.DeviceState) Position(org.traccar.model.Position) Config(org.traccar.config.Config) Event(org.traccar.model.Event)

Example 3 with Config

use of org.traccar.config.Config in project traccar by tananaev.

the class DistanceHandlerTest method testCalculateDistance.

@Test
public void testCalculateDistance() {
    DistanceHandler distanceHandler = new DistanceHandler(new Config(), null);
    Position position = distanceHandler.handlePosition(new Position());
    assertEquals(0.0, position.getAttributes().get(Position.KEY_DISTANCE));
    assertEquals(0.0, position.getAttributes().get(Position.KEY_TOTAL_DISTANCE));
    position.set(Position.KEY_DISTANCE, 100);
    position = distanceHandler.handlePosition(position);
    assertEquals(100.0, position.getAttributes().get(Position.KEY_DISTANCE));
    assertEquals(100.0, position.getAttributes().get(Position.KEY_TOTAL_DISTANCE));
}
Also used : Position(org.traccar.model.Position) Config(org.traccar.config.Config) Test(org.junit.Test)

Example 4 with Config

use of org.traccar.config.Config in project traccar by tananaev.

the class WebDataHandlerTest method testFormatRequest.

@Test
public void testFormatRequest() throws Exception {
    Config config = new Config();
    config.setString(Keys.FORWARD_URL, "http://localhost/?fixTime={fixTime}&gprmc={gprmc}&name={name}");
    Position position = position("2016-01-01 01:02:03.000", true, 20, 30);
    WebDataHandler handler = new WebDataHandler(config, Context.getIdentityManager(), null, null);
    assertEquals("http://localhost/?fixTime=1451610123000&gprmc=$GPRMC,010203.000,A,2000.0000,N,03000.0000,E,0.00,0.00,010116,,*05&name=test", handler.formatRequest(position));
}
Also used : Position(org.traccar.model.Position) Config(org.traccar.config.Config) Test(org.junit.Test)

Example 5 with Config

use of org.traccar.config.Config in project traccar by tananaev.

the class AlertEventHandlerTest method testAlertEventHandler.

@Test
public void testAlertEventHandler() {
    AlertEventHandler alertEventHandler = new AlertEventHandler(new Config(), new TestIdentityManager());
    Position position = new Position();
    position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
    Map<Event, Position> events = alertEventHandler.analyzePosition(position);
    assertNotNull(events);
    Event event = events.keySet().iterator().next();
    assertEquals(Event.TYPE_ALARM, event.getType());
}
Also used : Position(org.traccar.model.Position) Config(org.traccar.config.Config) TestIdentityManager(org.traccar.TestIdentityManager) Event(org.traccar.model.Event) BaseTest(org.traccar.BaseTest) Test(org.junit.Test)

Aggregations

Config (org.traccar.config.Config)9 Position (org.traccar.model.Position)6 Test (org.junit.Test)4 Event (org.traccar.model.Event)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 JSR353Module (com.fasterxml.jackson.datatype.jsr353.JSR353Module)2 Date (java.util.Date)2 DeviceState (org.traccar.model.DeviceState)2 TripsConfig (org.traccar.reports.model.TripsConfig)2 UnknownHostException (java.net.UnknownHostException)1 Before (org.junit.Before)1 BaseTest (org.traccar.BaseTest)1 TestIdentityManager (org.traccar.TestIdentityManager)1 AttributesManager (org.traccar.database.AttributesManager)1 CommandsManager (org.traccar.database.CommandsManager)1 ConnectionManager (org.traccar.database.ConnectionManager)1 DataManager (org.traccar.database.DataManager)1 DeviceManager (org.traccar.database.DeviceManager)1 DriversManager (org.traccar.database.DriversManager)1 GroupsManager (org.traccar.database.GroupsManager)1