feat: Add train pre-arrival broadcast via CRN create_target

This commit is contained in:
2026-08-02 11:37:41 +08:00
parent 805aba9f37
commit ef75671527
2 changed files with 94 additions and 1 deletions
+44 -1
View File
@@ -2,7 +2,7 @@
-- ============================================================================
-- 1. Configuration & Constants
-- ============================================================================
local APP_VERSION_TEXT = "v1.0.19"
local APP_VERSION_TEXT = "v1.0.20"
local CONFIG = {
CLOUD_URL = "ws://stabadmin.fse-media.group",
@@ -120,6 +120,11 @@ function Peripherals.init()
end
-- Speakers
Peripherals.speakers = { peripheral.find("speaker") }
-- Create Target Block (CRN)
Peripherals.createTarget = peripheral.find("create_target")
if Peripherals.createTarget then
pcall(Peripherals.createTarget.resize, 80, 10)
end
end
-- ============================================================================
@@ -2229,9 +2234,47 @@ end
-- 8. Main Logic
-- ============================================================================
local Main = {}
State.announcedApproaching = {}
function Main.loop()
while true do
if Peripherals.createTarget then
local lines = Peripherals.createTarget.dump()
for _, lineText in pairs(lines) do
if lineText and lineText ~= "" and lineText:match("[%w%?%[]") then
local etaVal, etaUnit, tName, dest
etaVal, etaUnit, tName, dest = string.match(lineText, "^(%d+)%s+(.-)%s+(%[.-%][^%s]*)%s+(.*)$")
if not tName then
etaVal, etaUnit, tName, dest = string.match(lineText, "^([^%s]+)%s+(.-)%s+(%[.-%][^%s]*)%s+(.*)$")
end
if not tName then
etaVal, etaUnit, tName, dest = string.match(lineText, "^([^%s]+)%s+(.-)%s+([^%s]+)%s+(.*)$")
end
if tName and etaVal then
if etaVal == "1" or etaVal == "现在" then
local announceKey = tName .. "_" .. dest
local now = os.epoch("utc")
-- Prevent spamming: only announce once per minute per train
if not State.announcedApproaching[announceKey] or (now - State.announcedApproaching[announceKey] > 60000) then
State.announcedApproaching[announceKey] = now
Utils.log("INFO", "Train Approaching: " .. tName)
Network.send({
type = "train_approaching",
stationName = State.stationName,
stationCode = State.stationCode,
stationPlatform = State.stationPlatform,
trainId = tName,
destination = dest,
eta = etaVal
})
end
end
end
end
end
end
if not Peripherals.station then
State.currentTrain = nil
Display.render()