📄 boxstate.java
字号:
} catch (PostAlertException exp) { Log.w("HANDLE", "PostAlertException: " + exp.getMessage()); } return; } checkScheduleCallback(ddb); Log.d("HANDLE", "Finished handling event, queue size="+deviceQ.size() + " callback: " + ddb.isScheduledCallback(this)); } private void checkScheduleCallback(DeviceDB ddb) { if (!isEmptyQ()) { long wait = waitTimeQ(); if (wait <= 0) wait = 1; Log.d("HANDLE", "Scheduling callback in " + wait + " ms ("+ (wait/1000)+" s)"); ddb.scheduleCallback(this, wait, 1); } else { ddb.cancelCallback(this); } } public void callback(DeviceDB ddb, int invocationsRemaining) { Log.setDefaultSubsystem("BOX_STATE_EVENTHANDLER"); if (isEmptyQ()) return; SendAlertDescr sad; int processCnt = 0; while ((sad = removeHeadQ()) != null) { Box b = null; if (sad.device instanceof Box) { b = (Box) sad.device; } if (b != null && !b.isUp()) { Log.d("CALLBACK", "Box down: " + b.getSysname()); // The box iself is down, this means we don't report modules down if any // Find the down event Event e = sad.event; if (e == null) { Log.w("CALLBACK", "Box " + b.getSysname() + " is down, but no start event found!"); continue; } // Ask the Box to update its status b.updateStatus(); // Create alert Alert a = ddb.alertFactory(e); // Set status (down or shadow) a.addVar("status", b.getStatusS()); // Update alerttype String alerttype = ""; if (b.getStatus() == Box.STATUS_SHADOW) { alerttype = "boxShadow"; a.setSeverity(Math.max(e.getSeverity()-SHADOW_SEVERITY_DEDUCTION,0)); } else if (b.getStatus() == Box.STATUS_DOWN) { alerttype = "boxDown"; } // Update varMap from database try { ResultSet rs = Database.query("SELECT * FROM module WHERE deviceid = " + e.getDeviceid()); ResultSetMetaData rsmd = rs.getMetaData(); if (rs.next()) { HashMap hm = Database.getHashFromResultSet(rs, rsmd); a.addVars(hm); } } catch (SQLException exp) { Log.w("BOX_STATE_EVENTHANDLER", "SQLException when fetching data from module("+e.getDeviceid()+"): " + exp.getMessage()); } // First send a warning if (!sad.sentWarning) { a.setState(Event.STATE_NONE); alerttype += "Warning"; // Schedule the real down event sad.sentWarning = true; addToQ(sad, sad.alertWait); } else { // Delete 'down' event when alert is posted a.addEvent(e); } a.setAlerttype(alerttype); Log.d("BOX_STATE_EVENTHANDLER", "CALLBACK", "Added alert: " + a); if (b.onMaintenance()) { // Do not post to alertq if box is on maintenace Log.d("HANDLE", "Not posting " + alerttype + " alert to alertq as the box is on maintenance"); a.setPostAlertq(false); } // Post the alert try { ddb.postAlert(a); } catch (PostAlertException exp) { Log.w("BOX_STATE_EVENTHANDLER", "CALLBACK", "While posting netel down alert, PostAlertException: " + exp.getMessage()); } } else { if (sad.device instanceof Module) { Module m = (Module)sad.device; b = (Box) m.getParent(); if (!b.isUp()) { // Ignore moduleDown when box is down Log.d("CALLBACK", "Ignoring module down (" + m.getModule() + "), as the box is down (" + b.getSysname() +")"); continue; } else { Log.d("CALLBACK", "Module down on: " + b.getSysname() + ", " + m.getModule()); } // Find the down event Event e = sad.event; if (e == null) { Log.w("BOX_STATE_EVENTHANDLER", "CALLBACK", m + " ("+m.getDeviceid()+") is down, but no start event found!"); continue; } // Create alert Alert a = ddb.alertFactory(e); // First send a warning String alerttype = "moduleDown"; if (!sad.sentWarning) { a.setState(Event.STATE_NONE); alerttype += "Warning"; // Schedule the real down event sad.sentWarning = true; addToQ(sad, sad.alertWait); } else { // Delete 'down' event when alert is posted a.addEvent(e); } a.setAlerttype(alerttype); Log.d("BOX_STATE_EVENTHANDLER", "CALLBACK", "Added moduleDown alert: " + a); if (b.onMaintenance()) { // Do not post to alertq if box is on maintenace, only register in alerthist Log.d("HANDLE", "Not posting moduleDown alert to alertq as the box is on maintenance"); a.setPostAlertq(false); } // Post the alert try { ddb.postAlert(a); } catch (PostAlertException exp) { Log.w("BOX_STATE_EVENTHANDLER", "CALLBACK", "While posting module down alert, PostAlertException: " + exp.getMessage()); } } } processCnt++; } checkScheduleCallback(ddb); Log.d("BOX_STATE_EVENTHANDLER", "CALLBACK", "Alert processing done, processed " + processCnt + ", queue size " + deviceQ.size()); } // Queue handling code private SortedMap deviceQ = new TreeMap(); private Map qMap = new HashMap(); private class SendAlertDescr { public Device device; public String eventtype; public boolean sentWarning; public long alertWait; public Event event; public SendAlertDescr(Device d, String type) { this(d, type, 0, null); } public SendAlertDescr(Device d, String type, long wait, Event e) { device = d; eventtype = type; sentWarning = false; alertWait = wait; event = e; } public boolean equals(Object o) { if (o instanceof SendAlertDescr) { SendAlertDescr sad = (SendAlertDescr)o; return device.getDeviceid() == sad.device.getDeviceid() && eventtype.equals(sad.eventtype); } return false; } public String toString() { return "Dev: " + device.getDeviceid() + " type: " + eventtype + " w: " + sentWarning + " alertWait: " + alertWait; } } private boolean isEmptyQ() { return deviceQ.isEmpty(); } private long waitTimeQ() { if (deviceQ.isEmpty()) return Long.MAX_VALUE; Long t = (Long) deviceQ.firstKey(); return t.longValue() - System.currentTimeMillis(); } private void addToQ(SendAlertDescr sad, long time) { List l; Long t = new Long(System.currentTimeMillis() + time * 1000); if ( (l=(List)deviceQ.get(t)) == null) deviceQ.put(t, l = new ArrayList()); l.add(sad); qMap.put(sad.device.getDeviceidI()+":"+sad.eventtype, t); } private SendAlertDescr removeFromQ(Device d, String eventtype) { Long t = (Long) qMap.remove(d.getDeviceidI()+":"+eventtype); if (t == null) return null; List l = (List) deviceQ.get(t); int idx = l.indexOf(new SendAlertDescr(d, eventtype)); SendAlertDescr sad = (SendAlertDescr) l.get(idx); l.remove(idx); if (l.isEmpty()) deviceQ.remove(t); return sad; } private SendAlertDescr removeHeadQ() { if (deviceQ.isEmpty()) return null; Long t = (Long) deviceQ.firstKey(); if (t.longValue() > System.currentTimeMillis()) return null; List l = (List) deviceQ.get(t); SendAlertDescr sad = (SendAlertDescr) l.remove(l.size()-1); if (l.isEmpty()) deviceQ.remove(t); qMap.remove(sad.device.getDeviceidI()+":"+sad.eventtype); return sad; } private void addToQ(Device d, String eventtype, long warningTime, long alertTime, Event e) { addToQ(new SendAlertDescr(d, eventtype, alertTime, e), warningTime); } private boolean isInQ(Device device, String eventtype) { return qMap.containsKey(device.getDeviceidI() + ":" + eventtype); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -