Skip to content
This repository has been archived by the owner on Mar 14, 2018. It is now read-only.

Commit

Permalink
fixed: observeInBackground(): allow tracking with reg.isObserving()
Browse files Browse the repository at this point in the history
  • Loading branch information
RaiMan committed Oct 26, 2017
1 parent d99c822 commit 458d8c3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
22 changes: 19 additions & 3 deletions API/src/main/java/org/sikuli/script/Region.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ private static void log(int level, String message, Object... args) {
* Flag, if an observer is running on this region {@link Settings}
*/
private boolean observing = false;
private boolean observingInBackground = false;
private float observeScanRate = Settings.ObserveScanRate;
private int repeatWaitTime = Settings.RepeatWaitTime;
/**
Expand Down Expand Up @@ -3517,8 +3518,10 @@ private boolean observeDo(double secs) {
return false;
}
if (observing) {
Debug.error("Region: observe: already running for this region. Only one allowed!");
return false;
if (!observingInBackground) {
Debug.error("Region: observe: already running for this region. Only one allowed!");
return false;
}
}
log(lvl, "observe: starting in " + this.toStringShort() + " for " + secs + " seconds");
int MaxTimePerScan = (int) (1000.0 / observeScanRate);
Expand Down Expand Up @@ -3563,7 +3566,7 @@ private boolean observeDo(double secs) {
}

/**
* start an observer in this region for the given time that runs in background for details about the observe event
* start an observer in this region for the given time that runs in background - for details about the observe event
* handler: {@link ObserverCallBack} for details about APPEAR/VANISH/CHANGE events: {@link ObserveEvent}
*
* @param secs time in seconds the observer should run
Expand All @@ -3574,12 +3577,24 @@ public boolean observeInBackground(double secs) {
Debug.error("Region: observeInBackground: already running for this region. Only one allowed!");
return false;
}
observing = true;
observingInBackground = true;
Thread observeThread = new Thread(new ObserverThread(secs));
observeThread.start();
log(lvl, "observeInBackground now running");
return true;
}

/**
* start an observer in this region that runs in background forever - for details about the observe event
* handler: {@link ObserverCallBack} for details about APPEAR/VANISH/CHANGE events: {@link ObserveEvent}
*
* @return false if not possible, true otherwise
*/
public boolean observeInBackground() {
return observeInBackground(Double.MAX_VALUE);
}

private class ObserverThread implements Runnable {

private double time;
Expand All @@ -3600,6 +3615,7 @@ public void run() {
public void stopObserver() {
log(lvl, "observe: request to stop observer for " + this.toStringShort());
observing = false;
observingInBackground = false;
}

/**
Expand Down
23 changes: 22 additions & 1 deletion API/src/main/java/org/sikuli/script/Sikulix.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,28 @@ public static void main(String[] args) throws FindFailed {
boolean playing = false;
if (args.length > 0 && "play".equals(args[0])) {
playing = true;
//-------- playground
//-------- playground Observe
// Region reg = new Region(0,0,80,80);
// reg.highlight(2);
// reg.onChange(new ObserverCallBack(){
// @Override
// public void changed(ObserveEvent evt) {
// if (evt.getCount() > 3) {
// p("in handler: %d - stopping", evt.getCount());
// evt.stopObserver();
// } else {
// p("in handler: %d", evt.getCount());
// }
// }
// });
// reg.observeInBackground();
// int n = 0;
// while (reg.isObserving()) {
// p("%d - observing", n);
// reg.wait(1.0);
// n++;
// }
//-------- playground VNC
// VNCScreen vs = vncStart("192.168.2.63", 5900, "vnc", 0, 0);
// p("%s", vs);
// vs.wait(3.0);
Expand Down

0 comments on commit 458d8c3

Please sign in to comment.