]> hydra-www.ietfng.org Git - acmetensortoys-esp-lua_core/commitdiff
net/*: Robustify calls to json parsers
authorNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Tue, 17 Jan 2017 08:59:19 +0000 (03:59 -0500)
committerNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Wed, 18 Jan 2017 02:18:02 +0000 (21:18 -0500)
net/nwfmqtt.lua
net/nwfnet-go.lua
net/nwfnet-sntp.lua

index e17e81b6d1dc04a8b6582405b0fd532c0aeebddc..917258908c9d729cf3aad03aa4c9814a45bd5018 100644 (file)
@@ -4,7 +4,7 @@ local self = {}
 function self.mkclient(cf) -- construct a client with config from json file cf
   local c, k, u, p, l
   if file.open(cf) then
-    local conf = cjson.decode(file.read())
+    local conf = cjson.decode(file.read() or "")
     if type(conf) == "table" then
       c = conf["clientid"]; k = conf["keepalive"]; u = conf["user"]; p = conf["pass"]; l = conf["clean"]
     end
@@ -24,7 +24,7 @@ end
 function self.connect(m,cf) -- make a connection with parameters from json file cf
   local broker, port, secure
   if file.open(cf) then
-    local conf = cjson.decode(file.read())
+    local conf = cjson.decode(file.read() or "")
     if type(conf) == "table" then
       broker = conf["broker"]; port = conf["port"]; secure = conf["secure"]
     end
index 962c142ea5871376fac4a0f6cb539804bf36a685..628813f68509f9e2eca38368ef27da6572c299fb 100644 (file)
@@ -11,7 +11,7 @@ wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(t) (require "nwf
 -- One-shot configuration options; useful to change many things at once
 -- at the next boot; all of these options are persisted by the ESP
 if file.open("nwfnet.conf","r") then
-  local conf = cjson.decode(file.read())
+  local conf = cjson.decode(file.read() or "")
   if type(conf) == "table" then
     local essid = conf["sta_essid"]; local pw = conf["sta_pw"]
     if essid ~= nil and pw ~= nil then wifi.sta.config(essid,pw,0) end
@@ -47,7 +47,7 @@ if file.open("nwfnet.cert","r") then
 end
 
 if file.open("nwfnet.conf2","r") then
-  local conf = cjson.decode(file.read())
+  local conf = cjson.decode(file.read() or "")
   if type(conf) == "table" then
     if conf["verify"] == 1 then print("Enabling certificate verification"); pcall(net.cert.verify,true) end
    else print("nwfnet.conf2 malformed")
index 059f7d1822fb987b5fc481c248e2dd6dd2f980ab..fc43089705885e94e6fdf2783d0a8b45da46a469 100644 (file)
@@ -2,7 +2,7 @@ local function dosntp(server)
   local nn = require "nwfnet"
   if not server then
     if file.open("nwfnet.conf2","r") then
-      local conf = cjson.decode(file.read())
+      local conf = cjson.decode(file.read() or "")
       if type(conf) == "table" then
         if conf["sntp"]   then print("Setting SNTP server"); server = conf["sntp"] end
        else print("nwfnet.conf2 malformed")