-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqttService.lua
78 lines (71 loc) · 2.42 KB
/
mqttService.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
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
local module = {}
module.name = 'Mqtt'
module.connected = false
module.onReceive = nil
local moduleConfig = nil
local mq = nil
local user_disconnect = false
local function echo (text)
print('['..module.name..'] '..text)
end
local function handle_message (conn, topic, data)
echo ("Message received! Topic "..topic .. " data " .. data )
if(module.onReceive ~= nil) then
module.onReceive(topic,data)
end
end
function module.init(cnf)
moduleConfig = cnf
mq = mqtt.Client(cnf.nodeId,60,cnf.user, cnf.pwd)
mq:on("message", handle_message)
mq:on('offline',function(client)
if (module.connected) then
echo('Disconnected from broker!')
if(user_disconnect == false) then
echo('Will attempt reconnect in 10 sec.')
tmr.create():alarm(10 * 1000, tmr.ALARM_SINGLE, module.connect)
else
module_disconnect = false
end
module.connected = false
end
end)
mq:lwt(moduleConfig.nodeTopic,'offline',moduleConfig.defaultQos)
end
function module.connect()
mq:connect(moduleConfig.host, moduleConfig.port, moduleConfig.secure, 0, function(con)
echo('Connected to broker on '.. moduleConfig.host ..'!')
module.connected = true
mq:publish(moduleConfig.nodeTopic,'online',moduleConfig.defaultQos,0)
for i=0,9 do
if(moduleConfig.subscribeTopics[i] ~= nil) then
mq:subscribe(moduleConfig.subscribeTopics[i],0, function(conn) echo("Successfully subscribed to " .. moduleConfig.subscribeTopics[i]) end)
end
end
end,
function(client,reason)
echo('Cannot connect to broker on '.. moduleConfig.host .. ':' .. moduleConfig.port ..'. Reason ' .. reason)
tmr.create():alarm(10 * 1000, tmr.ALARM_SINGLE, module.connect)
module.connected = false
end
)
end
function module.disconnect()
user_disconnect = true
mq:close()
end
function module.publish(topic,message,qos, retain)
if(module.connected == false) then
echo('Not connected to broker! Cannot publish yet!')
return
end
if (qos == nil) then
qos = moduleConfig.defaultQos
end
if (retain == nil) then
retain = 0
end
echo('Sending data to '..topic)
return mq:publish(moduleConfig.topic .. '/'..topic,message,qos,retain,function (client) echo('Data sent!') end)
end
return module