forked from liqinliqin/selfupdate.lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselfupdate.lua
More file actions
55 lines (47 loc) · 1.62 KB
/
selfupdate.lua
File metadata and controls
55 lines (47 loc) · 1.62 KB
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
yourfilename = "yourfilename" --这里写你自己需要更新的远端服务器上的名字
file.open(yourfilename, "r")
local temp = file.readline()
updateflag = string.sub(temp,0,string.find(temp,"\r\n"))
temp = nil
file.close()
Server = "yourserver.name"
socket=net.createConnection(net.TCP, 0)
socket:dns(Server, function(conn,ip) if ip == nil then print("DNS Fail.") node.restart() end end)
--开始连接服务器
socket:connect(80, Server)
socket:on("connection", function(sck)
print("connect to:"..Server)
toget()
end)
function toget()
--HTTP请求头定义
local rupdateflag = ""
socket:send("GET /"..yourfilename.." HTTP/1.1\r\n" ..
"Host: "..Server.."\r\n" ..
"Accept: */*" ..
"User-Agent: Nodemcu CHIPID:"..node.chipid().."\r\n\r\n")
end
socket:on("receive", function(sck, response)
local rupdateflag=string.sub(response,0,string.find(response,"\r\n"))
if updateflag ~= rupdateflag then
file.remove("temp.lua")
file.open("temp.lua", "a+")
file.writeline(response)
file.close()
file.remove(yourfilename)
file.rename("temp.lua",yourfilename)
file.remove("init.lua")
file.open("init.lua", "a+")
file.writeline("node.compile(yourfilename)")
file.writeline("dofile("..string.sub(yourfilename,0,string.find(".lua")-1..".lc)")
file.close()
node.restart()
else
end
end)
-- 在自己程序里开始部分写入
-- file.remove("init.lua")
-- file.open("init.lua", "a+")
-- file.writeline("dofile(update.lua)")
-- file.close()
-- node.restart()