fix(station): sync identity config from physical station

Detect train station peripherals from all discovered names and automatically write station_identity.cfg from the physical station identity so attached main hosts stop falling back to manual slave setup.
This commit is contained in:
2026-08-01 10:27:36 +08:00
parent 777e6f3371
commit b51e31e00f
+61 -5
View File
@@ -2,7 +2,7 @@
-- ============================================================================
-- 1. Configuration & Constants
-- ============================================================================
local APP_VERSION_TEXT = "v1.0.11"
local APP_VERSION_TEXT = "v1.0.12"
local CONFIG = {
CLOUD_URL = "ws://stabadmin.fse-media.group",
@@ -58,17 +58,28 @@ local State = {
local Peripherals = {}
function Peripherals.init()
-- Station Peripheral: prefer the back side, then fall back to any side.
-- Station Peripheral: prefer direct sides, then fall back to any discovered peripheral name.
local station = nil
local stationSide = nil
local checkedNames = {}
local preferredSides = { "back", "top", "bottom", "left", "right", "front" }
for _, side in ipairs(preferredSides) do
checkedNames[side] = true
if peripheral.getType(side) == "train_station" then
station = peripheral.wrap(side)
stationSide = side
break
end
end
if not station and peripheral.getNames then
for _, name in ipairs(peripheral.getNames()) do
if not checkedNames[name] and peripheral.getType(name) == "train_station" then
station = peripheral.wrap(name)
stationSide = name
break
end
end
end
Peripherals.station = station
State.hasStationPeripheral = station ~= nil
State.stationPeripheralSide = stationSide
@@ -140,7 +151,7 @@ function Utils.saveStationIdentity(stationCode, stationPlatform)
return false
end
handle.write(table.concat({
"# Manual station identity override for slave nodes",
"# Station identity cache / override",
"station_code=" .. Utils.trim(stationCode or ""),
"station_platform=" .. Utils.trim(stationPlatform or ""),
""
@@ -230,10 +241,25 @@ function Utils.resolveStationIdentity()
if physicalName == "" then physicalName = nil end
end
local stationCode = manual.station_code
local stationPlatform = manual.station_platform
local stationCode = nil
local stationPlatform = nil
local stationName = physicalName
if physicalName then
stationCode = Utils.getStationCode(physicalName) or Utils.getBroadcastStationCode(physicalName)
stationPlatform = Utils.getPlatformNumber(physicalName, manual.station_platform or "1")
else
stationCode = manual.station_code
stationPlatform = manual.station_platform
end
if (not stationCode or stationCode == "") and manual.station_code and manual.station_code ~= "" then
stationCode = manual.station_code
end
if (not stationPlatform or stationPlatform == "") and manual.station_platform and manual.station_platform ~= "" then
stationPlatform = manual.station_platform
end
if (not stationCode or stationCode == "") and stationName and stationName ~= "" then
stationCode = Utils.getStationCode(stationName) or Utils.getBroadcastStationCode(stationName)
end
@@ -268,6 +294,34 @@ function Utils.resolveStationIdentity()
}
end
function Utils.syncStationIdentityFromPeripheral()
if not Peripherals.station then
return false
end
local identity = Utils.resolveStationIdentity()
local stationCode = Utils.trim(identity.stationCode or "")
local stationPlatform = Utils.trim(identity.stationPlatform or "")
if stationCode == "" or stationCode == "UNK" or stationPlatform == "" then
return false
end
local manual = Utils.loadStationIdentity()
local oldCode = Utils.trim(manual.station_code or "")
local oldPlatform = Utils.trim(manual.station_platform or "")
if oldCode == stationCode and oldPlatform == stationPlatform then
return false
end
local ok = Utils.saveStationIdentity(stationCode, stationPlatform)
if ok then
Utils.log("INFO", "Synced station_identity.cfg from physical station: " .. stationCode .. " / P" .. stationPlatform)
return true
end
Utils.log("WARN", "Failed to sync " .. CONFIG.STATION_IDENTITY_FILE .. " from physical station")
return false
end
function Utils.refreshStationIdentity()
local identity = Utils.resolveStationIdentity()
State.stationName = identity.stationName
@@ -1955,6 +2009,8 @@ Peripherals.init()
Utils.ensureSlaveIdentityConfigured()
Audio.loadStationList()
Utils.refreshStationIdentity()
Utils.syncStationIdentityFromPeripheral()
Utils.refreshStationIdentity()
Utils.log("INFO", "StationBroadcast version " .. APP_VERSION_TEXT)
if Peripherals.station then
Utils.log("INFO", "Train station peripheral detected on " .. tostring(State.stationPeripheralSide or "unknown"))