diff --git a/src/lfs/device.lua b/src/lfs/device.lua index 99c218a..838c57e 100644 --- a/src/lfs/device.lua +++ b/src/lfs/device.lua @@ -2,7 +2,7 @@ local me = { id = "uuid:8f655392-a778-4fee-97b9-4825918" .. string.format("%x", node.chipid()), name = "Konnected", hwVersion = "3.0.0", - swVersion = "3.1.1", + swVersion = "3.1.2", http_port = math.floor(node.chipid()/1000) + 8000, urn = "urn:schemas-konnected-io:device:Security:1" } diff --git a/src/lfs/http_ota.lua b/src/lfs/http_ota.lua index e5aad4c..96bef32 100644 --- a/src/lfs/http_ota.lua +++ b/src/lfs/http_ota.lua @@ -68,7 +68,7 @@ finalise = function(sck) wifi.setmode(wifi.NULLMODE, false) collectgarbage();collectgarbage() -- run as separate task to maximise RAM available - node.task.post(function() node.flashreload(image) end) + node.task.post(function() node.LFS.reload(image) end) else print"Invalid save of image file" end diff --git a/src/lfs/server_receiver.lua b/src/lfs/server_receiver.lua index bd7fcf3..a4ff0ed 100644 --- a/src/lfs/server_receiver.lua +++ b/src/lfs/server_receiver.lua @@ -39,15 +39,14 @@ local function httpReceiver(sck, payload) print("Heap: ", node.heap(), "HTTP: ", "Discovery") elseif request.path == "/settings" then + print("Heap: ", node.heap(), "HTTP: ", "Settings") if mqttC ~= nil and request.method ~= "GET" then mqttC:on("offline", function(client) - print("Heap: ", node.heap(), "HTTP: ", "Settings") response.text(sck, require("server_settings")(request)) end) mqttC:close() return else - print("Heap: ", node.heap(), "HTTP: ", "Settings") response.text(sck, require("server_settings")(request)) end @@ -65,7 +64,17 @@ local function httpReceiver(sck, payload) elseif request.path == "/ota" then print("Heap: ", node.heap(), "HTTP: ", "OTA Update") - response.text(sck, require("ota")(request)) + if mqttC ~= nil and request.method ~= "GET" then + local uri = request.body.uri + local host, path, filename = string.match(uri, "%w+://([^/]+)(/[%w%p]+/)(.*)") + local f = file.open("ota_update.lua", "w") + f.writeline("return function() return " .. "'"..host.."'," .. "'"..path.."'," .. "'"..filename.."'" .. " end") + f.close() + response.text(sck, '{ "status":"ok", "host":"'.. host ..'", "path":"'.. path ..'", "filename":"'.. filename ..'" }') + tmr.create():alarm(1000, tmr.ALARM_SINGLE, function() print("Restarting for update") node.restart() end) + else + response.text(sck, require("ota")(request)) + end end sck, request, response = nil diff --git a/src/lfs/start.lua b/src/lfs/start.lua index 38ed24b..d26dca1 100644 --- a/src/lfs/start.lua +++ b/src/lfs/start.lua @@ -1,6 +1,6 @@ for fn in pairs(file.list()) do local fm = string.match(fn,".*%.lua-$") - if (fm) and fm ~= "init.lua" then + if (fm) and fm ~= "init.lua" and fm ~= "ota_update.lua" then print("Heap: ", node.heap(), "Compiling: ", fn) node.compile(fm) file.remove(fm) diff --git a/src/lfs/wifi.lua b/src/lfs/wifi.lua index 3eedaad..0440980 100644 --- a/src/lfs/wifi.lua +++ b/src/lfs/wifi.lua @@ -29,11 +29,18 @@ if wifi.sta.getconfig() == "" then end local bootApp = function() - print("Heap: ", node.heap(), "Booting Konnected application") - require("server") - print("Heap: ", node.heap(), "Loaded: ", "server") - require("application") - print("Heap: ", node.heap(), "Loaded: ", "application") + if file.exists("ota_update.lua") then + print("Performing OTA update...") + local host, path, filename = require("ota_update")() + file.remove("ota_update.lua") + LFS.http_ota(host, path, filename) + else + print("Heap: ", node.heap(), "Booting Konnected application") + require("server") + print("Heap: ", node.heap(), "Loaded: ", "server") + require("application") + print("Heap: ", node.heap(), "Loaded: ", "application") + end end local _ = tmr.create():alarm(900, tmr.ALARM_AUTO, function(t)