-- Petit WEB_IDE assez génial -- Source: https://github.com/matbgn/NodeMCU/tree/master/lib/web-ide print("\n web_ide.lua zf191020.1309 \n") local mPort = 88 local function editor(aceEnabled) -- feel free to disable the shiny Ajax.org Cloud Editor 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 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 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 if payload then DataToGet = DataToGet - string.len(payload) file.open(url, "a+") 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
"
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()
local ztmr_web_ide1 = tmr.create()
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
result = nil
sen = sen .. outp
end
sen = sen .. "| Name | Size | Edit | Rename | Compile | Delete | Run |
|---|---|---|---|---|---|---|
| " ..k.. " | " ..v.. " | " local editable = k:sub(-4, -1) == ".lua" or k:sub(-4, -1) == ".css" or k:sub(-5, -1) == ".html" or k:sub(-5, -1) == ".json" or k:sub(-4, -1) == ".txt" or k:sub(-4, -1) == ".csv" if editable then line = line .. "edit" end line = line .. " | rename | " if k:sub(-4, -1) == ".lua" then line = line .. "compile" end line = line .. " | delete | " if ((k:sub(-4, -1) == ".lua") or (k:sub(-3, -1) == ".lc")) then line = line .. "run |