]> hydra-www.ietfng.org Git - acmetensortoys-esp-lua_core/commitdiff
Slightly improve MQTT client construction
authorNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Thu, 13 Oct 2016 23:09:52 +0000 (19:09 -0400)
committerNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Thu, 13 Oct 2016 23:09:52 +0000 (19:09 -0400)
Add early check for nil user and password
Add support for cleansession flag from JSON file

net/nwfmqtt.lua

index 07ae8fb50dd482c0e10f5b30702f921073c48683..0882a4af927d5953385c89c62f43e31790e35887 100644 (file)
@@ -2,21 +2,24 @@
 local nwfnet = require "nwfnet"
 local self = {}
 function self.mkclient(cf) -- construct a client with config from json file cf
+  local c, k, u, p
   if file.open(cf) then
     local conf = cjson.decode(file.read())
-    local c, k, u, p
     if type(conf) == "table" then
-      c = conf["clientid"]; k = conf["keepalive"]; u = conf["user"]; p = conf["pass"]
+      c = conf["clientid"]; k = conf["keepalive"]; u = conf["user"]; p = conf["pass"]; l = conf["clean"]
     end
+    file.close()
+    if not u or not p then return nil end
     c = c or string.format("NODE-%06X",node.chipid())
     k = k or 1500
-    file.close()
-    local m = mqtt.Client(c,k,u,p)
+    l = l or 0
+    local m = mqtt.Client(c,k,u,p,l)
     m:on("connect", function(c) nwfnet:runnet("mqttconn",c) end)
     m:on("offline", function(c) nwfnet:runnet("mqttdscn",c) end)
     m:on("message", function(c,t,m) nwfnet:runmqtt(c,t,m) end)
     return m, u, c
   end
+  return nil
 end
 function self.connect(m,cf) -- make a connection with parameters from json file cf
   local broker, port, secure