New Alarm Alert as a Popup

I would start this a different route. It looks like you are polling for changes to alarm states, when really, we should be reacting to events relating to alarm state changes. This is going to be a long winded answer, so hear goes.

  1. Create a new shared script. For this example, create it at shared.alarming. The following will create an object, that extends Ignition’s AlarmListener interface. This is how we get the alarm event from the gateway to the client.

Post the following code:

from com.inductiveautomation.ignition.common.alarming import AlarmListener
class MyAlarmListener(AlarmListener):
	def __init__(self):
		system.util.getLogger("MyAlarmListener").info("Constructing Object")
		
	def onAcknowledge(self,evt):
		system.util.getLogger("MyAlarmListener").info("OnAcknowledge")
		system.util.sendMessage(project="prime",messageHandler="onAcknowledge",payload = {"event":evt},scope = "C")
		
	def onActive(self,evt):
		system.util.getLogger("MyAlarmListener").info("OnActive")
		system.util.sendMessage(project="prime",messageHandler="onActive",payload = {"event":evt},scope = "C")
			
	def onClear(self,evt):
		system.util.getLogger("MyAlarmListener").info("OnClear")
		system.util.sendMessage(project="prime",messageHandler="onClear",payload = {"event":evt},scope = "C")

If you dont need one of the event, let’s say onAcknowledge, replace the code with

def onAcknowledge(self,evt):
	pass
  1. Create message handlers in the client scope. You will need onActive, onAcknowledge, and onClear defined, in line with what you put in MyAlarmListener

Hear is some sample code, which may be put into onActive.

	from java.util import Date
	name = payload["event"].getName()
	dPath = payload["event"].getDisplayPathOrSource()
	tStamp = Date(payload["event"].getActiveData().getTimestamp())
	message = "<html>New Alarm <b>%s</b> occurred at %s."%(str(dPath),tStamp)
	system.gui.messageBox(message,"New Alarm")
#	print payload["event"]
  1. Register the alarm listener with the system. This script essentially says “Hey, listen for every alarm, and use MyAlarmListener to figure out what to do”. Put this in the gateway startup script.
from com.inductiveautomation.ignition.gateway import SRContext
from com.inductiveautomation.ignition.common import QualifiedPath
context = SRContext.get()

l = shared.alarming.MyAlarmListener()
system.util.getGlobals()["alarm-listener"] = l

n = context.getSystemProperties().getSystemName()
v = QualifiedPath.Builder()
#	v.setSystem(n)

system.util.getLogger("listener test").info("registering listener")
context.getAlarmManager().addListener(v.build(),l)
  1. Finally, you need this in the gateway shutdown script.
from com.inductiveautomation.ignition.gateway import SRContext
from com.inductiveautomation.ignition.common import QualifiedPath
if "alarm-listener" in system.util.getGlobals():
	context = SRContext.get()
	v = QualifiedPath.Builder()
	
	system.util.getLogger("listener test").info("unregistering listener")
	context.getAlarmManager().removeListener(v.build(),system.util.getGlobals()["alarm-listener"])

If you have any questions, please let me know.

9 Likes