feat: Implement exact 45s pre-arrival broadcast by delaying 1min signal

This commit is contained in:
2026-08-02 14:59:07 +08:00
parent dbf259efe5
commit c573260e5d
+41 -9
View File
@@ -2,7 +2,7 @@
-- ============================================================================
-- 1. Configuration & Constants
-- ============================================================================
local APP_VERSION_TEXT = "v1.0.25"
local APP_VERSION_TEXT = "v1.0.26"
local CONFIG = {
CLOUD_URL = "ws://stabadmin.fse-media.group",
@@ -2235,6 +2235,7 @@ end
-- ============================================================================
local Main = {}
State.announcedApproaching = {}
State.pendingApproaching = {}
function Main.loop()
while true do
@@ -2252,15 +2253,27 @@ function Main.loop()
end
if tName and etaVal then
-- 恢复当ETA为“1”时也触发广播(满足提前预告的需求)
if etaVal == "现在" or etaVal == "0" or etaVal == "1" then
local announceKey = tName .. "_" .. dest
local now = os.epoch("utc")
-- Prevent spamming: CRN's "1 min" can be fuzzy and stay for a while.
-- We set the cooldown to 1 minute (60000 ms) per train per destination.
if not State.announcedApproaching[announceKey] or (now - State.announcedApproaching[announceKey] > 60000) then
local announceKey = tName .. "_" .. dest
local now = os.epoch("utc")
-- 45秒预告逻辑:当读取到1分钟时,延迟15秒再播报
if etaVal == "1" then
if not State.announcedApproaching[announceKey] or (now - State.announcedApproaching[announceKey] > 120000) then
if not State.pendingApproaching[announceKey] then
State.pendingApproaching[announceKey] = {
triggerTime = now + 15000, -- 延迟15秒
tName = tName,
dest = dest,
etaVal = etaVal
}
end
end
elseif etaVal == "现在" or etaVal == "0" then
-- 如果列车已经进站(0或现在),并且没有被播报过,立即播报
if not State.announcedApproaching[announceKey] or (now - State.announcedApproaching[announceKey] > 120000) then
State.announcedApproaching[announceKey] = now
Utils.log("INFO", "Train Approaching: " .. tName)
State.pendingApproaching[announceKey] = nil
Utils.log("INFO", "Train Approaching (Now): " .. tName)
Network.send({
type = "train_approaching",
stationName = State.stationName,
@@ -2275,6 +2288,25 @@ function Main.loop()
end
end
end
-- 检查是否有到期的延迟播报(45秒预告)
local now = os.epoch("utc")
for key, info in pairs(State.pendingApproaching) do
if now >= info.triggerTime then
State.announcedApproaching[key] = now
State.pendingApproaching[key] = nil
Utils.log("INFO", "Train Approaching (45s mark): " .. info.tName)
Network.send({
type = "train_approaching",
stationName = State.stationName,
stationCode = State.stationCode,
stationPlatform = State.stationPlatform or "1",
trainId = info.tName,
destination = info.dest,
eta = info.etaVal
})
end
end
end
if not Peripherals.station then