Search in sources :

Example 1 with MotionEventHandler

use of org.traccar.handler.events.MotionEventHandler in project traccar by tananaev.

the class ReportUtils method detectTripsAndStops.

public static <T extends BaseReport> Collection<T> detectTripsAndStops(IdentityManager identityManager, DeviceManager deviceManager, Collection<Position> positionCollection, TripsConfig tripsConfig, boolean ignoreOdometer, Class<T> reportClass) {
    Collection<T> result = new ArrayList<>();
    ArrayList<Position> positions = new ArrayList<>(positionCollection);
    if (!positions.isEmpty()) {
        boolean trips = reportClass.equals(TripReport.class);
        MotionEventHandler motionHandler = new MotionEventHandler(identityManager, deviceManager, 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.handler.events.MotionEventHandler) ArrayList(java.util.ArrayList) Event(org.traccar.model.Event)

Aggregations

ArrayList (java.util.ArrayList)1 MotionEventHandler (org.traccar.handler.events.MotionEventHandler)1 DeviceState (org.traccar.model.DeviceState)1 Event (org.traccar.model.Event)1 Position (org.traccar.model.Position)1