- Добавлен процесс для чтения iio-датчиков
[weathermon.git] / openwrt-web / cgi-bin / meteo.lua
diff --git a/openwrt-web/cgi-bin/meteo.lua b/openwrt-web/cgi-bin/meteo.lua
new file mode 100755 (executable)
index 0000000..8e38a49
--- /dev/null
@@ -0,0 +1,110 @@
+#!/usr/bin/lua
+
+require "uci"
+
+cursor = uci.cursor()
+
+archive_dir = uci.get("weathermon","process","archive_dir")
+
+require "wm_util"
+
+command = url_decode(os.getenv('QUERY_STRING'))
+
+print("Content-Type: text/plain\r\n")
+
+if command == "state" or command == "" or not command then
+  print(get_file_content(uci.get("weathermon","process","dump_file")))
+
+elseif command == "props" then
+  print(get_file_content("/www/meteo/properties.json"))
+
+elseif command == "years" then
+  json = require "json"
+  local result = list_dir(archive_dir)
+  table.sort(result, function(a, b) return a > b; end)
+  print(json.encode(result))
+
+elseif startswith(command,"months/") then
+  json = require "json"
+  local year = string.match(command,"months/(%d+)")
+  local result = list_dir(archive_dir.."/"..year)
+  table.sort(result, function(a, b) return a > b; end)
+  print(json.encode(result))
+
+elseif startswith(command,"days/") then
+  json = require "json"
+  local year,month = string.match(command,"days/(%d+)/(%d+)")
+  local result = list_dir(archive_dir.."/"..year.."/"..month)
+  table.sort(result, function(a, b) return a > b; end)
+  print(json.encode(result))
+
+elseif startswith(command,"sensors/") then
+  json = require "json"
+  local year,month,day = string.match(command,"sensors/(%d+)/(%d+)/(%d+)")
+  local files = list_dir(archive_dir.."/"..year.."/"..month.."/"..day)
+  result = {}
+  for key,value in pairs(files) do
+    local id, sensor, param = string.match(value,"(%w+)%.(%w+)%.(%w+).json")
+    if id then
+      result[#result+1] = id.."."..sensor.."."..param
+    end  
+  end
+  table.sort(result)
+  print(json.encode(result))
+
+elseif startswith(command,"list/") then
+
+  sensor = string.sub(command,string.len("list/")+1)
+  lfs = require "lfs"
+  json = require "json"
+  dir = uci.get("weathermon","process","working_dir").."/www"
+  
+  params = {}
+  
+  for name in lfs.dir(dir) do
+    param=name:match(devid.."\."..sensor.."\.(%w+)\.json")
+    if param then
+      params[#params+1] = param
+    end  
+  end
+  
+  print(json.encode(params))  
+
+elseif startswith(command,"get/") then
+
+  sensor_path=string.sub(command,string.len("get/")+1)
+  devid,sensor,param = sensor_path:match("(%w+)/(%w+)/(.+)")
+
+  if param=="*" then
+
+    lfs = require "lfs"
+    dir = uci.get("weathermon","process","working_dir").."/www"
+
+    result = ""
+
+    for name in lfs.dir(dir) do
+      param=name:match(devid.."\."..sensor.."\.(%w+)\.json")
+      if param then
+        content = "\""..param.."\":"..get_file_content(dir.."/"..name)
+        if result=="" then
+          result = content
+        else
+          result = result..","..content
+        end  
+      end  
+    end
+
+    print("{"..result.."}")
+
+  else
+    print(get_file_content(uci.get("weathermon","process","working_dir").."/www/"..devid.."."..sensor.."."..param..".json"))
+  end  
+
+elseif startswith(command,"get-archive/") then
+
+  local sensor_path=string.sub(command,string.len("get-archive/")+1)
+  local year,month,day,devid,sensor,param = sensor_path:match("(%d+)/(%d+)/(%d+)/(%w+)/(%w+)/(%w+)")
+
+  print(get_file_content(archive_dir.."/"..year.."/"..month.."/"..day.."/"..devid.."."..sensor.."."..param..".json"))
+
+end