-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver.lua
60 lines (52 loc) · 1.21 KB
/
webserver.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
-- webserver.lua
local M = {}
local t = require "time"
local config = require "config"
local tz = require "tz"
local function wrapit(fn)
return function (conn)
local buf = "HTTP/1.1 200 OK\r\n" ..
"Content-type: application/json\r\n" ..
"Connection: close\r\n\r\n" ..
cjson.encode(fn())
conn:send(buf, function(c) c:close() end)
end
end
local function getstatus()
local R = {}
R.time = {rtctime.get()}
R.hms = t.getpos()
R.running = t.getrunning()
R.config = config.table
R.ntp = lastNtpResult
R.freemem = node.heap()
return R
end
local wrappedGetstatus = wrapit(getstatus)
function M.register(adder)
function addjson(path, fn)
adder("GET", path, wrapit(fn))
end
adder("GET", "/status", wrappedGetstatus)
addjson("/zones", function ()
return tz.getzones()
end)
adder("POST", "/set", function (conn, vars)
if vars.start then
t.start()
end
if vars.stop then
t.stop()
end
if vars.pos then
t.setpos(vars.pos)
end
if vars.zone then
if tz.exists(vars.zone) then
config.tz = vars.zone
end
end
wrappedGetstatus(conn)
end)
end
return M