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:
+61
-5
@@ -2,7 +2,7 @@
|
|||||||
-- ============================================================================
|
-- ============================================================================
|
||||||
-- 1. Configuration & Constants
|
-- 1. Configuration & Constants
|
||||||
-- ============================================================================
|
-- ============================================================================
|
||||||
local APP_VERSION_TEXT = "v1.0.11"
|
local APP_VERSION_TEXT = "v1.0.12"
|
||||||
|
|
||||||
local CONFIG = {
|
local CONFIG = {
|
||||||
CLOUD_URL = "ws://stabadmin.fse-media.group",
|
CLOUD_URL = "ws://stabadmin.fse-media.group",
|
||||||
@@ -58,17 +58,28 @@ local State = {
|
|||||||
local Peripherals = {}
|
local Peripherals = {}
|
||||||
|
|
||||||
function Peripherals.init()
|
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 station = nil
|
||||||
local stationSide = nil
|
local stationSide = nil
|
||||||
|
local checkedNames = {}
|
||||||
local preferredSides = { "back", "top", "bottom", "left", "right", "front" }
|
local preferredSides = { "back", "top", "bottom", "left", "right", "front" }
|
||||||
for _, side in ipairs(preferredSides) do
|
for _, side in ipairs(preferredSides) do
|
||||||
|
checkedNames[side] = true
|
||||||
if peripheral.getType(side) == "train_station" then
|
if peripheral.getType(side) == "train_station" then
|
||||||
station = peripheral.wrap(side)
|
station = peripheral.wrap(side)
|
||||||
stationSide = side
|
stationSide = side
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
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
|
Peripherals.station = station
|
||||||
State.hasStationPeripheral = station ~= nil
|
State.hasStationPeripheral = station ~= nil
|
||||||
State.stationPeripheralSide = stationSide
|
State.stationPeripheralSide = stationSide
|
||||||
@@ -140,7 +151,7 @@ function Utils.saveStationIdentity(stationCode, stationPlatform)
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
handle.write(table.concat({
|
handle.write(table.concat({
|
||||||
"# Manual station identity override for slave nodes",
|
"# Station identity cache / override",
|
||||||
"station_code=" .. Utils.trim(stationCode or ""),
|
"station_code=" .. Utils.trim(stationCode or ""),
|
||||||
"station_platform=" .. Utils.trim(stationPlatform or ""),
|
"station_platform=" .. Utils.trim(stationPlatform or ""),
|
||||||
""
|
""
|
||||||
@@ -230,10 +241,25 @@ function Utils.resolveStationIdentity()
|
|||||||
if physicalName == "" then physicalName = nil end
|
if physicalName == "" then physicalName = nil end
|
||||||
end
|
end
|
||||||
|
|
||||||
local stationCode = manual.station_code
|
local stationCode = nil
|
||||||
local stationPlatform = manual.station_platform
|
local stationPlatform = nil
|
||||||
local stationName = physicalName
|
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
|
if (not stationCode or stationCode == "") and stationName and stationName ~= "" then
|
||||||
stationCode = Utils.getStationCode(stationName) or Utils.getBroadcastStationCode(stationName)
|
stationCode = Utils.getStationCode(stationName) or Utils.getBroadcastStationCode(stationName)
|
||||||
end
|
end
|
||||||
@@ -268,6 +294,34 @@ function Utils.resolveStationIdentity()
|
|||||||
}
|
}
|
||||||
end
|
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()
|
function Utils.refreshStationIdentity()
|
||||||
local identity = Utils.resolveStationIdentity()
|
local identity = Utils.resolveStationIdentity()
|
||||||
State.stationName = identity.stationName
|
State.stationName = identity.stationName
|
||||||
@@ -1955,6 +2009,8 @@ Peripherals.init()
|
|||||||
Utils.ensureSlaveIdentityConfigured()
|
Utils.ensureSlaveIdentityConfigured()
|
||||||
Audio.loadStationList()
|
Audio.loadStationList()
|
||||||
Utils.refreshStationIdentity()
|
Utils.refreshStationIdentity()
|
||||||
|
Utils.syncStationIdentityFromPeripheral()
|
||||||
|
Utils.refreshStationIdentity()
|
||||||
Utils.log("INFO", "StationBroadcast version " .. APP_VERSION_TEXT)
|
Utils.log("INFO", "StationBroadcast version " .. APP_VERSION_TEXT)
|
||||||
if Peripherals.station then
|
if Peripherals.station then
|
||||||
Utils.log("INFO", "Train station peripheral detected on " .. tostring(State.stationPeripheralSide or "unknown"))
|
Utils.log("INFO", "Train station peripheral detected on " .. tostring(State.stationPeripheralSide or "unknown"))
|
||||||
|
|||||||
Reference in New Issue
Block a user