From 1f25b6eb30c1aee66f1e463c411f32fa0691ab46 Mon Sep 17 00:00:00 2001 From: Nathaniel Wesley Filardo Date: Tue, 17 Jan 2017 03:59:19 -0500 Subject: [PATCH] net/*: Robustify calls to json parsers --- net/nwfmqtt.lua | 4 ++-- net/nwfnet-go.lua | 4 ++-- net/nwfnet-sntp.lua | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/net/nwfmqtt.lua b/net/nwfmqtt.lua index e17e81b..9172589 100644 --- a/net/nwfmqtt.lua +++ b/net/nwfmqtt.lua @@ -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 diff --git a/net/nwfnet-go.lua b/net/nwfnet-go.lua index 962c142..628813f 100644 --- a/net/nwfnet-go.lua +++ b/net/nwfnet-go.lua @@ -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") diff --git a/net/nwfnet-sntp.lua b/net/nwfnet-sntp.lua index 059f7d1..fc43089 100644 --- a/net/nwfnet-sntp.lua +++ b/net/nwfnet-sntp.lua @@ -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") -- 2.50.1