X-Git-Url: https://git.rvb.name/weathermon.git/blobdiff_plain/e32107a7fe79ce34f3bdf860410a6d5455efdca7..be44087bf603e595c684d83c727e6441c7b7f39a:/bin/weathermon diff --git a/bin/weathermon b/bin/weathermon index 03fad42..fb431a7 100755 --- a/bin/weathermon +++ b/bin/weathermon @@ -118,7 +118,9 @@ end function submitValue(type,id,param,val) - if web_url then + val = tonumber(val) + + if web_url and val then local url = web_url.."?stype="..url_encode(type).."&sid="..url_encode(id).."¶m="..url_encode(param).."&value="..url_encode(val) @@ -133,8 +135,8 @@ function submitValue(type,id,param,val) return req_sock end}) - if code ~= 200 then - print("writing record to backlog...") + if code ~= 200 and backlog_con then + printLog("writing record to backlog...") backlog_con:execute(string.format("INSERT INTO queue(time_stamp,sensor_id,sensor,param,value) VALUES (datetime('now','localtime'),'%s','%s','%s',%f)",id,type,param,val)) end @@ -346,22 +348,18 @@ end if backlogdb then if not file_exists(backlogdb) then touch(backlogdb) - backlog_con = assert(env:connect(backlogdb)) - backlog_con:execute("CREATE TABLE queue(time_stamp datetime,sensor_id varchar(16),sensor varchar(16),param varchar(16),value float)") - else - backlog_con = assert(env:connect(backlogdb)) - end + end + backlog_con = assert(env:connect(backlogdb)) + backlog_con:execute("CREATE TABLE queue(time_stamp datetime,sensor_id varchar(16),sensor varchar(16),param varchar(16),value float)") end if logdb then if not file_exists(logdb) then touch(logdb) - log_con = assert(env:connect(logdb)) - log_con:execute("CREATE TABLE log(time_stamp datetime,sensor_id varchar(16),sensor varchar(16),param varchar(16),value float)") - log_con:execute("CREATE INDEX log_idx ON log(sensor_id,sensor,param,time_stamp)") - else - log_con = assert(env:connect(logdb)) - end + end + log_con = assert(env:connect(logdb)) + log_con:execute("CREATE TABLE log(time_stamp datetime,sensor_id varchar(16),sensor varchar(16),param varchar(16),value float)") + log_con:execute("CREATE INDEX log_idx ON log(sensor_id,sensor,param,time_stamp)") end if mqtt_host then @@ -385,24 +383,33 @@ else return end +serialin:setvbuf('no') + records = {} while 1 do + line=serialin:read("*l") + if line == nil then break end - printLog("Received: "..line); - if startswith(line,'{') then - processJson(line) - else - processLine(line) - end - if dump_file then - local f = io.open(dump_file,"w") - io.output(f) - io.write(json.encode(records)) - io.close(f) - end + pcall(function () + + printLog("Received: "..line) + if startswith(line,'{') then + processJson(line) + else + processLine(line) + end + + if dump_file then + local f = io.open(dump_file,"w") + io.output(f) + io.write(json.encode(records)) + io.close(f) + end + end) + end