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());
}
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;
}
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;
}
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;
}
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);
}
Aggregations