Search in sources :

Example 1 with DeviceState

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

the class MotionEventHandlerTest method testStopWithPositionIgnition.

@Test
public void testStopWithPositionIgnition() throws Exception {
    MotionEventHandler motionEventHandler = new MotionEventHandler(new TripsConfig(500, 300 * 1000, 300 * 1000, 0, true, false, 0.01));
    Position position = new Position();
    position.setTime(date("2017-01-01 00:00:00"));
    position.set(Position.KEY_MOTION, false);
    position.set(Position.KEY_IGNITION, true);
    DeviceState deviceState = new DeviceState();
    deviceState.setMotionState(true);
    deviceState.setMotionPosition(position);
    Position nextPosition = new Position();
    nextPosition.setTime(date("2017-01-01 00:02:00"));
    nextPosition.set(Position.KEY_MOTION, false);
    nextPosition.set(Position.KEY_IGNITION, false);
    Map<Event, Position> events = motionEventHandler.updateMotionState(deviceState, nextPosition);
    assertNotNull(events);
    Event event = events.keySet().iterator().next();
    assertEquals(Event.TYPE_DEVICE_STOPPED, event.getType());
    assertFalse(deviceState.getMotionState());
    assertNull(deviceState.getMotionPosition());
}
Also used : DeviceState(org.traccar.model.DeviceState) TripsConfig(org.traccar.reports.model.TripsConfig) Position(org.traccar.model.Position) Event(org.traccar.model.Event) Test(org.junit.Test) BaseTest(org.traccar.BaseTest)

Example 2 with DeviceState

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

the class OverspeedEventHandlerTest method testOverspeedWithStatus.

private void testOverspeedWithStatus(boolean notRepeat) throws Exception {
    OverspeedEventHandler overspeedEventHandler = new OverspeedEventHandler(15000, notRepeat);
    Position position = new Position();
    position.setTime(new Date(System.currentTimeMillis() - 30000));
    position.setSpeed(50);
    DeviceState deviceState = new DeviceState();
    deviceState.setOverspeedState(false);
    deviceState.setOverspeedPosition(position);
    Map<Event, Position> events = overspeedEventHandler.updateOverspeedState(deviceState, 40);
    assertNotNull(events);
    Event event = events.keySet().iterator().next();
    assertEquals(Event.TYPE_DEVICE_OVERSPEED, event.getType());
    assertEquals(notRepeat, deviceState.getOverspeedState());
}
Also used : DeviceState(org.traccar.model.DeviceState) Position(org.traccar.model.Position) Event(org.traccar.model.Event) Date(java.util.Date)

Example 3 with DeviceState

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

the class OverspeedEventHandlerTest method testOverspeedWithPosition.

private void testOverspeedWithPosition(boolean notRepeat) throws Exception {
    OverspeedEventHandler overspeedEventHandler = new OverspeedEventHandler(15000, notRepeat);
    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);
    assertNull(events);
    assertFalse(deviceState.getOverspeedState());
    assertEquals(position, deviceState.getOverspeedPosition());
    Position nextPosition = new Position();
    nextPosition.setTime(date("2017-01-01 00:00:10"));
    nextPosition.setSpeed(55);
    events = overspeedEventHandler.updateOverspeedState(deviceState, nextPosition, 40);
    assertNull(events);
    nextPosition.setTime(date("2017-01-01 00:00:20"));
    events = overspeedEventHandler.updateOverspeedState(deviceState, nextPosition, 40);
    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(notRepeat, deviceState.getOverspeedState());
    assertNull(deviceState.getOverspeedPosition());
    nextPosition.setTime(date("2017-01-01 00:00:30"));
    events = overspeedEventHandler.updateOverspeedState(deviceState, nextPosition, 40);
    assertNull(events);
    assertEquals(notRepeat, deviceState.getOverspeedState());
    if (notRepeat) {
        assertNull(deviceState.getOverspeedPosition());
    } else {
        assertNotNull(deviceState.getOverspeedPosition());
    }
    nextPosition.setTime(date("2017-01-01 00:00:40"));
    nextPosition.setSpeed(30);
    events = overspeedEventHandler.updateOverspeedState(deviceState, nextPosition, 40);
    assertNull(events);
    assertFalse(deviceState.getOverspeedState());
    assertNull(deviceState.getOverspeedPosition());
}
Also used : DeviceState(org.traccar.model.DeviceState) Position(org.traccar.model.Position) Event(org.traccar.model.Event)

Example 4 with DeviceState

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

the class ReportUtils method detectTripsAndStops.

