feat: display upcoming trains and delay info on monitor
This commit is contained in:
@@ -2101,6 +2101,23 @@ function Display.render()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Upcoming Trains
|
||||||
|
if State.upcomingTrains and #State.upcomingTrains > 0 then
|
||||||
|
local startY = State.currentTrain and 11 or 7
|
||||||
|
mon.setCursorPos(1, startY)
|
||||||
|
mon.setTextColor(COLORS.DEBUG)
|
||||||
|
mon.write("UPCOMING TRAINS:")
|
||||||
|
mon.setTextColor(COLORS.INFO)
|
||||||
|
for i = 1, math.min(3, #State.upcomingTrains) do
|
||||||
|
local t = State.upcomingTrains[i]
|
||||||
|
mon.setCursorPos(1, startY + i)
|
||||||
|
local delayStr = t.delay > 0 and (" [+" .. t.delay .. "m]") or ""
|
||||||
|
local lineText = string.format("%s->%s ETA:%ds%s", t.trainName, t.dest, t.eta, delayStr)
|
||||||
|
if #lineText > w then lineText = lineText:sub(1, w) end
|
||||||
|
mon.write(lineText)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- Logs
|
-- Logs
|
||||||
local y = h - 3
|
local y = h - 3
|
||||||
for i = 1, 3 do
|
for i = 1, 3 do
|
||||||
@@ -2242,6 +2259,7 @@ State.pendingApproaching = {}
|
|||||||
function Main.loop()
|
function Main.loop()
|
||||||
while true do
|
while true do
|
||||||
local now = os.epoch("utc")
|
local now = os.epoch("utc")
|
||||||
|
local newUpcoming = {}
|
||||||
|
|
||||||
-- Check Block Reader (Advanced Peripherals) first for precise ETA and Delay
|
-- Check Block Reader (Advanced Peripherals) first for precise ETA and Delay
|
||||||
if Peripherals.blockReader then
|
if Peripherals.blockReader then
|
||||||
@@ -2273,12 +2291,21 @@ function Main.loop()
|
|||||||
local realArrival = st.RealTimeArrival or 0
|
local realArrival = st.RealTimeArrival or 0
|
||||||
local etaTicks = realArrival - currentTick
|
local etaTicks = realArrival - currentTick
|
||||||
local etaSeconds = math.floor(etaTicks / 20)
|
local etaSeconds = math.floor(etaTicks / 20)
|
||||||
|
if etaSeconds < 0 then etaSeconds = 0 end
|
||||||
|
|
||||||
-- Calculate Delay in minutes
|
-- Calculate Delay in minutes
|
||||||
local delayTicks = realArrival - scheduledArrival
|
local delayTicks = realArrival - scheduledArrival
|
||||||
local delayMinutes = math.floor(delayTicks / (20 * 60))
|
local delayMinutes = math.floor(delayTicks / (20 * 60))
|
||||||
if delayMinutes < 0 then delayMinutes = 0 end
|
if delayMinutes < 0 then delayMinutes = 0 end
|
||||||
|
|
||||||
|
table.insert(newUpcoming, {
|
||||||
|
trainName = trainName,
|
||||||
|
dest = destination,
|
||||||
|
platform = platform,
|
||||||
|
eta = etaSeconds,
|
||||||
|
delay = delayMinutes
|
||||||
|
})
|
||||||
|
|
||||||
if etaSeconds >= 0 and etaSeconds <= 45 then
|
if etaSeconds >= 0 and etaSeconds <= 45 then
|
||||||
local announceKey = trainName .. "_" .. destination
|
local announceKey = trainName .. "_" .. destination
|
||||||
if not State.announcedApproaching[announceKey] then
|
if not State.announcedApproaching[announceKey] then
|
||||||
@@ -2348,6 +2375,14 @@ function Main.loop()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
table.insert(newUpcoming, {
|
||||||
|
trainName = tName,
|
||||||
|
dest = dest,
|
||||||
|
platform = platform or State.stationPlatform or "1",
|
||||||
|
eta = etaSeconds >= 0 and etaSeconds or 0,
|
||||||
|
delay = delay
|
||||||
|
})
|
||||||
|
|
||||||
-- 触发逻辑:当 ETA 已经明确 <= 45秒(包括30秒、15秒、现在等),且之前没有播报过
|
-- 触发逻辑:当 ETA 已经明确 <= 45秒(包括30秒、15秒、现在等),且之前没有播报过
|
||||||
if etaSeconds >= 0 and etaSeconds <= 45 then
|
if etaSeconds >= 0 and etaSeconds <= 45 then
|
||||||
@@ -2374,6 +2409,7 @@ function Main.loop()
|
|||||||
|
|
||||||
if not Peripherals.station then
|
if not Peripherals.station then
|
||||||
State.currentTrain = nil
|
State.currentTrain = nil
|
||||||
|
State.upcomingTrains = newUpcoming
|
||||||
Display.render()
|
Display.render()
|
||||||
Display.renderTerminal()
|
Display.renderTerminal()
|
||||||
os.sleep(0.5)
|
os.sleep(0.5)
|
||||||
@@ -2436,6 +2472,7 @@ function Main.loop()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
State.upcomingTrains = newUpcoming
|
||||||
Display.render()
|
Display.render()
|
||||||
Display.renderTerminal()
|
Display.renderTerminal()
|
||||||
os.sleep(0.5)
|
os.sleep(0.5)
|
||||||
|
|||||||
Reference in New Issue
Block a user