X-Git-Url: https://git.rvb.name/weathermon.git/blobdiff_plain/3cde6e4128c98b6b9c35f52fff560e4f20e2d4d8..9b024ef6c2d9a1f8260fadd562f8d2298a50eb3e:/bin/weather-lcd?ds=inline diff --git a/bin/weather-lcd b/bin/weather-lcd index 17436ef..d3283ca 100755 --- a/bin/weather-lcd +++ b/bin/weather-lcd @@ -19,7 +19,7 @@ graph_duration = uci.get(config_name,"display","graph_duration") if graph_duration and not(weather_db == "") then pcall(function () local dbdriver = require "luasql.sqlite3" - local env = assert(dbdriver.sqlite3()) + env = assert(dbdriver.sqlite3()) log_con = assert(env:connect(weather_db)) end) end @@ -155,11 +155,13 @@ function setup_pages(conn) write_command(conn,"screen_add "..pageid) write_command(conn,"screen_set "..pageid.." -cursor off -name "..pagetitle.." -duration "..graph_duration) write_command(conn,"widget_add "..pageid.." "..pageid..".title title") - write_command(conn,"widget_add "..pageid.." "..pageid..".max string") - write_command(conn,"widget_add "..pageid.." "..pageid..".min string") - for k = 3,height-1 do - write_command(conn,"widget_add "..pageid.." "..pageid..".place"..trim(tostring(k)).." string") - end + if height>2 then + write_command(conn,"widget_add "..pageid.." "..pageid..".max string") + write_command(conn,"widget_add "..pageid.." "..pageid..".min string") + for k = 3,height-1 do + write_command(conn,"widget_add "..pageid.." "..pageid..".place"..trim(tostring(k)).." string") + end + end write_command(conn,"widget_set "..pageid.." "..pageid..".title \"".. pagetitle.."\"") end end @@ -191,15 +193,16 @@ function process_graphs() local sensor = def["sensor"] local param = def["param"] local pageid = sensor.."."..param - local cur = assert (log_con:execute(string.format("select hour,avg(value) val from (select strftime('%%Y-%%m-%%d %%H',time_stamp)||':00' hour,value from log where sensor='%s' and param='%s') group by hour order by hour desc limit 24",sensor,param))) - local row = cur:fetch ({}, "a") + local sqlcur = assert (log_con:execute(string.format("select hour,avg(value) val from (select strftime('%%Y-%%m-%%d %%H',time_stamp)||':00' hour,value from log where sensor='%s' and param='%s') group by hour order by hour desc limit 24",sensor,param))) + local row = sqlcur:fetch ({}, "a") local vals = {} local maxval = -99999999 local minval = 99999999 + local len for k=width,1,-1 do if row then val = row["val"] * def["scale"] - row = cur:fetch ({}, "a") + row = sqlcur:fetch ({}, "a") else val = nil end @@ -207,16 +210,21 @@ function process_graphs() if val and (val > maxval) then maxval = val; end if val and (val < minval) then minval = val; end end + sqlcur:close() minval = math.floor(minval) - local minvalstr = trim(tostring(minval)) maxval = math.ceil(maxval) - local maxvalstr = trim(tostring(maxval)) - local len = math.max(string.len(minvalstr),string.len(maxvalstr)) - write_command(conn,"widget_set "..pageid.." "..pageid..".max "..trim(tostring(width-string.len(maxvalstr)+1)).." 2 "..maxvalstr) - for k = 3,height-1 do - write_command(conn,"widget_set "..pageid.." "..pageid..".place"..trim(tostring(k)).." "..trim(tostring(width-len+1)).." "..k.." \" "..string.rep("-",len-1).."\"") - end - write_command(conn,"widget_set "..pageid.." "..pageid..".min "..trim(tostring(width-string.len(minvalstr)+1)).." "..height.." "..minvalstr) + if height>2 then + local minvalstr = trim(tostring(minval)) + local maxvalstr = trim(tostring(maxval)) + len = math.max(string.len(minvalstr),string.len(maxvalstr)) + write_command(conn,"widget_set "..pageid.." "..pageid..".max "..trim(tostring(width-string.len(maxvalstr)+1)).." 2 "..maxvalstr) + write_command(conn,"widget_set "..pageid.." "..pageid..".min "..trim(tostring(width-string.len(minvalstr)+1)).." "..height.." "..minvalstr) + for k = 3,height-1 do + write_command(conn,"widget_set "..pageid.." "..pageid..".place"..trim(tostring(k)).." "..trim(tostring(width-len+1)).." "..k.." \" "..string.rep("-",len-1).."\"") + end + else + len = 0 + end local m = width for k=1,width do write_command(conn,"widget_del "..pageid.." "..pageid..".bar"..trim(tostring(k))) @@ -244,14 +252,19 @@ setup_pages(conn) while true do - vals = process_file() - process_vals(vals) +-- pcall( function () - if log_con then - process_graphs() - end + vals = process_file() + process_vals(vals) + + if weather_db then + process_graphs() + end - os.execute("inotifywait -e MODIFY \""..weather_file.."\"") - socket.sleep(3) + os.execute("inotifywait -e MODIFY \""..weather_file.."\"") + +-- end) + + socket.sleep(3) end