From c67426658a801cccbfc0a2f898a927a1ceb409a5 Mon Sep 17 00:00:00 2001 From: Christian Zufferey Date: Sun, 20 Oct 2019 13:37:48 +0200 Subject: [PATCH] =?UTF-8?q?Termin=C3=A9=20de=20tester=20le=20WEB=20IDE=20?= =?UTF-8?q?=C3=A0=20Matthieu=20Borgognon,=20il=20est=20super=20sexy,=20mai?= =?UTF-8?q?s=20consomme=20BEAUCOUP=20trop=20de=20RAM=20en=20l'=C3=A9tat.?= =?UTF-8?q?=208'344=20de=20plus=20que=20mon=20web=5Fide2.lua=20actuel=20!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WebIDE/Matthieu Borgognon/README.md | 23 ++- .../WebIDE/Matthieu Borgognon/a_script1.lua | 6 +- IDE_remote/WebIDE/Matthieu Borgognon/boot.lua | 2 +- .../WebIDE/Matthieu Borgognon/boot2.lua | 5 +- .../WebIDE/Matthieu Borgognon/rm_files.lua | 12 ++ .../WebIDE/Matthieu Borgognon/web_ide2.lua | 159 ++++++++++++++++++ .../{web_ide.lua => web_ide_MB.lua} | 86 +++++----- .../WebIDE/Matthieu Borgognon/web_srv2.lua | 78 +++++++++ 8 files changed, 320 insertions(+), 51 deletions(-) create mode 100644 IDE_remote/WebIDE/Matthieu Borgognon/rm_files.lua create mode 100644 IDE_remote/WebIDE/Matthieu Borgognon/web_ide2.lua rename IDE_remote/WebIDE/Matthieu Borgognon/{web_ide.lua => web_ide_MB.lua} (93%) create mode 100644 IDE_remote/WebIDE/Matthieu Borgognon/web_srv2.lua diff --git a/IDE_remote/WebIDE/Matthieu Borgognon/README.md b/IDE_remote/WebIDE/Matthieu Borgognon/README.md index 4e40dae..e35443e 100644 --- a/IDE_remote/WebIDE/Matthieu Borgognon/README.md +++ b/IDE_remote/WebIDE/Matthieu Borgognon/README.md @@ -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**
+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 diff --git a/IDE_remote/WebIDE/Matthieu Borgognon/a_script1.lua b/IDE_remote/WebIDE/Matthieu Borgognon/a_script1.lua index 5b61bd6..4902305 100644 --- a/IDE_remote/WebIDE/Matthieu Borgognon/a_script1.lua +++ b/IDE_remote/WebIDE/Matthieu Borgognon/a_script1.lua @@ -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 diff --git a/IDE_remote/WebIDE/Matthieu Borgognon/boot.lua b/IDE_remote/WebIDE/Matthieu Borgognon/boot.lua index cfabff5..817b626 100644 --- a/IDE_remote/WebIDE/Matthieu Borgognon/boot.lua +++ b/IDE_remote/WebIDE/Matthieu Borgognon/boot.lua @@ -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 diff --git a/IDE_remote/WebIDE/Matthieu Borgognon/boot2.lua b/IDE_remote/WebIDE/Matthieu Borgognon/boot2.lua index e26895a..cdaec67 100644 --- a/IDE_remote/WebIDE/Matthieu Borgognon/boot2.lua +++ b/IDE_remote/WebIDE/Matthieu Borgognon/boot2.lua @@ -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 diff --git a/IDE_remote/WebIDE/Matthieu Borgognon/rm_files.lua b/IDE_remote/WebIDE/Matthieu Borgognon/rm_files.lua new file mode 100644 index 0000000..81241cb --- /dev/null +++ b/IDE_remote/WebIDE/Matthieu Borgognon/rm_files.lua @@ -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") diff --git a/IDE_remote/WebIDE/Matthieu Borgognon/web_ide2.lua b/IDE_remote/WebIDE/Matthieu Borgognon/web_ide2.lua new file mode 100644 index 0000000..e0eeb01 --- /dev/null +++ b/IDE_remote/WebIDE/Matthieu Borgognon/web_ide2.lua @@ -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:/// will list all the files in the MCU +http:///newfile.lua displays the file on your browser +http:///newfile.lua?edit allows to creates or edits the specified script in your browser +http:///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("

NodeMCU IDE

") + + if vars=="edit" then + conn:send("") + conn:send("

Back to file list\n") + conn:send("

run") + conn:send("



") + end + + if vars=="run" then + conn:send("") + local st, result=pcall(dofile, url) + conn:send(tostring(result)) + conn:send("") + end + + if url=="" then + local l = file.list(); + for k,v in pairs(l) do + conn:send(""..k..", size:"..v.."
") + end + end + + conn:send("") + + 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()) + diff --git a/IDE_remote/WebIDE/Matthieu Borgognon/web_ide.lua b/IDE_remote/WebIDE/Matthieu Borgognon/web_ide_MB.lua similarity index 93% rename from IDE_remote/WebIDE/Matthieu Borgognon/web_ide.lua rename to IDE_remote/WebIDE/Matthieu Borgognon/web_ide_MB.lua index f1c5922..2151e9f 100644 --- a/IDE_remote/WebIDE/Matthieu Borgognon/web_ide.lua +++ b/IDE_remote/WebIDE/Matthieu Borgognon/web_ide_MB.lua @@ -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\n404 - File Not FoundYa done goofed.") 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 .. "NodeMCU IDE" + + sen = sen .. "NodeMCU IDE" sen = sen .. "" sen = sen .. "

NodeMCU IDE

" - + 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 .. " [Run File] [Main Page] " - + elseif vars == "run" then sen = sen .. "Output of the run:
"
- 
+
          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 .. "

[Edit File] [Run Again] [Main Page]" 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] = "[Restart]" end message[#message + 1] = "" - + 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 diff --git a/IDE_remote/WebIDE/Matthieu Borgognon/web_srv2.lua b/IDE_remote/WebIDE/Matthieu Borgognon/web_srv2.lua new file mode 100644 index 0000000..2389b01 --- /dev/null +++ b/IDE_remote/WebIDE/Matthieu Borgognon/web_srv2.lua @@ -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("

"..zfilename.." not found - 404 error

Home
") + 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) + +