use of org.netxms.ui.eclipse.alarmviewer.dialogs.AlarmReminderDialog 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();
}
Aggregations