public static <T extends BaseReport> Collection<T> detectTripsAndStops(Collection<Position> positionCollection, TripsConfig tripsConfig, boolean ignoreOdometer, Class<T> reportClass) {
    Collection<T> result = new ArrayList<>();
    ArrayList<Position> positions = new ArrayList<>(positionCollection);
    if (positions != null && !positions.isEmpty()) {
        boolean trips = reportClass.equals(TripReport.class);
        MotionEventHandler motionHandler = new MotionEventHandler(tripsConfig);
        DeviceState deviceState = new DeviceState();
        deviceState.setMotionState(isMoving(positions, 0, tripsConfig));
        int startEventIndex = trips == deviceState.getMotionState() ? 0 : -1;
        int startNoEventIndex = -1;
        for (int i = 0; i < positions.size(); i++) {
            Map<Event, Position> event = motionHandler.updateMotionState(deviceState, positions.get(i), isMoving(positions, i, tripsConfig));
            if (startEventIndex == -1 && (trips != deviceState.getMotionState() && deviceState.getMotionPosition() != null || trips == deviceState.getMotionState() && event != null)) {
                startEventIndex = i;
                startNoEventIndex = -1;
            } else if (trips != deviceState.getMotionState() && startEventIndex != -1 && deviceState.getMotionPosition() == null && event == null) {
                startEventIndex = -1;
            }
            if (startNoEventIndex == -1 && (trips == deviceState.getMotionState() && deviceState.getMotionPosition() != null || trips != deviceState.getMotionState() && event != null)) {
                startNoEventIndex = i;
            } else if (startNoEventIndex != -1 && deviceState.getMotionPosition() == null && event == null) {
                startNoEventIndex = -1;
            }
            if (startEventIndex != -1 && startNoEventIndex != -1 && event != null && trips != deviceState.getMotionState()) {
                result.add(calculateTripOrStop(positions, startEventIndex, startNoEventIndex, ignoreOdometer, reportClass));
                startEventIndex = -1;
            }
        }
        if (startEventIndex != -1 && (startNoEventIndex != -1 || !trips)) {
            result.add(calculateTripOrStop(positions, startEventIndex, startNoEventIndex != -1 ? startNoEventIndex : positions.size() - 1, ignoreOdometer, reportClass));
        }
    }
    return result;
}
Also used : DeviceState(org.traccar.model.DeviceState) Position(org.traccar.model.Position) MotionEventHandler(org.traccar.events.MotionEventHandler) ArrayList(java.util.ArrayList) Event(org.traccar.model.Event)

Example 5 with DeviceState

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

the class MotionEventHandlerTest method testMotionWithPosition.

@Test
public void testMotionWithPosition() throws Exception {
    MotionEventHandler motionEventHandler = new MotionEventHandler(new TripsConfig(500, 300 * 1000, 300 * 1000, 0, false, false, 0.01));
    Position position = new Position();
    position.setTime(date("2017-01-01 00:00:00"));
    position.set(Position.KEY_MOTION, true);
    position.set(Position.KEY_TOTAL_DISTANCE, 0);
    DeviceState deviceState = new DeviceState();
    deviceState.setMotionState(false);
    deviceState.setMotionPosition(position);
    Position nextPosition = new Position();
    nextPosition.setTime(date("2017-01-01 00:02:00"));
    nextPosition.set(Position.KEY_MOTION, true);
    nextPosition.set(Position.KEY_TOTAL_DISTANCE, 200);
    Map<Event, Position> events = motionEventHandler.updateMotionState(deviceState, nextPosition);
    assertNull(events);
    nextPosition.set(Position.KEY_TOTAL_DISTANCE, 600);
    events = motionEventHandler.updateMotionState(deviceState, nextPosition);
    assertNotNull(events);
    Event event = events.keySet().iterator().next();
    assertEquals(Event.TYPE_DEVICE_MOVING, event.getType());
    assertTrue(deviceState.getMotionState());
    assertNull(deviceState.getMotionPosition());
    deviceState.setMotionState(false);
    deviceState.setMotionPosition(position);
    nextPosition.setTime(date("2017-01-01 00:06:00"));
    nextPosition.set(Position.KEY_TOTAL_DISTANCE, 200);
    events = motionEventHandler.updateMotionState(deviceState, nextPosition);
    assertNotNull(event);
    event = events.keySet().iterator().next();
    assertEquals(Event.TYPE_DEVICE_MOVING, event.getType());
    assertTrue(deviceState.getMotionState());
    assertNull(deviceState.getMotionPosition());
}
Also used : DeviceState(org.traccar.model.DeviceState) TripsConfig(org.traccar.reports.model.TripsConfig) Position(org.traccar.model.Position) Event(org.traccar.model.Event) Test(org.junit.Test) BaseTest(org.traccar.BaseTest)

Aggregations

DeviceState (org.traccar.model.DeviceState)10 Event (org.traccar.model.Event)9 Position (org.traccar.model.Position)9 Test (org.junit.Test)3 BaseTest (org.traccar.BaseTest)3 TripsConfig (org.traccar.reports.model.TripsConfig)3 Date (java.util.Date)2 Device (org.traccar.model.Device)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 MotionEventHandler (org.traccar.events.MotionEventHandler)1