Weather panel backend call.
[weathermon.git] / bin / weather-backlog
1 #!/usr/bin/lua
2
3 local json = require("json")
4 local socket = require("socket")
5 local http = require("socket.http")
6
7 require "wm_util"
8
9 function getConfig(configname)
10
11   local command,f
12
13   local uci=require("uci")
14   local cur=uci.cursor()
15   local config
16   if configname then
17     config=configname
18   else
19     config="weathermon"
20   end
21
22   web_url  = cur.get(config,"web","url")
23   web_user = cur.get(config,"web","user")
24   web_timeout = cur.get(config,"web","timeout")
25   web_pass = cur.get(config,"web","password")
26  
27   if not web_timeout then
28     web_timeout = 10
29   end
30
31   backlogdb = cur.get(config,"process","backlogdb")
32
33 end
34
35 function submitValue(timestamp,type,id,param,val)
36
37   local url = web_url.."?stype="..url_encode(type).."&sid="..url_encode(id).."&param="..url_encode(param).."&value="..url_encode(val).."&time="..url_encode(timestamp)
38
39   if web_user then
40     url = url:gsub("//","//"..web_user..":"..web_pass.."@",1)
41   end
42
43   local result,code = http.request ({
44     url=url, create=function()
45       local req_sock = socket.tcp()
46       req_sock:settimeout(web_timeout)
47       return req_sock
48     end})
49
50   if code ~= 200 then
51     return false
52   end
53
54   return true
55
56 end
57
58 getConfig(arg[1])
59
60 if backlogdb then
61
62   local dbdriver = require "luasql.sqlite3"
63   env = assert(dbdriver.sqlite3())
64   if file_exists(backlogdb) then
65     backlog_con = assert(env:connect(backlogdb))
66   end
67
68   cursor = assert(backlog_con:execute("SELECT rowid,time_stamp,sensor_id,sensor,param,value FROM queue LIMIT 400"))
69   row = cursor:fetch ({}, "a")
70   while row do
71     if submitValue(row.time_stamp,row.sensor,row.sensor_id,row.param,row.value) then
72       backlog_con:execute(string.format("DELETE FROM queue WHERE rowid='%s'",row.rowid))
73     end
74     row = cursor:fetch (row, "a")
75   end
76
77 end