How to get "GatewayContext" in Gateway Event (timer) script

Hello,

Wondering if anyone knows how to gain access to the “gateway context” from within a gateway event script in v8?

in v7 I think it would have been something like:

from com.inductiveautomation.ignition.gateway import SRContext
context = SRContext.get()

but haven’t been able to find anything similar in the v8 SDK

Thanks in advance!

-Paul

1 Like

SRContext was renamed IgnitionGateway. Everything else should be the same.

2 Likes

awesome – thanks Kevin!

Hello,

Wondering if using IgnitionGateway in place of SRContext worked for you? Using the same class path but switching to IgnitionGateway, I’m getting cannot import IgnitionGateway or other flavor of error.

Thanks,

Rick

Are you using Ignition 8? Where / what kind of script are you trying to use this in?

Hi Kevin,

Using 8.01. I originally had the script in a package under user-lib … pkg.(a number of script files)

I’ve since moved the package to the global gateway scripts. All was working well (either place) except for the call to SRContext.

Finding this thread I gave IgnitionGateway a try but it isn’t being found. I’ve NOT tried putting this back under user-lib.

Thanks,

Rick

Sure, but where is this script being called from? What scope?

Ah, sorry … long running SFC also in the global project.

Hmm. Well I think that should work… not sure what’s going on. Can you post the full error message when you get a chance?

Hey Kevin,

Well, what I said wasn’t the error, got past that a few days ago, that’s what I get for putting this down a few days :frowning:

Anyway, the error is this:

> Error in Step 'Action' in chart 'a6bea3af-834b-43ba-942e-ff3b9b406a20' for station 'Test-OP005 Traceback (most recent call last):
>   File "<Script name="">", line 16, in onStart
>   File "<module:sit.telegrams.actions>", line 180, in run
>   File "<module:sit.telegrams.util>", line 38, in tag_subscribe
> AttributeError: 'com.inductiveautomation.ignition.gateway.tags.mode' object has no attribute 'subscribe'

So, IgnitionGateway is found. The original developer had this method to make the subscription:

def tag_subscribe(path, listener):
	tp = TagPathParser.parse(path)
	IgnitionGateway.get().getTagManager().subscribe(tp, listener)
	return (tp, listener)

Thanks,

Rick

TagManager’s API has changed. You’re venturing into Module SDK territory at this point.

It would probably be best to create a new topic for this. I’m not sure if what you’re doing is even safe :grimacing:

:slight_smile: Ya, what I didn’t post was the original developer said, and I paraphrase … “shakey territory”

I did find the documentation on TagManager this and it seems subscribe is gone, as the error indicates, but subscribeAsync is available. I’ll give it a shot and report back.

Thanks … helps to talk this through - in public no less :roll_eyes:

-Rick

So reporting back, IgnitionGateway does of course work and instead of .subscribe, .subscribeAsync should be used. There are two overloads of subscribeAsync, the one taking one path and one listener must be private because it is not recognized, so I used the list version that has the two "list" parameters required (list of tags, list of listeners).

The TagChangeListener I used is from com.inductiveautomation.ignition.common.tags.model.event ... like this

from com.inductiveautomation.ignition.common.tags.model.event import TagChangeListener

Here is the top portion of my handler

class HandshakeListener(TagChangeListener):
	def __init__(self, parent):
 		self.__parent = parent
	def tagChanged(self, event):
		objValue = event.getValue()
		value = objValue.getValue()

Notice above I've broken out to values ... objValue represents an object with multiple properties, one of which is value. To get "the value" of the tag, call get getValue() on the the event object value.

-Rick

I sure hope you are keeping track of these (this?) listeners with the system.util.getGlobals() dictionary -- otherwise you will be attaching code to platform objects that will survive code edits and will leak jython interpreters along the way. See this discussion:

Hi, was this renamed again?


v8.0.9

No, it’s just only accessible in Gateway scope, and you’re in Designer scope in the script console.

2 Likes

ops, Thanks @Kevin.Herron