I'm doing some experiments with openHAB2 to control a simple relay that is linked to my garage lights.
I've created a rule for this Switch item, so the relay can switch back to the ON state after one second from the user click on the UI button.
To prevent user from multiple clicking in a short time (e.g. user clicks again after a 1st click and before the end of thread sleep), I've put a ReentrantLock and it seems to work.
When a second clicks arrives, I see an exception thrown at runtime from openHAB:
[ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Garage lights':
org.eclipse.xtext.xbase.interpreter.impl.EvaluationException: java.lang.IllegalMonitorStateException
Why is it not caught from my catch block? How can I catch it?
Here is my code:
import java.util.concurrent.locks.ReentrantLock
val ReentrantLock lockLights = new ReentrantLock()
rule "Garage lights"
when
Item GarageLightsSwitch received command OFF
then
try {
if(lockLights.tryLock()) {
Thread::sleep(1000)
GarageLightsSwitch.sendCommand(ON)
}
}
catch (Exception e) {
logInfo("Generic exception")
}
finally {
lockLights.unlock()
}
end
The item is configured this way:
Switch GarageLightsSwitch "Garage lights" <switch> {gpio="pin:6 initialValue:high" }
My sitemap is:
sitemap default label="My sitemap" {
Switch item=GarageLightsSwitch mappings=[OFF="CLICK ME"]
}
Thanks in advance