feat(station): add first-run slave identity setup

Remove station_name from the slave identity config and prompt for station code and platform on startup when no train station peripheral and no usable identity file are present, so slave nodes can be configured interactively on first boot.
This commit is contained in:
2026-08-01 07:54:56 +08:00
parent 6315e1eaa1
commit 918e0c68e5
+93 -26
View File
@@ -2,7 +2,7 @@
-- ============================================================================ -- ============================================================================
-- 1. Configuration & Constants -- 1. Configuration & Constants
-- ============================================================================ -- ============================================================================
local APP_VERSION_TEXT = "v1.0.7" local APP_VERSION_TEXT = "v1.0.8"
local CONFIG = { local CONFIG = {
CLOUD_URL = "ws://stabadmin.fse-media.group", CLOUD_URL = "ws://stabadmin.fse-media.group",
@@ -97,23 +97,7 @@ function Utils.trim(value)
return tostring(value or ""):match("^%s*(.-)%s*$") return tostring(value or ""):match("^%s*(.-)%s*$")
end end
function Utils.writeStationIdentityTemplate()
if fs.exists(CONFIG.STATION_IDENTITY_FILE) then return end
local handle = fs.open(CONFIG.STATION_IDENTITY_FILE, "w")
if not handle then return end
handle.write(table.concat({
"# Manual station identity override for slave nodes",
"# Leave a value empty to keep auto detection when available",
"station_name=",
"station_code=",
"station_platform=",
""
}, "\n"))
handle.close()
end
function Utils.loadStationIdentity() function Utils.loadStationIdentity()
Utils.writeStationIdentityTemplate()
if not fs.exists(CONFIG.STATION_IDENTITY_FILE) then if not fs.exists(CONFIG.STATION_IDENTITY_FILE) then
return {} return {}
end end
@@ -139,6 +123,94 @@ function Utils.loadStationIdentity()
return data return data
end end
function Utils.saveStationIdentity(stationCode, stationPlatform)
local handle = fs.open(CONFIG.STATION_IDENTITY_FILE, "w")
if not handle then
return false
end
handle.write(table.concat({
"# Manual station identity override for slave nodes",
"station_code=" .. Utils.trim(stationCode or ""),
"station_platform=" .. Utils.trim(stationPlatform or ""),
""
}, "\n"))
handle.close()
return true
end
function Utils.hasUsableManualIdentity(data)
return data
and type(data) == "table"
and data.station_code and data.station_code ~= ""
and data.station_platform and data.station_platform ~= ""
end
function Utils.promptSlaveIdentity()
while true do
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
term.clear()
term.setCursorPos(1, 1)
term.setTextColor(colors.yellow)
term.write("Station Broadcast " .. APP_VERSION_TEXT)
term.setCursorPos(1, 3)
term.setTextColor(colors.white)
term.write("No train station peripheral found.")
term.setCursorPos(1, 4)
term.write("Configure this computer as a slave node.")
term.setCursorPos(1, 6)
term.write("Station code (e.g. cb-03): ")
term.setCursorPos(1, 8)
term.write("Platform number (e.g. 6): ")
term.setCursorPos(30, 6)
term.setTextColor(colors.cyan)
local stationCode = Utils.trim(read() or ""):lower()
term.setCursorPos(31, 8)
local stationPlatform = Utils.trim(read() or "")
if stationCode == "" or stationPlatform == "" then
term.setCursorPos(1, 10)
term.setTextColor(colors.red)
term.write("Both station code and platform are required.")
term.setCursorPos(1, 12)
term.setTextColor(colors.lightGray)
term.write("Press any key to retry...")
os.pullEvent("key")
else
local ok = Utils.saveStationIdentity(stationCode, stationPlatform)
if ok then
term.setCursorPos(1, 10)
term.setTextColor(colors.green)
term.write("Saved to " .. CONFIG.STATION_IDENTITY_FILE)
term.setCursorPos(1, 12)
term.setTextColor(colors.lightGray)
term.write("Press any key to continue...")
os.pullEvent("key")
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
term.clear()
return true
end
term.setCursorPos(1, 10)
term.setTextColor(colors.red)
term.write("Failed to save " .. CONFIG.STATION_IDENTITY_FILE)
term.setCursorPos(1, 12)
term.setTextColor(colors.lightGray)
term.write("Press any key to retry...")
os.pullEvent("key")
end
end
end
function Utils.ensureSlaveIdentityConfigured()
if Peripherals.station then return end
local manual = Utils.loadStationIdentity()
if Utils.hasUsableManualIdentity(manual) then return end
Utils.promptSlaveIdentity()
end
function Utils.resolveStationIdentity() function Utils.resolveStationIdentity()
local manual = Utils.loadStationIdentity() local manual = Utils.loadStationIdentity()
local physicalName = nil local physicalName = nil
@@ -149,11 +221,8 @@ function Utils.resolveStationIdentity()
local stationCode = manual.station_code local stationCode = manual.station_code
local stationPlatform = manual.station_platform local stationPlatform = manual.station_platform
local stationName = manual.station_name local stationName = physicalName
if not stationName or stationName == "" then
stationName = physicalName
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
@@ -171,10 +240,7 @@ function Utils.resolveStationIdentity()
stationCode = Utils.getStationCode(stationName) or Utils.getBroadcastStationCode(stationName) stationCode = Utils.getStationCode(stationName) or Utils.getBroadcastStationCode(stationName)
end end
local hasManualIdentity = local hasManualIdentity = Utils.hasUsableManualIdentity(manual)
(manual.station_name and manual.station_name ~= "") or
(manual.station_code and manual.station_code ~= "") or
(manual.station_platform and manual.station_platform ~= "")
local identityMode = "auto" local identityMode = "auto"
if hasManualIdentity then if hasManualIdentity then
@@ -1876,6 +1942,7 @@ end
-- 9. Bootstrap -- 9. Bootstrap
-- ============================================================================ -- ============================================================================
Peripherals.init() Peripherals.init()
Utils.ensureSlaveIdentityConfigured()
Audio.loadStationList() Audio.loadStationList()
Utils.refreshStationIdentity() Utils.refreshStationIdentity()
Utils.log("INFO", "StationBroadcast version " .. APP_VERSION_TEXT) Utils.log("INFO", "StationBroadcast version " .. APP_VERSION_TEXT)
@@ -1883,7 +1950,7 @@ if Peripherals.station then
Utils.log("INFO", "Train station peripheral detected on back") Utils.log("INFO", "Train station peripheral detected on back")
else else
Utils.log("WARN", "No train station peripheral on back; using manual slave mode") Utils.log("WARN", "No train station peripheral on back; using manual slave mode")
Utils.log("INFO", "Edit " .. CONFIG.STATION_IDENTITY_FILE .. " to set station_name/station_code/station_platform") Utils.log("INFO", "Edit " .. CONFIG.STATION_IDENTITY_FILE .. " to set station_code/station_platform")
end end
parallel.waitForAny( parallel.waitForAny(