Search in sources :

Example 1 with EventType

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

the class EventManagerImpl method raiseEvent.

// 
// 
// Basic event management.
// 
/**
 * Raise Event
 * @param type
 * @param time
 * @param rtnApplicable - does this event return to normal?
 * @param alarmLevel
 * @param message
 * @param context
 */
public void raiseEvent(EventType type, long time, boolean rtnApplicable, int alarmLevel, TranslatableMessage message, Map<String, Object> context) {
    if (state != RUNNING)
        return;
    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;
    }
    // Determine if the event should be suppressed.
    TranslatableMessage autoAckMessage = null;
    for (EventManagerListenerDefinition l : listeners) {
        autoAckMessage = l.autoAckEventWithMessage(type);
        if (autoAckMessage != null)
            break;
    }
    EventInstance evt = new EventInstance(type, time, rtnApplicable, alarmLevel, message, context);
    if (autoAckMessage == null)
        setHandlers(evt);
    // Check to see if we are Not Logging these
    if (alarmLevel != AlarmLevels.DO_NOT_LOG) {
        eventDao.saveEvent(evt);
    }
    // Create user alarm records for all applicable users
    List<Integer> eventUserIds = new ArrayList<Integer>();
    Set<String> emailUsers = new HashSet<String>();
    for (User user : userDao.getActiveUsers()) {
        // user should be skipped.
        if (type.excludeUser(user))
            continue;
        if (Permissions.hasEventTypePermission(user, type)) {
            eventUserIds.add(user.getId());
            if (user.getReceiveAlarmEmails() > AlarmLevels.IGNORE && alarmLevel >= user.getReceiveAlarmEmails() && !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(EventType.EventTypeNames.AUDIT))) {
                for (UserEventListener l : this.userEventListeners) {
                    if (l.getUserId() == user.getId()) {
                        Common.backgroundProcessing.addWorkItem(new EventNotifyWorkItem(user, l, evt, true, false, false, false));
                    }
                }
                // Add to the UserEventCache if the user has recently accessed their events
                this.userEventCache.addEvent(user.getId(), evt);
            }
        }
    }
    DateTime now = new DateTime(Common.timer.currentTimeMillis());
    for (MailingList ml : MailingListDao.instance.getAlarmMailingLists(alarmLevel)) {
        ml.appendAddresses(emailUsers, now);
    }
    // No Audit or Do Not Log events are User Events
    if ((eventUserIds.size() > 0) && (alarmLevel != AlarmLevels.DO_NOT_LOG) && (!evt.getEventType().getEventType().equals(EventType.EventTypeNames.AUDIT))) {
        eventDao.insertUserEvents(evt.getId(), eventUserIds, true);
        if (autoAckMessage == null)
            lastAlarmTimestamp = Common.timer.currentTimeMillis();
    }
    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(EventType.EventTypeNames.AUDIT)))
        this.acknowledgeEvent(evt, time, null, autoAckMessage);
    else {
        if (evt.isRtnApplicable()) {
            if (alarmLevel > highestActiveAlarmLevel) {
                int oldValue = highestActiveAlarmLevel;
                highestActiveAlarmLevel = alarmLevel;
                SystemEventType.raiseEvent(new SystemEventType(SystemEventType.TYPE_MAX_ALARM_LEVEL_CHANGED), time, false, getAlarmLevelChangeMessage("event.alarmMaxIncreased", oldValue));
            }
        }
        // Call raiseEvent handlers.
        handleRaiseEvent(evt, emailUsers);
        if (log.isDebugEnabled())
            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) MailingList(com.serotonin.m2m2.vo.mailingList.MailingList) DateTime(org.joda.time.DateTime) UserEventListener(com.serotonin.m2m2.rt.event.UserEventListener) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) EventManagerListenerDefinition(com.serotonin.m2m2.module.EventManagerListenerDefinition) HashSet(java.util.HashSet)

Example 2 with EventType

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

the class EventHandlersDwr method saveProcessEventHandler.

