fix(gate): fix attempt to call a nil value by moving checkAndApplyUpdate to the bottom
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
local DEFAULT_SERVER_BASE = "http://ticket.fse-media.group"
|
local DEFAULT_SERVER_BASE = "http://ticket.fse-media.group"
|
||||||
local DEFAULT_SERVER_PATH = "/api/tickets/check"
|
local DEFAULT_SERVER_PATH = "/api/tickets/check"
|
||||||
local GATE_OPEN_SECONDS = 2
|
local GATE_OPEN_SECONDS = 2
|
||||||
local VERSION = "v1.5.20"
|
local VERSION = "v1.5.21"
|
||||||
local VERSION_CHECK_INTERVAL = 5
|
local VERSION_CHECK_INTERVAL = 5
|
||||||
|
|
||||||
local serverURL = nil
|
local serverURL = nil
|
||||||
@@ -156,66 +156,6 @@ local function jsonDecode(str)
|
|||||||
if ok then return res else return nil end
|
if ok then return res else return nil end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function checkAndApplyUpdate()
|
|
||||||
local serverBase = guessBaseFromStatusURL(serverURL)
|
|
||||||
local txt = fetchHTTP(serverBase .. '/api/check-update?_t=' .. tostring(os.epoch and os.epoch("utc") or math.random(1, 999999)))
|
|
||||||
if not txt then return end
|
|
||||||
local data = jsonDecode(txt)
|
|
||||||
if not data or not data.update_counter then return end
|
|
||||||
local remoteTs = tonumber(data.update_counter)
|
|
||||||
|
|
||||||
if not remoteTs or remoteTs <= 0 then return end
|
|
||||||
|
|
||||||
local attemptFile = ".last_update_ts_gate"
|
|
||||||
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
|
|
||||||
if type(drawReadyScreen) == "function" then
|
|
||||||
pcall(drawReadyScreen)
|
|
||||||
end
|
|
||||||
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_gate.lua", env)
|
|
||||||
if fn then
|
|
||||||
local ok, res = pcall(fn, "--silent")
|
|
||||||
if not ok then
|
|
||||||
lastUpdateError = string.sub(tostring(res), 1, 30)
|
|
||||||
if term and term.setTextColor then
|
|
||||||
term.setTextColor(colors.red)
|
|
||||||
print("OTA Update Error: " .. tostring(res))
|
|
||||||
term.setTextColor(colors.white)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else
|
|
||||||
lastUpdateError = "loadfile failed: " .. tostring(err)
|
|
||||||
if term and term.setTextColor then
|
|
||||||
term.setTextColor(colors.red)
|
|
||||||
print("OTA Update Error: " .. tostring(err))
|
|
||||||
term.setTextColor(colors.white)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
isUpdating = false
|
|
||||||
if type(drawReadyScreen) == "function" then
|
|
||||||
pcall(drawReadyScreen)
|
|
||||||
end
|
|
||||||
|
|
||||||
if not lastUpdateError then
|
|
||||||
os.reboot()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function setServerConnected(ok)
|
local function setServerConnected(ok)
|
||||||
if serverConnected == ok then return end
|
if serverConnected == ok then return end
|
||||||
serverConnected = ok
|
serverConnected = ok
|
||||||
@@ -1515,6 +1455,66 @@ local function mainLoop()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function checkAndApplyUpdate()
|
||||||
|
local serverBase = guessBaseFromStatusURL(serverURL)
|
||||||
|
local txt = fetchHTTP(serverBase .. '/api/check-update?_t=' .. tostring(os.epoch and os.epoch("utc") or math.random(1, 999999)))
|
||||||
|
if not txt then return end
|
||||||
|
local data = jsonDecode(txt)
|
||||||
|
if not data or not data.update_counter then return end
|
||||||
|
local remoteTs = tonumber(data.update_counter)
|
||||||
|
|
||||||
|
if not remoteTs or remoteTs <= 0 then return end
|
||||||
|
|
||||||
|
local attemptFile = ".last_update_ts_gate"
|
||||||
|
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
|
||||||
|
if type(drawReadyScreen) == "function" then
|
||||||
|
pcall(drawReadyScreen)
|
||||||
|
end
|
||||||
|
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_gate.lua", env)
|
||||||
|
if fn then
|
||||||
|
local ok, res = pcall(fn, "--silent")
|
||||||
|
if not ok then
|
||||||
|
lastUpdateError = string.sub(tostring(res), 1, 30)
|
||||||
|
if term and term.setTextColor then
|
||||||
|
term.setTextColor(colors.red)
|
||||||
|
print("OTA Update Error: " .. tostring(res))
|
||||||
|
term.setTextColor(colors.white)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
lastUpdateError = "loadfile failed: " .. tostring(err)
|
||||||
|
if term and term.setTextColor then
|
||||||
|
term.setTextColor(colors.red)
|
||||||
|
print("OTA Update Error: " .. tostring(err))
|
||||||
|
term.setTextColor(colors.white)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
isUpdating = false
|
||||||
|
if type(drawReadyScreen) == "function" then
|
||||||
|
pcall(drawReadyScreen)
|
||||||
|
end
|
||||||
|
|
||||||
|
if not lastUpdateError then
|
||||||
|
os.reboot()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local function backgroundSyncTask()
|
local function backgroundSyncTask()
|
||||||
pcall(function() refreshRemoteLuaVersion(guessBaseFromStatusURL(serverURL)) end)
|
pcall(function() refreshRemoteLuaVersion(guessBaseFromStatusURL(serverURL)) end)
|
||||||
while true do
|
while true do
|
||||||
|
|||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
local VERSION = "v1.5.20"
|
local VERSION = "v1.5.21"
|
||||||
local MACHINE_SIDE = "bottom"
|
local MACHINE_SIDE = "bottom"
|
||||||
local PAYMENT_SIDE = "right"
|
local PAYMENT_SIDE = "right"
|
||||||
local PRESET_AMOUNTS = { 5, 10, 15, 20 }
|
local PRESET_AMOUNTS = { 5, 10, 15, 20 }
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
local CURRENT_STATION_CODE = 'Ticket-Machine'
|
local CURRENT_STATION_CODE = 'Ticket-Machine'
|
||||||
local API_BASE = 'http://ticket.fse-media.group/api'
|
local API_BASE = 'http://ticket.fse-media.group/api'
|
||||||
local VERSION = 'v1.5.20'
|
local VERSION = 'v1.5.21'
|
||||||
local UPDATE_CHECK_INTERVAL = 5
|
local UPDATE_CHECK_INTERVAL = 5
|
||||||
|
|
||||||
-- ###########################
|
-- ###########################
|
||||||
|
|||||||
Reference in New Issue
Block a user