Search in sources :

Example 1 with BulkAlarmStateChangeData

use of org.netxms.client.events.BulkAlarmStateChangeData in project netxms by netxms.

the class AlarmNotifier method init.

/**
 * Initialize alarm notifier
 */
public static void init(NXCSession session) {
    AlarmNotifier.session = session;
    ps = Activator.getDefault().getPreferenceStore();
    workspaceUrl = Platform.getInstanceLocation().getURL();
    checkSounds();
    lastReminderTime = System.currentTimeMillis();
    try {
        Map<Long, Alarm> alarms = session.getAlarms();
        for (Alarm a : alarms.values()) {
            alarmStates.put(a.getId(), a.getState());
            if (a.getState() == Alarm.STATE_OUTSTANDING)
                outstandingAlarms++;
        }
    } catch (Exception e) {
    }
    listener = new SessionListener() {

        @Override
        public void notificationHandler(SessionNotification n) {
            if ((n.getCode() == SessionNotification.NEW_ALARM) || (n.getCode() == SessionNotification.ALARM_CHANGED)) {
                processNewAlarm((Alarm) n.getObject());
            } else if ((n.getCode() == SessionNotification.ALARM_TERMINATED) || (n.getCode() == SessionNotification.ALARM_DELETED)) {
                Alarm a = (Alarm) n.getObject();
                Integer state = alarmStates.get(a.getId());
                if (state != null) {
                    if (state == Alarm.STATE_OUTSTANDING)
                        outstandingAlarms--;
                    alarmStates.remove(a.getId());
                }
            } else if (n.getCode() == SessionNotification.MULTIPLE_ALARMS_RESOLVED) {
                BulkAlarmStateChangeData d = (BulkAlarmStateChangeData) n.getObject();
                for (Long id : d.getAlarms()) {
                    Integer state = alarmStates.get(id);
                    if (state != null) {
                        if (state == Alarm.STATE_OUTSTANDING) {
                            outstandingAlarms--;
                        }
                    }
                    alarmStates.put(id, Alarm.STATE_RESOLVED);
                }
            } else if (n.getCode() == SessionNotification.MULTIPLE_ALARMS_TERMINATED) {
                BulkAlarmStateChangeData d = (BulkAlarmStateChangeData) n.getObject();
                for (Long id : d.getAlarms()) {
                    Integer state = alarmStates.get(id);
                    if (state != null) {
                        if (state == Alarm.STATE_OUTSTANDING)
                            outstandingAlarms--;
                        alarmStates.remove(id);
                    }
                }
            }
        }
    };
    session.addListener(listener);
    Thread reminderThread = new Thread(new Runnable() {

        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                }
                IPreferenceStore ps = Activator.getDefault().getPreferenceStore();
                long currTime = System.currentTimeMillis();
                if (// $NON-NLS-1$
                ps.getBoolean("OUTSTANDING_ALARMS_REMINDER") && (outstandingAlarms > 0) && (lastReminderTime + 300000 <= currTime)) {
                    Display.getDefault().syncExec(new Runnable() {

                        @Override
                        public void run() {
                            soundQueue.offer(SEVERITY_TEXT[SEVERITY_TEXT.length - 1]);
                            AlarmReminderDialog dlg = new AlarmReminderDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
                            dlg.open();
                        }
                    });
                    lastReminderTime = currTime;
                }
            }
        }
    }, // $NON-NLS-1$
    "AlarmReminderThread");
    reminderThread.setDaemon(true);
    reminderThread.start();
    Thread playerThread = new Thread(new Runnable() {

        @Override
        public void run() {
            while (true) {
                String soundId;
                try {
                    soundId = soundQueue.take();
                } catch (InterruptedException e) {
                    continue;
                }
                Clip sound = null;
                try {
                    String fileName = getSoundAndDownloadIfRequired(soundId);
                    if (fileName != null) {
                        sound = (Clip) AudioSystem.getLine(new Line.Info(Clip.class));
                        sound.open(AudioSystem.getAudioInputStream(new File(workspaceUrl.getPath(), fileName).getAbsoluteFile()));
                        sound.start();
                        while (!sound.isRunning()) Thread.sleep(10);
                        while (sound.isRunning()) {
                            Thread.sleep(10);
                        }
                    }
                } catch (Exception e) {
                    // $NON-NLS-1$
                    Activator.logError("Exception in alarm sound player", e);
                } finally {
                    if ((sound != null) && sound.isOpen())
                        sound.close();
                }
            }
        }
    }, "AlarmSoundPlayer");
    playerThread.setDaemon(true);
    playerThread.start();
}
Also used : BulkAlarmStateChangeData(org.netxms.client.events.BulkAlarmStateChangeData) StatusDisplayInfo(org.netxms.ui.eclipse.console.resources.StatusDisplayInfo) IOException(java.io.IOException) Clip(javax.sound.sampled.Clip) AlarmReminderDialog(org.netxms.ui.eclipse.alarmviewer.dialogs.AlarmReminderDialog) Alarm(org.netxms.client.events.Alarm) SessionListener(org.netxms.client.SessionListener) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) SessionNotification(org.netxms.client.SessionNotification) File(java.io.File)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 Clip (javax.sound.sampled.Clip)1 IPreferenceStore (org.eclipse.jface.preference.IPreferenceStore)1 SessionListener (org.netxms.client.SessionListener)1 SessionNotification (org.netxms.client.SessionNotification)1 Alarm (org.netxms.client.events.Alarm)1 BulkAlarmStateChangeData (org.netxms.client.events.BulkAlarmStateChangeData)1 AlarmReminderDialog (org.netxms.ui.eclipse.alarmviewer.dialogs.AlarmReminderDialog)1 StatusDisplayInfo (org.netxms.ui.eclipse.console.resources.StatusDisplayInfo)1