Navigation using a Momentary Button issue

I’m using a Momentary button for Logging In. The controlValue is derived from a tag from the PLC which is a Pushbutton.

I want it to navigate to another window after it is pressed and after it checks the value of another property (which is a tag). I wrote a script in actionperformed but it doesn’t work (It logs in but doesn’t navigate)

if event.source.controlValue==1:
    if event.source.currentUserLevel>0: 
        system.nav.swapTo("Window")
    else:
        system.gui.warningBox("Login Failed")

Your help is appreciated! Thank You!

This might be a silly question but you do have a window named “Window”? did you try without permissions and see if the warning box pops up?

If “Window” does exist is it in any folders? If so the full path to the window needs to be provided.

Are you getting any error?

Best,
Nick Mudge
Ignition Consultant
nickmudge.info

No, I do not.
There are no permissions currently in any of those windows! The warning box pops up even if the conditions in the if statement are satisfied. I'm guessing its a timing issue and its reading the values from the PLC even before the PLC is able to edit them.

I made it to work by binding the tag values to the Root Container and made a script in propertyChange of the Root Container. So, whenever any property of the Root Container changes, it automatically navigates to the "Window" screen.

It worked alright but my boss needs a simpler solution so that he can get a better understanding of how it works!

[quote=“nmudge”]If “Window” does exist is it in any folders? If so the full path to the window needs to be provided.

Are you getting any error?

Best,
Nick Mudge
Ignition Consultant
nickmudge.info[/quote]

No, It is not inside any folder.
The warning box pops up all the time and there are no errors in the script.

This probably means that whatever you are using to populate the currentUserLevel is not working. You should post the code for the binding or whatever scripting you are trying to use to populate that value.

The CurrentUserLevel automatically gets changed by the PLC once the Login Pushbutton is pressed(once the controlValue becomes 1). The problem, however, is that the script is running before the CurrentUserLevel is edited by the PLC, thus the error! I tried using the time.sleep function but to no avail.

Is there a way where we can let the button perform its action (set controlValue to 1) first and then wait a couple of seconds and then check the if statement?

Use system.util.invokeLater. The second argument takes a delay in milliseconds.

Manual entry:
inductiveautomation.com/support/ … elater.htm

Best,
Nick Mudge
Ignition Consultant
nickmudge.info/

Would this work:

if event.propertyName == "currentUserLevel":
	if event.newValue > 0:
		system.nav.swapTo("Window")
	else:
		system.gui.warningBox("Login Failed")

That looks good, Nick. That code would go in the propertyChange event of the Root Container (for oxanemi’s info). There would be no code in the momentary button and when the PLC code finally changed the custom property, then this code would go be affected. Very good.

[quote=“nmudge”]Would this work:

if event.propertyName == "currentUserLevel": if event.newValue > 0: system.nav.swapTo("Window") else: system.gui.warningBox("Login Failed") [/quote]

It worked for the navigation part well! But when the login fails, it doesnt show up the warning box!

The initial value of currentUserLevel is 0 and it stays the same when a login fails. I tried using this code but to no avail.

if controlValue==1:
    if event.propertyName == "currentUserLevel":
	    if event.newValue > 0:
		    system.nav.swapTo("Window")
	    elif event.newValue==event.oldValue:
		    system.gui.warningBox("Login Failed")

I tried using it without the first if statement and it still did not return a warning!

You could try setting the currentUserLevel to negative one (-1) when a login fails and then set it back to zero (0) after the system message pops up. Of course, you’ll have to update your code to look for -1.

I put the script in propertyChange. Since there isn’t any property change, that else statement isn’t executing. Is there a workaround?

I don't know much about PLC's but my boss thinks doing that may fault it.

Well, Incase he agrees. Where do I write the code for it? In the mousebutton pressed events?

You would change it in the PLC. There are an infinite number of ways of solving this. Look into trying to use client tags to figure out if the button was pressed.

When a login fails does the PLC set event.source.currentUserLevel to 0?
If so then you can use the script below. Put it on the propertyChange event on the button.
This script does not require changing the PLC.

if event.propertyName == "controlValue":		
	if event.newValue == 1:
		event.source.currentUserLevel = -1

if event.propertyName == "currentUserLevel":		
	if event.newValue > 0:
		system.nav.swapTo("Window")
	elif event.newValue == 0:
		system.gui.warningBox("Login Failed")

Nick Mudge
Ignition Consultant
nickmudge.info

[quote=“nmudge”]When a login fails does the PLC set event.source.currentUserLevel to 0?
If so then you can use the script below. Put it on the propertyChange event on the button.
This script does not require changing the PLC.

if event.propertyName == "controlValue":		
	if event.newValue == 1:
		event.source.currentUserLevel = -1

if event.propertyName == "currentUserLevel":		
	if event.newValue > 0:
		system.nav.swapTo("Window")
	elif event.newValue == 0:
		system.gui.warningBox("Login Failed")

Nick Mudge
Ignition Consultant
nickmudge.info[/quote]

No, the Initial value of currentUserLevel is 0 and it stays 0 if the login fails. If the login is successful, only then the value changes!

Okay. Please try the script anyway as it may still work.

Still no warning box even though its setting the currentUserLevel property to -1! Do I need to make the currentUserLevel property bi-directional?