feat: refactor configuration loading and HTTP helpers to use WebBridge functions
This commit is contained in:
@@ -3,56 +3,42 @@
|
|||||||
-- Listens to the inventory master's broadcasts via modem and
|
-- Listens to the inventory master's broadcasts via modem and
|
||||||
-- forwards state to the web server via HTTP.
|
-- forwards state to the web server via HTTP.
|
||||||
-- Also polls the web server for commands and sends them to the master.
|
-- Also polls the web server for commands and sends them to the master.
|
||||||
|
--
|
||||||
|
-- Uses cc-platform-core for shared infrastructure (config, HTTP, modem).
|
||||||
|
-- Service-specific logic (command dispatch, state forwarding) remains here.
|
||||||
|
|
||||||
|
local WebBridge = require('platform.webbridge')
|
||||||
|
local Channels = require('platform.channels')
|
||||||
|
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
-- Configuration
|
-- Configuration (via platform)
|
||||||
-------------------------------------------------
|
|
||||||
|
|
||||||
-- Web server URL (change to your Docker host IP/hostname)
|
|
||||||
local SERVER_URL = "http://localhost"
|
|
||||||
local POLL_INTERVAL = 0.5 -- seconds between command polls
|
|
||||||
local STATE_INTERVAL = 1 -- seconds between state forwards
|
|
||||||
|
|
||||||
-- Modem channels (must match inventoryManager.lua)
|
|
||||||
local BROADCAST_CHANNEL = 4200
|
|
||||||
local ORDER_CHANNEL = 4201
|
|
||||||
local BRIDGE_REPLY_CHANNEL = 4206 -- dedicated reply channel for bridge
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
-- Load config from file if present
|
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
|
|
||||||
local _baseDir = fs.getDir(shell.getRunningProgram())
|
local _baseDir = fs.getDir(shell.getRunningProgram())
|
||||||
local function _path(rel) return fs.combine(_baseDir, rel) end
|
|
||||||
|
|
||||||
-- Persistent config path: survives Opus package updates
|
local config, configSource = WebBridge.loadConfig({
|
||||||
local _PERSIST_DIR = "usr/config/inventory-manager"
|
serverUrl = "http://localhost",
|
||||||
local function _configPath(rel)
|
pollInterval = 0.5,
|
||||||
if fs.isDir(_PERSIST_DIR) or fs.isDir("packages/inventory-manager") then
|
stateInterval = 1,
|
||||||
if not fs.isDir(_PERSIST_DIR) then fs.makeDir(_PERSIST_DIR) end
|
apiKey = nil,
|
||||||
return fs.combine(_PERSIST_DIR, rel)
|
}, {
|
||||||
end
|
"usr/config/inventory-manager/.webbridge_config",
|
||||||
return _path(rel)
|
fs.combine(_baseDir, ".webbridge_config"),
|
||||||
|
})
|
||||||
|
|
||||||
|
local SERVER_URL = config.serverUrl
|
||||||
|
local POLL_INTERVAL = config.pollInterval
|
||||||
|
local STATE_INTERVAL = config.stateInterval
|
||||||
|
local API_KEY = config.apiKey
|
||||||
|
|
||||||
|
if configSource then
|
||||||
|
print("[CONFIG] Loaded from " .. configSource)
|
||||||
end
|
end
|
||||||
|
|
||||||
local CONFIG_FILE = _configPath(".webbridge_config")
|
-- Channels from platform registry (matches inventoryManager.lua)
|
||||||
local API_KEY = nil -- optional API key for server auth
|
local BROADCAST_CHANNEL = Channels.get('inventory.broadcast')
|
||||||
|
local ORDER_CHANNEL = Channels.get('inventory.order')
|
||||||
local function loadConfig()
|
local BRIDGE_REPLY_CHANNEL = Channels.get('inventory.bridge')
|
||||||
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 end
|
|
||||||
if cfg.pollInterval then POLL_INTERVAL = cfg.pollInterval end
|
|
||||||
if cfg.stateInterval then STATE_INTERVAL = cfg.stateInterval end
|
|
||||||
if cfg.apiKey then API_KEY = cfg.apiKey end
|
|
||||||
print("[CONFIG] Loaded from " .. CONFIG_FILE)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
-- State
|
-- State
|
||||||
@@ -64,66 +50,28 @@ local modemName = nil
|
|||||||
local running = true
|
local running = true
|
||||||
|
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
-- Find modem
|
-- HTTP helpers (thin wrappers around platform)
|
||||||
-------------------------------------------------
|
|
||||||
|
|
||||||
local function findModem()
|
|
||||||
for _, name in ipairs(peripheral.getNames()) do
|
|
||||||
if peripheral.getType(name) == "modem" then
|
|
||||||
modem = peripheral.wrap(name)
|
|
||||||
modemName = name
|
|
||||||
modem.open(BROADCAST_CHANNEL)
|
|
||||||
modem.open(BRIDGE_REPLY_CHANNEL)
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
-------------------------------------------------
|
|
||||||
-- HTTP helpers
|
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
|
|
||||||
local function httpPost(path, body)
|
local function httpPost(path, body)
|
||||||
local url = SERVER_URL .. path
|
local result, err = WebBridge.httpPost(SERVER_URL .. path, body,
|
||||||
local data = textutils.serialiseJSON(body)
|
WebBridge.authHeaders(API_KEY))
|
||||||
local headers = { ["Content-Type"] = "application/json" }
|
if not result and err then
|
||||||
if API_KEY then headers["Authorization"] = "Bearer " .. API_KEY end
|
print(string.format("[ERR] HTTP POST %s: %s", path, tostring(err)))
|
||||||
|
|
||||||
local ok, result = pcall(function()
|
|
||||||
local response = http.post(url, data, headers)
|
|
||||||
if response then
|
|
||||||
local responseData = response.readAll()
|
|
||||||
response.close()
|
|
||||||
return responseData
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|
||||||
if ok then
|
|
||||||
return result
|
|
||||||
end
|
end
|
||||||
print(string.format("[ERR] HTTP POST %s: %s", path, tostring(result)))
|
return result
|
||||||
return nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function httpGet(path)
|
local function httpGet(path)
|
||||||
local url = SERVER_URL .. path
|
local rawBody, err = WebBridge.httpGet(SERVER_URL .. path,
|
||||||
local headers = nil
|
WebBridge.authHeaders(API_KEY))
|
||||||
if API_KEY then headers = { ["Authorization"] = "Bearer " .. API_KEY } end
|
if not rawBody then
|
||||||
local ok, result = pcall(function()
|
if err then
|
||||||
local response = http.get(url, headers)
|
print(string.format("[ERR] HTTP GET %s: %s", path, tostring(err)))
|
||||||
if response then
|
|
||||||
local data = response.readAll()
|
|
||||||
response.close()
|
|
||||||
return textutils.unserialiseJSON(data)
|
|
||||||
end
|
end
|
||||||
end)
|
return nil
|
||||||
|
|
||||||
if ok then
|
|
||||||
return result
|
|
||||||
end
|
end
|
||||||
print(string.format("[ERR] HTTP GET %s: %s", path, tostring(result)))
|
return textutils.unserialiseJSON(rawBody)
|
||||||
return nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
@@ -347,9 +295,12 @@ local function main()
|
|||||||
print("===================================")
|
print("===================================")
|
||||||
print("")
|
print("")
|
||||||
|
|
||||||
loadConfig()
|
-- Config already loaded at require-time via WebBridge.loadConfig above
|
||||||
|
|
||||||
if findModem() then
|
modem, modemName = WebBridge.findModem()
|
||||||
|
if modem then
|
||||||
|
WebBridge.openChannels(modem,
|
||||||
|
{ 'inventory.broadcast', 'inventory.bridge' })
|
||||||
print("[OK] Modem: " .. modemName)
|
print("[OK] Modem: " .. modemName)
|
||||||
else
|
else
|
||||||
print("[WARN] No modem found! Bridge needs a modem.")
|
print("[WARN] No modem found! Bridge needs a modem.")
|
||||||
|
|||||||
Reference in New Issue
Block a user