BasicAlarmEvent to PyAlarmEvent

Is there a way to get a PyAlarmEvent from the BasicAlarmEvent returned from system.alarm.queryStatus?

That function returns the BasicAlarmEvent which has several built-in methods to get alarm information but cannot get associated data.

A PyAlarmEvent can access associated data values. How can I get the BasicAlarmEvent to a PyAlarmEvent or how can I get the PyAlarmEvent to begin with?

PyAlarmEvent is basically just retrieving the entire list of properties, then checking if the string you provided matches the string name of a property. You could do this yourself; something like this (untested):

class WrappedAlarmEvent:
	def __init__(self, basicEvent):
		self.event = basicEvent
		self.properties = {prop.name: event.getOrDefault(prop) for prop in event.properties}

	def get(prop):
		return self.properties.get(prop)


### Usage:

alarms = [WrappedAlarmEvent(alarm) for alarm in system.alarm.queryStatus()]

if alarms:
	print alarms[0].get("associatedData")

It errors out saying prop is not defined. I think it’s the prop.name but not for sure. I changed it to for prop in event.properties not in the dictionary and print prop, it says 'java.beans.PropertyChangeEvent' object has no attribute 'properties'

Oh, my bad - typo in the code. Change self.event = event to self.event = basicEvent.

class WrappedAlarmEvent:
	def __init__(self, basicEvent):
		self.event = basicEvent
		
		self.properties = {prop.name: (event.getOrDefault(prop) for prop in event.properties)}

	def get(prop):
		return self.properties.get(prop)

if event.propertyName == "selectedAlarms":
	alarmDatasetInfo = system.dataset.toPyDataSet(event.newValue)
	if alarmDatasetInfo.getRowCount() >= 1:
		for row in alarmDatasetInfo:
			sourcePath = str(row["Source"])
			alarms = [WrappedAlarmEvent(alarm) for alarm in system.alarm.queryStatus(source=[sourcePath])]
			
			if alarms:
				print alarms[0].get("keyPosition")

same error
File "event:propertyChange", line 15, in

File "event:propertyChange", line 5, in init

NameError: global name 'prop' is not defined

Okay, took a bit more doing:

class WrappedAlarmEvent:
	methods = {
		'Clear': 'clearedData',
		'Active': 'activeData',
		'Ack': 'ackData'
	}
	
	def __init__(self, event):
		self.event = event
		self.properties = {prop.getName(): event.getOrDefault(prop) for prop in event.properties}
		self.properties.update({prop.getName(): event.getOrDefault(prop) for prop in getattr(event, WrappedAlarmEvent.methods[str(event.lastEventState)]).properties})

	def get(self, prop):
		return self.properties.get(prop)
		
	def __repr__(self):
		return str(self.properties)

#if event.propertyName == "selectedAlarms":
#	alarmDatasetInfo = system.dataset.toPyDataSet(event.newValue)
#	if alarmDatasetInfo.getRowCount() >= 1:
#		for row in alarmDatasetInfo:
#			sourcePath = str(row["Source"])
alarms = [WrappedAlarmEvent(alarm) for alarm in system.alarm.queryStatus()]

for alarm in alarms:
	print alarm.event.displayPathOrSource
	for prop, value in alarm.properties.items():
		print '\t', prop, '=', value
2 Likes

It keeps telling me prop is not defined. The ‘prop.getName()’ part, prop is not defined yet, so we can use it yet right?

I’m guessing you’re not testing this on 8? Try this instead:

class WrappedAlarmEvent:
	methods = {
		'Clear': 'clearedData',
		'Active': 'activeData',
		'Ack': 'ackData'
	}
	
	def __init__(self, event):
		self.event = event
		self.properties = dict((prop.getName(), event.getOrDefault(prop)) for prop in event.properties)
		self.properties.update(dict((prop.getName(), event.getOrDefault(prop)) for prop in getattr(event, WrappedAlarmEvent.methods[str(event.lastEventState)]).properties))

	def get(self, prop):
		return self.properties.get(prop)
		
	def __repr__(self):
		return str(self.properties)

#if event.propertyName == "selectedAlarms":
#	alarmDatasetInfo = system.dataset.toPyDataSet(event.newValue)
#	if alarmDatasetInfo.getRowCount() >= 1:
#		for row in alarmDatasetInfo:
#			sourcePath = str(row["Source"])
alarms = [WrappedAlarmEvent(alarm) for alarm in system.alarm.queryStatus()]

for alarm in alarms:
	print alarm.event.displayPathOrSource
	for prop, value in alarm.properties.items():
		print '\t', prop, '=', value

Self is not defined error on line 11

Fix the indentation on line 11.

Its intended to be in the WrappedAlarmEvent class, then intended once under the init function.

I moved line 11 to line 10, then hit enter and now its working. I guess it did not see the indentation correctly. Thank you to all of you.

Excuse me, why there is no getEventTime() method about BasicAlarmEvent? I want to build my own AlarmStatusTable, but can’t get eventtime from AlarmEventObject.

The alarm event doesn’t directly keep track of the event time. Instead you get the EventData from whatever state the alarm is in:
image
And then EventData has a getTimestamp method that returns a long timestamp.

1 Like

For anyone reading this in the future; in 8.1.11 we changed things around internally so every AlarmEvent you get in scripting will automatically be a PyAlarmEvent that does the wrapping here automatically.

2 Likes