fix(client): 修复多个同名同目的地的列车互相清除 pending 状态的问题

当存在多个列车(如无名列车 ??? 开往同一目的地)时:
- 第二个 ETA 较远的列车会因为 `etaSeconds > 45` 走进 `else` 分支
- 原逻辑在 `else` 分支会直接清空该 key 的 pending 状态
- 导致排在前面的列车永远无法积累 5 秒的稳定期

修改:
1. 移除 `else` 分支,统一交由末尾的清理循环处理
2. 使用 `if not seenApproachKeys[announceKey]` 确保同一个 tick 只有 ETA 最近(排最前)的列车更新状态
This commit is contained in:
2026-08-03 11:14:40 +08:00
parent 619d4e9a20
commit eb018dbae9
+57 -57
View File
@@ -1,6 +1,6 @@
-- 1. Configuration & Constants -- 1. Configuration & Constants
local APP_VERSION_TEXT = "v1.0.50" local APP_VERSION_TEXT = "v1.0.51"
local CONFIG = { local CONFIG = {
CLOUD_URL = "ws://stabadmin.fse-media.group", CLOUD_URL = "ws://stabadmin.fse-media.group",
@@ -2704,38 +2704,38 @@ function Main.loop()
end end
if etaSeconds >= 0 and etaSeconds <= 45 then if etaSeconds >= 0 and etaSeconds <= 45 then
seenApproachKeys[announceKey] = true if not seenApproachKeys[announceKey] then
local pending = State.pendingApproaching[announceKey] seenApproachKeys[announceKey] = true
if not pending then local pending = State.pendingApproaching[announceKey]
State.pendingApproaching[announceKey] = { since = now, lastEta = etaSeconds } if not pending then
else State.pendingApproaching[announceKey] = { since = now, lastEta = etaSeconds }
if etaSeconds > (tonumber(pending.lastEta) or etaSeconds) + 15 then else
pending.since = now if etaSeconds > (tonumber(pending.lastEta) or etaSeconds) + 15 then
pending.since = now
end
pending.lastEta = etaSeconds
end end
pending.lastEta = etaSeconds
end
local stableMs = tonumber(CONFIG.APPROACHING_STABLE_MS) or 15000 local stableMs = tonumber(CONFIG.APPROACHING_STABLE_MS) or 15000
local currentPending = State.pendingApproaching[announceKey] local currentPending = State.pendingApproaching[announceKey]
if currentPending and (now - (currentPending.since or now)) >= stableMs and not State.announcedApproaching[announceKey] then if currentPending and (now - (currentPending.since or now)) >= stableMs and not State.announcedApproaching[announceKey] then
State.announcedApproaching[announceKey] = now State.announcedApproaching[announceKey] = now
Utils.log("INFO", string.format("Train Approaching: %s (ETA: %.1fs, Delay: %dm)", trainName, etaSeconds, delayMinutes)) Utils.log("INFO", string.format("Train Approaching: %s (ETA: %.1fs, Delay: %dm)", trainName, etaSeconds, delayMinutes))
Network.send({ Network.send({
type = "train_approaching", type = "train_approaching",
stationName = State.stationName, stationName = State.stationName,
stationCode = State.stationCode, stationCode = State.stationCode,
stationPlatform = platform, stationPlatform = platform,
trainId = trainName, trainId = trainName,
destination = destination, destination = destination,
eta = string.format("%.1fs", etaSeconds), eta = string.format("%.1fs", etaSeconds),
etaSeconds = etaSeconds, etaSeconds = etaSeconds,
delay = displayDelayMinutes, delay = displayDelayMinutes,
cancelled = cancelled, cancelled = cancelled,
reason = reason reason = reason
}) })
end
end end
else
State.pendingApproaching[announceKey] = nil
end end
end end
end end
@@ -2838,36 +2838,36 @@ function Main.loop()
end end
if etaSeconds >= 0 and etaSeconds <= 45 then if etaSeconds >= 0 and etaSeconds <= 45 then
seenApproachKeys[announceKey] = true if not seenApproachKeys[announceKey] then
local pending = State.pendingApproaching[announceKey] seenApproachKeys[announceKey] = true
if not pending then local pending = State.pendingApproaching[announceKey]
State.pendingApproaching[announceKey] = { since = now, lastEta = etaSeconds } if not pending then
else State.pendingApproaching[announceKey] = { since = now, lastEta = etaSeconds }
if etaSeconds > (tonumber(pending.lastEta) or etaSeconds) + 3 then else
pending.since = now if etaSeconds > (tonumber(pending.lastEta) or etaSeconds) + 15 then
pending.since = now
end
pending.lastEta = etaSeconds
end end
pending.lastEta = etaSeconds
end
local stableMs = tonumber(CONFIG.APPROACHING_STABLE_MS) or 15000 local stableMs = tonumber(CONFIG.APPROACHING_STABLE_MS) or 15000
local currentPending = State.pendingApproaching[announceKey] local currentPending = State.pendingApproaching[announceKey]
if currentPending and (now - (currentPending.since or now)) >= stableMs and not State.announcedApproaching[announceKey] then if currentPending and (now - (currentPending.since or now)) >= stableMs and not State.announcedApproaching[announceKey] then
State.announcedApproaching[announceKey] = now State.announcedApproaching[announceKey] = now
Utils.log("INFO", string.format("Train Approaching: %s (ETA: %ds, Delay: %dm)", tName, etaSeconds, delay)) Utils.log("INFO", string.format("Train Approaching: %s (ETA: %ds, Delay: %dm)", tName, etaSeconds, delay))
Network.send({ Network.send({
type = "train_approaching", type = "train_approaching",
stationName = State.stationName, stationName = State.stationName,
stationCode = State.stationCode, stationCode = State.stationCode,
stationPlatform = platform or State.stationPlatform or "1", stationPlatform = platform or State.stationPlatform or "1",
trainId = tName, trainId = tName,
destination = dest, destination = dest,
eta = etaStr, eta = etaStr,
etaSeconds = etaSeconds, etaSeconds = etaSeconds,
delay = delay delay = delay
}) })
end
end end
else
State.pendingApproaching[announceKey] = nil
end end
end end
end end