Переход на отображение через lcdd, мелкие правки и доработки.
authorRoman Bazalevsky <rvb@rvb.name>
Tue, 21 Jan 2020 09:18:01 +0000 (12:18 +0300)
committerRoman Bazalevsky <rvb@rvb.name>
Tue, 21 Jan 2020 09:18:01 +0000 (12:18 +0300)
bin/weather-lcd [new file with mode: 0755]
bin/weathermon
bin/weathermon-iio

diff --git a/bin/weather-lcd b/bin/weather-lcd
new file mode 100755 (executable)
index 0000000..69cd421
--- /dev/null
@@ -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
+
index fdf092a3406aa0c62a02fdc4954af2a618288e04..bd9ca80200d5e226cddfce4c1c212d98463f8aa8 100755 (executable)
@@ -157,9 +157,6 @@ function processJson(str)
   end
 
   if not (sensor_type==nil or sensor_id==nil or sensor_type=='' or sensor_id=='') then
-    if next(sensor)==nil then
-      sensor["command"]="alarm"
-    end
     local record = {}
     for k,v in pairs(sensor) do
       storeRecord(sensor_id,sensor_type,k,v)
@@ -411,6 +408,7 @@ while 1 do
         io.close(f)
       end)  
     end
+
   end)
     
 end
index dcbca6430d3dc4bc0bfde364be9a1c0d55068043..c5a8102f6f038fc74bb76571b1613e25f6e5c37a 100755 (executable)
@@ -157,7 +157,7 @@ function init_serial_device(device,subsystem,parameters)
     if subsystem == "mhz" then
     
       getparameter["function"] = get_mhz 
-      getparameter["name"] = "CO2_PPM"
+      getparameter["name"] = "CO2PPM"
       getparameter["sensor"] = "MHZ19"
       parameters[#parameters+1] = getparameter