Terminé de tester le WEB IDE à Matthieu Borgognon, il est super sexy, mais consomme BEAUCOUP trop de RAM en l'état. 8'344 de plus que mon web_ide2.lua actuel !

This commit is contained in:
Christian Zufferey
2019-10-20 13:37:48 +02:00
parent 32dc293415
commit c67426658a
8 changed files with 320 additions and 51 deletions

View File

@@ -7,7 +7,26 @@ Son fichier tenu à jour se trouve dans son dépôt ici:
https://github.com/matbgn/NodeMCU/tree/master/lib/web-ide
Mais, j'en ai fait une petite copie locale ;-)
Mais, j'en ai fait une petite copie locale pour pouvoir bosser dessus ;-)
## Changes
* ajouté le header de version ;-)
* refactorisé les timers utilisés
* ajouté l'utilisation de UTF-8
## Problématique
Ce WEB IDE consomme BEAUCOUP trop de RAM par rapport à *mon* WEB IDE (**web_ide2.lua**) !
web_ide_MB.lua **22'784**<br>
web_ide2.lua **3'1128**
diff 31128-22784= **8'344** de plus !
Ce n'est donc pas possible de l'utiliser en l'état :-(
zf191020.1128
zf191020.1333

View File

@@ -1,8 +1,6 @@
f= "web_ide.lc" if file.exists(f)==false then
print("no exist")
end
f= "web_ide.lc" if file.exists(f) then dofile(f) else dofile(string.gsub(f, ".lc", ".lua")) end
@@ -10,7 +8,7 @@ end
f= "web_ide.lua" if file.exists(f) then dofile(f) end
node.compile("hello.lua")
f= "web_ide.lc" if file.exists(f) then dofile(f) end

View File

@@ -1,6 +1,6 @@
-- Scripts à charger après le boot pour démarrer le core system
print("\n boot.lua zf191020.1150 \n")
print("\n boot.lua zf191020.1318 \n")
-- charge ses propres secrets
f= "secrets_energy.lua" if file.exists(f) then dofile(f) end

View File

@@ -1,6 +1,6 @@
-- Scripts à charger après le boot pour démarrer son appli
print("\n boot2.lua zf191020.1151 \n")
print("\n boot2.lua zf191020.1336 \n")
second_chance=nil
function heartbeat()
@@ -10,12 +10,15 @@ function heartbeat()
boottimer1:alarm(1*1000, tmr.ALARM_AUTO, function()
xfois =2
blink_LED ()
print(node.heap())
end)
end
--f= "0_get_data.lua" if file.exists(f) then dofile(f) end
--f= "0_send_data.lua" if file.exists(f) then dofile(f) end
--f= "0_cron.lua" if file.exists(f) then dofile(f) end
f= "web_ide_MB.lua" if file.exists(f) then dofile(f) end
--f= "web_ide2.lua" if file.exists(f) then dofile(f) end
f=nil
--heartbeat=nil

View File

@@ -0,0 +1,12 @@
-- pour effacer TOUS les fichiers qui se trouve dans la flash du NodeMCU
print("\n rm_files.lua zf180907.1511 \n")
l=file.list() i=0
for k,v in pairs(l) do
i=i+v
file.remove(k)
end
print("-------------------------------")
print("\nC'est tout effaced :-) \n")

View File

