use of org.traccar.model.Device in project traccar by tananaev.
the class BaseProtocolDecoder method findDeviceId.
private long findDeviceId(SocketAddress remoteAddress, String... uniqueIds) {
if (uniqueIds.length > 0) {
long deviceId = 0;
Device device = null;
try {
for (String uniqueId : uniqueIds) {
if (uniqueId != null) {
device = Context.getIdentityManager().getByUniqueId(uniqueId);
if (device != null) {
deviceId = device.getId();
break;
}
}
}
} catch (Exception e) {
Log.warning(e);
}
if (deviceId == 0 && Context.getConfig().getBoolean("database.registerUnknown")) {
return addUnknownDevice(uniqueIds[0]);
}
if (device != null && !device.getDisabled() || Context.getConfig().getBoolean("database.storeDisabled")) {
return deviceId;
}
StringBuilder message = new StringBuilder();
if (deviceId == 0) {
message.append("Unknown device -");
} else {
message.append("Disabled device -");
}
for (String uniqueId : uniqueIds) {
message.append(" ").append(uniqueId);
}
if (remoteAddress != null) {
message.append(" (").append(((InetSocketAddress) remoteAddress).getHostString()).append(")");
}
Log.warning(message.toString());
}
return 0;
}
use of org.traccar.model.Device in project traccar by tananaev.
the class WebDataHandler method prepareJsonPayload.
protected String prepareJsonPayload(Position position) {
Map<String, Object> data = new HashMap<>();
Device device = Context.getIdentityManager().getById(position.getDeviceId());
data.put(KEY_POSITION, position);
if (device != null) {
data.put(KEY_DEVICE, device);
}
try {
return Context.getObjectMapper().writeValueAsString(data);
} catch (JsonProcessingException e) {
Log.warning(e);
return null;
}
}
use of org.traccar.model.Device 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.Device in project traccar by tananaev.
the class DeviceResource method get.
@GET
public Collection<Device> get(@QueryParam("all") boolean all, @QueryParam("userId") long userId, @QueryParam("uniqueId") List<String> uniqueIds, @QueryParam("id") List<Long> deviceIds) throws SQLException {
DeviceManager deviceManager = Context.getDeviceManager();
Set<Long> result = null;
if (all) {
if (Context.getPermissionsManager().getUserAdmin(getUserId())) {
result = deviceManager.getAllItems();
} else {
Context.getPermissionsManager().checkManager(getUserId());
result = deviceManager.getManagedItems(getUserId());
}
} else if (uniqueIds.isEmpty() && deviceIds.isEmpty()) {
if (userId == 0) {
userId = getUserId();
}
Context.getPermissionsManager().checkUser(getUserId(), userId);
if (Context.getPermissionsManager().getUserAdmin(getUserId())) {
result = deviceManager.getAllUserItems(userId);
} else {
result = deviceManager.getUserItems(userId);
}
} else {
result = new HashSet<>();
for (String uniqueId : uniqueIds) {
Device device = deviceManager.getByUniqueId(uniqueId);
Context.getPermissionsManager().checkDevice(getUserId(), device.getId());
result.add(device.getId());
}
for (Long deviceId : deviceIds) {
Context.getPermissionsManager().checkDevice(getUserId(), deviceId);
result.add(deviceId);
}
}
return deviceManager.getItems(result);
}
use of org.traccar.model.Device 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