-
Notifications
You must be signed in to change notification settings - Fork 16
/
mqtt-queue-helper.lua
36 lines (36 loc) · 1.04 KB
/
mqtt-queue-helper.lua
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
------------------------------------------------------------------------------
-- MQTT queuing publish helper
--
-- LICENSE: http://opensource.org/licenses/MIT
-- Vladimir Dronnikov <[email protected]>
--
-- Example:
-- local mqtt = require("mqtt").Client(NAME, 60)
-- local pub = dofile("mqtt-queue-helper.lua")(mqtt)
-- pub(topic1, pload1); pub(topic2, pload2, qos); pub(topic3, pload3, 2, true)
------------------------------------------------------------------------------
do
-- factory
local make_publisher = function(client)
local queue = { }
local is_sending = false
local function send()
if #queue > 0 then
local tp = table.remove(queue, 1)
client:publish(tp[1], tp[2], tp[3], tp[4], send)
else
is_sending = false
end
end
-- NB: {...} stopped working (
return function(topic, message, qos, retain)
queue[#queue + 1] = {topic, message, qos, retain}
if not is_sending then
is_sending = true
send()
end
end
end
-- expose
return make_publisher
end