use of org.traccar.model.Device in project traccar by tananaev.
the class BaseProtocolDecoder method addUnknownDevice.
public long addUnknownDevice(String uniqueId) {
Device device = new Device();
device.setName(uniqueId);
device.setUniqueId(uniqueId);
device.setCategory(Context.getConfig().getString("database.registerUnknown.defaultCategory"));
long defaultGroupId = Context.getConfig().getLong("database.registerUnknown.defaultGroupId");
if (defaultGroupId != 0) {
device.setGroupId(defaultGroupId);
}
try {
Context.getDeviceManager().addItem(device);
Log.info("Automatically registered device " + uniqueId);
if (defaultGroupId != 0) {
Context.getPermissionsManager().refreshDeviceAndGroupPermissions();
Context.getPermissionsManager().refreshAllExtendedPermissions();
}
return device.getId();
} catch (SQLException e) {
Log.warning(e);
return 0;
}
}
use of org.traccar.model.Device in project traccar by tananaev.
the class WebDataHandler method formatRequest.
public String formatRequest(Position position) {
Device device = Context.getIdentityManager().getById(position.getDeviceId());
String request = url.replace("{name}", device.getName()).replace("{uniqueId}", device.getUniqueId()).replace("{status}", device.getStatus()).replace("{deviceId}", String.valueOf(position.getDeviceId())).replace("{protocol}", String.valueOf(position.getProtocol())).replace("{deviceTime}", String.valueOf(position.getDeviceTime().getTime())).replace("{fixTime}", String.valueOf(position.getFixTime().getTime())).replace("{valid}", String.valueOf(position.getValid())).replace("{latitude}", String.valueOf(position.getLatitude())).replace("{longitude}", String.valueOf(position.getLongitude())).replace("{altitude}", String.valueOf(position.getAltitude())).replace("{speed}", String.valueOf(position.getSpeed())).replace("{course}", String.valueOf(position.getCourse())).replace("{statusCode}", calculateStatus(position));
if (position.getAddress() != null) {
try {
request = request.replace("{address}", URLEncoder.encode(position.getAddress(), StandardCharsets.UTF_8.name()));
} catch (UnsupportedEncodingException error) {
Log.warning(error);
}
}
if (request.contains("{attributes}")) {
try {
String attributes = Context.getObjectMapper().writeValueAsString(position.getAttributes());
request = request.replace("{attributes}", URLEncoder.encode(attributes, StandardCharsets.UTF_8.name()));
} catch (UnsupportedEncodingException | JsonProcessingException error) {
Log.warning(error);
}
}
if (request.contains("{gprmc}")) {
request = request.replace("{gprmc}", formatSentence(position));
}
return request;
}
use of org.traccar.model.Device in project traccar by tananaev.
the class GeofenceManager method recalculateDevicesGeofences.
public void recalculateDevicesGeofences() {
for (Device device : Context.getDeviceManager().getAllDevices()) {
List<Long> deviceGeofenceIds = device.getGeofenceIds();
if (deviceGeofenceIds == null) {
deviceGeofenceIds = new ArrayList<>();
} else {
deviceGeofenceIds.clear();
}
Position lastPosition = Context.getIdentityManager().getLastPosition(device.getId());
if (lastPosition != null && getAllDeviceItems(device.getId()) != null) {
deviceGeofenceIds.addAll(getCurrentDeviceGeofences(lastPosition));
}
device.setGeofenceIds(deviceGeofenceIds);
}
}
use of org.traccar.model.Device in project traccar by tananaev.
the class PermissionsManager method refreshDeviceAndGroupPermissions.
public final void refreshDeviceAndGroupPermissions() {
groupPermissions.clear();
devicePermissions.clear();
try {
GroupTree groupTree = new GroupTree(Context.getGroupsManager().getItems(Context.getGroupsManager().getAllItems()), Context.getDeviceManager().getAllDevices());
for (Permission groupPermission : dataManager.getPermissions(User.class, Group.class)) {
Set<Long> userGroupPermissions = getGroupPermissions(groupPermission.getOwnerId());
Set<Long> userDevicePermissions = getDevicePermissions(groupPermission.getOwnerId());
userGroupPermissions.add(groupPermission.getPropertyId());
for (Group group : groupTree.getGroups(groupPermission.getPropertyId())) {
userGroupPermissions.add(group.getId());
}
for (Device device : groupTree.getDevices(groupPermission.getPropertyId())) {
userDevicePermissions.add(device.getId());
}
}
for (Permission devicePermission : dataManager.getPermissions(User.class, Device.class)) {
getDevicePermissions(devicePermission.getOwnerId()).add(devicePermission.getPropertyId());
}
groupDevices.clear();
for (long groupId : Context.getGroupsManager().getAllItems()) {
for (Device device : groupTree.getDevices(groupId)) {
getGroupDevices(groupId).add(device.getId());
}
}
} catch (SQLException | ClassNotFoundException error) {
Log.warning(error);
}
deviceUsers.clear();
for (Map.Entry<Long, Set<Long>> entry : devicePermissions.entrySet()) {
for (long deviceId : entry.getValue()) {
getAllDeviceUsers(deviceId).add(entry.getKey());
}
}
}
use of org.traccar.model.Device in project traccar by tananaev.
the class FuelDropEventHandler method analyzePosition.
@Override
protected Map<Event, Position> analyzePosition(Position position) {
Device device = Context.getIdentityManager().getById(position.getDeviceId());
if (device == null) {
return null;
}
if (!Context.getIdentityManager().isLatestPosition(position)) {
return null;
}
double fuelDropThreshold = Context.getDeviceManager().lookupAttributeDouble(device.getId(), ATTRIBUTE_FUEL_DROP_THRESHOLD, 0, false);
if (fuelDropThreshold > 0) {
Position lastPosition = Context.getIdentityManager().getLastPosition(position.getDeviceId());
if (position.getAttributes().containsKey(Position.KEY_FUEL_LEVEL) && lastPosition != null && lastPosition.getAttributes().containsKey(Position.KEY_FUEL_LEVEL)) {
double drop = lastPosition.getDouble(Position.KEY_FUEL_LEVEL) - position.getDouble(Position.KEY_FUEL_LEVEL);
if (drop >= fuelDropThreshold) {
Event event = new Event(Event.TYPE_DEVICE_FUEL_DROP, position.getDeviceId(), position.getId());
event.set(ATTRIBUTE_FUEL_DROP_THRESHOLD, fuelDropThreshold);
return Collections.singletonMap(event, position);
}
}
}
return null;
}
Aggregations