Search in sources :

Example 1 with Notification

use of org.traccar.model.Notification 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 Notification

use of org.traccar.model.Notification in project traccar by traccar.

the class NotificationManager method updateEvent.

public void updateEvent(Event event, Position position) {
    try {
        getDataManager().addObject(event);
    } catch (SQLException error) {
        Log.warning(error);
    }
    if (position != null && geocodeOnRequest && Context.getGeocoder() != null && position.getAddress() == null) {
        position.setAddress(Context.getGeocoder().getAddress(position.getLatitude(), position.getLongitude(), null));
    }
    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() != null && Context.getGeofenceManager().checkItemPermission(userId, event.getGeofenceId())) {
            if (usersToForward != null) {
                usersToForward.add(userId);
            }
            boolean sentWeb = false;
            boolean sentMail = false;
            boolean sentSms = Context.getSmppManager() == null;
            for (long notificationId : getEffectiveNotifications(userId, deviceId, event.getServerTime())) {
                Notification notification = getById(notificationId);
                if (getById(notificationId).getType().equals(event.getType())) {
                    if (!sentWeb && notification.getWeb()) {
                        Context.getConnectionManager().updateEvent(userId, event);
                        sentWeb = true;
                    }
                    if (!sentMail && notification.getMail()) {
                        NotificationMail.sendMailAsync(userId, event, position);
                        sentMail = true;
                    }
                    if (!sentSms && notification.getSms()) {
                        NotificationSms.sendSmsAsync(userId, event, position);
                        sentSms = true;
                    }
                }
                if (sentWeb && sentMail && sentSms) {
                    break;
                }
            }
        }
    }
    if (Context.getEventForwarder() != null) {
        Context.getEventForwarder().forwardEvent(event, position, usersToForward);
    }
}
Also used : SQLException(java.sql.SQLException) Notification(org.traccar.model.Notification)

Aggregations

Notification (org.traccar.model.Notification)2 SQLException (java.sql.SQLException)1 HashSet (java.util.HashSet)1 StorageException (org.traccar.storage.StorageException)1