3 socket = require "socket"
10 if not config_name then
11 config_name = "weathermon"
14 weather_file = uci.get(config_name,"process","dump_file")
16 function round(num, numDecimalPlaces)
17 local mult = 10^(numDecimalPlaces or 0)
18 return math.floor(num * mult + 0.5) / mult
22 return (s:gsub("^%s*(.-)%s*$", "%1"))
25 function lpad(s, l, c)
26 local res = string.rep(c or ' ', l - #s) .. s
30 function process_file()
31 txt = get_file_content(weather_file)
32 data = json.decode(txt)
33 for k,v in pairs(data) do
38 function get_display_config()
39 width = tonumber(uci.get(config_name,"display","width"))
40 height = tonumber(uci.get(config_name,"display","height"))
41 cols = tonumber(uci.get(config_name,"display","columns"))
42 title = uci.get(config_name,"display","title")
43 col_width = math.floor(width/cols)
48 local page, def, line, col, pos
50 cur.foreach(config_name,"display", function(s)
52 local pagetitle = s["title"]
56 if not (pagetitle=="") then
63 for k,v in pairs(s["parameter"]) do
71 def["scale"] = tonumber(v[5])
72 def["round"] = tonumber(v[6])
73 def["pos"] = tonumber(v[7])
84 pages[#pages+1] = page
85 pagetitles[#pages] = pagetitle
90 function connect_server()
91 local ip = uci.get(config_name,"display","server")
92 local port = uci.get(config_name,"display","port")
93 conn = socket.connect(ip,port)
97 function write_command(conn,command)
98 conn:send(command.."\n")
99 return(conn:receive())
102 function setup_pages(conn)
103 write_command(conn,"hello")
104 write_command(conn,"client_set -name weathermon")
105 for i,page in pairs(pages) do
106 local pageid = "page"..trim(tostring(i))
107 write_command(conn,"screen_add "..pageid)
108 local pagetitle = pagetitles[i]
109 if not(pagetitle == "") then
110 write_command(conn,"screen_set -name "..pagetitle)
111 write_command(conn,"widget_add "..pageid.." "..pageid..".title title")
112 write_command(conn,"widget_set "..pageid.." "..pageid..".title \"".. pagetitle.."\"")
114 for j,def in pairs(page) do
115 defid = def["sensor"].."."..def["param"]
116 write_command(conn,"widget_add "..pageid.." "..defid.." string")
121 function process_vals(vals)
122 for i,page in pairs(pages) do
123 local pageid = "page"..trim(tostring(i))
124 for j,def in pairs(page) do
125 val = vals[def["sensor"]][def["param"]]
127 val = round(val * def["scale"], def["round"])
129 defid = def["sensor"].."."..def["param"]
130 val = lpad(tostring(val),def["pos"]).." "..def["unit"]
131 if not(def["label"] == "") then
132 val = def["label"]..": "..val
134 write_command(conn,"widget_set "..pageid.." "..defid.." "..trim(tostring((def["col"]-1)*col_width+1)).." "..def["line"].." \""..val.."\"")
141 conn = connect_server()
149 vals = process_file()
152 os.execute("inotifywait -e MODIFY \""..weather_file.."\"")