Search in sources :

Example 1 with StorageException

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);
    }
}
Also used : StorageException(org.traccar.storage.StorageException) Notification(org.traccar.model.Notification) HashSet(java.util.HashSet)

Example 2 with StorageException

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();
    }
}
Also used : Group(org.traccar.model.Group) HashSet(java.util.HashSet) Set(java.util.Set) Device(org.traccar.model.Device) Permission(org.traccar.model.Permission) StorageException(org.traccar.storage.StorageException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with StorageException

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));
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashMap(java.util.HashMap) Form(javax.ws.rs.core.Form) Statistics(org.traccar.model.Statistics) StorageException(org.traccar.storage.StorageException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Date(java.util.Date)

Example 4 with StorageException

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;
    }
}
Also used : Device(org.traccar.model.Device) StorageException(org.traccar.storage.StorageException)

Example 5 with StorageException

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();
        }
    }
}
Also used : Device(org.traccar.model.Device) Logger(org.slf4j.Logger) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) HashSet(java.util.HashSet) StorageException(org.traccar.storage.StorageException) Permission(org.traccar.model.Permission) Map(java.util.Map) Group(org.traccar.model.Group) Context(org.traccar.Context) BaseModel(org.traccar.model.BaseModel) Group(org.traccar.model.Group) Device(org.traccar.model.Device) Permission(org.traccar.model.Permission) StorageException(org.traccar.storage.StorageException) HashSet(java.util.HashSet)

Aggregations

StorageException (org.traccar.storage.StorageException)10 HashSet (java.util.HashSet)4 Device (org.traccar.model.Device)4 Date (java.util.Date)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Set (java.util.Set)3 Permission (org.traccar.model.Permission)3 Position (org.traccar.model.Position)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Context (org.traccar.Context)2 StatisticsManager (org.traccar.database.StatisticsManager)2 Group (org.traccar.model.Group)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Channel (io.netty.channel.Channel)1 Timeout (io.netty.util.Timeout)1 Method (java.lang.reflect.Method)1 SocketAddress (java.net.SocketAddress)1