X-Git-Url: https://git.rvb.name/weathermon.git/blobdiff_plain/d63010ef6c3cc76ac2a035c0249c87a78e38d980..76cb925db11bf98d7fc7030401f05f68043cc89a:/bin/weathermon diff --git a/bin/weathermon b/bin/weathermon index 6980cc8..804ba25 100755 --- a/bin/weathermon +++ b/bin/weathermon @@ -1,9 +1,6 @@ #!/usr/bin/lua local json = require("json") -local socket = require("socket") - -local http = require("socket.http") require "wm_util" @@ -24,37 +21,12 @@ function getConfig(configname) web_user = cur.get(config,"web","user") web_timeout = cur.get(config,"web","timeout") web_pass = cur.get(config,"web","password") - web_devid = cur.get(config,"web","devid") - - web_iface = cur.get(config,"web","iface") if not web_timeout then web_timeout = 10 end - if web_iface then - - command = '/sbin/ifconfig '..web_iface..' | grep \'\\\' | sed -n \'1p\' | tr -s \' \' | cut -d \' \' -f3 | cut -d \':\' -f2' - f=io.popen(command,'r') - ip_addr=f:read() - - end - - if not web_devid then - - if web_iface then - io.input("/sys/class/net/"..web_iface.."/address") - else - io.input("/sys/class/net/eth0/address") - end - - local mac = io.read("*line") - mac = mac:gsub(":","") - mac = mac:upper() - - web_devid = mac - - end + web_devid = get_devid(config) logging = cur.get(config,"logging","enabled") touch_file = cur.get(config,"logging","touch_file") @@ -118,7 +90,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 +107,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 @@ -144,10 +118,6 @@ function submitValue(type,id,param,val) log_con:execute(string.format("INSERT INTO log(time_stamp,sensor_id,sensor,param,value) VALUES (datetime('now','localtime'),'%s','%s','%s',%f)",id,type,param,val)) end - if touch_file then - touch(touch_file) - end - end function storeRecord(id,sensor,param,value) @@ -198,7 +168,9 @@ function processJson(str) for k,v in pairs(sensor) do storeRecord(sensor_id,sensor_type,k,v) printLog("Type = "..sensor_type..", ID = "..sensor_id..", Param = "..k..", Value = \""..v.."\"") - submitValue(sensor_type,sensor_id,k,v) + if web_url then + submitValue(sensor_type,sensor_id,k,v) + end if mqtt_client then mqtt_path=string.gsub(mqtt_topic,"{(.-)}", function (name) @@ -214,9 +186,11 @@ function processJson(str) return '{'..name..'}' end end) - mqtt_client:connect(mqtt_host,mqtt_port) + if not mqtt_client:socket() then + mqtt_client:reconnect() + end mqtt_client:publish(mqtt_path,v) - mqtt_client:disconnect() + mqtt_client:loop() end end else @@ -260,7 +234,9 @@ function processLine(str) for k,v in pairs(sensor) do storeRecord(sensor_id,sensor_type,k,v) printLog("Type = "..sensor_type..", ID = "..sensor_id..", Param = "..k..", Value = "..v) - submitValue(sensor_type,sensor_id,k,v) + if web_url then + submitValue(sensor_type,sensor_id,k,v) + end if mqtt_client then mqtt_path=string.gsub(mqtt_topic,"{(.-)}", function (name) @@ -276,9 +252,8 @@ function processLine(str) return '{'..name..'}' end end) - mqtt_client:connect(mqtt_host,mqtt_port) mqtt_client:publish(mqtt_path,v) - mqtt_client:disconnect() + mqtt_client:loop() end end else @@ -318,9 +293,8 @@ function processLine(str) return '{'..name..'}' end end) - mqtt_client:connect(mqtt_host,mqtt_port) mqtt_client:publish(mqtt_path,msg_body) - mqtt_client:disconnect() + mqtt_client:loop() end if alarm_exec then command=alarm_exec.. @@ -346,22 +320,23 @@ 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 web_url then + http = require("socket.http") + socket = require("socket") end if mqtt_host then @@ -370,6 +345,7 @@ if mqtt_host then if mqtt_user then mqtt_client:login_set(mqtt_user, mqtt_passwd) end + mqtt_client:connect(mqtt_host,mqtt_port) end if serial_port then @@ -385,6 +361,8 @@ else return end +serialin:setvbuf('no') + records = {} while 1 do @@ -404,11 +382,19 @@ while 1 do processLine(line) end + if touch_file then + pcall(function () + touch(touch_file) + end) + end + if dump_file then - local f = io.open(dump_file,"w") - io.output(f) - io.write(json.encode(records)) - io.close(f) + pcall(function () + local f = io.open(dump_file,"w") + io.output(f) + io.write(json.encode(records)) + io.close(f) + end) end end)