X-Git-Url: https://git.rvb.name/weathermon.git/blobdiff_plain/be44087bf603e595c684d83c727e6441c7b7f39a..5bc9573c3235d959463e75f6103b027d6f5efe84:/bin/weathermon?ds=inline diff --git a/bin/weathermon b/bin/weathermon index fb431a7..fdf092a 100755 --- a/bin/weathermon +++ b/bin/weathermon @@ -1,9 +1,7 @@ #!/usr/bin/lua local json = require("json") -local socket = require("socket") - -local http = require("socket.http") +local signal = require("posix.signal") require "wm_util" @@ -24,37 +22,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") @@ -128,12 +101,7 @@ function submitValue(type,id,param,val) url = url:gsub("//","//"..web_user..":"..web_pass.."@",1) end - local result,code = http.request ({ - url=url, create=function() - local req_sock = socket.tcp() - req_sock:settimeout(web_timeout) - return req_sock - end}) + local result,code = http.request (url) if code ~= 200 and backlog_con then printLog("writing record to backlog...") @@ -146,10 +114,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) @@ -200,7 +164,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) @@ -216,9 +182,11 @@ function processJson(str) return '{'..name..'}' end end) - mqtt_client:connect(mqtt_host,mqtt_port) - mqtt_client:publish(mqtt_path,v) - mqtt_client:disconnect() + if not mqtt_client:socket() then + mqtt_client:reconnect() + end + mqtt_client:publish(mqtt_path,v,0,false) + mqtt_client:loop() end end else @@ -262,7 +230,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) @@ -278,9 +248,11 @@ function processLine(str) return '{'..name..'}' end end) - mqtt_client:connect(mqtt_host,mqtt_port) - mqtt_client:publish(mqtt_path,v) - mqtt_client:disconnect() + if not mqtt_client:socket() then + mqtt_client:reconnect() + end + mqtt_client:publish(mqtt_path,v,0,false) + mqtt_client:loop() end end else @@ -320,9 +292,11 @@ function processLine(str) return '{'..name..'}' end end) - mqtt_client:connect(mqtt_host,mqtt_port) - mqtt_client:publish(mqtt_path,msg_body) - mqtt_client:disconnect() + if not mqtt_client:socket() then + mqtt_client:reconnect() + end + mqtt_client:publish(mqtt_path,msg_body,0,false) + mqtt_client:loop() end if alarm_exec then command=alarm_exec.. @@ -340,6 +314,19 @@ end getConfig(arg[1]) +signal.signal(signal.SIGTERM, function(signum) + + printLog("Terminating...") + local pids = get_children() + for k,v in pairs(pids) do + printLog("Terminating subprocess "..tostring(v).."...") + signal.kill(v,signal.SIGTERM) + end + printLog("Exiting...") + os.exit(0) + +end) + if backlogdb or logdb then local dbdriver = require "luasql.sqlite3" env = assert(dbdriver.sqlite3()) @@ -362,12 +349,18 @@ if logdb then log_con:execute("CREATE INDEX log_idx ON log(sensor_id,sensor,param,time_stamp)") end +if web_url then + http = require("socket.http") + http.TIMEOUT = web_timeout +end + if mqtt_host then MQTT = require "mosquitto" mqtt_client = MQTT.new(mqtt_id) 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 @@ -404,11 +397,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)