-
Notifications
You must be signed in to change notification settings - Fork 11
Event functions
Event functions are the basic primitives to build up scenes in the framework.
Event.event(<event>,<action>)
<event>
is a Lua table with a 'type' key.
e.g. {type='property', deviceID=58, propertyName='value'}
.
<action>
is a Lua function that should be called when an event is received matching the first argument.
e.g. function(env) fibaro:debug("Ding!") end
Functions take one argument, an 'environment' with context info.
An event handler that turns on lamp with id 77 when motion sensor with id 58 is triggered would look like this
Event.event({type='property', deviceID=58, propertyName='value'},
function(env) if fibaro:getValue(58,'value')>'0' then fibaro:call(77,'turnOn') end end)
In this case we check if the sensor was breached and then turn on the light.
The framework is helpful and complements the Fibaro event above with a 'value' field before handing it over to the event handler. It also allows for 'constraint' pattern in the matching event. E.g.
Event.event({type='property', deviceID=58, value='$>0'},
function(env) fibaro:call(77,'turnOn') end)
This handler only matches if the value is above '0' so the handler can safely turn on the light when called without having to check the sensor value. Constraints can be '>', '<', '>=', '<=', '~='. The check can be against a numeric value or a string. For checks with equality no constraint operator is needed as fields are matched with each other.
Event.event({type='global', name='TimeOfDay', value='Night'},
function(env) fibaro:debug('TimeOfDay is set to Night') end)
Event.post(<event>[,<time>])
Event.schedule(<time>,<action>[,<options>])
Event.cancel(<postRef>)
Event.postRemote(<SceneID>,<event>)
This post an event to another scene. If the scene is running the EventRunner framework the event will be posted there. Other scenes will get it with fibaro:args(). To implement a 'ping' function to make sure that another scene is alive could be implemented like this. First the watching scene that checks if scene 'scene2' is alive
lastPong = osTime()
Event:event({type='check'},function(env)
if osTime()-lastPong >= 2*60 then
Log(LOG.LOG,"Scene %s not answering, restarting scene",scene2)
fibaro:killScenes(scene2)
fibaro:startScene(scene2)
end
Event.postRemote(scene2,{type='ping'})
Event.post({type='check',"+/00:02") -- check every 2 minutes
end)
Event.event({type='pong'},function(env) lastPong=osTime() end)
Then we have scene2 that answers 'ping' requests. A received remote event has a '._from' field with the scene that sent the event. This makes it convenient to respond to the sender
Event.event({type='ping'},function(env) Event.postRemote(env.event._from,{type='pong'}) end)
...