Accessing tag path with writeAsync callback?

I am trying to write a custom function that will be called whenever we want to write to a tag in a Perspective project, with the intention of logging the operator action.

Is it possible to access the actual tagPath and tagValue used in the system.tag.writeAsync method, when inside the callback function?

It looks like only the quality code is returned, which seems odd to me. If the quality is BAD, how will we know WHICH write item was bad?

My function looks something like this at the moment:

	scriptName = "perspective.tag.writeAsync"
	
	def writeResponse(tagResponses):
		
		scriptName = "perspective.tag.writeResponse"
		
		for result in tagResponses:
			if result.good:
				logger.info(scriptName + " success writing '%s'" % (result))
				###perspective.log.operAction("Tag Write", target, value, session)
			else:
				#### TO DO: ###perspective.log.error()
				logger.error(scriptName + " - failed to write '%s' " % (result))
			# end if
		# end for
	# end def
	
	logger.info(scriptName + "SessionInfo: %s" % sessionInfo)
	system.tag.writeAsync(tagPaths, values, writeResponse)
	
	
# end def

Make sure tagPaths is in scope somehow, either by passing it into your function or making sure it’s defined beforehand.

Example:

def handleResponses(paths, responses):
    # do stuff here

tagPaths = [...]
values = [...]
system.tag.writeAsync(tagPaths, values, lambda x: handleResponses(tagPaths, x))

You know which item was bad by matching the indices up.

2 Likes

Thanks Kevin that seems to be just what i need! Might need to school myself up on what that lambda part is doing though.

Something else that is confusing me a bit here, and with Perspective in general..
When using system.perspective.getSessionInfo(), i was hoping that this would return the currently active session, in relation to where the script is being called.
For instance, if i'm clicking a button, and run this script, i would like to have the session corresponding to the page and the user that is currently pressing the button.

I see that it can return multiple sessions, and that you can filter by username and project, but is there a guaranteed way to return the exact session you're already in?
It would make sense to be able to return based on this property for instance:

self.session.props.id

Well, sure, one of the properties in the list of objects returned by getSessionInfo is the session id, so you just have to do that filtering yourself if you want the info on the current session.

	sessionId = self.session.props.id
	
	sessionInfos = system.perspective.getSessionInfo()
	
	filtered = filter(lambda x: x.id == sessionId, sessionInfos)
	
	if len(filtered) > 0:
		print filtered[0]

(there’s that lambda again)

If you’ve already got a Perspective component, you don’t need to call system.perspective.getSessionInfo, also; you can just call self.session.getInfo() to get the local session’s info.

https://docs.inductiveautomation.com/display/DOC81/Perspective+Component+Methods#PerspectiveComponentMethods-CustomMethods

2 Likes

Oh, yeah, do that. Way better.

Thanks guys, I believe it needs to be self.session.getInfo() FYI.
The confusing part of that manual section on a session object, is that it says that .getInfo() returns a list of sessions, when it is actually only returning the one object related to this particular session object.