From c573260e5d812f9d119a56bcdd5bab908b7293f1 Mon Sep 17 00:00:00 2001 From: HenryDu8133 <813367384@qq.com> Date: Sun, 2 Aug 2026 14:59:07 +0800 Subject: [PATCH] feat: Implement exact 45s pre-arrival broadcast by delaying 1min signal --- station_broadcast.lua | 50 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/station_broadcast.lua b/station_broadcast.lua index bf0b4af..351debf 100644 --- a/station_broadcast.lua +++ b/station_broadcast.lua @@ -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