feat: announce delay alerts as soon as they appear

This commit is contained in:
2026-08-02 18:58:54 +08:00
parent ea22c79008
commit 278de7de6b
+52 -1
View File
@@ -2,7 +2,7 @@
-- ============================================================================ -- ============================================================================
-- 1. Configuration & Constants -- 1. Configuration & Constants
-- ============================================================================ -- ============================================================================
local APP_VERSION_TEXT = "v1.0.37" local APP_VERSION_TEXT = "v1.0.38"
local CONFIG = { local CONFIG = {
CLOUD_URL = "ws://stabadmin.fse-media.group", CLOUD_URL = "ws://stabadmin.fse-media.group",
@@ -2308,6 +2308,7 @@ end
-- ============================================================================ -- ============================================================================
local Main = {} local Main = {}
State.announcedApproaching = {} State.announcedApproaching = {}
State.announcedAlerts = {}
State.pendingApproaching = {} State.pendingApproaching = {}
local function parseEtaText(etaStr) local function parseEtaText(etaStr)
@@ -2471,6 +2472,11 @@ function Main.loop()
State.announcedApproaching[k] = nil State.announcedApproaching[k] = nil
end end
end end
for k, v in pairs(State.announcedAlerts) do
if now - v > 300000 then
State.announcedAlerts[k] = nil
end
end
for i, stop in ipairs(data.TrainStops) do for i, stop in ipairs(data.TrainStops) do
local st = stop.Station local st = stop.Station
@@ -2523,6 +2529,27 @@ function Main.loop()
cancelled = cancelled, cancelled = cancelled,
reason = reason reason = reason
}) })
local alertKey = announceKey .. "::delay=" .. tostring(delayMinutes) .. "::reason=" .. tostring(reason or "") .. "::cancel=" .. tostring(cancelled)
if cancelled or delayMinutes > 0 then
if not State.announcedAlerts[alertKey] then
State.announcedAlerts[alertKey] = now
Utils.log("INFO", string.format("Train Alert: %s (ETA: %ds, Delay: %dm, Cancelled: %s, Reason: %s)", trainName, etaSeconds, delayMinutes, tostring(cancelled), tostring(reason or "")))
Network.send({
type = "train_approaching",
stationName = State.stationName,
stationCode = State.stationCode,
stationPlatform = platform,
trainId = trainName,
destination = destination,
eta = tostring(etaSeconds) .. "s",
etaSeconds = etaSeconds,
delay = delayMinutes,
cancelled = cancelled,
reason = reason
})
end
end
if etaSeconds >= 0 and etaSeconds < 30 then if etaSeconds >= 0 and etaSeconds < 30 then
if not State.announcedApproaching[announceKey] then if not State.announcedApproaching[announceKey] then
@@ -2557,6 +2584,11 @@ function Main.loop()
State.announcedApproaching[k] = nil State.announcedApproaching[k] = nil
end end
end end
for k, v in pairs(State.announcedAlerts) do
if now - v > 300000 then
State.announcedAlerts[k] = nil
end
end
for _, lineText in pairs(lines) do for _, lineText in pairs(lines) do
if lineText and lineText ~= "" and lineText:match("[%w%?%[]") then if lineText and lineText ~= "" and lineText:match("[%w%?%[]") then
@@ -2611,6 +2643,25 @@ function Main.loop()
reason = nil reason = nil
}) })
local alertKey = announceKey .. "::delay=" .. tostring(delay)
if delay > 0 then
if not State.announcedAlerts[alertKey] then
State.announcedAlerts[alertKey] = now
Utils.log("INFO", string.format("Train Delay Alert: %s (ETA: %ds, Delay: %dm)", tName, etaSeconds, delay))
Network.send({
type = "train_approaching",
stationName = State.stationName,
stationCode = State.stationCode,
stationPlatform = platform or State.stationPlatform or "1",
trainId = tName,
destination = dest,
eta = etaStr,
etaSeconds = etaSeconds,
delay = delay
})
end
end
-- 触发逻辑:当 ETA 已经明确小于30秒(包括15秒、现在等),且之前没有播报过 -- 触发逻辑:当 ETA 已经明确小于30秒(包括15秒、现在等),且之前没有播报过
if etaSeconds >= 0 and etaSeconds < 30 then if etaSeconds >= 0 and etaSeconds < 30 then
if not State.announcedApproaching[announceKey] then if not State.announcedApproaching[announceKey] then