perf(gate): 大幅优化启动速度与票卡事件响应

- 版本号从 v1.5.31 升级至 v1.5.32
- 启动参数优化:外设等待间隔 0.5s→0.2s、最大重试 40→20、握手重试 20→8、启动重绘时长 20s→3s
- handshakeMonitor:移除不必要的 clearOk 严格检查,满足 scale+size 即视为握手成功
- 移除启动阶段阻塞的两次同步 HTTP 请求(站名映射/远程版本),改由后台异步任务执行
- startupRedrawTask:简化为仅启动初重绘 3 次即进入长休眠,避免频繁重绘抢占主循环
- 票卡处理:扫描去重窗口 500ms→200ms、PASS 后回退休眠 0.35s→0.1s
- 后台版本检查:VERSION_CHECK_INTERVAL 5s→15s,降低网络占用
This commit is contained in:
2026-08-15 19:45:20 +08:00
parent 228077a49c
commit 2748aa91ca
+25 -33
View File
@@ -1,13 +1,13 @@
local DEFAULT_SERVER_BASE = "https://ticket.fse-media.group" local DEFAULT_SERVER_BASE = "https://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.31" local VERSION = "v1.5.32"
local VERSION_CHECK_INTERVAL = 5 local VERSION_CHECK_INTERVAL = 15
local PERIPHERAL_WAIT_INTERVAL = 0.5 local PERIPHERAL_WAIT_INTERVAL = 0.2
local PERIPHERAL_WAIT_MAX_RETRIES = 40 local PERIPHERAL_WAIT_MAX_RETRIES = 20
local MONITOR_HANDSHAKE_RETRIES = 20 local MONITOR_HANDSHAKE_RETRIES = 8
local STARTUP_REDRAW_DURATION = 20 local STARTUP_REDRAW_DURATION = 3
local STARTUP_REDRAW_INTERVAL = 0.5 local STARTUP_REDRAW_INTERVAL = 1
local serverURL = nil local serverURL = nil
@@ -291,19 +291,21 @@ local function handshakeMonitor(target)
local scaleOk = false local scaleOk = false
local getSizeOk = false local getSizeOk = false
local w, h = nil, nil local w, h = nil, nil
for _ = 1, MONITOR_HANDSHAKE_RETRIES do for i = 1, MONITOR_HANDSHAKE_RETRIES do
if safeCallMonitor(target, "setTextScale", 0.5) then scaleOk = true end if safeCallMonitor(target, "setTextScale", 0.5) then scaleOk = true end
w, h = safeCallTerm("getSize") w, h = safeCallTerm("getSize")
if type(w) == "number" and type(h) == "number" and w >= 2 and h >= 2 then if type(w) == "number" and type(h) == "number" and w >= 2 and h >= 2 then
getSizeOk = true getSizeOk = true
end end
if scaleOk and getSizeOk then if scaleOk and getSizeOk then
local cOk = safeCallMonitor(target, "setBackgroundColor", colors.black) safeCallMonitor(target, "setBackgroundColor", colors.black)
local c2Ok = safeCallMonitor(target, "setTextColor", colors.white) safeCallMonitor(target, "setTextColor", colors.white)
local clearOk = safeCallMonitor(target, "clear") safeCallMonitor(target, "clear")
if clearOk then return true end return true
end
if i < MONITOR_HANDSHAKE_RETRIES then
sleep(PERIPHERAL_WAIT_INTERVAL)
end end
sleep(PERIPHERAL_WAIT_INTERVAL)
local rewrapOk, rewrapped = pcall(function() local rewrapOk, rewrapped = pcall(function()
if monitorSide and peripheral and type(peripheral.isPresent) == "function" and peripheral.isPresent(monitorSide) and type(peripheral.wrap) == "function" then if monitorSide and peripheral and type(peripheral.isPresent) == "function" and peripheral.isPresent(monitorSide) and type(peripheral.wrap) == "function" then
local okW, wd = pcall(peripheral.wrap, monitorSide) local okW, wd = pcall(peripheral.wrap, monitorSide)
@@ -997,14 +999,6 @@ if type(cfg.side_station_codes) == "table" then
if next(tmp) ~= nil then sideStationCodeBySide = tmp end if next(tmp) ~= nil then sideStationCodeBySide = tmp end
end end
pcall(function()
refreshStationNameMap(guessBaseFromStatusURL(serverURL))
end)
pcall(function()
refreshRemoteLuaVersion(guessBaseFromStatusURL(serverURL))
end)
pcall(function() pcall(function()
if modeBySide == nil then if modeBySide == nil then
waitForPeripherals(true) waitForPeripherals(true)
@@ -1758,7 +1752,7 @@ local function shouldProcessScan(scan, side, eventName)
local now = os.epoch("utc") local now = os.epoch("utc")
local prev = recentScans[key] local prev = recentScans[key]
recentScans[key] = now recentScans[key] = now
if prev and (now - prev) < 500 then if prev and (now - prev) < 200 then
return false return false
end end
return true return true
@@ -1811,7 +1805,7 @@ local function mainLoop()
local ev = pack(os.pullEvent()) local ev = pack(os.pullEvent())
if ev[1] == "ticket_scanned" or ev[1] == "ic_card_scanned" then if ev[1] == "ticket_scanned" or ev[1] == "ic_card_scanned" then
processInspectionEvent(ev[1], ev) processInspectionEvent(ev[1], ev)
os.sleep(0.35) os.sleep(0.1)
drawReadyScreen() drawReadyScreen()
elseif ev[1] == "peripheral" or ev[1] == "peripheral_detach" or ev[1] == "monitor_resize" or ev[1] == "config_updated" then elseif ev[1] == "peripheral" or ev[1] == "peripheral_detach" or ev[1] == "monitor_resize" or ev[1] == "config_updated" then
pcall(drawReadyScreen) pcall(drawReadyScreen)
@@ -1822,17 +1816,14 @@ local function mainLoop()
end end
local function startupRedrawTask() local function startupRedrawTask()
local startTs = (os.epoch and os.epoch("utc")) or (os.time() * 3600000) local count = 0
local endTs = startTs + STARTUP_REDRAW_DURATION * 1000 while count < 3 do
local nextRedraw = startTs sleep(0.5)
pcall(drawReadyScreen)
count = count + 1
end
while true do while true do
local now = (os.epoch and os.epoch("utc")) or (os.time() * 3600000) sleep(600)
if now <= endTs then
pcall(drawReadyScreen)
sleep(STARTUP_REDRAW_INTERVAL)
else
sleep(60)
end
end end
end end
@@ -1896,6 +1887,7 @@ end
local function backgroundSyncTask() local function backgroundSyncTask()
pcall(function() refreshRemoteLuaVersion(guessBaseFromStatusURL(serverURL)) end) pcall(function() refreshRemoteLuaVersion(guessBaseFromStatusURL(serverURL)) end)
pcall(function() refreshStationNameMap(guessBaseFromStatusURL(serverURL)) end)
while true do while true do
sleep(VERSION_CHECK_INTERVAL) sleep(VERSION_CHECK_INTERVAL)
pcall(function() refreshRemoteLuaVersion(guessBaseFromStatusURL(serverURL)) end) pcall(function() refreshRemoteLuaVersion(guessBaseFromStatusURL(serverURL)) end)