Skip to content

Commit

Permalink
Merge pull request #172 from konnected-io/aws-ota-fix
Browse files Browse the repository at this point in the history
Fix OTA updates when connected to AWS.
heythisisnate authored Aug 26, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents d51955c + 2568770 commit 29819e0
Showing 5 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/lfs/device.lua
Original file line number Diff line number Diff line change
@@ -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"
}
2 changes: 1 addition & 1 deletion src/lfs/http_ota.lua
Original file line number Diff line number Diff line change
@@ -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
15 changes: 12 additions & 3 deletions src/lfs/server_receiver.lua
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/lfs/start.lua
Original file line number Diff line number Diff line change
@@ -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)
17 changes: 12 additions & 5 deletions src/lfs/wifi.lua
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 29819e0

Please sign in to comment.