Way to write to perspective sesson properties from wherever?

I have a function that handles a banner messaging system that exists in both the Vision and Perspective version of our application. I am trying to merge their shared set of scripts into one script module.

Then function writes the banner message to the database, and then 1) toggles a client tag to refresh the query or 2) toggled a session custom property that’s used in the named query to do the same in perspective. But now I am trying to merge these together into one function that is called after the database write completes.

I have this working for vision but don’t know what I can put in perspective to make it work

def refreshMessages():
    from env import isPerspective, isVision
    if isPerspective():
        #????
    elif isVision(): # This tells the table to refresh(vision)
        import system.tag
	    if system.tag.read('[client]refreshMessages'):
	    	system.tag.write('[client]refreshMessages', 0)
	    else:
	    	system.tag.write('[client]refreshMessages', 1)

Is there a way to read/write directly to a session property analogous to system.tag?

Python’s builtin functions getattr and setattr - although you’ll need to get a generic reference to the session object inside your refreshMessages function. I think I posted a possible way to do that here on the forums a while back…there’s a relevant caution right below it from @pturmel :slight_smile:

Ok, looking through your posts, any tips to find it?

From a post from me. I thought I had made a post on this. Boy do I have egg on my face.

1 Like

I tried doing this

def refreshMessages():
    from test import universalPrint as uPrint
	if isPerspective():
		uPrint('Env is Perspective')
		uPrint('here 1')
		from com.inductiveautomation.perspective.gateway.session import InternalSession
		uPrint('here 2')
		session = InternalSession.SESSION.get()
		uPrint('here 3')
		uPrint("session: " + str(session))
		value = getattr(session.custom, "refreshMessages")
		if value == 0:
			setattr(session.custom, "refreshMessages",1)
		else:
			setattr(session.custom, "refreshMessages",0)
	else: # This tells the table to refresh(vision)
		uPrint('Env is Vision')
		if system.tag.read(tagName):
			system.tag.write(tagName, 0)
		else:
			system.tag.write(tagName, 1)

But it doesn’t get past here 1 and the error that gets logged is global name 'self' is not defined. Any thoughts? I don’t know whats going on.

The alternative here I guess would be to feed the session itself to the very first and every subsequent function with a session=None keyword argument I guess right?