refactor: streamline configuration loading and channel setup in web bridge

This commit is contained in:
MayaTheShy
2026-03-26 15:19:42 -04:00
parent fc1b23470e
commit 9ff2ce7ff2

View File

@@ -2,41 +2,43 @@
-- Connects to server via WebSocket for instant bidirectional communication. -- Connects to server via WebSocket for instant bidirectional communication.
-- Forwards turtle modem messages to server and server commands to turtles. -- Forwards turtle modem messages to server and server commands to turtles.
-- Falls back to HTTP polling if WebSocket is unavailable. -- Falls back to HTTP polling if WebSocket is unavailable.
--
-- Uses cc-platform-core for shared infrastructure (config, HTTP, modem, channels).
local WebBridge = require('platform.webbridge')
local Channels = require('platform.channels')
-------------------------------------------------
-- Configuration (via platform)
-------------------------------------------------
local _baseDir = fs.getDir(shell.getRunningProgram()) local _baseDir = fs.getDir(shell.getRunningProgram())
local function _path(rel) return fs.combine(_baseDir, rel) end
-- Default config (overridable via .webbridge_config) local config, configSource = WebBridge.loadConfig({
local SERVER_URL = "http://localhost:4200" serverUrl = "http://localhost:4200",
local WS_URL = "ws://localhost:4200/ws/bridge" wsUrl = nil, -- derived from serverUrl if not set
local CHANNEL_RECEIVE = 101 }, {
local STATUS_CHANNEL = 102 fs.combine(_baseDir, ".webbridge_config"),
local COMMAND_CHANNEL = 100 })
local POCKET_CHANNEL = 103
-- Load config from file if present local SERVER_URL = config.serverUrl
local CONFIG_FILE = _path(".webbridge_config") local WS_URL = config.wsUrl or SERVER_URL:gsub("^http", "ws") .. "/ws/bridge"
if fs.exists(CONFIG_FILE) then
local f = fs.open(CONFIG_FILE, "r") if configSource then
local data = f.readAll() print("[CONFIG] Loaded from " .. configSource)
f.close()
local ok, cfg = pcall(textutils.unserialiseJSON, data)
if ok and cfg then
if cfg.serverUrl then
SERVER_URL = cfg.serverUrl
WS_URL = cfg.serverUrl:gsub("^http", "ws") .. "/ws/bridge"
end
if cfg.wsUrl then WS_URL = cfg.wsUrl end
if cfg.channelReceive then CHANNEL_RECEIVE = cfg.channelReceive end
if cfg.statusChannel then STATUS_CHANNEL = cfg.statusChannel end
if cfg.commandChannel then COMMAND_CHANNEL = cfg.commandChannel end
if cfg.pocketChannel then POCKET_CHANNEL = cfg.pocketChannel end
print("[CONFIG] Loaded from " .. CONFIG_FILE)
end
end end
-- Find peripherals -- Channels from platform registry
local modem = peripheral.find("modem") local COMMAND_CHANNEL = Channels.get('remoteturtle.command')
local CHANNEL_RECEIVE = Channels.get('remoteturtle.response')
local STATUS_CHANNEL = Channels.get('remoteturtle.status')
local POCKET_CHANNEL = Channels.get('remoteturtle.pocket')
-------------------------------------------------
-- Peripherals
-------------------------------------------------
local modem, modemSide = WebBridge.findModem(true) -- prefer wireless
local monitor = peripheral.find("monitor") local monitor = peripheral.find("monitor")
if not modem then if not modem then
@@ -48,9 +50,12 @@ if hasMonitor then
monitor.setTextScale(0.5) monitor.setTextScale(0.5)
end end
modem.open(CHANNEL_RECEIVE) -- Open channels via platform (respects dual-mode migration)
modem.open(STATUS_CHANNEL) WebBridge.openChannels(modem, {
modem.open(POCKET_CHANNEL) 'remoteturtle.response',
'remoteturtle.status',
'remoteturtle.pocket',
})
-- Track turtles and stats -- Track turtles and stats
local turtles = {} local turtles = {}
@@ -202,32 +207,24 @@ local function wsSend(data)
return false return false
end end
-- ========== HTTP Fallback Functions ========== -- ========== HTTP Helpers (via platform) ==========
local function httpPost(path, data) local function httpPost(path, data)
local success, result = pcall(function() local result = WebBridge.httpPost(SERVER_URL .. path, data)
local response = http.post(SERVER_URL .. path, textutils.serializeJSON(data), { ["Content-Type"] = "application/json" }) if result then
if response then local ok, parsed = pcall(textutils.unserialiseJSON, result)
local content = response.readAll() if ok then return parsed end
response.close() end
return textutils.unserializeJSON(content) return nil
end
return nil
end)
return success and result or nil
end end
local function httpGet(path) local function httpGet(path)
local success, result = pcall(function() local result = WebBridge.httpGet(SERVER_URL .. path)
local response = http.get(SERVER_URL .. path) if result then
if response then local ok, parsed = pcall(textutils.unserialiseJSON, result)
local content = response.readAll() if ok then return parsed end
response.close() end
return textutils.unserializeJSON(content) return nil
end
return nil
end)
return success and result or nil
end end
-- ========== Forward Turtle Modem Message to Server ========== -- ========== Forward Turtle Modem Message to Server ==========