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
|
||||
-- forwards state to the web server via HTTP.
|
||||
-- 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
|
||||
-------------------------------------------------
|
||||
|
||||
-- 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
|
||||
-- Configuration (via platform)
|
||||
-------------------------------------------------
|
||||
|
||||
local _baseDir = fs.getDir(shell.getRunningProgram())
|
||||
local function _path(rel) return fs.combine(_baseDir, rel) end
|
||||
|
||||
-- Persistent config path: survives Opus package updates
|
||||
local _PERSIST_DIR = "usr/config/inventory-manager"
|
||||
local function _configPath(rel)
|
||||
if fs.isDir(_PERSIST_DIR) or fs.isDir("packages/inventory-manager") then
|
||||
if not fs.isDir(_PERSIST_DIR) then fs.makeDir(_PERSIST_DIR) end
|
||||
return fs.combine(_PERSIST_DIR, rel)
|
||||
end
|
||||
return _path(rel)
|
||||
local config, configSource = WebBridge.loadConfig({
|
||||
serverUrl = "http://localhost",
|
||||
pollInterval = 0.5,
|
||||
stateInterval = 1,
|
||||
apiKey = nil,
|
||||
}, {
|
||||
"usr/config/inventory-manager/.webbridge_config",
|
||||
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
|
||||
|
||||
local CONFIG_FILE = _configPath(".webbridge_config")
|
||||
local API_KEY = nil -- optional API key for server auth
|
||||
|
||||
local function loadConfig()
|
||||
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
|
||||
-- Channels from platform registry (matches inventoryManager.lua)
|
||||
local BROADCAST_CHANNEL = Channels.get('inventory.broadcast')
|
||||
local ORDER_CHANNEL = Channels.get('inventory.order')
|
||||
local BRIDGE_REPLY_CHANNEL = Channels.get('inventory.bridge')
|
||||
|
||||
-------------------------------------------------
|
||||
-- State
|
||||
@@ -64,66 +50,28 @@ local modemName = nil
|
||||
local running = true
|
||||
|
||||
-------------------------------------------------
|
||||
-- Find modem
|
||||
-------------------------------------------------
|
||||
|
||||
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
|
||||
-- HTTP helpers (thin wrappers around platform)
|
||||
-------------------------------------------------
|
||||
|
||||
local function httpPost(path, body)
|
||||
local url = SERVER_URL .. path
|
||||
local data = textutils.serialiseJSON(body)
|
||||
local headers = { ["Content-Type"] = "application/json" }
|
||||
if API_KEY then headers["Authorization"] = "Bearer " .. API_KEY end
|
||||
|
||||
local ok, result = pcall(function()
|
||||
local response = http.post(url, data, headers)
|
||||
if response then
|
||||
local responseData = response.readAll()
|
||||
response.close()
|
||||
return responseData
|
||||
local result, err = WebBridge.httpPost(SERVER_URL .. path, body,
|
||||
WebBridge.authHeaders(API_KEY))
|
||||
if not result and err then
|
||||
print(string.format("[ERR] HTTP POST %s: %s", path, tostring(err)))
|
||||
end
|
||||
end)
|
||||
|
||||
if ok then
|
||||
return result
|
||||
end
|
||||
print(string.format("[ERR] HTTP POST %s: %s", path, tostring(result)))
|
||||
return nil
|
||||
end
|
||||
|
||||
local function httpGet(path)
|
||||
local url = SERVER_URL .. path
|
||||
local headers = nil
|
||||
if API_KEY then headers = { ["Authorization"] = "Bearer " .. API_KEY } end
|
||||
local ok, result = pcall(function()
|
||||
local response = http.get(url, headers)
|
||||
if response then
|
||||
local data = response.readAll()
|
||||
response.close()
|
||||
return textutils.unserialiseJSON(data)
|
||||
local rawBody, err = WebBridge.httpGet(SERVER_URL .. path,
|
||||
WebBridge.authHeaders(API_KEY))
|
||||
if not rawBody then
|
||||
if err then
|
||||
print(string.format("[ERR] HTTP GET %s: %s", path, tostring(err)))
|
||||
end
|
||||
end)
|
||||
|
||||
if ok then
|
||||
return result
|
||||
end
|
||||
print(string.format("[ERR] HTTP GET %s: %s", path, tostring(result)))
|
||||
return nil
|
||||
end
|
||||
return textutils.unserialiseJSON(rawBody)
|
||||
end
|
||||
|
||||
-------------------------------------------------
|
||||
@@ -347,9 +295,12 @@ local function main()
|
||||
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)
|
||||
else
|
||||
print("[WARN] No modem found! Bridge needs a modem.")
|
||||
|
||||
Reference in New Issue
Block a user