Search in sources :

Example 6 with AlarmLevels

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

the class SystemEventType method registerEventType.

private static void registerEventType(String subtype, String key) {
    TYPE_NAMES.addElement(subtype);
    AlarmLevels level = AlarmLevels.fromValue(SystemSettingsDao.getInstance().getIntValue(SYSTEM_SETTINGS_PREFIX + subtype));
    EVENT_TYPES.put(subtype, new EventTypeVO(new SystemEventType(subtype, 0, null), new TranslatableMessage(key), level));
}
Also used : EventTypeVO(com.serotonin.m2m2.vo.event.EventTypeVO) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) AlarmLevels(com.serotonin.m2m2.rt.event.AlarmLevels)

Example 7 with AlarmLevels

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

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 8 with AlarmLevels

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

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 9 with AlarmLevels

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

the class PublisherVO 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");
    // Legacy conversion for publishType
    if (jsonObject.containsKey("publishType")) {
        String publishTypeCode = jsonObject.getString("publishType");
        int publishTypeId = PUBLISH_TYPE_CODES.getId(publishTypeCode);
        if (publishTypeId == -1)
            throw new TranslatableJsonException("emport.error.invalid", "publishType", publishTypeCode, PUBLISH_TYPE_CODES.getCodeList());
        publishType = publishTypeId;
    } else if (jsonObject.containsKey("changesOnly")) {
        boolean changesOnly = getBoolean(jsonObject, "changesOnly");
        if (changesOnly) {
            this.publishType = PublishType.CHANGES_ONLY;
        } else {
            this.publishType = PublishType.ALL;
        }
    }
    String text = jsonObject.getString("snapshotSendPeriodType");
    if (text != null) {
        snapshotSendPeriodType = Common.TIME_PERIOD_CODES.getId(text);
        if (snapshotSendPeriodType == -1)
            throw new TranslatableJsonException("emport.error.invalid", "snapshotSendPeriodType", text, Common.TIME_PERIOD_CODES.getCodeList());
    }
    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());
                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()));
                }
            }
        }
    }
}
Also used : ExportCodes(com.serotonin.m2m2.util.ExportCodes) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonObject(com.serotonin.json.type.JsonObject)

Example 10 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={}, message={}, time={}", type, message.translate(Common.getTranslations()), new Date(time));
        }
    }
}
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)

Aggregations

AlarmLevels (com.serotonin.m2m2.rt.event.AlarmLevels)19 ExportCodes (com.serotonin.m2m2.util.ExportCodes)14 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)11 ArrayList (java.util.ArrayList)11 EventInstance (com.serotonin.m2m2.rt.event.EventInstance)9 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)8 UserEventLevelSummary (com.serotonin.m2m2.rt.event.UserEventLevelSummary)7 HashMap (java.util.HashMap)7 EventTypeVO (com.serotonin.m2m2.vo.event.EventTypeVO)6 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)5 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)5 User (com.serotonin.m2m2.vo.User)5 ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)4 SystemEventType (com.serotonin.m2m2.rt.event.type.SystemEventType)4 EnumMap (java.util.EnumMap)4 Test (org.junit.Test)4 JsonObject (com.serotonin.json.type.JsonObject)3 UserEventListener (com.serotonin.m2m2.rt.event.UserEventListener)3 Collections (java.util.Collections)3 HashSet (java.util.HashSet)3