use of org.traccar.storage.StorageException in project traccar by tananaev.
the class NotificationManager method updateEvent.
public void updateEvent(Event event, Position position) {
try {
getDataManager().addObject(event);
} catch (StorageException error) {
LOGGER.warn("Event save error", error);
}
long deviceId = event.getDeviceId();
Set<Long> users = Context.getPermissionsManager().getDeviceUsers(deviceId);
Set<Long> usersToForward = null;
if (Context.getEventForwarder() != null) {
usersToForward = new HashSet<>();
}
for (long userId : users) {
if ((event.getGeofenceId() == 0 || Context.getGeofenceManager().checkItemPermission(userId, event.getGeofenceId())) && (event.getMaintenanceId() == 0 || Context.getMaintenancesManager().checkItemPermission(userId, event.getMaintenanceId()))) {
if (usersToForward != null) {
usersToForward.add(userId);
}
final Set<String> notificators = new HashSet<>();
for (long notificationId : getEffectiveNotifications(userId, deviceId, event.getEventTime())) {
Notification notification = getById(notificationId);
if (getById(notificationId).getType().equals(event.getType())) {
boolean filter = false;
if (event.getType().equals(Event.TYPE_ALARM)) {
String alarmsAttribute = notification.getString("alarms");
if (alarmsAttribute == null) {
filter = true;
} else {
List<String> alarms = Arrays.asList(alarmsAttribute.split(","));
filter = !alarms.contains(event.getString(Position.KEY_ALARM));
}
}
if (!filter) {
notificators.addAll(notification.getNotificatorsTypes());
}
}
}
if (position != null && position.getAddress() == null && geocodeOnRequest && Context.getGeocoder() != null) {
position.setAddress(Context.getGeocoder().getAddress(position.getLatitude(), position.getLongitude(), null));
}
for (String notificator : notificators) {
Context.getNotificatorManager().getNotificator(notificator).sendAsync(userId, event, position);
}
}
}
if (Context.getEventForwarder() != null) {
Context.getEventForwarder().forwardEvent(event, position, usersToForward);
}
}
use of org.traccar.storage.StorageException in project traccar by tananaev.
the class PermissionsManager method refreshDeviceAndGroupPermissions.
public final void refreshDeviceAndGroupPermissions() {
writeLock();
try {
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 (StorageException | ClassNotFoundException error) {
LOGGER.warn("Refresh device permissions error", error);
}
deviceUsers.clear();
for (Map.Entry<Long, Set<Long>> entry : devicePermissions.entrySet()) {
for (long deviceId : entry.getValue()) {
getAllDeviceUsers(deviceId).add(entry.getKey());
}
}
} finally {
writeUnlock();
}
}
use of org.traccar.storage.StorageException in project traccar by tananaev.
the class StatisticsManager method checkSplit.
private void checkSplit() {
int currentUpdate = Calendar.getInstance().get(SPLIT_MODE);
if (lastUpdate.getAndSet(currentUpdate) != currentUpdate) {
Statistics statistics = new Statistics();
synchronized (this) {
statistics.setCaptureTime(new Date());
statistics.setActiveUsers(users.size());
statistics.setActiveDevices(deviceProtocols.size());
statistics.setRequests(requests);
statistics.setMessagesReceived(messagesReceived);
statistics.setMessagesStored(messagesStored);
statistics.setMailSent(mailSent);
statistics.setSmsSent(smsSent);
statistics.setGeocoderRequests(geocoderRequests);
statistics.setGeolocationRequests(geolocationRequests);
if (!deviceProtocols.isEmpty()) {
Map<String, Integer> protocols = new HashMap<>();
for (String protocol : deviceProtocols.values()) {
protocols.compute(protocol, (key, count) -> count != null ? count + 1 : 1);
}
statistics.setProtocols(protocols);
}
users.clear();
deviceProtocols.clear();
requests = 0;
messagesReceived = 0;
messagesStored = 0;
mailSent = 0;
smsSent = 0;
geocoderRequests = 0;
geolocationRequests = 0;
}
try {
dataManager.addObject(statistics);
} catch (StorageException e) {
LOGGER.warn("Error saving statistics", e);
}
String url = config.getString(Keys.SERVER_STATISTICS);
if (url != null) {
String time = DateUtil.formatDate(statistics.getCaptureTime());
Form form = new Form();
form.param("version", getClass().getPackage().getImplementationVersion());
form.param("captureTime", time);
form.param("activeUsers", String.valueOf(statistics.getActiveUsers()));
form.param("activeDevices", String.valueOf(statistics.getActiveDevices()));
form.param("requests", String.valueOf(statistics.getRequests()));
form.param("messagesReceived", String.valueOf(statistics.getMessagesReceived()));
form.param("messagesStored", String.valueOf(statistics.getMessagesStored()));
form.param("mailSent", String.valueOf(statistics.getMailSent()));
form.param("smsSent", String.valueOf(statistics.getSmsSent()));
form.param("geocoderRequests", String.valueOf(statistics.getGeocoderRequests()));
form.param("geolocationRequests", String.valueOf(statistics.getGeolocationRequests()));
if (statistics.getProtocols() != null) {
try {
form.param("protocols", objectMapper.writeValueAsString(statistics.getProtocols()));
} catch (JsonProcessingException e) {
LOGGER.warn("Failed to serialize protocols", e);
}
}
client.target(url).request().async().post(Entity.form(form));
}
}
}
use of org.traccar.storage.StorageException in project traccar by tananaev.
the class DeviceManager method addUnknownDevice.
@Override
public long addUnknownDevice(String uniqueId) {
Device device = new Device();
device.setName(uniqueId);
device.setUniqueId(uniqueId);
device.setCategory(Context.getConfig().getString(Keys.DATABASE_REGISTER_UNKNOWN_DEFAULT_CATEGORY));
long defaultGroupId = Context.getConfig().getLong(Keys.DATABASE_REGISTER_UNKNOWN_DEFAULT_GROUP_ID);
if (defaultGroupId != 0) {
device.setGroupId(defaultGroupId);
}
try {
addItem(device);
LOGGER.info("Automatically registered device " + uniqueId);
if (defaultGroupId != 0) {
Context.getPermissionsManager().refreshDeviceAndGroupPermissions();
Context.getPermissionsManager().refreshAllExtendedPermissions();
}
return device.getId();
} catch (StorageException e) {
LOGGER.warn("Automatic device registration error", e);
return 0;
}
}
use of org.traccar.storage.StorageException in project traccar by tananaev.
the class ExtendedObjectManager method refreshExtendedPermissions.
public void refreshExtendedPermissions() {
if (getDataManager() != null) {
try {
Collection<Permission> databaseGroupPermissions = getDataManager().getPermissions(Group.class, getBaseClass());
Collection<Permission> databaseDevicePermissions = getDataManager().getPermissions(Device.class, getBaseClass());
writeLock();
groupItems.clear();
deviceItems.clear();
deviceItemsWithGroups.clear();
for (Permission groupPermission : databaseGroupPermissions) {
groupItems.computeIfAbsent(groupPermission.getOwnerId(), key -> new HashSet<>()).add(groupPermission.getPropertyId());
}
for (Permission devicePermission : databaseDevicePermissions) {
deviceItems.computeIfAbsent(devicePermission.getOwnerId(), key -> new HashSet<>()).add(devicePermission.getPropertyId());
deviceItemsWithGroups.computeIfAbsent(devicePermission.getOwnerId(), key -> new HashSet<>()).add(devicePermission.getPropertyId());
}
for (Device device : Context.getDeviceManager().getAllDevices()) {
long groupId = device.getGroupId();
while (groupId > 0) {
deviceItemsWithGroups.computeIfAbsent(device.getId(), key -> new HashSet<>()).addAll(groupItems.getOrDefault(groupId, new HashSet<>()));
Group group = Context.getGroupsManager().getById(groupId);
groupId = group != null ? group.getGroupId() : 0;
}
}
} catch (StorageException | ClassNotFoundException error) {
LOGGER.warn("Refresh permissions error", error);
} finally {
writeUnlock();
}
}
}
Aggregations