Get Perspective Session Page Info from PageIds

Just for completeness/posterity, here is a sample of full code that does what I set out to do, ie browse active sessions for a given project, determine if a given view is open on those sessions, and then grab the session id and a couple of session custom props from those sessions and build it out into a map to write to a JSON tag.

Session Monitor Browse Code
from com.inductiveautomation.ignition.gateway import IgnitionGateway
import json

project_name = sytem.util.getProjectName()
target_view_path = 'path/to/desired/view'

context = IgnitionGateway.get()
mod_mgr = context.getModuleManager()
# need to get "com.inductiveautomation.perspective.common.api.PropertyType" from module manager to
# get the "props" and "custom" enums for fetching the session props and the custom props
prop_type_class = mod_mgr.resolveClass('com.inductiveautomation.perspective.common.api.PropertyType')
props_enum = prop_type_class.valueOf('props')
custom_enum = prop_type_class.valueOf('custom')
# the below uses the gateway context module manager to function essentially as an import statement
persp_ctx_class = mod_mgr.resolveClass('com.inductiveautomation.perspective.gateway.api.PerspectiveContext')
# instantiate the PerspectiveContext using the gateway context
persp_ctx = persp_ctx_class.get(context)
# get the PerspectiveSessionMonitor object running in gateway context
psm = persp_ctx.getSessionMonitor()
# grab <InternalSession> object list for the target project
int_sessions = psm.getClientSessionsForProject(project_name)
active_sessions = {}
# loop over internal sessions to build a map of active sessions with the target view open
for i, sess in enumerate(int_sessions):
	# loop over the open pages for each session and get the id for each
	for page in sess.getPages():
		# this returns the page id that matches the gateway Status -> Connections -> Perspective Sessions -> Session detail page
		page_id = page.getId()
		views = page.getViews()
		# iterate over the views to see if any of the ids match the target view path
		view_id, view = next(((str(view.getId()), view) for view in views if target_view_path in str(view.getId())), (None, None))
		if view_id and view:
			# if view_id is found, that means this session has the target view open
			# grab the session object from the view, we will use this to grab the session id, and custom props
			session = view.getSession()
			# grab session props and custom props using "getPropertyTreeOf" method of SessionScriptWrapper.SafetyWrapper
			session_props = session.getPropertyTreeOf(props_enum)
			session_custom = session.getPropertyTreeOf(custom_enum)
			session_id = session_props.get('id')
			prop1 = session_custom.get('custom_prop_1_name', None)
			prop2 = session_custom.get('custom_prop_2_name', None)
			# build object to store for the active session
			active_sessions[session_id] = {
				'custom_prop_1_name': prop1, 
				'custom_prop_2_name': prop2
			}
# convert the active sessions object to json and write to string memory tag
active_sessions = json.dumps(active_sessions)
system.tag.writeBlocking(['[default]path/to/active_sessions_tag'], [active_sessions])

Thanks to @pturmel and @PGriffith for getting me off the ground/out of the ditch on this. You guys rock!

4 Likes