36
CONTENTS
function mirror_outlet_1_to_2()
outlet[2].state=outlet[1].physical_state
for i,t,data in event.stream(event.change_listener(outlet[1])) do
if data.key=="physical_state" then
outlet[2].state=data.value
end
end
end
The
event.local_time
and
event.utc_time
functions take any number of time-matching
as arguments and creates an event queue where events appear in time moments matching the conditions. This can
be seen as an improved
wait_until
function which doesn't miss triggers if handling the event takes too long.
The most important difference is that new checks are "edge-triggered", not "level-triggered": an event is placed
into the queue only when the condition becomes true; no events are created when the condition stays true until it
becomes false and then true again. For example, if you start a loop waiting for
{hour=7,min=10}
at 7:10, the
event will not be created right away and the loop will not be executed until the next day, whereas
wait_until
would return immediately. This is because a "level-triggered" approach might have to add an infinite number of
events to the queue. This also means that you usually won't have to add e.g. min=0,sec=0' or similar to the
condition to make it start only at the beginning of an hour. Additionally,
wait_until
only handles local time,
which corresponds to
event.local_time
behaviour, but you can match UTC time with
event.utc_time
,
which has no corresponding
wait_until
option. Events have a timestamp and a property table as components;
the property table contains the following fields:
•
index
: the condition's number in the argument list;
•
time
: the time matching the condition (not necessarily exactly one of the timestamp).
The following example function can be used to check outlet 1 state and report if it remains physically off for more
than an hour. Note that we do not simply poll the outlet e.g. every minute, but subscribe to state change events
instead so that we don't miss any changes. We also create the change event queue before the initial state check so
that we don't miss any state changes during initialization.
function monitor_1()
local reported
local off_since
local changes=event.change_listener(outlet[1])
if not outlet[1].physical_state then
off_since=os.time() -- We don't know that for sure but that's when we start monitoring
end
for i,t,data in event.stream(changes,event.utc_time({sec=0})) do
if i==2 then
if off_since and t-off_since>3600 then
if not reported then
log.warning("Off for too long, since %s",os.date("%c",off_since))
reported=true
end
end
elseif data.key=="physical_state" then
if data.value==true then
off_since=nil
if reported then
log.notice("On again, phew")
reported=true
end
elseif off_since==nil then
off_since=t
end
end
end
end
Of course we could have used e.g.
event.send
instead of
log
, or flipped another outlet, etc.
DLI LPC9 User’s Guide: 1.7.24.0
Summary of Contents for LPC9
Page 1: ...DLI LPC9 User s Guide 1 7 24 0 ...
Page 81: ......