From 2eddb8b84c3fc6ebf1b5126ff1de0f68b2760387 Mon Sep 17 00:00:00 2001 From: HenryDu8133 <813367384@qq.com> Date: Sat, 11 Jul 2026 13:03:44 +0800 Subject: [PATCH] style: update OTA version indicators to lime/yellow/red and output errors to CC terminal --- gate.lua | 103 +++++++++++++++++++++++++++++++++++----------- ticketmachine.lua | 22 ++++++---- 2 files changed, 94 insertions(+), 31 deletions(-) diff --git a/gate.lua b/gate.lua index c04cff7..36a731a 100644 --- a/gate.lua +++ b/gate.lua @@ -89,7 +89,6 @@ local speaker = peripheral.find("speaker") local inspection = peripheral.find("ticket_inspection_machine") local serverConnected = nil -local versionMismatch = nil local isUpdating = false @@ -98,18 +97,73 @@ local function nowMs() end +local function fetchHTTP(url) + if not http then + print('Error: HTTP API is disabled in ComputerCraft config!') + setServerConnected(false) + return nil + end + + if http.get then + local ok, res, err = pcall(http.get, url) + if not ok or not res then + if err then print('HTTP GET failed: ' .. tostring(err) .. ' url=' .. tostring(url)) end + setServerConnected(false) + return nil + end + local data = res.readAll() + res.close() + setServerConnected(true) + return data + end + + if not http.request then + print('Error: HTTP API is disabled in ComputerCraft config!') + return nil + end + + local okReq, reqOkOrErr = pcall(http.request, { url = url, method = 'GET' }) + if not okReq or not reqOkOrErr then + print('HTTP request failed: ' .. tostring(reqOkOrErr) .. ' url=' .. tostring(url)) + setServerConnected(false) + return nil + end + + while true do + local ev, u, p1, p2 = os.pullEvent() + if ev == 'http_success' and u == url then + local res = p1 + local data = res.readAll() + res.close() + setServerConnected(true) + return data + elseif ev == 'http_failure' and u == url then + print('HTTP GET failed: ' .. tostring(p1 or p2 or 'http_failure') .. ' url=' .. tostring(url)) + setServerConnected(false) + return nil + else + os.queueEvent(ev, u, p1, p2) + sleep(0) + end + end +end + +local function jsonDecode(str) + if not str then return nil end + local ok, res = pcall(textutils.unserializeJSON, str) + if ok then return res else return nil end +end local function checkAndApplyUpdate() local serverBase = guessBaseFromStatusURL(serverURL) - if #serverBase == 0 then return end - local url = serverBase:gsub("/+$", "") .. "/api/check-update?_t=" .. tostring(nowMs() + math.random(1, 999999)) - local ok, data = getJSON(url) - if not ok or not data or not data.update_counter then return end + local txt = fetchHTTP(serverBase .. '/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 - -- Prevent infinite reboot loops: only update once per timestamp local attemptFile = ".last_update_ts" if fs.exists(attemptFile) then local f = fs.open(attemptFile, "r") @@ -119,37 +173,43 @@ local function checkAndApplyUpdate() return end end - - -- We have a NEW remote timestamp. Trigger update! + isUpdating = true pcall(drawReadyScreen) sleep(0.5) - - -- Record that we are attempting this timestamp + local f = fs.open(attemptFile, "w") if f then f.write(tostring(remoteTs)); f.close() end - - -- Load and run update script + 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 - pcall(drawReadyScreen) + if pcall then + pcall(drawReadyScreen) + end - if lastUpdateError then - sleep(3) + if not lastUpdateError then + os.reboot() end - - -- Reboot - os.reboot() end local function setServerConnected(ok) @@ -199,17 +259,14 @@ local function drawVersionIndicator(w) local s = tostring(VERSION or "") if #s == 0 then return end if w < #s then return end - local markerColor = colors.yellow + local markerColor = colors.lime local markerText = "* " if isUpdating then markerColor = colors.yellow markerText = "^ing" - elseif versionMismatch == true then + elseif lastUpdateError then markerColor = colors.red markerText = "* " - elseif versionMismatch == false then - markerColor = colors.lime - markerText = " " end termDev.setBackgroundColor(colors.black) termDev.setTextColor(colors.gray) diff --git a/ticketmachine.lua b/ticketmachine.lua index 83f0c13..5ca5564 100644 --- a/ticketmachine.lua +++ b/ticketmachine.lua @@ -661,17 +661,14 @@ local lastUpdateError = nil local function drawVersionIndicator() if w < 1 then return end - local markerColor = colors.yellow + local markerColor = colors.lime local markerText = '* ' if isUpdating then markerColor = colors.yellow markerText = '^ing' - elseif versionMismatch == true then + elseif lastUpdateError then markerColor = colors.red markerText = '* ' - elseif versionMismatch == false then - markerColor = colors.lime - markerText = ' ' end termDev.setBackgroundColor(colors.black) termDev.setTextColor(colors.gray) @@ -720,18 +717,27 @@ local function checkAndApplyUpdate() 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 pcall(drawVersionIndicator) - if lastUpdateError then - sleep(3) + if not lastUpdateError then + os.reboot() end - os.reboot() end local function backgroundSyncTask()