Observe on Change event exits script or brings up IDE and continues running

Asked by Chuck

I got my original code from a launchpad answer. The code works great... once.

I have the code looping, after the first run:
if started headless (command line) it just exits.
if started in the IDE, the IDE opens up (As if it finsihed), but the script continues to run.

I have no idea what i've done.

def changed(event):

    print "something changed in "
    sleep(1)
    Settings.isChanged = True
    event.region.stopObserver()
    doht()

def ht_level_change():

    htlevelReg = Region(1060,707,27,15)

    print "Checking bigScreen Video Region"
    r = htlevelReg
    r.onChange(10, changed)
    Settings.isChanged = False
    Settings.ObserveScanRate = 1
    r.observe(300, background=True)
    while Settings.isChanged == False:
        type('5')
        sleep(1.4)
     # observing for 5 seconds (script pauses here)

def doht():

    sleep(2)
    ht_level_change()
doht()

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Chuck
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

what version of Sikukli?

Revision history for this message
Chuck (enuro12) said :
#2

1.0.1

Revision history for this message
Chuck (enuro12) said :
#3

This happens on both my Lubuntu 12.04, Lubuntu 13.10 and Windows 7 x64 systems.

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

I am just on the way with testing.

And I doubt, that this runs on 1.0.1 at all (observe (especially in background) will work reliably only in version 1.1.0+)

my result: it only catches the first change and then it runs endless.

You should create your own "observer" using exists(…, 0).

Revision history for this message
Chuck (enuro12) said :
#5

The reason i dont use exists is the 'area' changes each time. I just observe that area to determine when to continue.

It most defiantly will loop, for some reason you just cant stop it. You've got to terminate Java to make it stop.

Currently i run the script twice once i'm ready and it works 60+ times. Anyway... I thought it was just my poor coding. Glad it's not me :)

I'll look forward to 1.1.0

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

This is a simple alternative, to watch changes in one region and do something when it changes:

# define the region to observe
# find your solution
r = find("1392145650656.png").grow(300)
r.highlight(2)

# called when region changes
# this is your worker (the handler with observe)
def doSomething():
    print "has changed"
    wait(2)

# capture the first content
img = capture(r)
max = 5

while max > 0: # for testing
#while True: # for real running
    wait(1) # defines the observe scan rate
    if not r.exists(img,0):
        doSomething()
        # capture the content to watch for change
        img = capture(r)
        max -= 1

# the script can be stopped using shift-alt-c

Revision history for this message
Chuck (enuro12) said :
#7

Thanks i'll give it a try.