From 937040944ffdf7819b7ce7bee23a3d346718c0e4 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Thu, 19 Nov 2015 19:33:05 +0100 Subject: Major refactoring, splitting stuff into separate files. --- diller/mq.lua | 74 +++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 42 insertions(+), 32 deletions(-) (limited to 'diller/mq.lua') diff --git a/diller/mq.lua b/diller/mq.lua index f3136d6..524bff2 100644 --- a/diller/mq.lua +++ b/diller/mq.lua @@ -1,33 +1,44 @@ local P = {} -local m, topic, cid +local m, topic, cid, connected local subscriptions = {} local function mq_client_connected(con) info("mqtt: connected") + connected = true end local function subscribed() info("mqtt: subscribed") end -local function disconnect() - info("mqtt: Lost wifi connection, disconnecting") - m:close() - m = nil +local function offline() + info("mqtt: disconnected") + connected = false +-- m:close() +-- m = nil end local function on_message(client, topic, message) -- p("mqtt: on_message: topic="..topic..", message="..message) - local _, _, new_property_name = string.find(topic, '/property/([%a%d-]+)/name$') - if new_property_name then - print("msg cmd=new-property-name id="..new_property_name..", name="..message) - return; + local _, _, key = string.find(topic, '/property/([%a%d-]+)/name$') + if key then + local filename = 'property-'..key..'-name' + local old = read_cfg(filename, false) + if old ~= message then + write_cfg(filename, message) + end + print("msg cmd=new-property-name key="..key..", name=\""..message.."\"") end - local _, _, new_description_name = string.find(topic, '/property/([%a%d-]+)/description$') - if new_description_name then - print("msg cmd=new-property-description id="..new_description_name..", description="..message) + local _, _, key = string.find(topic, '/property/([%a%d-]+)/description$') + if key then + local filename = 'property-'..key..'-description' + local old = read_cfg(filename, false) + if old ~= message then + write_cfg(filename, message) + end + print("msg cmd=new-property-description key="..key..", description=\""..message.."\"") end end @@ -36,26 +47,16 @@ function mq_on_timer() -- local status = wifi.sta.status() -- p("mqtt: checking status, status="..status) - local status = wifi.sta.getip() - - if status and status ~= "0.0.0.0" then - if not m then - info("mqtt: connecting") + info("mqtt: connecting") - -- client id, keepalive, username, password - m = mqtt.Client(cid, 120) - -- m:on("connect", mq_connected) - m:on("offline", function() m:close(); m = nil end) - m:on("message", on_message) + -- client id, keepalive, username, password + m = mqtt.Client(cid, 120) + -- m:on("connect", mq_connected) + m:on("offline", offline) + m:on("message", on_message) - -- host, port, secure, auto_reconnect, function(client), ssl=8883 - m:connect("trygvis.io", 1883, 0, 0, mq_client_connected) - end - else - if m then - disconnect() - end - end + -- host, port, secure, auto_reconnect, function(client), ssl=8883 + m:connect("trygvis.io", 1883, 0, 1, mq_client_connected) end function P.init(timer_id, device_id, client_id) @@ -63,7 +64,8 @@ function P.init(timer_id, device_id, client_id) topic = "/diller/"..device_id - tmr.alarm(timer_id, 4 * 1000, 1, mq_on_timer) + -- Wait a bit to get a connection first + tmr.alarm(timer_id, 5 * 1000, 0, mq_on_timer) end function P.subscribe(path) @@ -88,10 +90,18 @@ function P.publish(path, payload) end path = topic.."/"..path - info("mqtt: path="..path) + -- info("mqtt: path="..path) m:publish(path, payload, 0, 0) return true, 'ok' end +function P.status() + if connected then + return 'connected' + else + return 'disconnected' + end +end + return P -- cgit v1.2.3