Веб переделан/
[weathermon.git] / openwrt-web / cgi-bin / meteo.lua
1 #!/usr/bin/lua
2
3 require "uci"
4
5 cursor = uci.cursor()
6
7 archive_dir = uci.get("weathermon","process","archive_dir")
8
9 require "wm_util"
10
11 command = url_decode(os.getenv('QUERY_STRING'))
12
13 print("Content-Type: text/plain\r\n")
14
15 if command == "state" or command == "" or not command then
16   print(get_file_content(uci.get("weathermon","process","dump_file")))
17
18 elseif command == "props" then
19   print(get_file_content("/www/meteo/properties.json"))
20
21 elseif command == "years" then
22   json = require "json"
23   local result = list_dir(archive_dir)
24   table.sort(result, function(a, b) return a > b; end)
25   print(json.encode(result))
26
27 elseif startswith(command,"months/") then
28   json = require "json"
29   local year = string.match(command,"months/(%d+)")
30   local result = list_dir(archive_dir.."/"..year)
31   table.sort(result, function(a, b) return a > b; end)
32   print(json.encode(result))
33
34 elseif startswith(command,"days/") then
35   json = require "json"
36   local year,month = string.match(command,"days/(%d+)/(%d+)")
37   local result = list_dir(archive_dir.."/"..year.."/"..month)
38   table.sort(result, function(a, b) return a > b; end)
39   print(json.encode(result))
40
41 elseif startswith(command,"sensors/") then
42   json = require "json"
43   local year,month,day = string.match(command,"sensors/(%d+)/(%d+)/(%d+)")
44   local files = list_dir(archive_dir.."/"..year.."/"..month.."/"..day)
45   result = {}
46   for key,value in pairs(files) do
47     local id, sensor, param = string.match(value,"(%w+)%.(%w+)%.(%w+).json")
48     if id then
49       result[#result+1] = id.."."..sensor.."."..param
50     end  
51   end
52   table.sort(result)
53   print(json.encode(result))
54
55 elseif startswith(command,"list/") then
56
57   sensor = string.sub(command,string.len("list/")+1)
58   lfs = require "lfs"
59   json = require "json"
60   dir = uci.get("weathermon","process","working_dir").."/www"
61   
62   params = {}
63   
64   for name in lfs.dir(dir) do
65     param=name:match(devid.."\."..sensor.."\.(%w+)\.json")
66     if param then
67       params[#params+1] = param
68     end  
69   end
70   
71   print(json.encode(params))  
72
73 elseif startswith(command,"get/") then
74
75   sensor_path=string.sub(command,string.len("get/")+1)
76   devid,sensor,param = sensor_path:match("(%w+)/(%w+)/(.+)")
77
78   if param=="*" then
79
80     lfs = require "lfs"
81     dir = uci.get("weathermon","process","working_dir").."/www"
82
83     result = ""
84
85     for name in lfs.dir(dir) do
86       param=name:match(devid.."\."..sensor.."\.(%w+)\.json")
87       if param then
88         content = "\""..param.."\":"..get_file_content(dir.."/"..name)
89         if result=="" then
90           result = content
91         else
92           result = result..","..content
93         end  
94       end  
95     end
96
97     print("{"..result.."}")
98
99   else
100     print(get_file_content(uci.get("weathermon","process","working_dir").."/www/"..devid.."."..sensor.."."..param..".json"))
101   end  
102
103 elseif startswith(command,"get-archive/") then
104
105   local sensor_path=string.sub(command,string.len("get-archive/")+1)
106   local year,month,day,devid,sensor,param = sensor_path:match("(%d+)/(%d+)/(%d+)/(%w+)/(%w+)/(%w+)")
107
108   print(get_file_content(archive_dir.."/"..year.."/"..month.."/"..day.."/"..devid.."."..sensor.."."..param..".json"))
109
110 end