Search in sources :

Example 1 with MotionEventHandler

use of org.traccar.events.MotionEventHandler 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 2 with MotionEventHandler

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

the class Context method initEventsModule.

private static void initEventsModule() {
    geofenceManager = new GeofenceManager(dataManager);
    calendarManager = new CalendarManager(dataManager);
    notificationManager = new NotificationManager(dataManager);
    Properties velocityProperties = new Properties();
    velocityProperties.setProperty("file.resource.loader.path", Context.getConfig().getString("templates.rootPath", "templates") + "/");
    velocityProperties.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogChute");
    String address;
    try {
        address = config.getString("web.address", InetAddress.getLocalHost().getHostAddress());
    } catch (UnknownHostException e) {
        address = "localhost";
    }
    String webUrl = URIUtil.newURI("http", address, config.getInteger("web.port", 8082), "", "");
    webUrl = Context.getConfig().getString("web.url", webUrl);
    velocityProperties.setProperty("web.url", webUrl);
    velocityEngine = new VelocityEngine();
    velocityEngine.init(velocityProperties);
    motionEventHandler = new MotionEventHandler(tripsConfig);
    overspeedEventHandler = new OverspeedEventHandler(Context.getConfig().getLong("event.overspeed.minimalDuration") * 1000, Context.getConfig().getBoolean("event.overspeed.notRepeat"));
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) CalendarManager(org.traccar.database.CalendarManager) NotificationManager(org.traccar.database.NotificationManager) UnknownHostException(java.net.UnknownHostException) GeofenceManager(org.traccar.database.GeofenceManager) MotionEventHandler(org.traccar.events.MotionEventHandler) OverspeedEventHandler(org.traccar.events.OverspeedEventHandler) Properties(java.util.Properties)

Aggregations

MotionEventHandler (org.traccar.events.MotionEventHandler)2 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 VelocityEngine (org.apache.velocity.app.VelocityEngine)1 CalendarManager (org.traccar.database.CalendarManager)1 GeofenceManager (org.traccar.database.GeofenceManager)1 NotificationManager (org.traccar.database.NotificationManager)1 OverspeedEventHandler (org.traccar.events.OverspeedEventHandler)1 DeviceState (org.traccar.model.DeviceState)1 Event (org.traccar.model.Event)1 Position (org.traccar.model.Position)1