To avoid a long Thread::sleep()
, it would be better to use a timer instead:
var tSystemStart = null //timer for scheduled restore of devices based on last known state
var count = 0 //counter for schedule (just to be nice)
rule "reload or system start"
when
System started //will also trigger when rule file is reloaded
then
if(tSystemStart !== null) tSystemStart.cancel //if there is already a timer, cancel it
tSystemStart = createTimer(now.plusSeconds(1), [ | //create the timer
if (Lamp3.state != NULL) { //is the state not NULL?
Lamp3.sendCommand(Lamp3.state.toString) //then restore devices
Lamp4.sendCommand(Lamp4.state.toString)
logInfo("systemStart", "Restored Devices!")
tSystemStart = null
}
else { //items aren't restored yet, so
logInfo("systemStart", "Items not restored yet!")
count = count + 1 //count up
if (count < 21) { //only try for 20 seconds
logInfo("systemStart", "Loop! No. {}",count)
tSystemStart.reschedule(now.plusSeconds(1)) //reschedule the timer
}
else {
logInfo("systemStart", "Waiting for 20 Seconds! Give up now.")
tSystemStart = null
count = 0
}
}
])
end
You could omit the counter, because the timer should not harm the system, in contrast
to while () [ Thread::sleep() }
, see