I have a very strange behavious with a function and timers inside of it.
I try to implement a generic function for random timers related to items. If the timer end, the function send a given state to the related item.
I will use this for a random behaviour of my house for rollershutter down and other things.
I put a lot of debug code to see what is going on.
var HashMap<String, Timer> tRandomTimers = newHashMap()
// this function is used to wait for a random interval and then set the item to a state
// GenericItem = item
// Hashmap of the Timer
// Numberitem with the maximum of delay
// new state after interval is reached
val Functions$Function4<GenericItem, HashMap<String, Timer>, NumberItem, State, Boolean> startRandomTimerWithAction= [ relatedItem, tRandomTimers, nuRandomTimerTime, newitemstate |
var int randomTime
logInfo("TIMERCHECK", "randomTimer relatedItem: " + relatedItem.name)
//logInfo("TIMERCHECK", "randomTimer nuRandomTimerTime: " + nuRandomTimer.state)
// ensure that random value is greater then 0
if((nuRandomTimer.state as DecimalType).intValue == 0)
randomTime = 2
else
randomTime = (new java.util.Random).nextInt((nuRandomTimer.state as DecimalType).intValue) + 2
if(tRandomTimers.get(relatedItem.name) === null)
{
logInfo("TIMERCHECK", "randomTimer CreateTimer: " + relatedItem.name + " - " + randomTime)
tRandomTimers.put(relatedItem.name, createTimer(now.plusSeconds(randomTime))
[|
logInfo("TIMERCHECK", "randomTimer TimerEnded: " + relatedItem.name)
relatedItem.sendCommand(newitemstate.toString)
tRandomTimers.remove(relatedItem.name)
logInfo("TIMERCHECK", "randomTimer tRandomTimers: " + tRandomTimers)
logInfo("TIMERCHECK", "randomTimer ###############################")
])
}
else
{
logInfo("TIMERCHECK", "randomTimer reschedule: " + relatedItem.name + " - " + randomTime)
tRandomTimers.get(relatedItem.name).reschedule(now.plusSeconds(randomTime))
}
logInfo("TIMERCHECK", "randomTimer tRandomTimers: " + tRandomTimers)
logInfo("TIMERCHECK", "randomTimer ###############################")
true
]
I can start this function with this command:
startRandomTimerWithAction.apply(swRandomTimer01, tRandomTimers, nuRandomTimer, OFF)
Now to my problem:
If two timers are filled with similar values, it sometimes happen, that some of the timer do not end. Timer is in the HashMap but does not end. It looks like the timer is never created. Is this a problem with ThreadSave of functions?
My code to test this:
startRandomTimerWithAction.apply(swRandomTimer01, tRandomTimers, nuRandomTimer, OFF)
startRandomTimerWithAction.apply(swRandomTimer02, tRandomTimers, nuRandomTimer, OFF)
startRandomTimerWithAction.apply(swRandomTimer03, tRandomTimers, nuRandomTimer, OFF)
startRandomTimerWithAction.apply(swRandomTimer04, tRandomTimers, nuRandomTimer, OFF)
output:
2018-02-21 09:36:42.049 [INFO ] [se.smarthome.model.script.TIMERCHECK] - randomTimer relatedItem: swRandomTimer01
2018-02-21 09:36:42.049 [INFO ] [se.smarthome.model.script.TIMERCHECK] - randomTimer CreateTimer: swRandomTimer01 - 2
2018-02-21 09:36:42.049 [INFO ] [se.smarthome.model.script.TIMERCHECK] - randomTimer tRandomTimers: {swRandomTimer01=org.eclipse.smarthome.model.script.internal.actions.TimerImpl@8c1fef}
2018-02-21 09:36:42.049 [INFO ] [se.smarthome.model.script.TIMERCHECK] - randomTimer ###############################
2018-02-21 09:36:42.049 [INFO ] [se.smarthome.model.script.TIMERCHECK] - randomTimer relatedItem: swRandomTimer02
2018-02-21 09:36:42.065 [INFO ] [se.smarthome.model.script.TIMERCHECK] - randomTimer CreateTimer: swRandomTimer02 - 2
2018-02-21 09:36:42.065 [INFO ] [se.smarthome.model.script.TIMERCHECK] - randomTimer tRandomTimers: {swRandomTimer02=org.eclipse.smarthome.model.script.internal.actions.TimerImpl@b381e6, swRandomTimer01=org.eclipse.smarthome.model.script.internal.actions.TimerImpl@8c1fef}
2018-02-21 09:36:42.065 [INFO ] [se.smarthome.model.script.TIMERCHECK] - randomTimer ###############################
2018-02-21 09:36:42.065 [INFO ] [se.smarthome.model.script.TIMERCHECK] - randomTimer relatedItem: swRandomTimer03
2018-02-21 09:36:42.065 [INFO ] [se.smarthome.model.script.TIMERCHECK] - randomTimer CreateTimer: swRandomTimer03 - 2
2018-02-21 09:36:42.065 [INFO ] [se.smarthome.model.script.TIMERCHECK] - randomTimer tRandomTimers: {swRandomTimer02=org.eclipse.smarthome.model.script.internal.actions.TimerImpl@b381e6, swRandomTimer03=org.eclipse.smarthome.model.script.internal.actions.TimerImpl@1b115d4, swRandomTimer01=org.eclipse.smarthome.model.script.internal.actions.TimerImpl@8c1fef}
2018-02-21 09:36:42.065 [INFO ] [se.smarthome.model.script.TIMERCHECK] - randomTimer ###############################
2018-02-21 09:36:42.065 [INFO ] [se.smarthome.model.script.TIMERCHECK] - randomTimer relatedItem: swRandomTimer04
2018-02-21 09:36:42.065 [INFO ] [se.smarthome.model.script.TIMERCHECK] - randomTimer CreateTimer: swRandomTimer04 - 2
2018-02-21 09:36:42.080 [INFO ] [se.smarthome.model.script.TIMERCHECK] - randomTimer tRandomTimers: {swRandomTimer04=org.eclipse.smarthome.model.script.internal.actions.TimerImpl@1f3c362, swRandomTimer02=org.eclipse.smarthome.model.script.internal.actions.TimerImpl@b381e6, swRandomTimer03=org.eclipse.smarthome.model.script.internal.actions.TimerImpl@1b115d4, swRandomTimer01=org.eclipse.smarthome.model.script.internal.actions.TimerImpl@8c1fef}
2018-02-21 09:36:42.080 [INFO ] [se.smarthome.model.script.TIMERCHECK] - randomTimer ###############################
2018-02-21 09:36:44.049 [INFO ] [se.smarthome.model.script.TIMERCHECK] - randomTimer TimerEnded: swRandomTimer01
2018-02-21 09:36:44.049 [INFO ] [se.smarthome.model.script.TIMERCHECK] - randomTimer tRandomTimers: {swRandomTimer04=org.eclipse.smarthome.model.script.internal.actions.TimerImpl@1f3c362, swRandomTimer02=org.eclipse.smarthome.model.script.internal.actions.TimerImpl@b381e6, swRandomTimer03=org.eclipse.smarthome.model.script.internal.actions.TimerImpl@1b115d4}
2018-02-21 09:36:44.049 [INFO ] [se.smarthome.model.script.TIMERCHECK] - randomTimer ###############################
2018-02-21 09:36:44.081 [INFO ] [se.smarthome.model.script.TIMERCHECK] - randomTimer TimerEnded: swRandomTimer04
2018-02-21 09:36:44.081 [INFO ] [se.smarthome.model.script.TIMERCHECK] - randomTimer tRandomTimers: {swRandomTimer02=org.eclipse.smarthome.model.script.internal.actions.TimerImpl@b381e6, swRandomTimer03=org.eclipse.smarthome.model.script.internal.actions.TimerImpl@1b115d4}
2018-02-21 09:36:44.081 [INFO ] [se.smarthome.model.script.TIMERCHECK] - randomTimer ###############################
You can see, all 4 timers are filled with 2 sec. But only two of the (01 and 04) will really end. The two others exist in the Hashmap again but will never end.
Can someone give me a hint what is wrong?
I have a workaround but not sure why this is working. I think the whole problem is related to the rule execusion time speed?
startRandomTimerWithAction.apply(swRandomTimer01, tRandomTimers, nuRandomTimer, OFF)
Thread::sleep(500)
startRandomTimerWithAction.apply(swRandomTimer02, tRandomTimers, nuRandomTimer, OFF)
Thread::sleep(500)
startRandomTimerWithAction.apply(swRandomTimer03, tRandomTimers, nuRandomTimer, OFF)
Thread::sleep(500)
startRandomTimerWithAction.apply(swRandomTimer04, tRandomTimers, nuRandomTimer, OFF)