While loop

Asked by Dylan Moreland

I'm trying to make a wireless status notification system based on the wireless icon in the system tray.

The way it'll work is it will run an be idle until it sees the Wireless Radio Disconnected symbol (the computer with an "x" next to it). When it sees this it will give me a popup.

Then when it sees the Acquiring IP symbol (the computer with the bouncing ball), it gives me another popup.

Then when it sees the Online symbol (the computer with the wireless symbol) it will give me a third popup and start all over again, waiting for the Disconnected sign.

I figured I needed a while loop, but for some reason it gives me a fake Disconnected popup in the beginning, and then once I go through the enire program (Disconnected, Acquiring, Online), it just loops all three popups.

Here is the program so far:

setAutoWaitTimeout(FOREVER)
while(1):
    while not exists("1317412001875.png"):
        sleep(0.5)

    popup("Internet connection has been lost.")
    wait("5.png")
    popup("Negotiating with server...")
    wait("J.png")
    popup("Online.")

If you could help, that would be great because this has been bugging me for forever.

Thanks a ton,
Dylan

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Harry Readinger (temporary22) said :
#1

A few things from my experience:

1) try limiting your find/exists to a region if possible and just observe that region. For example take a screenshot of your system tray (sysTray.png) and try this before the loop:

st = find("sysTray.png")
str = Region(st)

That makes a Region on the rectangle returned from the find(). doing find/exists in this region will be MUCH faster, and possibly fix your problem.

SO you can then just adapt your code like this:

st = find("sysTray.png")
str = Region(st)

setAutoWaitTimeout(FOREVER)

while(1):
    while not str.exists("1317412001875.png"):
        sleep(0.5)

    popup("Internet connection has been lost.")
    str.wait("5.png")
    popup("Negotiating with server...")
    str.wait("J.png")
    popup("Online.")

2) If that alone doesn't help try not using FOREVER (which personally I have never had work). To test this try specifying a length of time to wait, say 1 hour:

st = find("sysTray.png")
str = Region(st)

while(1):
    while not str.exists("1317412001875.png"):
        sleep(0.5)

    popup("Internet connection has been lost.")
    str.wait("5.png", 3600)
    popup("Negotiating with server...")
    str.wait("J.png", 3600)
    popup("Online.")

See if either of those help

Revision history for this message
RaiMan (raimund-hocke) said :
#2

@ Harry

Yes and yes and one more yes:

When setting up a workflow in Sikuli, restricting the region is the best you can do to speed things up and get more reliability and robustness.

But in many cases, the content of the restricted region (as the system tray in this example) is changing over the time. So taking a screen shot of the region and find it, does not help.

The other thing, most people try on the first run, to define a fixed with absolute coordinates (which might help e.g. with content, that is always at the same place: most games, full screen apps, ...).

@ Dylan

This could be an alternative in your situation:
sdim = getBounds() # the screen dimensions
# to be adjusted to your needs
trayW = 300
trayH = 30
# the tray region
tray = Region(sdim.x-trayW, sdim.y-trayH, trayW, trayH)

put this at the beginning of your script and use it later to restrict the find as suggested by Harry.

--- your workflow
your script will contain a loop, that runs forever without having a stop condition. So be aware, that you have to kill it, if you want it to stop (abort key alt-shift-c).

I would do it less complex: what you are interested in, is that you are offline and that you are back online again.
So you just have to observer the existence of the symbol being online.

Another challenge I think you have: the 3 pictures are to similar, so you have to raise the needed similarity to try to differentiate the images. Concentrating on one image (the being online symbol would even help to make it robust).

So the script could be:

sdim = getBounds() # the screen dimensions
# to be adjusted to your needs
trayW = 300
trayH = 30
# the tray region
tray = Region(sdim.x-trayW, sdim.y-trayH, trayW, trayH)

while True: # we run it forever
    while tray.exists(Pattern("wlan-online.png"), similar(0.9))
        wait(1) # looking every second is enough
    # now we are offline
    popup("Now offline, waiting for reconnect")
    while not tray.exists(Pattern("wlan-online.png"), similar(0.9))
        wait(1) # looking every second is enough

Can you help with this problem?

Provide an answer of your own, or ask Dylan Moreland for more information if necessary.

To post a message you must log in.