Thank you, Rich and all other friends for the issue, really interesting
I had a rule with Thread::sleep() and it is now time to review it with timers
Now I realize that it could be dangerous against the architecture of the openhab machine
So thank you to your suggestions, I tried to set up an environment to test new design pattern
I have only one item to drive my automation
Switch timerTest
if it is ON I need to make some stuff every 3 seconds
if it is OFF, I don’t need to do anything
This is my rule :
var Integer tick = 0
var Timer myTimer = null
rule "Timer Rule"
when Item timerTest changed
then
logInfo( "Timer Rule" , "rule triggered - timerTest = " + timerTest.state)
if(myTimer !== null)
myTimer = null;
// initiate the environment
tick = 0
logInfo( "Timer Rule" , "---- create the timer ---")
myTimer = createTimer(now.plusSeconds(3), [ |
logInfo("Timer Rule","==== TIMER WAKE ON")
if (timerTest.state == OFF) {
myTimer = null;
logInfo( "Timer Rule" , "do not reschedule it anymore")
}
else
{
// do some stuff here bla bla
tick = tick + 1
logInfo( "Timer Rule" , "***** do some stuff here bla bla - tick = " + tick)
myTimer.reschedule(now.plusSeconds(3))
logInfo( "Timer Rule" , "rescheduled")
}
logInfo("Timer Rule","==== TIMER END")
])
logInfo( "Timer Rule" , "---- end of the rule ---")
end
So as I expected
- the rule is executed immediately and the createTimer doesn’t stop the execution
- the code in the createTimer brackets is executed as soon as the timer is due
- the scope of the code in the createTimer brackets is the same of the rule
the trace result is this:
2019-01-06 22:36:37.066 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'timer.rules'
2019-01-06 22:36:40.162 [ome.event.ItemCommandEvent] - Item 'timerTest' received command ON
2019-01-06 22:36:40.180 [vent.ItemStateChangedEvent] - timerTest changed from OFF to ON
2019-01-06 22:36:40.942 [INFO ] [se.smarthome.model.script.Timer Rule] - rule triggered - timerTest = ON
2019-01-06 22:36:40.949 [INFO ] [se.smarthome.model.script.Timer Rule] - ---- create the timer ---
2019-01-06 22:36:40.966 [INFO ] [se.smarthome.model.script.Timer Rule] - ---- end of the rule ---
2019-01-06 22:36:43.959 [INFO ] [se.smarthome.model.script.Timer Rule] - ==== TIMER WAKE ON
2019-01-06 22:36:43.981 [INFO ] [se.smarthome.model.script.Timer Rule] - ***** do some stuff here bla bla - tick = 1
2019-01-06 22:36:43.995 [INFO ] [se.smarthome.model.script.Timer Rule] - rescheduled
2019-01-06 22:36:44.002 [INFO ] [se.smarthome.model.script.Timer Rule] - ==== TIMER END
2019-01-06 22:36:46.994 [INFO ] [se.smarthome.model.script.Timer Rule] - ==== TIMER WAKE ON
2019-01-06 22:36:47.014 [INFO ] [se.smarthome.model.script.Timer Rule] - ***** do some stuff here bla bla - tick = 2
2019-01-06 22:36:47.028 [INFO ] [se.smarthome.model.script.Timer Rule] - rescheduled
2019-01-06 22:36:47.034 [INFO ] [se.smarthome.model.script.Timer Rule] - ==== TIMER END
2019-01-06 22:36:50.026 [INFO ] [se.smarthome.model.script.Timer Rule] - ==== TIMER WAKE ON
2019-01-06 22:36:50.047 [INFO ] [se.smarthome.model.script.Timer Rule] - ***** do some stuff here bla bla - tick = 3
2019-01-06 22:36:50.059 [INFO ] [se.smarthome.model.script.Timer Rule] - rescheduled
2019-01-06 22:36:50.066 [INFO ] [se.smarthome.model.script.Timer Rule] - ==== TIMER END
2019-01-06 22:36:53.058 [INFO ] [se.smarthome.model.script.Timer Rule] - ==== TIMER WAKE ON
2019-01-06 22:36:53.080 [INFO ] [se.smarthome.model.script.Timer Rule] - ***** do some stuff here bla bla - tick = 4
2019-01-06 22:36:53.112 [INFO ] [se.smarthome.model.script.Timer Rule] - rescheduled
2019-01-06 22:36:53.119 [INFO ] [se.smarthome.model.script.Timer Rule] - ==== TIMER END
2019-01-06 22:36:53.951 [ome.event.ItemCommandEvent] - Item 'timerTest' received command OFF
2019-01-06 22:36:53.972 [vent.ItemStateChangedEvent] - timerTest changed from ON to OFF
2019-01-06 22:36:53.985 [INFO ] [se.smarthome.model.script.Timer Rule] - rule triggered - timerTest = OFF
2019-01-06 22:36:53.997 [INFO ] [se.smarthome.model.script.Timer Rule] - ---- create the timer ---
2019-01-06 22:36:54.016 [INFO ] [se.smarthome.model.script.Timer Rule] - ---- end of the rule ---
2019-01-06 22:36:56.107 [INFO ] [se.smarthome.model.script.Timer Rule] - ==== TIMER WAKE ON
2019-01-06 22:36:56.119 [INFO ] [se.smarthome.model.script.Timer Rule] - do not reschedule it anymore
2019-01-06 22:36:56.128 [INFO ] [se.smarthome.model.script.Timer Rule] - ==== TIMER END
2019-01-06 22:36:57.005 [INFO ] [se.smarthome.model.script.Timer Rule] - ==== TIMER WAKE ON
2019-01-06 22:36:57.014 [INFO ] [se.smarthome.model.script.Timer Rule] - do not reschedule it anymore
2019-01-06 22:36:57.019 [INFO ] [se.smarthome.model.script.Timer Rule] - ==== TIMER END
but my problem is when I disimiss the timer
As you can see in the last lines of the trace, there is an extra execution of the code in the timer bracket, althought I didn’t reschedule it anymore
22:36:56.119 line didn’t retrigger the timer, so why I have an other execution on 22:36:57.005?
can anybody help me to understand what is wrong in my code?