-
Notifications
You must be signed in to change notification settings - Fork 1
/
httpserver.lua
72 lines (61 loc) · 2.2 KB
/
httpserver.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
61
62
63
64
65
66
67
68
69
70
71
72
srv=net.createServer(net.TCP,10)
srv:listen(80, function(conn)
conn:on("receive", function(client, request)
local response = ""
local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP")
if (method == nil) then
_, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP")
end
local _GET = {}
if (vars ~= nil) then
for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
_GET[k] = v
print("Processing HTTP command: "..k.."/"..v)
end
end
local _on,_off = "",""
-- XXX/clean up the old routines
if (_GET.auto == "frenzy") then
-- Change to only do this for 30 seconds
start_servo()
start_wiggle_servo(position_change_ms_fast)
elseif (_GET.auto == "tease") then
-- Change to only do this for 30 seconds
start_servo()
start_wiggle_servo(position_change_ms_slow)
elseif (_GET.auto == "fulltravel") then
stop_wiggle_servo()
start_servo(1)
set_servo_position(100)
tmr.delay(1000*1000)
stop_servo()
elseif (_GET.auto == "lower") then
stop_wiggle_servo()
start_servo(100)
stop_servo()
elseif (_GET.setposition ~= nil) then
stop_wiggle_servo()
start_servo(_GET.setposition)
stop_servo()
elseif (_GET.auto == "raise") then
sensor.stop()
elseif (_GET.auto == "sensor") then
sensor.start()
end
if file.open("webpage.html", "r") then
reponse = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"
-- read 1K chunks to get around the socket buffer size limits
while true do
block = file.read(1024)
if not block then break end
response = response..block
end
file.close()
end
print("Response length: "..string.len(response))
-- XXX/fix the 1460 byte response size limit
client:send(response)
client:close()
collectgarbage()
end)
end)