use of org.traccar.session.DeviceState in project traccar by traccar.
the class MotionEventHandler method analyzePosition.
@Override
protected Map<Event, Position> analyzePosition(Position position) {
long deviceId = position.getDeviceId();
Device device = identityManager.getById(deviceId);
if (device == null) {
return null;
}
if (!identityManager.isLatestPosition(position) || !tripsConfig.getProcessInvalidPositions() && !position.getValid()) {
return null;
}
Map<Event, Position> result = null;
DeviceState deviceState = deviceManager.getDeviceState(deviceId);
if (deviceState.getMotionState() == null) {
deviceState.setMotionState(position.getBoolean(Position.KEY_MOTION));
} else {
result = updateMotionState(deviceState, position);
}
deviceManager.setDeviceState(deviceId, deviceState);
return result;
}
use of org.traccar.session.DeviceState in project traccar by traccar.
the class OverspeedEventHandler method analyzePosition.
@Override
protected Map<Event, Position> analyzePosition(Position position) {
long deviceId = position.getDeviceId();
Device device = deviceManager.getById(deviceId);
if (device == null) {
return null;
}
if (!deviceManager.isLatestPosition(position) || !position.getValid()) {
return null;
}
double speedLimit = deviceManager.lookupAttributeDouble(deviceId, ATTRIBUTE_SPEED_LIMIT, 0, true, false);
double positionSpeedLimit = position.getDouble(Position.KEY_SPEED_LIMIT);
if (positionSpeedLimit > 0) {
speedLimit = positionSpeedLimit;
}
double geofenceSpeedLimit = 0;
long overspeedGeofenceId = 0;
if (geofenceManager != null && device.getGeofenceIds() != null) {
for (long geofenceId : device.getGeofenceIds()) {
Geofence geofence = geofenceManager.getById(geofenceId);
if (geofence != null) {
double currentSpeedLimit = geofence.getDouble(ATTRIBUTE_SPEED_LIMIT);
if (currentSpeedLimit > 0 && geofenceSpeedLimit == 0 || preferLowest && currentSpeedLimit < geofenceSpeedLimit || !preferLowest && currentSpeedLimit > geofenceSpeedLimit) {
geofenceSpeedLimit = currentSpeedLimit;
overspeedGeofenceId = geofenceId;
}
}
}
}
if (geofenceSpeedLimit > 0) {
speedLimit = geofenceSpeedLimit;
}
if (speedLimit == 0) {
return null;
}
Map<Event, Position> result = null;
DeviceState deviceState = deviceManager.getDeviceState(deviceId);
if (deviceState.getOverspeedState() == null) {
deviceState.setOverspeedState(position.getSpeed() > speedLimit);
deviceState.setOverspeedGeofenceId(position.getSpeed() > speedLimit ? overspeedGeofenceId : 0);
} else {
result = updateOverspeedState(deviceState, position, speedLimit, overspeedGeofenceId);
}
deviceManager.setDeviceState(deviceId, deviceState);
return result;
}
use of org.traccar.session.DeviceState in project traccar by traccar.
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(identityManager, positions, startEventIndex, startNoEventIndex, ignoreOdometer, reportClass));
startEventIndex = -1;
}
}
if (startEventIndex != -1 && (startNoEventIndex != -1 || !trips)) {
result.add(calculateTripOrStop(identityManager, positions, startEventIndex, startNoEventIndex != -1 ? startNoEventIndex : positions.size() - 1, ignoreOdometer, reportClass));
}
}
return result;
}
use of org.traccar.session.DeviceState in project traccar by traccar.
the class MotionEventHandlerTest method testMotionWithPosition.
@Test
public void testMotionWithPosition() throws Exception {
MotionEventHandler motionEventHandler = new MotionEventHandler(null, null, 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());
}
use of org.traccar.session.DeviceState in project traccar by traccar.
the class MotionEventHandlerTest method testStopWithPositionIgnition.
@Test
public void testStopWithPositionIgnition() throws Exception {
MotionEventHandler motionEventHandler = new MotionEventHandler(null, null, 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());
}
Aggregations