use of org.traccar.model.DeviceState in project traccar by tananaev.
the class MotionEventHandlerTest method testStopWithPositionIgnition.
@Test
public void testStopWithPositionIgnition() throws Exception {
MotionEventHandler motionEventHandler = new MotionEventHandler(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());
}
use of org.traccar.model.DeviceState in project traccar by tananaev.
the class OverspeedEventHandlerTest method testOverspeedWithStatus.
private void testOverspeedWithStatus(boolean notRepeat) throws Exception {
OverspeedEventHandler overspeedEventHandler = new OverspeedEventHandler(15000, notRepeat);
Position position = new Position();
position.setTime(new Date(System.currentTimeMillis() - 30000));
position.setSpeed(50);
DeviceState deviceState = new DeviceState();
deviceState.setOverspeedState(false);
deviceState.setOverspeedPosition(position);
Map<Event, Position> events = overspeedEventHandler.updateOverspeedState(deviceState, 40);
assertNotNull(events);
Event event = events.keySet().iterator().next();
assertEquals(Event.TYPE_DEVICE_OVERSPEED, event.getType());
assertEquals(notRepeat, deviceState.getOverspeedState());
}
use of org.traccar.model.DeviceState 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());
}
use of org.traccar.model.DeviceState 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;
}
use of org.traccar.model.DeviceState 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());
}
Aggregations