If I have not explained this properly please help me to correct my question. My question may be related to Java timer, or it may be related to general problem solving.
Dear reader, you don't need to understand what OpenHAB is, what an OpenHAB rule is, or indeed what MQTT does. But I'll use these terms anyway to set the scene of my question.
In OpenHAB I have a rule that responds to messages published to an MQTT topic. When I turn a light dimmer down, an burst of "down" MQTT messages are sent to a broker. Each message fires an OpenHAB rule which reads the current value of the light and subtracts 1 then writes it back.
To ensure the rule does not fire concurrently in different threads (thus preventing the light from dimming at the correct rate), a colleague of mine recommended I add lock.lock like this:
rule "ArduinoBedroomVector"
when
Item Bedroomvector received update
then
lock.lock()
try {
// rules here
var Number lightcircuit1level = BedroomCeilingLight.state as DecimalType
switch(Bedroomvector.state) {
case "light_1_up" : {
lightcircuit1level = lightcircuit1level + 3
if(lightcircuit1level>100) lightcircuit1level = 100
sendCommand(BedroomCeilingLight, lightcircuit1level);
}
case "light_1_down" : {
lightcircuit1level = lightcircuit1level -3
if(lightcircuit1level<0) lightcircuit1level = 0
sendCommand(BedroomCeilingLight, lightcircuit1level);
}
}
}
finally {
lock.unlock()
}
end
and this worked a treat.
Now my actuator doesn't miss "down" messages.
However because the actuator takes a little while to respond to each individual message (it's running over 433MHz RF transmission, each RF message message takes .5 seconds to send), the dimming commands to the actuator are queuing up.
So I need to introduce a way of checking to see whether the rule ran in the last e.g. 0.6 seconds. If it did, then increment the value, but don't send the command. If it didn't, increment the value and finally send the command.
For example this means I could dim the light up and down continuously and as long as I don't stop, the light level wouldn't change. Then as soon as I decide to stop dimming up or down, the light level will be finalised and set.
Better still a timing "rule" that would allow me to continuously change the level but only set the light level every 0.5 seconds according to what the latest level is.
I'm sure this is just a case of creating a timer which is checked at the time the rule is run, but I can't get my head around when the timer should be created and checked. This is probably blindingly obvious to many people.