-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmcunode.lua
227 lines (213 loc) · 5.22 KB
/
mcunode.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
local moduleName = "mcunode"
local M = {}
_G[moduleName] = M
local handlers = {}
local files = {}
--plugin
function ls()
local l = file.list()
for k,v in pairs(l) do
print("name:"..k..", size:"..v)
end
end
function cat(filename)
local line
file.open(filename, "r")
while 1 do
line = file.readline()
if line == nil then
break
end
line = string.gsub(line, "\n", "")
print(line)
end
file.close()
end
local function urlDecode(str)
if str == nil then
return nil
end
str = string.gsub(str, '+', ' ')
str = string.gsub(str, '%%(%x%x)', function(h) return string.char(tonumber(h, 16)) end)
return str
end
-- 解析request请求参数
local function parseRequest(data)
--print("data"..data)
local req = {}
local param = nil
req.parameter = {}
req.method = 'GET'
req.protocol = 'http'
-- 解析请求路径及请求参数
_,_,req.path,param = string.find(data,"([^%?]+)%??([^%s]*)") --1 16 gpio pin=1&val=0 path ="gpio?pin=1&val=0"
--print("param"..param)
if param ~= nil then --解析请求参数
--print(param.."not nil")
for key,value in string.gmatch(param,"([^%?%&]+)=([^%?%&]+)") do
--print(key..value)
req.parameter[key] = urlDecode(value) --pin 1 , val 0 param ="gpio?pin=1&val=0"
end
param = nil
end
--print("request path : " .. req.path)
data = nil
req.getParam = function(name) --获取请求参数
if(name == nil) then
return req.parameter
end
return req.parameter[name] --"farry"
end
return req
end
-- tabel to string
local function renderString(subs)
local content = ""
for k,v in pairs(subs) do
content = content.. k .."=" .. v ..","
end
if ( #content == 0 ) then
return nil
end
return string.sub(content,1,#content -1)
end
-- 渲染返回数据
local function render(conn,res)
local body = nil
local attr = res.attribute
html=""
if res.file then
--print("file")
file.open(res.file,"r")
--print("response file : " .. res.file)
while true do
local line = file.readline()
if line == nil then
break
end
if attr then
for k, v in pairs(attr) do
line= string.gsub(line, '{{'..k..'}}', v)
end
end
--html=html..line
conn:send(line)
line=""
end
file.close()
elseif res.body then
--print("body")
body = res.body
if attr then
for k, v in pairs(attr) do
body = string.gsub(body, '{{'..k..'}}', v)
end
end
--print(body)
html=body
conn:send(html)
elseif attr then
--print("attr")
body = renderString(attr)
if body then
html=body
conn:send(html)
end
end
--print("send"..html)
html=nil
res = nil
body = nil
attr = nil
end
local function receive(conn,data)
local s = tmr.now() -- start time
local req = parseRequest(data)
local func = handlers["/"..req.path]
local response = {}
response.attribute = {} --当file或body不为nil时,做变量置换,否则body为attribute解析之后的结果
response.body = nil --响应体
response.file = nil -- 静态文件
response.setAttribute = function(k,v)
if(type(k) == "table") then
for key,val in pairs(k) do
response.attribute[key] = val
end
else
response.attribute[k] = v
end
end
response.getAttribute = function(k)
if(k == nil) then
return response.attribute
end
return response.attribute[k]
end
tmr.wdclr()
if func == nil then -- 没有匹配路径
node.input(data)
function s_output(str)
if (conn~=nil and str~='') then
conn:send(str)
end
end
node.output(s_output,1)
elseif func == "file" then
response.file = req.path
else
response = func(req,response)
end
req = nil
if response then
render(conn,response)
end
response = nil
local e = tmr.now() -- end time
--print("heap:" .. node.heap() .. "bytes, start_time : ".. s .. "us,end_time:".. e .."us,total_time : " .. (e-s) .."us")
collectgarbage("collect")
--node.output(nil)
end
function M.handle(path, func)
handlers[path] = func
end
function M.connect( id , host , ssid , pwd )
station_cfg={}
station_cfg.ssid=ssid
station_cfg.pwd=pwd
station_cfg.save=false
station_cfg.auto=true
conn = net.createConnection(net.TCP, 0)
conn:on("connection", function(sk, c)
sk:send(id)
tmr.alarm(2, 100000, 1, function() sk:send('<h1></h1>') end)
end)
conn:on('receive', receive)
if (ssid~=nil) then
wifi.setmode(wifi.STATION)
wifi.sta.config(station_cfg) --set your ap info !!!!!!
wifi.sta.autoconnect(1)
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()==nil then
print("Connect AP, Waiting...")
else
tmr.stop(1)
conn:connect(8001,host)
print("Mcunode Terminal and web proxy is running !\nId is " .. id.." host is "..host)
local l = file.list()
for k,v in pairs (l) do
if not string.find(k,".lc") then
handlers["/"..k] = "file"
end
end
l = nil
for k,v in pairs (handlers) do
print("path:" .. k)
end
--collectgarbage("collect")
--tmr.delay(100000)
print("localhost ip : "..wifi.sta.getip())
end
end)
end
end
return M