Search in sources :

Example 21 with Event

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

the class MotionEventHandlerTest method testMotionWithStatus.

@Test
public void testMotionWithStatus() throws Exception {
    MotionEventHandler motionEventHandler = new MotionEventHandler(new TripsConfig(500, 300 * 1000, 300 * 1000, 0, false, false, 0.01));
    Position position = new Position();
    position.setTime(new Date(System.currentTimeMillis() - 360000));
    position.set(Position.KEY_MOTION, true);
    DeviceState deviceState = new DeviceState();
    deviceState.setMotionState(false);
    deviceState.setMotionPosition(position);
    Map<Event, Position> events = motionEventHandler.updateMotionState(deviceState);
    assertNotNull(events);
    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) Date(java.util.Date) Test(org.junit.Test) BaseTest(org.traccar.BaseTest)

Example 22 with Event

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

the class MotionEventHandler method updateMotionState.

public Map<Event, Position> updateMotionState(DeviceState deviceState, Position position, boolean newMotion) {
    Map<Event, Position> result = null;
    Boolean oldMotion = deviceState.getMotionState();
    long currentTime = position.getFixTime().getTime();
    if (newMotion != oldMotion) {
        if (deviceState.getMotionPosition() == null) {
            deviceState.setMotionPosition(position);
        }
    } else {
        deviceState.setMotionPosition(null);
    }
    Position motionPosition = deviceState.getMotionPosition();
    if (motionPosition != null) {
        long motionTime = motionPosition.getFixTime().getTime();
        double distance = ReportUtils.calculateDistance(motionPosition, position, false);
        Boolean ignition = null;
        if (tripsConfig.getUseIgnition() && position.getAttributes().containsKey(Position.KEY_IGNITION)) {
            ignition = position.getBoolean(Position.KEY_IGNITION);
        }
        if (newMotion) {
            if (motionTime + tripsConfig.getMinimalTripDuration() <= currentTime || distance >= tripsConfig.getMinimalTripDistance()) {
                result = newEvent(deviceState, newMotion);
            }
        } else {
            if (motionTime + tripsConfig.getMinimalParkingDuration() <= currentTime || ignition != null && !ignition) {
                result = newEvent(deviceState, newMotion);
            }
        }
    }
    return result;
}
Also used : Position(org.traccar.model.Position) Event(org.traccar.model.Event)

Example 23 with Event

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

the class MotionEventHandler method analyzePosition.

@Override
protected Map<Event, Position> analyzePosition(Position position) {
    long deviceId = position.getDeviceId();
    Device device = Context.getIdentityManager().getById(deviceId);
    if (device == null) {
        return null;
    }
    if (!Context.getIdentityManager().isLatestPosition(position) || !tripsConfig.getProcessInvalidPositions() && !position.getValid()) {
        return null;
    }
    Map<Event, Position> result = null;
    DeviceState deviceState = Context.getDeviceManager().getDeviceState(deviceId);
    if (deviceState.getMotionState() == null) {
        deviceState.setMotionState(position.getBoolean(Position.KEY_MOTION));
    } else {
        result = updateMotionState(deviceState, position);
    }
    Context.getDeviceManager().setDeviceState(deviceId, deviceState);
    return result;
}
Also used : DeviceState(org.traccar.model.DeviceState) Position(org.traccar.model.Position) Device(org.traccar.model.Device) Event(org.traccar.model.Event)

Example 24 with Event

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

the class ConnectionManager method updateDeviceState.

public Map<Event, Position> updateDeviceState(long deviceId) {
    DeviceState deviceState = Context.getDeviceManager().getDeviceState(deviceId);
    Map<Event, Position> result = new HashMap<>();
    Map<Event, Position> event = Context.getMotionEventHandler().updateMotionState(deviceState);
    if (event != null) {
        result.putAll(event);
    }
    event = Context.getOverspeedEventHandler().updateOverspeedState(deviceState, Context.getDeviceManager().lookupAttributeDouble(deviceId, OverspeedEventHandler.ATTRIBUTE_SPEED_LIMIT, 0, false));
    if (event != null) {
        result.putAll(event);
    }
    return result;
}
Also used : DeviceState(org.traccar.model.DeviceState) Position(org.traccar.model.Position) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Event(org.traccar.model.Event)

Example 25 with Event

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

the class ConnectionManager method updateDevice.

public void updateDevice(final long deviceId, String status, Date time) {
    Device device = Context.getIdentityManager().getById(deviceId);
    if (device == null) {
        return;
    }
    String oldStatus = device.getStatus();
    device.setStatus(status);
    if (enableStatusEvents && !status.equals(oldStatus)) {
        String eventType;
        Map<Event, Position> events = new HashMap<>();
        switch(status) {
            case Device.STATUS_ONLINE:
                eventType = Event.TYPE_DEVICE_ONLINE;
                break;
            case Device.STATUS_UNKNOWN:
                eventType = Event.TYPE_DEVICE_UNKNOWN;
                if (updateDeviceState) {
                    events.putAll(updateDeviceState(deviceId));
                }
                break;
            default:
                eventType = Event.TYPE_DEVICE_OFFLINE;
                if (updateDeviceState) {
                    events.putAll(updateDeviceState(deviceId));
                }
                break;
        }
        events.put(new Event(eventType, deviceId), null);
        Context.getNotificationManager().updateEvents(events);
    }
    Timeout timeout = timeouts.remove(deviceId);
    if (timeout != null) {
        timeout.cancel();
    }
    if (time != null) {
        device.setLastUpdate(time);
    }
    if (status.equals(Device.STATUS_ONLINE)) {
        timeouts.put(deviceId, GlobalTimer.getTimer().newTimeout(new TimerTask() {

            @Override
            public void run(Timeout timeout) throws Exception {
                if (!timeout.isCancelled()) {
                    updateDevice(deviceId, Device.STATUS_UNKNOWN, null);
                    activeDevices.remove(deviceId);
                }
            }
        }, deviceTimeout, TimeUnit.MILLISECONDS));
    }
    try {
        Context.getDeviceManager().updateDeviceStatus(device);
    } catch (SQLException error) {
        Log.warning(error);
    }
    updateDevice(device);
}
Also used : TimerTask(org.jboss.netty.util.TimerTask) Position(org.traccar.model.Position) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Device(org.traccar.model.Device) Timeout(org.jboss.netty.util.Timeout) Event(org.traccar.model.Event)

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