@DwrPermission(user = true)
public ProcessResult saveProcessEventHandler(String eventType, String eventSubtype, int eventTypeRef1, int eventTypeRef2, int handlerId, String xid, String alias, boolean disabled, String activeProcessCommand, int activeProcessTimeout, String inactiveProcessCommand, int inactiveProcessTimeout) {
    ProcessEventHandlerVO handler = new ProcessEventHandlerVO();
    handler.setDefinition(ModuleRegistry.getEventHandlerDefinition(ProcessEventHandlerDefinition.TYPE_NAME));
    handler.setActiveProcessCommand(activeProcessCommand);
    handler.setActiveProcessTimeout(activeProcessTimeout);
    handler.setInactiveProcessCommand(inactiveProcessCommand);
    handler.setInactiveProcessTimeout(inactiveProcessTimeout);
    return save(eventType, eventSubtype, eventTypeRef1, eventTypeRef2, handler, handlerId, xid, alias, disabled);
}
Also used : ProcessEventHandlerVO(com.serotonin.m2m2.vo.event.ProcessEventHandlerVO) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 3 with EventType

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

the class EventHandlerImporter method importImpl.

@Override
protected void importImpl() {
    String xid = json.getString("xid");
    if (StringUtils.isBlank(xid))
        xid = ctx.getEventHandlerDao().generateUniqueXid();
    AbstractEventHandlerVO<?> handler = ctx.getEventHandlerDao().getEventHandler(xid);
    if (handler == null) {
        String typeStr = json.getString("handlerType");
        if (StringUtils.isBlank(typeStr))
            addFailureMessage("emport.eventHandler.missingType", xid, ModuleRegistry.getEventHandlerDefinitionTypes());
        else {
            EventHandlerDefinition<?> def = ModuleRegistry.getEventHandlerDefinition(typeStr);
            if (def == null)
                addFailureMessage("emport.eventHandler.invalidType", xid, typeStr, ModuleRegistry.getEventHandlerDefinitionTypes());
            else {
                handler = def.baseCreateEventHandlerVO();
                handler.setXid(xid);
            }
        }
    }
    JsonObject et = json.getJsonObject("eventType");
    JsonArray ets = json.getJsonArray("eventTypes");
    EventType eventType = null;
    List<EventType> eventTypes = null;
    try {
        // Find the event type.
        if (et != null)
            eventType = ctx.getReader().read(EventType.class, et);
        else if (ets != null) {
            eventTypes = new ArrayList<EventType>(ets.size());
            Iterator<JsonValue> iter = ets.iterator();
            while (iter.hasNext()) eventTypes.add(ctx.getReader().read(EventType.class, iter.next()));
        }
        ctx.getReader().readInto(handler, json);
        // Now validate it. Use a new response object so we can distinguish errors in this vo from other errors.
        ProcessResult voResponse = new ProcessResult();
        handler.validate(voResponse);
        if (voResponse.getHasMessages())
            setValidationMessages(voResponse, "emport.eventHandler.prefix", xid);
        else {
            // Sweet.
            boolean isnew = handler.getId() == Common.NEW_ID;
            // Save it.
            if (et != null)
                ctx.getEventHandlerDao().saveEventHandler(eventType, handler);
            else
                ctx.getEventHandlerDao().saveEventHandler(handler);
            if (eventTypes != null)
                for (EventType type : eventTypes) ctx.getEventHandlerDao().addEventHandlerMappingIfMissing(handler.getId(), type);
            addSuccessMessage(isnew, "emport.eventHandler.prefix", xid);
        }
    } catch (TranslatableJsonException e) {
        addFailureMessage("emport.eventHandler.prefix", xid, e.getMsg());
    } catch (JsonException e) {
        addFailureMessage("emport.eventHandler.prefix", xid, getJsonExceptionMessage(e));
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) EventType(com.serotonin.m2m2.rt.event.type.EventType) ArrayList(java.util.ArrayList) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) JsonObject(com.serotonin.json.type.JsonObject) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonArray(com.serotonin.json.type.JsonArray) Iterator(java.util.Iterator)

Example 4 with EventType

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

the class EventHandlerDao method saveEventHandler.

private AbstractEventHandlerVO<?> saveEventHandler(final String typeName, final String subtypeName, final int typeRef1, final int typeRef2, final AbstractEventHandlerVO<?> handler) {
    getTransactionTemplate().execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            if (handler.getId() == Common.NEW_ID)
                insertEventHandler(typeName, subtypeName, typeRef1, typeRef2, handler);
            else
                updateEventHandler(handler);
            if (handler.getAddedEventTypes() != null)
                for (EventType et : handler.getAddedEventTypes()) EventHandlerDao.instance.addEventHandlerMappingIfMissing(handler.getId(), et);
        }
    });
    return getEventHandler(handler.getId());
}
Also used : AuditEventType(com.serotonin.m2m2.rt.event.type.AuditEventType) EventType(com.serotonin.m2m2.rt.event.type.EventType) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Example 5 with EventType

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

