Timer like function that persists between parse invocations

I'm a novice at writing HE device drivers...

I'd like to implement some functionality in a driver that executes when a particular packet has not been seen for X seconds. X will be something like 3 or 5 seconds.

I see code fragments in driver implementations like:

Date date = new Date()

that seem to capture a timestamp when executed. So, it seems, I should be able to compare that to a previous time and only execute the code of interest when elapsed time exceeds the required time.

What can I do to have the variable "persist" across invocations of parse()? Should I do a sendEvent so that it is saved in an attribute?

Look at something like runIn

1 Like

Thanks!

1 Like

Alternately--

If I do a spin loop in the parse() method with something like a sleep in the loop...

Is that code running in a separate thread so it doesn't block processing of other events? Said another way, is there something like a pthread_yield available? Or, is code execution (other than my driver code) blocked until I return from the parse()?

Oh boy, you're getting into fun stuff! I believe each iteration of parse() will spawn it's own thread, but I'm sure there is a thread pool so you don't want to have endless loops inside parse() otherwise you may exhaust the pool. Every zwave command will call parse() so if lots of events fire at once, you have lots of threads going on. I try to make parse very fast executing for that reason. That said, you can do something like what you're talking about. I have a working example in this code. hubitat-kevo/Kevo_Plus_Integration.groovy at master · dcmeglio/hubitat-kevo · GitHub it's not a sleep, it basically waits on a semaphore.

I think what you're looking for is pauseExecution(milli) but as I said, I'd be cautious here. I think the way you're suggesting using this will lead to starvation when you possibly cause all the threads in the thread pool to be exhausted. It's hard to say without knowing more about how you're planning to code this.

2 Likes

Sounds like good advice!

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.