Search in sources :

Example 1 with AlarmLevels

use of com.serotonin.m2m2.rt.event.AlarmLevels in project ma-core-public by infiniteautomation.

the class MailingListService method getAlarmAddresses.

/**
 *  Get any addresses for mailing lists that are mailed on alarm level up to and including 'alarmLevel'
 *
 * @param time of gathering addresses used to determine if a list is inactive
 * @param types for types of entries to return
 */
public Set<String> getAlarmAddresses(AlarmLevels alarmLevel, long time, RecipientListEntryType... types) {
    PermissionHolder user = Common.getUser();
    this.permissionService.ensureAdminRole(user);
    List<MailingList> result = new ArrayList<>();
    // TODO Mango 4.0 this is only weakly consistent
    cache.asMap().forEach((id, ml) -> {
        if (ml.getReceiveAlarmEmails().value() >= 0 && ml.getReceiveAlarmEmails().value() <= alarmLevel.value()) {
            result.add(ml);
        }
    });
    Set<String> addresses = new HashSet<>();
    for (MailingList list : result) {
        addresses.addAll(getActiveRecipients(list.getEntries(), time, types));
    }
    return addresses;
}
Also used : MailingList(com.serotonin.m2m2.vo.mailingList.MailingList) ArrayList(java.util.ArrayList) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) HashSet(java.util.HashSet)

Example 2 with AlarmLevels

use of com.serotonin.m2m2.rt.event.AlarmLevels in project ma-core-public by infiniteautomation.

the class EventInstanceService method getActiveSummary.

/**
 * Get the active summary of events for a user
 */
public List<UserEventLevelSummary> getActiveSummary() throws PermissionException {
    PermissionHolder user = Common.getUser();
    this.permissionService.ensurePermission(user, eventsViewPermission.getPermission());
    Map<AlarmLevels, UserEventLevelSummary> summaries = new EnumMap<>(AlarmLevels.class);
    for (AlarmLevels level : AlarmLevels.values()) {
        if (level == AlarmLevels.IGNORE) {
            continue;
        }
        summaries.put(level, new UserEventLevelSummary(level));
    }
    for (EventInstance event : this.getAllActiveUserEvents()) {
        UserEventLevelSummary summary = summaries.get(event.getAlarmLevel());
        summary.increment(event);
    }
    return new ArrayList<>(summaries.values());
}
Also used : EventInstance(com.serotonin.m2m2.rt.event.EventInstance) ArrayList(java.util.ArrayList) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) UserEventLevelSummary(com.serotonin.m2m2.rt.event.UserEventLevelSummary) EnumMap(java.util.EnumMap) AlarmLevels(com.serotonin.m2m2.rt.event.AlarmLevels)

Example 3 with AlarmLevels

use of com.serotonin.m2m2.rt.event.AlarmLevels in project ma-core-public by infiniteautomation.

the class EventManagerImpl method raiseEvent.

// 
// 
// Basic event management.
// 
/**
 * Raise Event
 * @param rtnApplicable - does this event return to normal?
 */
@Override
public void raiseEvent(EventType type, long time, boolean rtnApplicable, AlarmLevels alarmLevel, TranslatableMessage message, Map<String, Object> context) {
    if (state != ILifecycleState.RUNNING)
        return;
    long nowTimestamp = Common.timer.currentTimeMillis();
    if (time > nowTimestamp) {
        log.warn("Raising event in the future! type=" + type + ", message='" + message.translate(Common.getTranslations()) + "' now=" + new Date(nowTimestamp) + " event=" + new Date(time) + " deltaMs=" + (time - nowTimestamp));
    }
    if (alarmLevel == AlarmLevels.IGNORE)
        return;
    // Check if there is an event for this type already active.
    EventInstance dup = get(type);
    if (dup != null) {
        // Check the duplicate handling.
        boolean discard = canDiscard(type, message);
        if (discard)
            return;
    // Otherwise we just continue...
    } else if (!rtnApplicable) {
        // Check if we've already seen this type recently.
        boolean recent = isRecent(type, message);
        if (recent)
            return;
    }
    EventInstance evt = new EventInstance(type, time, rtnApplicable, alarmLevel, message, context);
    evt.setReadPermission(type.getEventPermission(context, permissionService));
    // Determine if the event should be suppressed (automatically acknowledged and handlers dont run)
    TranslatableMessage autoAckMessage = null;
    for (EventManagerListenerDefinition l : listeners) {
        try {
            autoAckMessage = l.autoAckEventWithMessage(evt);
        } catch (Exception e) {
            log.warn("Error in event manager listener, continuing", e);
        }
        if (autoAckMessage != null)
            break;
    }
    for (EventManagerListenerDefinition l : listeners) {
        try {
            evt = l.modifyEvent(evt);
        } catch (Exception e) {
            log.warn("Error in event manager listener, continuing", e);
        }
        if (evt == null) {
            return;
        }
    }
    loadHandlers(evt);
    // Check to see if we are Not Logging these
    if (alarmLevel != AlarmLevels.DO_NOT_LOG) {
        eventDao.saveEvent(evt);
    }
    // set of email addresses which have been configured to receive events over a certain level
    Set<String> emailUsers = new HashSet<>();
    List<Integer> userIdsToNotify = new ArrayList<>();
    UserEventListener multicaster = userEventMulticaster;
    for (User user : usersService.getEnabledUsers()) {
        // user should be skipped.
        if (type.excludeUser(user))
            continue;
        if (type.hasPermission(user, permissionService)) {
            // add email addresses for users which have been configured to receive events over a certain level
            if (user.getReceiveAlarmEmails().value() > AlarmLevels.IGNORE.value() && alarmLevel.value() >= user.getReceiveAlarmEmails().value() && !StringUtils.isEmpty(user.getEmail()))
                emailUsers.add(user.getEmail());
            // Notify All User Event Listeners of the new event
            if ((alarmLevel != AlarmLevels.DO_NOT_LOG) && (!evt.getEventType().getEventType().equals(EventTypeNames.AUDIT))) {
                userIdsToNotify.add(user.getId());
            }
        }
    }
    if (multicaster != null)
        Common.backgroundProcessing.addWorkItem(new EventNotifyWorkItem(userIdsToNotify, multicaster, evt, true, false, false, false));
    // add email addresses for mailing lists which have been configured to receive events over a certain level
    emailUsers.addAll(mailingListService.getAlarmAddresses(alarmLevel, time, RecipientListEntryType.MAILING_LIST, RecipientListEntryType.ADDRESS, RecipientListEntryType.USER));
    if (evt.isRtnApplicable()) {
        activeEventsLock.writeLock().lock();
        try {
            activeEvents.add(evt);
        } finally {
            activeEventsLock.writeLock().unlock();
        }
    } else if (evt.getEventType().isRateLimited()) {
        recentEventsLock.writeLock().lock();
        try {
            recentEvents.add(evt);
        } finally {
            recentEventsLock.writeLock().unlock();
        }
    }
    if ((autoAckMessage != null) && (alarmLevel != AlarmLevels.DO_NOT_LOG) && (!evt.getEventType().getEventType().equals(EventTypeNames.AUDIT)))
        this.acknowledgeEvent(evt, time, null, autoAckMessage);
    else {
        if (evt.isRtnApplicable()) {
            if (alarmLevel.value() > highestActiveAlarmLevel) {
                int oldValue = highestActiveAlarmLevel;
                highestActiveAlarmLevel = alarmLevel.value();
                SystemEventType.raiseEvent(new SystemEventType(SystemEventType.TYPE_MAX_ALARM_LEVEL_CHANGED), time, false, getAlarmLevelChangeMessage("event.alarmMaxIncreased", oldValue));
            }
        }
        // Call raiseEvent handlers.
        handleRaiseEvent(evt, emailUsers);
        if (log.isTraceEnabled())
            log.trace("Event raised: type=" + type + ", message=" + message.translate(Common.getTranslations()));
    }
}
Also used : EventInstance(com.serotonin.m2m2.rt.event.EventInstance) SystemEventType(com.serotonin.m2m2.rt.event.type.SystemEventType) User(com.serotonin.m2m2.vo.User) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Date(java.util.Date) UserEventListener(com.serotonin.m2m2.rt.event.UserEventListener) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) EventManagerListenerDefinition(com.serotonin.m2m2.module.EventManagerListenerDefinition) HashSet(java.util.HashSet)

