Search in sources :

Example 16 with Event

use of org.traccar.model.Event 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 17 with Event

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

the class Events method getObjects.

public static Collection<Event> getObjects(long userId, Collection<Long> deviceIds, Collection<Long> groupIds, Collection<String> types, Date from, Date to) throws SQLException {
    ReportUtils.checkPeriodLimit(from, to);
    ArrayList<Event> result = new ArrayList<>();
    for (long deviceId : ReportUtils.getDeviceList(deviceIds, groupIds)) {
        Context.getPermissionsManager().checkDevice(userId, deviceId);
        Collection<Event> events = Context.getDataManager().getEvents(deviceId, from, to);
        boolean all = types.isEmpty() || types.contains(Event.ALL_EVENTS);
        for (Event event : events) {
            if (all || types.contains(event.getType())) {
                long geofenceId = event.getGeofenceId();
                if (geofenceId == 0 || Context.getGeofenceManager().checkItemPermission(userId, geofenceId)) {
                    result.add(event);
                }
            }
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) Event(org.traccar.model.Event)

Example 18 with Event

use of org.traccar.model.Event 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 19 with Event

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

the class AlertEventHandlerTest method testAlertEventHandler.

@Test
public void testAlertEventHandler() throws Exception {
    AlertEventHandler alertEventHandler = new AlertEventHandler();
    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) Event(org.traccar.model.Event) BaseTest(org.traccar.BaseTest) Test(org.junit.Test)

Example 20 with Event

use of org.traccar.model.Event 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

Event (org.traccar.model.Event)31 Position (org.traccar.model.Position)25 Device (org.traccar.model.Device)9 DeviceState (org.traccar.model.DeviceState)9 Test (org.junit.Test)6 BaseTest (org.traccar.BaseTest)6 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 TripsConfig (org.traccar.reports.model.TripsConfig)3 Date (java.util.Date)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Path (javax.ws.rs.Path)2 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 SQLException (java.sql.SQLException)1 GET (javax.ws.rs.GET)1 POST (javax.ws.rs.POST)1 Timeout (org.jboss.netty.util.Timeout)1 TimerTask (org.jboss.netty.util.TimerTask)1 MotionEventHandler (org.traccar.events.MotionEventHandler)1