-
Notifications
You must be signed in to change notification settings - Fork 3
/
WaitFor.sel
executable file
·83 lines (71 loc) · 1.81 KB
/
WaitFor.sel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!./Selene
-- Event notification example (using timers and MQTT)
-- Callbacks
function bip_timer()
print 'bip'
end
function bip_timer2()
print 'bip2'
end
function handle_tata( topic, msg )
print("Lua received t:'" .. topic .."' m:'".. msg .."'");
SelShared.set('tata', msg)
return true
end
function handle_toto( topic, msg )
print("Lua received t:'" .. topic .."' m:'".. msg .."'");
SelShared.set('toto', msg)
return true
end
-- Update functions
function update_tata()
print("update_tata()")
end
function update_toto()
print("update_toto()")
print( "toto='".. SelShared.get("toto") .."'" )
end
-- Connection, subscription and finally waiting for messages
Brk, err = SelMQTT.connect( "tcp://localhost:1883", { reliable=false } )
if not Brk then
print( err )
return
end
_, err = Brk:subscribe( {
{ topic = "/tata/+/truc", func=handle_tata, trigger=update_tata, trigger_once=false, qos=SelMQTT.QoSConst("QoS0") },
{ topic = "/toto",func=handle_toto, trigger=update_toto, trigger_once=true }
} )
if err then
print( err )
return
end
-- Create a timer with immediate function
itmr, err = SelTimer.create { when=3.5, interval=2, clockid=SelTimer.ClockModeConst("CLOCK_MONOTONIC"), ifunc=bip_timer }
if err then
print(err)
return
end
-- Another one with a task
ttmr, err = SelTimer.create { when=7, interval=3.5, clockid=SelTimer.ClockModeConst("CLOCK_MONOTONIC"), task=bip_timer2, once=true }
if err then
print(err)
return
end
-- Wait for events
if not table.pack then
function table.pack (...)
return {n=select('#',...); ...}
end
end
while true do
local rt = table.pack( Selene.WaitFor(itmr, ttmr, io.stdin) )
for _,ret in ipairs(rt) do
if type(ret) == 'function' then
ret()
elseif ret == io.stdin then
print('reading from stdin')
local l = io.read()
if l == nil then break end
end
end
end