feat: simplify OTA updates with timestamp
This commit is contained in:
+42
-26
@@ -14,8 +14,6 @@ end
|
||||
|
||||
local serverConnected = nil
|
||||
local versionMismatch = nil
|
||||
local failedUpdateVersion = nil
|
||||
local pendingMachineUpdate = false
|
||||
local isUpdating = false
|
||||
local state = {}
|
||||
|
||||
@@ -716,31 +714,49 @@ local function drawVersionIndicator()
|
||||
termDev.setTextColor(colors.white)
|
||||
end
|
||||
|
||||
local function checkAndApplyUpdate()
|
||||
if type(CFG) ~= 'table' then return end
|
||||
local remoteTs = tonumber(CFG.update_timestamp)
|
||||
|
||||
if not remoteTs or remoteTs <= 0 then return end
|
||||
|
||||
local attemptFile = ".last_update_ts"
|
||||
if fs.exists(attemptFile) then
|
||||
local f = fs.open(attemptFile, "r")
|
||||
local tried = tonumber(f and f.readAll() or "0") or 0
|
||||
if f then f.close() end
|
||||
if tried >= remoteTs then
|
||||
return
|
||||
end
|
||||
end
|
||||
isUpdating = true
|
||||
pcall(drawVersionIndicator)
|
||||
sleep(0.5)
|
||||
|
||||
local f = fs.open(attemptFile, "w")
|
||||
if f then f.write(tostring(remoteTs)); f.close() end
|
||||
|
||||
local env = setmetatable({ AUTO_UPDATE_SILENT = true }, { __index = _G or getfenv() or _ENV })
|
||||
local fn, err = loadfile("update_machine.lua", env)
|
||||
if fn then
|
||||
local ok, res = pcall(fn, "--silent")
|
||||
if not ok then
|
||||
lastUpdateError = string.sub(tostring(res), 1, 30)
|
||||
end
|
||||
else
|
||||
lastUpdateError = "loadfile failed: " .. tostring(err)
|
||||
end
|
||||
|
||||
isUpdating = false
|
||||
pcall(drawVersionIndicator)
|
||||
|
||||
os.reboot()
|
||||
end
|
||||
|
||||
local function backgroundSyncTask()
|
||||
pcall(refreshConfigOnce)
|
||||
while true do
|
||||
if type(CFG) == 'table' and type(CFG.force_update) == 'boolean' and CFG.force_update == true and versionMismatch == true then
|
||||
isUpdating = true
|
||||
pcall(drawVersionIndicator)
|
||||
sleep(0.5)
|
||||
|
||||
-- Extreme fallback: just run the update script directly
|
||||
local baseEnv = (getfenv and getfenv()) or _ENV or _G
|
||||
local env = setmetatable({ AUTO_UPDATE_SILENT = true }, { __index = baseEnv })
|
||||
local fn = loadfile('update_machine.lua', env)
|
||||
if fn then
|
||||
local ok, err = pcall(fn, '--silent')
|
||||
if not ok then
|
||||
lastUpdateError = string.sub(tostring(err), 1, 30)
|
||||
end
|
||||
else
|
||||
lastUpdateError = "loadfile failed"
|
||||
end
|
||||
|
||||
isUpdating = false
|
||||
pcall(drawVersionIndicator)
|
||||
sleep(10) -- Sleep 10s to prevent spamming
|
||||
end
|
||||
pcall(checkAndApplyUpdate)
|
||||
sleep(UPDATE_CHECK_INTERVAL)
|
||||
pcall(refreshConfigOnce)
|
||||
end
|
||||
@@ -910,7 +926,7 @@ local function sameLogicalStation(a, b)
|
||||
end
|
||||
|
||||
local function fetchConfig()
|
||||
local txt = fetchHTTP(API_BASE .. '/config')
|
||||
local txt = fetchHTTP(API_BASE .. '/config?_t=' .. tostring(os.epoch and os.epoch("utc") or math.random(1, 999999)))
|
||||
return txt and jsonDecode(txt) or nil
|
||||
end
|
||||
|
||||
@@ -1033,7 +1049,7 @@ local function waitButtons()
|
||||
end
|
||||
end
|
||||
elseif ev == 'trigger_update' then
|
||||
pendingMachineUpdate = true
|
||||
-- obsolete
|
||||
elseif ev == 'config_updated' then
|
||||
if state and state.page == 'home' then return end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user