From 81a276ab7840d6f9c64da80fe5d3bad88fb99b83 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Thu, 15 Oct 2015 19:24:05 +0200 Subject: wip --- diller/mq.lua | 52 ++++++++++++---------------------------------------- 1 file changed, 12 insertions(+), 40 deletions(-) (limited to 'diller/mq.lua') diff --git a/diller/mq.lua b/diller/mq.lua index 74f3ef8..212d964 100644 --- a/diller/mq.lua +++ b/diller/mq.lua @@ -1,48 +1,19 @@ local P = {} local m, topic, cid -local active_subscriptions = {} -local pending_subscriptions = {} -local pending_subcsription +local subscriptions = {} local function p(msg) print("MQTT: "..msg) end --- Subscribe to all registered subscriptions --- TODO: subscriptions that are not successfull will remain in state = 1. Might not be a real problem if it happens, the server probably disallowed the subscription. -local function create_subscriptions() - print("create_subscriptions, pending count "..table.getn(pending_subscriptions)..", pending: "..tostring(pending_subscription)) - - if (pending_subscription) then - return - end - - pending_subcsription = table.remove(pending_subscriptions) - - -- The API claims that the callback has this signature: function(client, topic, message) - -- but that doesn't seems right from the source code. The code doesn't - -- remember the topics that is being subscribed to. - m:subscribe(pending_subcsription, 0, function(client) - print("success: "..pending_subcsription) - table.insert(active_subscriptions, pending_subscription) - pending_subscription = nil - create_subscriptions() - end) -end - local function mq_client_connected(con) p("connected") - - create_subscriptions() end local function disconnect() p("Lost wifi connection, disconnecting") m:close() m = nil - - -- append active_subscriptions to pending_subscriptions - active_subscriptions = {} end function mq_on_timer() @@ -66,21 +37,21 @@ function mq_on_timer() end else if m then - disconnect() + disconnect() end end end -function P.init(timer_id, client_id) +function P.init(timer_id, device_id, client_id) cid = client_id - topic = "/esp8266/"..client_id + topic = "/diller/"..device_id - tmr.alarm(timer_id, 3 * 1000, 1, mq_on_timer) -end + tmr.alarm(timer_id, 4 * 1000, 1, mq_on_timer) +end function P.subscribe(path, cb) - subscription = subscriptions[path] + local subscription = subscriptions[path] if subscription then print("subscription on "..path.." already registered, state="..tostring(subscription.state)) return @@ -88,15 +59,16 @@ function P.subscribe(path, cb) print("Registering subscription on "..path) - pending_subscriptions[path] = true + subscriptions[path] = true + -- pending_subscriptions[path] = true - create_subscriptions() + -- create_subscriptions() end function P.publish(path, payload) if not m then - print("Not connected, dropping message to "..path) - return false, 'not connected' + print("Not connected, dropping message to "..path) + return false, 'not connected' end path = topic.."/"..path -- cgit v1.2.3