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.
-- Forwards turtle modem messages to server and server commands to turtles.
-- 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 function _path(rel) return fs.combine(_baseDir, rel) end
-- Default config (overridable via .webbridge_config)
local SERVER_URL = "http://localhost:4200"
local WS_URL = "ws://localhost:4200/ws/bridge"
local CHANNEL_RECEIVE = 101
local STATUS_CHANNEL = 102
local COMMAND_CHANNEL = 100
local POCKET_CHANNEL = 103
local config, configSource = WebBridge.loadConfig({
serverUrl = "http://localhost:4200",
wsUrl = nil, -- derived from serverUrl if not set
}, {
fs.combine(_baseDir, ".webbridge_config"),
})
-- Load config from file if present
local CONFIG_FILE = _path(".webbridge_config")
if fs.exists(CONFIG_FILE) then
local f = fs.open(CONFIG_FILE, "r")
local data = f.readAll()
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
local SERVER_URL = config.serverUrl
local WS_URL = config.wsUrl or SERVER_URL:gsub("^http", "ws") .. "/ws/bridge"
if configSource then
print("[CONFIG] Loaded from " .. configSource)
end
-- Find peripherals
local modem = peripheral.find("modem")
-- Channels from platform registry
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")
if not modem then
@@ -48,9 +50,12 @@ if hasMonitor then
monitor.setTextScale(0.5)
end
modem.open(CHANNEL_RECEIVE)
modem.open(STATUS_CHANNEL)
modem.open(POCKET_CHANNEL)
-- Open channels via platform (respects dual-mode migration)
WebBridge.openChannels(modem, {
'remoteturtle.response',
'remoteturtle.status',
'remoteturtle.pocket',
})
-- Track turtles and stats
local turtles = {}
@@ -202,32 +207,24 @@ local function wsSend(data)
return false
end
-- ========== HTTP Fallback Functions ==========
-- ========== HTTP Helpers (via platform) ==========
local function httpPost(path, data)
local success, result = pcall(function()
local response = http.post(SERVER_URL .. path, textutils.serializeJSON(data), { ["Content-Type"] = "application/json" })
if response then
local content = response.readAll()
response.close()
return textutils.unserializeJSON(content)
end
return nil
end)
return success and result or nil
local result = WebBridge.httpPost(SERVER_URL .. path, data)
if result then
local ok, parsed = pcall(textutils.unserialiseJSON, result)
if ok then return parsed end
end
return nil
end
local function httpGet(path)
local success, result = pcall(function()
local response = http.get(SERVER_URL .. path)
if response then
local content = response.readAll()
response.close()
return textutils.unserializeJSON(content)
end
return nil
end)
return success and result or nil
local result = WebBridge.httpGet(SERVER_URL .. path)
if result then
local ok, parsed = pcall(textutils.unserialiseJSON, result)
if ok then return parsed end
end
return nil
end
-- ========== Forward Turtle Modem Message to Server ==========