X-Git-Url: https://git.rvb.name/weathermon.git/blobdiff_plain/da331fea01b7d2bc15b7628366b03b9e3ba1a395..21464a29a442522e1f34fe885f7060f0fecf9a59:/bin/weather-lcd diff --git a/bin/weather-lcd b/bin/weather-lcd new file mode 100755 index 0000000..69cd421 --- /dev/null +++ b/bin/weather-lcd @@ -0,0 +1,158 @@ +#!/usr/bin/lua + +socket = require "socket" +json = require "json" +require "uci" +cur = uci.cursor() +require "wm_util" + +config_name = arg[1] +if not config_name then + config_name = "weathermon" +end + +weather_file = uci.get(config_name,"process","dump_file") + +function round(num, numDecimalPlaces) + local mult = 10^(numDecimalPlaces or 0) + return math.floor(num * mult + 0.5) / mult +end + +function trim(s) + return (s:gsub("^%s*(.-)%s*$", "%1")) +end + +function lpad(s, l, c) + local res = string.rep(c or ' ', l - #s) .. s + return res, res ~= s +end + +function process_file() + txt = get_file_content(weather_file) + data = json.decode(txt) + for k,v in pairs(data) do + return v + end +end + +function get_display_config() + width = tonumber(uci.get(config_name,"display","width")) + height = tonumber(uci.get(config_name,"display","height")) + cols = tonumber(uci.get(config_name,"display","columns")) + title = uci.get(config_name,"display","title") + col_width = math.floor(width/cols) + + pages = {} + pagetitles = {} + + local page, def, line, col, pos + + cur.foreach(config_name,"display", function(s) + page = {} + local pagetitle = s["title"] + if not pagetitle then + pagetitle = title + end + if not (pagetitle=="") then + line = 2 + else + line = 1 + end + col = 1 + pos = 0 + for k,v in pairs(s["parameter"]) do + def = {} + if not(v == "") then + v = split(v,":") + def["sensor"] = v[1] + def["param"] = v[2] + def["label"] = v[3] + def["unit"] = v[4] + def["scale"] = tonumber(v[5]) + def["round"] = tonumber(v[6]) + def["pos"] = tonumber(v[7]) + def["line"] = line + def["col"] = col + page[#page+1] = def + end + col = col + 1 + if col > cols then + col = 1 + line = line + 1 + end + end + pages[#pages+1] = page + pagetitles[#pages] = pagetitle + end) + +end + +function connect_server() + local ip = uci.get(config_name,"display","server") + local port = uci.get(config_name,"display","port") + conn = socket.connect(ip,port) + return conn +end + +function write_command(conn,command) + conn:send(command.."\n") + return(conn:receive()) +end + +function setup_pages(conn) + write_command(conn,"hello") + write_command(conn,"client_set -name weathermon") + for i,page in pairs(pages) do + local pageid = "page"..trim(tostring(i)) + write_command(conn,"screen_add "..pageid) + local pagetitle = pagetitles[i] + if not(pagetitle == "") then + write_command(conn,"screen_set -name "..pagetitle) + write_command(conn,"widget_add "..pageid.." "..pageid..".title title") + write_command(conn,"widget_set "..pageid.." "..pageid..".title \"".. pagetitle.."\"") + end + for j,def in pairs(page) do + defid = def["sensor"].."."..def["param"] + write_command(conn,"widget_add "..pageid.." "..defid.." string") + end + end +end + +function process_vals(vals) + for i,page in pairs(pages) do + local pageid = "page"..trim(tostring(i)) + for j,def in pairs(page) do + val = vals[def["sensor"]][def["param"]] + if val then + val = round(val * def["scale"], def["round"]) + end + defid = def["sensor"].."."..def["param"] + val = lpad(tostring(val),def["pos"]).." "..def["unit"] + if not(def["label"] == "") then + val = def["label"]..": "..val + end + write_command(conn,"widget_set "..pageid.." "..defid.." "..trim(tostring((def["col"]-1)*col_width+1)).." "..def["line"].." \""..val.."\"") + end + end +end + +get_display_config() + +conn = connect_server() + +setup_pages(conn) + +while true do + + pcall(function () + + vals = process_file() + process_vals(vals) + + os.execute("inotifywait -e MODIFY \""..weather_file.."\"") + socket.sleep(3) + + end) + +end +