Startup Navigation to View based off of IP Address

Hello, I am working to display specific views based off of a computer/HMI IP address on session startup.
The Session startup script is:

def onStartup(session):
	ip = session.props.address
	view = nav.handleStartup(ip)
	system.perspective.navigate(view=view)

With corresponding scripts:

# IPMapping
IP_TO_HMI_MAPPING = {
	"192.168.57.234":1,
        # 2-12 here...,
	"192.168.57.250":13 #this is the main computer
}
def handleStartup(ip):
	import system.perspective
	hmiNumber = IPMapping.IP_TO_HMI_MAPPING.get(ip, 'Unknown')
	if hmiNumber in range(1,13):
		return "HMI_Screens/Main_Screen"
	elif hmiNumber == 13:
		return "Main/Main"
	else:
		return "Unknown_View"

When I launched a session on my localhost, I expected to see my "Unknown_View" view, but I just see a blank screen.
I get the following error in my logs on the gateway:
java.lang.IllegalArgumentException: No perspective page attached to this thread.
Should there be a change script on the session property instead? Open to any suggestions.

1 Like

I believe Perspective needs to have a page open, because that provides the URL. So passing a view into system.perspective.navigate on startup won't work because no pages are open yet. I'd suggest making your startup views pages instead.

1 Like

Depending on how your project is built you could use the Page Startup script. Can confirm that the system.navigate function works in there. As per the docs, the Page Startup event script only executes " when a Perspective page starts in a new tab or window". So you will probably have to start the script with a condition checking if you want to execute the script or not. You can do that by accessing the Page ID property (page.props.pageId) or the page path property (page.props.path).

1 Like