I have created a new device type for integrating with my Total Connect alarm system, and a few operations which connect directly to the alarm console through wireless connection require that you initiate them, and them keep “polling” them until completed (arm and disarm fall in that category).
Of course I can’t do “while (arm() != finished) {}”, as that would flood the system with requests; what I would really want to do is “while(!done) {done = arm(); sleep(5000);}”, or something along those lines. I couldn’t find a way to do that, nor to schedule an event… Am I missing something obvious? For the time being I’m starting the operation and not polling it, hoping for the best (which is ok, even if it implies keeping a connection session alive longer than needed).
OK, then create a device attribute like “ConditionReady”. Then subscribe to the attribute condition you want and have an event handler method for when it is true. Have your checking function run on a cron job to fire on a scheduled job every so many seconds/minutes. In the handler you can unschedule the cron job.
.....
... some code
def waitSeconds = 30
startTimer(waitSeconds)
}
def startTimer(seconds) {
log.debug "in start timer"
def now = new Date()
def runTime = new Date(now.getTime() + (seconds * 1000))
runOnce(runTime, myTimerEventHandler)
}
def myTimerEventHandler() {
//do the things that I want delayed in this function
log.debug "doing the delayed things"
}
Yes, it makes sense; in fact, it’s quite similar to how I’m doing things right now.
Still, I wonder why such a mechanism can’t be all part of the device type, without the need of an external ad-hoc app.
Well, I tried, both runOnce() and runIn(); and they are not available on a deviceType: 9:06:07 PM: error groovy.lang.MissingMethodException: No signature of method: script1396832766704231713829.runOnce() is applicable for argument types: (java.util.Date, java.lang.String) values: [Mon Apr 07 01:06:17 UTC 2014, myHandler]
Same question! I need a way in a device type to delay execution of a button press, in case the button is pressed more than once in a certain time range. Also, can confirm that currently the scheduling functions DO NOT work in device types.
You can use “delay 1000” in a device driver to wait a second before proceeding. I had trouble with using the command directly, but found that this works reliably: