Sleep() in smart app?


(Brian) #1

I am having trouble implementing a sleep(60000) in my while loop. How can I pause a loop for a while so I dont flood the API im calling?

im getting this error
java.lang.SecurityException: Invoking class script14057093772401260321749.sleep() is not allowed @ line 140


runIn(), runOnce(), pause(msec) which one to use in this example
(Chrisb) #2

I tried forever to figure this out too.

I finally found pause(milliseconds) worked. Well, I mean it worked in the groovy language but I quickly ran into another barrier. I have a loop and I paused for just 12 seconds. However after the 4th run through the loop I got this error:

12:53:52 PM: error java.util.concurrent.TimeoutException: Execution timed out after 40 units. Start time: Thu Jul 03 16:53:02 UTC 2014 @ line 72

I finally ended up getting around the issue by being able to combine my variables and send it all in one http request. Here’s the details of my saga:

So, while pause should work as far as the groovy language is concerned, I think you might run into the same time out issue.


(George Sudarkoff) #3

Schedule your smart app to run in 60000 and quit? You’ll probably want save some state for the next iteration.


(Brian) #4

finally got an answer from ST

runIn(60, myhandler)

my whole loop ended up looking like this

def checkTime(){
	if (now() > timeToday(checkTime).time && now() < timeToday(arrivalTime).time){ 
        runIn(60, scheduledRun)
    }
    else {
    	runIn (5*60, checkTime)
    }
}

def scheduledRun() {
	log.debug "Running"
	checkTrafficHandler()
}

(George Sudarkoff) #5

That looks too complex. Why runIn(60, scheduledRun)? Why not just call the function directly there? Also, if your SmartApp skips a bit (as it happens sometimes) it will stop working until you update it. I’d use cron to fire the app every five minutes, call the function directly and voila (unless you have something else more complex going on in your app).


(Brian) #6

I was having trouble with creating a seudo loop that didnt crash the IDE. This is the last point that I left off before code cleaning.

I’m not familiar with running cronjob on groovy, do you have a code example? I wouldn’t mind having you look over my full code since you seem to be better at DRY coding then me :smile:

Im trying to implement hue’s right now, but I dont own any and the virtuals dont seem to work.


(Brian) #7

Cron is now working in the updated code!