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:
+93
-26
@@ -2,7 +2,7 @@
|
||||
-- ============================================================================
|
||||
-- 1. Configuration & Constants
|
||||
-- ============================================================================
|
||||
local APP_VERSION_TEXT = "v1.0.7"
|
||||
local APP_VERSION_TEXT = "v1.0.8"
|
||||
|
||||
local CONFIG = {
|
||||
CLOUD_URL = "ws://stabadmin.fse-media.group",
|
||||
@@ -97,23 +97,7 @@ function Utils.trim(value)
|
||||
return tostring(value or ""):match("^%s*(.-)%s*$")
|
||||
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()
|
||||
Utils.writeStationIdentityTemplate()
|
||||
if not fs.exists(CONFIG.STATION_IDENTITY_FILE) then
|
||||
return {}
|
||||
end
|
||||
@@ -139,6 +123,94 @@ function Utils.loadStationIdentity()
|
||||
return data
|
||||
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()
|
||||
local manual = Utils.loadStationIdentity()
|
||||
local physicalName = nil
|
||||
@@ -149,11 +221,8 @@ function Utils.resolveStationIdentity()
|
||||
|
||||
local stationCode = manual.station_code
|
||||
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
|
||||
stationCode = Utils.getStationCode(stationName) or Utils.getBroadcastStationCode(stationName)
|
||||
end
|
||||
@@ -171,10 +240,7 @@ function Utils.resolveStationIdentity()
|
||||
stationCode = Utils.getStationCode(stationName) or Utils.getBroadcastStationCode(stationName)
|
||||
end
|
||||
|
||||
local hasManualIdentity =
|
||||
(manual.station_name and manual.station_name ~= "") or
|
||||
(manual.station_code and manual.station_code ~= "") or
|
||||
(manual.station_platform and manual.station_platform ~= "")
|
||||
local hasManualIdentity = Utils.hasUsableManualIdentity(manual)
|
||||
|
||||
local identityMode = "auto"
|
||||
if hasManualIdentity then
|
||||
@@ -1876,6 +1942,7 @@ end
|
||||
-- 9. Bootstrap
|
||||
-- ============================================================================
|
||||
Peripherals.init()
|
||||
Utils.ensureSlaveIdentityConfigured()
|
||||
Audio.loadStationList()
|
||||
Utils.refreshStationIdentity()
|
||||
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")
|
||||
else
|
||||
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
|
||||
|
||||
parallel.waitForAny(
|
||||
|
||||
Reference in New Issue
Block a user