Search in sources :

Example 1 with EventManagerListenerDefinition

use of com.serotonin.m2m2.module.EventManagerListenerDefinition 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 EventManagerListenerDefinition

use of com.serotonin.m2m2.module.EventManagerListenerDefinition in project ma-core-public by infiniteautomation.

the class MockMangoLifecycle method initialize.

/**
 * Startup a dummy Mango with a basic infrastructure
 */
public void initialize() {
    Common.MA_HOME = System.getProperty("ma.home");
    if (Common.MA_HOME == null)
        Common.MA_HOME = ".";
    // Add in modules
    for (Module module : modules) ModuleRegistry.addModule(module);
    Providers.add(IMangoLifecycle.class, this);
    // TODO Licensing Providers.add(ICoreLicense.class, new CoreLicenseDefinition());
    // TODO Licensing Providers.add(ITimedLicenseRegistrar.class, new TimedLicenseRegistrar());
    Common.free = false;
    // Startup a simulation timer provider
    Providers.add(TimerProvider.class, getSimulationTimerProvider());
    // Make sure that Common and other classes are properly loaded
    Common.envProps = getEnvProps();
    MangoRestSpringConfiguration.initializeObjectMapper();
    Common.JSON_CONTEXT.addResolver(new EventTypeResolver(), EventType.class);
    Common.JSON_CONTEXT.addResolver(new BaseChartRenderer.Resolver(), ChartRenderer.class);
    Common.JSON_CONTEXT.addResolver(new BaseTextRenderer.Resolver(), TextRenderer.class);
    Common.JSON_CONTEXT.addResolver(new EmailRecipientResolver(), EmailRecipient.class);
    Common.JSON_CONTEXT.addResolver(new VirtualSerialPortConfigResolver(), VirtualSerialPortConfig.class);
    Common.JSON_CONTEXT.addConverter(new MapWrapConverter(), MapWrap.class);
    for (Module module : ModuleRegistry.getModules()) {
        module.preInitialize(true, false);
    }
    // Start the Database so we can use Daos (Base Dao requires this)
    if (Common.databaseProxy == null) {
        Common.databaseProxy = getDatabaseProxy();
        Common.databaseProxy.initialize(null);
    }
    // Ensure we start with the proper timer
    Common.backgroundProcessing = getBackgroundProcessing();
    Common.backgroundProcessing.initialize(false);
    for (Module module : ModuleRegistry.getModules()) {
        module.postDatabase(true, false);
    }
    // Utilities
    // So we can add users etc.
    EventType.initialize();
    SystemEventType.initialize();
    AuditEventType.initialize();
    // Setup Common Object Mapper
    Common.objectMapper = new CommonObjectMapper();
    // Do this last as Event Types have listeners
    for (SystemSettingsListenerDefinition def : ModuleRegistry.getSystemSettingListenerDefinitions()) def.registerListener();
    // Event Manager
    Common.eventManager = getEventManager();
    Common.eventManager.initialize(false);
    for (EventManagerListenerDefinition def : ModuleRegistry.getDefinitions(EventManagerListenerDefinition.class)) Common.eventManager.addListener(def);
    Common.runtimeManager = getRuntimeManager();
    Common.runtimeManager.initialize(false);
    if (Common.serialPortManager == null)
        Common.serialPortManager = getSerialPortManager();
    for (Module module : ModuleRegistry.getModules()) {
        module.postInitialize(true, false);
    }
    for (Runnable task : STARTUP_TASKS) {
        try {
            task.run();
        } catch (Exception e) {
            fail(e.getMessage());
        }
    }
}
Also used : BaseTextRenderer(com.serotonin.m2m2.view.text.BaseTextRenderer) BaseChartRenderer(com.serotonin.m2m2.view.chart.BaseChartRenderer) SystemSettingsListenerDefinition(com.serotonin.m2m2.module.SystemSettingsListenerDefinition) MapWrapConverter(com.serotonin.m2m2.util.MapWrapConverter) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) EmailRecipientResolver(com.serotonin.m2m2.vo.mailingList.EmailRecipientResolver) VirtualSerialPortConfigResolver(com.infiniteautomation.mango.io.serial.virtual.VirtualSerialPortConfigResolver) CommonObjectMapper(com.infiniteautomation.mango.util.CommonObjectMapper) Module(com.serotonin.m2m2.module.Module) EventManagerListenerDefinition(com.serotonin.m2m2.module.EventManagerListenerDefinition) EventTypeResolver(com.serotonin.m2m2.rt.event.type.EventTypeResolver)

Aggregations

EventManagerListenerDefinition (com.serotonin.m2m2.module.EventManagerListenerDefinition)2 VirtualSerialPortConfigResolver (com.infiniteautomation.mango.io.serial.virtual.VirtualSerialPortConfigResolver)1 CommonObjectMapper (com.infiniteautomation.mango.util.CommonObjectMapper)1 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 Module (com.serotonin.m2m2.module.Module)1 SystemSettingsListenerDefinition (com.serotonin.m2m2.module.SystemSettingsListenerDefinition)1 EventInstance (com.serotonin.m2m2.rt.event.EventInstance)1 UserEventListener (com.serotonin.m2m2.rt.event.UserEventListener)1 EventTypeResolver (com.serotonin.m2m2.rt.event.type.EventTypeResolver)1 SystemEventType (com.serotonin.m2m2.rt.event.type.SystemEventType)1 MapWrapConverter (com.serotonin.m2m2.util.MapWrapConverter)1 BaseChartRenderer (com.serotonin.m2m2.view.chart.BaseChartRenderer)1 BaseTextRenderer (com.serotonin.m2m2.view.text.BaseTextRenderer)1 User (com.serotonin.m2m2.vo.User)1 EmailRecipientResolver (com.serotonin.m2m2.vo.mailingList.EmailRecipientResolver)1 MailingList (com.serotonin.m2m2.vo.mailingList.MailingList)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1