Example 4 with AlarmLevels

use of com.serotonin.m2m2.rt.event.AlarmLevels in project ma-core-public by infiniteautomation.

the class DataSourceVO method jsonRead.

@Override
public void jsonRead(JsonReader reader, JsonObject jsonObject) throws JsonException {
    // Not reading XID so can't do this: super.jsonRead(reader, jsonObject);
    if (jsonObject.containsKey("name"))
        name = getString(jsonObject, "name");
    if (jsonObject.containsKey("enabled"))
        enabled = getBoolean(jsonObject, "enabled");
    JsonObject alarmCodeLevels = jsonObject.getJsonObject("alarmLevels");
    if (alarmCodeLevels != null) {
        ExportCodes eventCodes = getEventCodes();
        if (eventCodes != null && eventCodes.size() > 0) {
            for (String code : alarmCodeLevels.keySet()) {
                int eventId = eventCodes.getId(code);
                if (!eventCodes.isValidId(eventId))
                    throw new TranslatableJsonException("emport.error.eventCode", code, eventCodes.getCodeList());
                String text = alarmCodeLevels.getString(code);
                try {
                    setAlarmLevel(eventId, AlarmLevels.fromName(text));
                } catch (IllegalArgumentException | NullPointerException e) {
                    throw new TranslatableJsonException("emport.error.alarmLevel", text, code, Arrays.asList(AlarmLevels.values()));
                }
            }
        }
    }
    String text = jsonObject.getString("purgeType");
    if (text != null) {
        purgeType = Common.TIME_PERIOD_CODES.getId(text);
        if (purgeType == -1)
            throw new TranslatableJsonException("emport.error.invalid", "purgeType", text, Common.TIME_PERIOD_CODES.getCodeList());
    }
}
Also used : ExportCodes(com.serotonin.m2m2.util.ExportCodes) JsonObject(com.serotonin.json.type.JsonObject) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException)

Example 5 with AlarmLevels

use of com.serotonin.m2m2.rt.event.AlarmLevels in project ma-core-public by infiniteautomation.

the class DataSourceVO method setAlarmLevel.

/**
 * Set an alarm level based on the sub-type of the data source event type
 * which MUST (and already is) one of the codes in getEventCodes()
 */
public void setAlarmLevel(String subType, AlarmLevels level) throws ValidationException {
    ExportCodes codes = getEventCodes();
    int eventId = codes.getId(subType);
    if (eventId == -1) {
        ProcessResult result = new ProcessResult();
        result.addContextualMessage("alarmLevel", "emport.error.eventCode", subType, codes.getCodeList());
        throw new ValidationException(result);
    }
    alarmLevels.put(eventId, level);
}
Also used : ExportCodes(com.serotonin.m2m2.util.ExportCodes) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult)

Aggregations

AlarmLevels (com.serotonin.m2m2.rt.event.AlarmLevels)10 ExportCodes (com.serotonin.m2m2.util.ExportCodes)8 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)6 ArrayList (java.util.ArrayList)6 EventInstance (com.serotonin.m2m2.rt.event.EventInstance)5 UserEventLevelSummary (com.serotonin.m2m2.rt.event.UserEventLevelSummary)4 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)4 HashMap (java.util.HashMap)4 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)3 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)3 User (com.serotonin.m2m2.vo.User)3 EventTypeVO (com.serotonin.m2m2.vo.event.EventTypeVO)3 ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)2 JsonObject (com.serotonin.json.type.JsonObject)2 UserEventListener (com.serotonin.m2m2.rt.event.UserEventListener)2 SystemEventType (com.serotonin.m2m2.rt.event.type.SystemEventType)2 Collections (java.util.Collections)2 HashSet (java.util.HashSet)2 JsonGetter (com.fasterxml.jackson.annotation.JsonGetter)1 JsonSetter (com.fasterxml.jackson.annotation.JsonSetter)1