@@ -0,0 +1,159 @@
-- Petit WEB IDE tout simple autonome
-- ATTENTION: tourne sur le port 88 !
print("\n web_ide2.lua zf190706.1430 \n")
--[[
XChip's NodeMCU IDE
Create, Edit and run NodeMCU files using your webbrowser.
Examples:
http://<mcu_ip>/ will list all the files in the MCU
http://<mcu_ip>/newfile.lua displays the file on your browser
http://<mcu_ip>/newfile.lua?edit allows to creates or edits the specified script in your browser
http://<mcu_ip>/newfile.lua?run it will run the specified script and will show the returned value
]]--
srv=net.createServer(net.TCP)
srv:listen(88,function(conn)
local rnrn=0
local Status = 0
local DataToGet = 0
local method=""
local url=""
local vars=""
conn:on("receive",function(conn,payload)
if Status==0 then
_, _, method, url, vars = string.find(payload, "([A-Z]+) /([^?]*)%??(.*) HTTP")
print(method, url, vars)
end
if method=="POST" then
if Status==0 then
--print("status", Status)
_,_,DataToGet, payload = string.find(payload, "Content%-Length: (%d+)(.+)")
if DataToGet~=nil then
DataToGet = tonumber(DataToGet)
--print(DataToGet)
rnrn=1
Status = 1
else
print("bad length")
end
end
-- find /r/n/r/n
if Status==1 then
--print("status", Status)
local payloadlen = string.len(payload)
local mark = "\r\n\r\n"
local i
for i=1, payloadlen do
if string.byte(mark, rnrn) == string.byte(payload, i) then
rnrn=rnrn+1
if rnrn==5 then
payload = string.sub(payload, i+1,payloadlen)
file.open(url, "w")
file.close()
Status=2
break
end
else
rnrn=1
end
end
if Status==1 then
return
end
end
if Status==2 then
--print("status", Status)
if payload~=nil then
DataToGet=DataToGet-string.len(payload)
--print("DataToGet:", DataToGet, "payload len:", string.len(payload))
file.open(url, "a+")
file.write(payload)
file.close()
else
conn:send("HTTP/1.1 200 OK\r\n\r\nERROR")
Status=0
end
if DataToGet==0 then
conn:send("HTTP/1.1 200 OK\r\n\r\nOK")
Status=0
end
end
return
end
DataToGet = -1
if url == "favicon.ico" then
conn:send("HTTP/1.1 404 file not found")
return
end
conn:send("HTTP/1.1 200 OK\r\n\r\n")
-- it wants a file in particular
if url~="" and vars=="" then
DataToGet = 0
return
end
conn:send("<html><body><h1>NodeMCU IDE</h1>")
if vars=="edit" then
conn:send("<script>function tag(c){document.getElementsByTagName('w')[0].innerHTML=c};\n")
conn:send("var x=new XMLHttpRequest()\nx.onreadystatechange=function(){if(x.readyState==4) document.getElementsByName('t')[0].value = x.responseText; };\nx.open('GET',location.pathname,true)\nx.send()</script>")
conn:send("<h2><a href='/'>Back to file list</a>\n")
conn:send("<br><br><button onclick=\"tag('Saving');x.open('POST',location.pathname,true);\nx.onreadystatechange=function(){if(x.readyState==4) tag(x.responseText);};\nx.send(new Blob([document.getElementsByName('t')[0].value],{type:'text/plain'}));\">Save</button><a href='?run'>run</a><w></w>")
conn:send("</h2><br><textarea name=t cols=110 rows=50></textarea></br>")
end
if vars=="run" then
conn:send("<verbatim>")
local st, result=pcall(dofile, url)
conn:send(tostring(result))
conn:send("</verbatim>")
end
if url=="" then
local l = file.list();
for k,v in pairs(l) do
conn:send("<a href='"..k.."?edit'>"..k.."</a>, size:"..v.."<br>")
end
end
conn:send("</body></html>")
end)
conn:on("sent",function(conn)
if DataToGet>=0 and method=="GET" then
if file.open(url, "r") then
file.seek("set", DataToGet)
local line=file.read(512)
file.close()
if line then
conn:send(line)
DataToGet = DataToGet + 512
if (string.len(line)==512) then
return
end
end
end
end
conn:close()
end)
end)
print("listening, free:", node.heap())

View File