the class EventInstanceDao method createEventType.

static EventType createEventType(ResultSet rs, int offset) throws SQLException {
    String typeName = rs.getString(offset);
    String subtypeName = rs.getString(offset + 1);
    EventType type;
    if (typeName.equals(EventType.EventTypeNames.DATA_POINT))
        type = new DataPointEventType(rs.getInt(offset + 2), rs.getInt(offset + 3));
    else if (typeName.equals(EventType.EventTypeNames.DATA_SOURCE))
        type = new DataSourceEventType(rs.getInt(offset + 2), rs.getInt(offset + 3));
    else if (typeName.equals(EventType.EventTypeNames.SYSTEM))
        type = new SystemEventType(subtypeName, rs.getInt(offset + 2));
    else if (typeName.equals(EventType.EventTypeNames.PUBLISHER))
        type = new PublisherEventType(rs.getInt(offset + 2), rs.getInt(offset + 3));
    else if (typeName.equals(EventType.EventTypeNames.AUDIT))
        throw new ShouldNeverHappenException("AUDIT events should not exist here. Consider running the SQL: DELETE FROM events WHERE typeName='AUDIT';");
    else {
        EventTypeDefinition def = ModuleRegistry.getEventTypeDefinition(typeName);
        if (def == null) {
            // Create Missing Event Type
            type = new MissingEventType(typeName, null, rs.getInt(offset + 2), rs.getInt(offset + 3));
        } else {
            type = def.createEventType(subtypeName, rs.getInt(offset + 2), rs.getInt(offset + 3));
            if (type == null) {
                // Create Missing Event type
                type = new MissingEventType(typeName, subtypeName, rs.getInt(offset + 2), rs.getInt(offset + 3));
            }
        }
    }
    return type;
}
Also used : DataSourceEventType(com.serotonin.m2m2.rt.event.type.DataSourceEventType) SystemEventType(com.serotonin.m2m2.rt.event.type.SystemEventType) MissingEventType(com.serotonin.m2m2.rt.event.type.MissingEventType) DataSourceEventType(com.serotonin.m2m2.rt.event.type.DataSourceEventType) EventType(com.serotonin.m2m2.rt.event.type.EventType) SystemEventType(com.serotonin.m2m2.rt.event.type.SystemEventType) DataPointEventType(com.serotonin.m2m2.rt.event.type.DataPointEventType) PublisherEventType(com.serotonin.m2m2.rt.event.type.PublisherEventType) MissingEventType(com.serotonin.m2m2.rt.event.type.MissingEventType) PublisherEventType(com.serotonin.m2m2.rt.event.type.PublisherEventType) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) DataPointEventType(com.serotonin.m2m2.rt.event.type.DataPointEventType) EventTypeDefinition(com.serotonin.m2m2.module.EventTypeDefinition)

Aggregations

EventType (com.serotonin.m2m2.rt.event.type.EventType)10 SystemEventType (com.serotonin.m2m2.rt.event.type.SystemEventType)9 AuditEventType (com.serotonin.m2m2.rt.event.type.AuditEventType)7 User (com.serotonin.m2m2.vo.User)6 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)6 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)4 EventInstance (com.serotonin.m2m2.rt.event.EventInstance)4 ArrayList (java.util.ArrayList)4 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)3 DataPointEventType (com.serotonin.m2m2.rt.event.type.DataPointEventType)3 DataSourceEventType (com.serotonin.m2m2.rt.event.type.DataSourceEventType)3 MissingEventType (com.serotonin.m2m2.rt.event.type.MissingEventType)3 PublisherEventType (com.serotonin.m2m2.rt.event.type.PublisherEventType)3 EventTypeVO (com.serotonin.m2m2.vo.event.EventTypeVO)3 EventTypeDefinition (com.serotonin.m2m2.module.EventTypeDefinition)2 UserEventListener (com.serotonin.m2m2.rt.event.UserEventListener)2 LongPair (com.serotonin.m2m2.vo.pair.LongPair)2 DateTime (org.joda.time.DateTime)2 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)1 JsonException (com.serotonin.json.JsonException)1