I would like to retrieve the 10 most recent alarms from system.alarm.querystatus. What should I do?
def getAlarms(self, value, app=None):
states = []
for i in range(len(self.custom.states)):
states.append(self.custom.states[i])
alarms = system.alarm.queryStatus(state=states, priority=["Critical"])#, all_properties=[('App', '=', app)])
data = []
for alarm in alarms:
displayPath = alarm.getDisplayPath()
priority = alarm.getPriority()
priorityOrdinal = alarm.getPriority().ordinal()
state = alarm.getState().ordinal() #0=ClearUnacked, 1=ClearAcked, 2=ActiveUnacked, 3=ActiveAcked
stateOrder = [1,0,3,2].index(state)
id = alarm.getId()
name = alarm.getName()
timestamp = None
timestampMS = None
eventValue = None
active = {}
if alarm.getActiveData() != None:
for prop in alarm.getActiveData().getValues():
active[prop.getProperty().getName()] = prop.getValue()
clear = {}
if alarm.getClearedData() != None:
for prop in alarm.getClearedData().getValues():
clear[prop.getProperty().getName()] = prop.getValue()
stateDict = active if state in [2,3] else clear
if "eventTime" in stateDict:
timestamp = system.date.format(stateDict["eventTime"], "M/d/yy hh:mm:ss a")
timestampMS = stateDict["eventTime"]
if "eventValue" in stateDict:
eventValue = str(stateDict["eventValue"])
ack = {"isAcked":False, "id":id}
if state in [1,3]:
ack["isAcked"] = True
for prop in alarm.getAckData().getValues():
propValue = str(prop.getValue())
if prop.getProperty().getName() == "eventTime":
propValue = system.date.format(prop.getValue(), "M/d/yy hh:mm:ss a")
ack[prop.getProperty().getName()] = propValue
ack = system.util.jsonEncode(ack)
if state == 2:
data.append({"id":id, "displayPath":displayPath, "name":name, "priority":priority, "priorityOrdinal":priorityOrdinal, "state":state, "stateOrder":stateOrder, "timestamp":timestamp, "timestampMS":timestampMS, "value":eventValue, "ack":ack})
data.sort(key=lambda x: (x["stateOrder"], x["priorityOrdinal"], x["timestampMS"]), reverse=True)
return data
instead of all of these "getFunction()" calls you can drop the "get" and access them in a more pythonic way:
e.g.
alarm.getClearedData().getValues() -->
alarm.clearedData.values
active[prop.getProperty().getName()] = prop.getValue() ->
active[prop.property] = prop.value
Basically, just drop the "get" and make it camelCase.
Jython automatically converts JavaBean conventioned Java getters and setters into a more Pythonic style by removing the gets/sets to make them more familiar to Python programmers and also simpler to read and write.
Something I find useful is running this on an obj to see what methods/props it has, and their types. You can also refer to the javadocs:
obj = prop
for d in dir(obj): print d, getattr(obj, d)
You can also replace this above with this below:
states = list(self.custom.states)
1 Like