@@ -1,7 +1,7 @@
-- Petit WEB_IDE assez génial
-- Source: https://github.com/matbgn/NodeMCU/tree/master/lib/web-ide
print("\n web_ide.lua zf191020.1211 \n")
print("\n web_ide.lua zf191020.1309 \n")
local mPort = 88
@@ -10,97 +10,97 @@ local function editor(aceEnabled) -- feel free to disable the shiny Ajax.org Clo
local AceEnabled = aceEnabled == nil and true or aceEnabled
srv = net.createServer(net.TCP)
srv:listen(mPort, function(conn)
local rnrn = 0
local Status = 0
local DataToGet = 0
local method = ""
local url = ""
local vars = ""
conn:on("receive", function(sck, payload)
if Status == 0 then
_, _, method, url, vars = string.find(payload, "([A-Z]+) /([^?]*)%??(.*) HTTP")
end
if method == "POST" then
if Status == 0 then
_, _, DataToGet, payload = string.find(payload, "Content%-Length: (%d+)(.+)")
if DataToGet then
DataToGet = tonumber(DataToGet)
rnrn = 1
Status = 1
Status = 1
else
print("bad length")
end
end
if Status == 1 then
local payloadlen = string.len(payload)
local mark = "\r\n\r\n"
local i
for i=1, payloadlen do
for i=1, payloadlen do
if string.byte(mark, rnrn) == string.byte(payload, i) then
rnrn = rnrn + 1
if rnrn == 5 then
payload = string.sub(payload, i+1, payloadlen)
file.open(url, "w")
file.close()
file.close()
Status = 2
break
end
else
rnrn = 1
end
end
if Status == 1 then
return
end
end
if Status == 1 then
return
end
end
if Status == 2 then
if payload then
DataToGet = DataToGet - string.len(payload)
file.open(url, "a+")
file.write(payload)
file.close()
file.write(payload)
file.close()
else
sck:send("HTTP/1.1 200 OK\r\n\r\nERROR")
Status = 0
end
if DataToGet == 0 then
sck:send("HTTP/1.1 200 OK\r\n\r\nOK")
Status = 0
end
end
return
end
-- end of POST method handling
DataToGet = -1
if url == "favicon.ico" then
sck:send("HTTP/1.1 404 file not found\r\nServer: NodeMCU IDE\r\nContent-Type: text/html\r\n\r\n<html><head><title>404 - File Not Found</title></head><body>Ya done goofed.</body></html>")
return
end
end
local sen = "HTTP/1.1 200 OK\r\nServer: NodeMCU IDE\r\nContent-Type: text/html\r\nPragma: no-cache\r\nCache-Control: no-cache\r\n\r\n"
-- it wants a file in particular
if url ~= "" and vars == "" then
DataToGet = 0
sck:send(sen)
return
end
sen = sen .. "<html><head><title>NodeMCU IDE</title><meta name=\"viewport\" content=\"width=device-width,initial-scale=1.0\"><meta http-equiv=\"Expires\" content=\"-1\" />"
sen = sen .. "<html><head><title>NodeMCU IDE</title><meta charset='utf-8' name=\"viewport\" content=\"width=device-width,initial-scale=1.0\"><meta http-equiv=\"Expires\" content=\"-1\" />"
sen = sen .. "<style>a:link{color:white;} a:visited{color:white;} a:hover{color:yellow;} a:active{color:green;}</style></head>"
sen = sen .. "<body style=\"background-color:#333333;color:#dddddd\"><h1><a href='/'>NodeMCU IDE</a></h1>"
if vars == "edit" then
if AceEnabled then
local mode = 'ace/mode/'
@@ -119,19 +119,19 @@ local function editor(aceEnabled) -- feel free to disable the shiny Ajax.org Clo
sen = sen .. "<script>function tag(c){document.getElementsByTagName('w')[0].innerHTML=c};var x=new XMLHttpRequest();x.onreadystatechange=function(){if(x.readyState==4) setSource(x.responseText);};"
.. "x.open('GET',location.pathname);x.send()</script><button onclick=\"tag('Saving, wait!');x.open('POST',location.pathname);x.onreadystatechange=function(){console.log(x.readyState);"
.. "if(x.readyState==4) tag(x.responseText);};x.send(new Blob([getSource()],{type:'text/plain'}));\">Save</button> <a href='?run'>[Run File]</a> <a href=\"/\">[Main Page]</a> <w></w>"
elseif vars == "run" then
sen = sen .. "Output of the run:<hr><pre>"
function s_output(str) sen = sen .. str end
node.output(s_output, 0) -- re-direct output to function s_output.
local st, result = pcall(dofile, url)
-- delay the output capture by 1000 milliseconds to give some time to the user routine in pcall()
-- tmr.alarm(0, 1000, tmr.ALARM_SINGLE, function()
-- tmr.alarm(0, 1000, tmr.ALARM_SINGLE, function()
local ztmr_web_ide1 = tmr.create()
ztmr_web_ide1:alarm(1000, tmr.ALARM_SINGLE, function()
ztmr_web_ide1:alarm(1000, tmr.ALARM_SINGLE, function()
ztmr_web_ide1=nil node.output(nil)
if result then
local outp = tostring(result):sub(1,1300) -- to fit in one send() packet
@@ -141,28 +141,28 @@ local function editor(aceEnabled) -- feel free to disable the shiny Ajax.org Clo
sen = sen .. "</pre><hr><a href=\"?edit\">[Edit File]</a> <a href=\"?run\">[Run Again]</a> <a href=\"/\">[Main Page]</a></body></html>"
sck:send(sen)
end)
return
elseif vars == "rename" then
file.rename(url:match("(.+)\/"), url:match("\/(.+)"))
url = ""
elseif vars == "compile" then
collectgarbage()
node.compile(url)
url = ""
elseif vars == "delete" then
file.remove(url)
url = ""
elseif vars == "restart" then
node.restart()
return
end
local message = {}
message[#message + 1] = sen
sen = nil
@@ -191,7 +191,7 @@ local function editor(aceEnabled) -- feel free to disable the shiny Ajax.org Clo
message[#message + 1] = "<a href='#' onclick='var x=new XMLHttpRequest();x.open(\"GET\",\"/?restart\");x.send();setTimeout(function(){location.href=\"/\"},5000);this.innerText=\"[Please wait...]\";return false'>[Restart]</a>"
end
message[#message + 1] = "</body></html>"
local function send_table(sk)
if #message > 0 then
sk:send(table.remove(message, 1))
@@ -203,7 +203,7 @@ local function editor(aceEnabled) -- feel free to disable the shiny Ajax.org Clo
sck:on("sent", send_table)
send_table(sck)
end)
conn:on("sent", function(sck)
if DataToGet >= 0 and method == "GET" then
if file.open(url, "r") then
@@ -216,15 +216,15 @@ local function editor(aceEnabled) -- feel free to disable the shiny Ajax.org Clo
DataToGet = DataToGet + chunkSize
if string.len(line) == chunkSize then return end
end
end
end
end
sck:close()
sck = nil
end)
end)
end
local ztmr_web_ide2 = tmr.create()
ztmr_web_ide2:alarm(500, tmr.ALARM_AUTO, function()
if (wifi.sta.status() == wifi.STA_GOTIP) then

View File

@@ -0,0 +1,78 @@
-- petit script de serveur WEB avec Active Server Page ZYX
print("\n web_srv2.lua zf190728.1021 \n")
ztemp=12
-- envoie sur le port ouvert mais depuis l'environnement global !
function zout(zstring)
zzclient:send(zstring) -- envoie le résultat du code lua inline
end
-- envoie un fichier HTML sur le port. ATTENTION: longueur de la ligne maximale de 1'024 bytes !
function send_file(zclient, zfilename)
print("start send html...")
zclient:send("HTTP/1.1 200 OK\n")
zclient:send("Content-Type: text/html\n\n")
zzclient = zclient -- export le port sur l'environnement global !
if zfilename == "" then zfilename = "z_index.html" end
file_web = file.open(zfilename, "r")
if file_web then
repeat
local line = file_web:read('\n')
if line then
if string.find(line, "<%%") then
-- print("start lua...")
flag_lua_code = true -- bascule sur le code lua inline
lua_code = ""
elseif string.find(line, "%%>") then
-- print("stop lua...")
flag_lua_code = false -- revient sur le code HTML
-- print("Et voici le code lua inline:\n"..lua_code)
loadstring(lua_code)() --on exécute ici le code lua inline !
elseif flag_lua_code then
-- print(line)
lua_code = lua_code..line -- récupère le code lua inline
else
zclient:send(line) -- envoie le code HTML
end
end
until not line
file_web:close() file_web = nil
else
zclient:send("<html><h1>"..zfilename.." not found - 404 error</h1><a href='/'>Home</a><br></html>")
end
end
srv = net.createServer()
srv:listen(80, function(conn)
conn:on("receive", function(client, request)
_, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP")
print("zrequest: \n---\n"..request.."---")
-- print("method: ", method) print("path: ", path) print("vars: ", vars)
if not string.find(request, "/favicon.ico") then
print("coucou")
if (method == nil) then
_, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP")
end
print("method: ", method) print("path: ", path) print("vars: ", vars)
_GET = {}
if (vars ~= nil) then
for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
_GET[k] = v
print(k..": "..v)
end
end
file_html=string.gsub(path, "/", "")
-- print("file_html: ",file_html)
send_file(client, file_html)
end
end)
conn:on("sent", function(c) c:close() end)
end)