use of org.netxms.client.events.Alarm in project netxms by netxms.
the class AlarmListAdapter method getView.
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getView(int, android.view.View,
* android.view.ViewGroup)
*/
@SuppressLint("RtlHardcoded")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView message, source, date;
LinearLayout view, texts, info, icons;
ImageView severity, state;
if (// new alarm, create fields
convertView == null) {
severity = new ImageView(context);
severity.setPadding(5, 5, 5, 2);
state = new ImageView(context);
state.setPadding(5, 5, 5, 2);
icons = new LinearLayout(context);
icons.setOrientation(LinearLayout.VERTICAL);
icons.addView(severity);
icons.addView(state);
source = new TextView(context);
source.setPadding(5, 2, 5, 2);
source.setTextColor(r.getColor(R.color.text_color));
date = new TextView(context);
date.setPadding(5, 2, 5, 2);
date.setTextColor(r.getColor(R.color.text_color));
date.setGravity(Gravity.RIGHT);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.RIGHT;
date.setLayoutParams(lp);
message = new TextView(context);
message.setPadding(5, 2, 5, 2);
message.setTextColor(r.getColor(R.color.text_color));
info = new LinearLayout(context);
info.setOrientation(LinearLayout.HORIZONTAL);
info.addView(source);
info.addView(date);
texts = new LinearLayout(context);
texts.setOrientation(LinearLayout.VERTICAL);
lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
texts.setLayoutParams(lp);
texts.addView(info);
texts.addView(message);
view = new CheckableLinearLayout(context);
view.addView(icons);
view.addView(texts);
} else {
// get reference to existing alarm
view = (CheckableLinearLayout) convertView;
icons = (LinearLayout) view.getChildAt(1);
severity = (ImageView) icons.getChildAt(0);
state = (ImageView) icons.getChildAt(1);
texts = (LinearLayout) view.getChildAt(2);
info = (LinearLayout) texts.getChildAt(0);
source = (TextView) info.getChildAt(0);
date = (TextView) info.getChildAt(1);
message = (TextView) texts.getChildAt(1);
}
// get node name
Alarm alarm = alarms.get(position);
source.setText(getObjectName(alarm.getSourceObjectId()));
date.setText(DateFormat.getDateTimeInstance().format(alarm.getLastChangeTime()));
message.setText(alarm.getMessage());
severity.setImageResource(getAlarmIconSeverity(alarm));
state.setImageResource(getAlarmIconState(alarm));
return view;
}
use of org.netxms.client.events.Alarm 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();
}
use of org.netxms.client.events.Alarm in project netxms by netxms.
the class NXCSession method getAlarm.
/**
* Get information about single active alarm. Terminated alarms cannot be accessed with this call.
*
* @param alarmId alarm ID
* @return alarm object
* @throws IOException if socket I/O error occurs
* @throws NXCException if NetXMS server returns an error or operation was timed out
*/
public Alarm getAlarm(long alarmId) throws IOException, NXCException {
NXCPMessage msg = newMessage(NXCPCodes.CMD_GET_ALARM);
msg.setFieldInt32(NXCPCodes.VID_ALARM_ID, (int) alarmId);
sendMessage(msg);
final NXCPMessage response = waitForRCC(msg.getMessageId());
return new Alarm(response);
}
use of org.netxms.client.events.Alarm in project netxms by netxms.
the class AlarmTest method testGetEvents.
// NOTE: server must have at least 1 active alarm for this test.
public void testGetEvents() throws Exception {
final NXCSession session = connect();
HashMap<Long, Alarm> list = session.getAlarms();
if (list.size() > 0) {
final Long alarmId = list.keySet().iterator().next();
List<EventInfo> events = session.getAlarmEvents(alarmId);
for (EventInfo e : events) {
System.out.println(e.getTimeStamp() + " " + e.getName() + " " + e.getMessage());
}
}
session.disconnect();
}
use of org.netxms.client.events.Alarm in project netxms by netxms.
the class AlarmTest method testAlarmUpdate.
// NOTE: server must have at least 1 active alarm for this test.
// This alarm will be terminated during test.
public void testAlarmUpdate() throws Exception {
final NXCSession session = connect();
HashMap<Long, Alarm> list = session.getAlarms();
if (list.size() > 0) {
final Semaphore s = new Semaphore(0);
final List<Long> alarmIds = new ArrayList<Long>(2);
alarmIds.add(list.keySet().iterator().next());
// Made up alarm ID to check if it is returned
alarmIds.add(123456789L);
final boolean[] success = new boolean[1];
success[0] = false;
session.addListener(new SessionListener() {
public void notificationHandler(SessionNotification n) {
List<Long> termAlarmIds = ((BulkAlarmStateChangeData) n.getObject()).getAlarms();
long termAlarmId = termAlarmIds.get(0);
assertEquals(SessionNotification.MULTIPLE_ALARMS_TERMINATED, n.getCode());
assertEquals(alarmIds.get(0).longValue(), termAlarmId);
success[0] = true;
s.release();
}
});
session.subscribe(NXCSession.CHANNEL_ALARMS);
Map<Long, Integer> terminationFails = session.bulkTerminateAlarms(alarmIds);
assertTrue(terminationFails.get(alarmIds.get(1)) == RCC.INVALID_ALARM_ID);
assertTrue(s.tryAcquire(3, TimeUnit.SECONDS));
assertEquals(true, success[0]);
}
session.disconnect();
}
Aggregations