forked from peter-kutak/NODEMCU-LUA-OTA-ESP8266
-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.lua
196 lines (170 loc) · 5.58 KB
/
client.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
function SaveX(sErr)
if (sErr) then
s.err = sErr
end
file.remove("s.txt")
file.open("s.txt","w+")
for k, v in pairs(s) do
file.writeline(k .. "=" .. v)
end
file.close()
collectgarbage()
end
function mysplit(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={} ; i=1
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
t[i] = str
i = i + 1
end
return t
end
function dwn()
-- body
n = n + 1
v = data[n]
if v == nil then
--dofile(data[1]..".lc")
bootfile= string.gsub(data[1], '\.lua$','') --string.gsub(s, '\....$','')
s.boot = bootfile..".lc"
SaveX("No error")
node.restart()
else
print("Filename: "..v)
filename=v
file.remove(v);
file.open(v, "w+")
payloadFound = false
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(sck, payload)
if (payloadFound == true) then
file.write(payload)
file.flush()
else
if (string.find(payload,"\r\n\r\n") ~= nil) then
file.write(string.sub(payload,string.find(payload,"\r\n\r\n") + 4))
file.flush()
payloadFound = true
end
end
payload = nil
collectgarbage()
end)
conn:on("disconnection", function(sck)
sck = nil
file.close()
if (v~=nil) then
ext = string.sub(v, -3)
if (ext == "lua") then
node.compile(filename)
end
dwn()
end
end)
conn:on("connection", function(conn)
conn:send("GET /"..s.path.."/uploads/"..id.."/"..v.." HTTP/1.0\r\n"..
"Host: "..s.host.."\r\n"..
"Connection: close\r\n"..
"Accept-Charset: utf-8\r\n"..
"Accept-Encoding: \r\n"..
"User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n"..
"Accept: */*\r\n\r\n")
end)
conn:connect(80,s.host)
end
end
function FileList(sck,c)
print "initialized"
local nStart, nEnd = string.find(c, "\n\n")
if (nEnde == nil) then
nStart, nEnd = string.find(c, "\r\n\r\n")
end
c = string.sub(c,nEnd+1)
print("length: "..string.len(c))
data = mysplit(c, "\n") -- fill the field with filenames
--for k,v in pairs(data) do
n = 1
v = data[n]
print("Filename: "..v)
filename=v
file.remove(v);
file.open(v, "w+")
payloadFound = false
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(sck, payload)
if (payloadFound == true) then
file.write(payload)
file.flush()
else
if (string.find(payload,"\r\n\r\n") ~= nil) then
file.write(string.sub(payload,string.find(payload,"\r\n\r\n") + 4))
file.flush()
payloadFound = true
end
end
payload = nil
collectgarbage()
end)
conn:on("disconnection", function(sck)
sck = nil
file.close()
if (v ~= nil) then
ext = string.sub(v, -3)
if (ext == "lua") then
node.compile(v)
end
end
dwn()
end)
conn:on("connection", function(sck)
sck:send("GET /"..s.path.."/uploads/"..id.."/"..v.." HTTP/1.0\r\n"..
"Host: "..s.host.."\r\n"..
"Connection: close\r\n"..
"Accept-Charset: utf-8\r\n"..
"Accept-Encoding: \r\n"..
"User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n"..
"Accept: */*\r\n\r\n")
end)
conn:connect(80,s.host)
--end
collectgarbage()
end
print("fetch lua..")
data = {}
filename=nil
LoadX()
wifi.setmode (wifi.STATION)
wifi.sta.config(s.ssid, s.pwd)
wifi.sta.autoconnect (1)
iFail = 20 -- trying to connect to AP in 30sec, if not then reboot
tmr.alarm (1, 1500, 1, function ( )
iFail = iFail -1
print(iFail)
if (iFail == 0) then
SaveX("could not access "..s.ssid)
node.restart()
end
if wifi.sta.getip ( ) == nil then
print(s.ssid..": "..iFail)
else
print ("ip: " .. wifi.sta.getip ( ))
tmr.stop (1)
-- get list of files
conn=net.createConnection(net.TCP, 0)
conn:on("connection",function(sck, payload)
sck:send("GET /".. s.path .."/node.php?id="..id.."&list"..
" HTTP/1.0\r\n"..
"Host: "..s.domain.."\r\n"..
"Accept: */*\r\n"..
"User-Agent: Mozilla/4.0 (compatible; esp8266 Lua;)"..
"\r\n\r\n")
end)
conn:on("receive", FileList)
--sGet = "GET /".. s.path .. " HTTP/1.1\r\nHost: " .. s.domain .. "\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n"
conn:connect(80,s.host)
end
collectgarbage()
end)
print(collectgarbage("count").." kB used")