feat: add bridge reply channel and enhance command processing for improved communication

This commit is contained in:
MayaTheShy
2026-03-25 22:36:52 -04:00
parent f10108bd48
commit b9081f26a8

View File

@@ -16,6 +16,7 @@ local STATE_INTERVAL = 1 -- seconds between state forwards
-- Modem channels (must match inventoryManager.lua) -- Modem channels (must match inventoryManager.lua)
local BROADCAST_CHANNEL = 4200 local BROADCAST_CHANNEL = 4200
local ORDER_CHANNEL = 4201 local ORDER_CHANNEL = 4201
local BRIDGE_REPLY_CHANNEL = 4206 -- dedicated reply channel for bridge
------------------------------------------------- -------------------------------------------------
-- Load config from file if present -- Load config from file if present
@@ -72,6 +73,7 @@ local function findModem()
modem = peripheral.wrap(name) modem = peripheral.wrap(name)
modemName = name modemName = name
modem.open(BROADCAST_CHANNEL) modem.open(BROADCAST_CHANNEL)
modem.open(BRIDGE_REPLY_CHANNEL)
return true return true
end end
end end
@@ -144,7 +146,7 @@ local function processCommand(cmd)
print(string.format("[CMD] %s", action)) print(string.format("[CMD] %s", action))
if action == "order" then if action == "order" then
modem.transmit(ORDER_CHANNEL, BROADCAST_CHANNEL, { modem.transmit(ORDER_CHANNEL, BRIDGE_REPLY_CHANNEL, {
type = "order", type = "order",
commandId = cmd.commandId, commandId = cmd.commandId,
itemName = cmd.itemName, itemName = cmd.itemName,
@@ -152,45 +154,81 @@ local function processCommand(cmd)
dropperName = cmd.dropperName, dropperName = cmd.dropperName,
}) })
elseif action == "scan" then elseif action == "scan" then
modem.transmit(ORDER_CHANNEL, BROADCAST_CHANNEL, { modem.transmit(ORDER_CHANNEL, BRIDGE_REPLY_CHANNEL, {
type = "scan", type = "scan",
commandId = cmd.commandId, commandId = cmd.commandId,
}) })
elseif action == "toggle_pause" then elseif action == "toggle_pause" then
modem.transmit(ORDER_CHANNEL, BROADCAST_CHANNEL, { modem.transmit(ORDER_CHANNEL, BRIDGE_REPLY_CHANNEL, {
type = "toggle_pause", type = "toggle_pause",
commandId = cmd.commandId, commandId = cmd.commandId,
}) })
elseif action == "toggle_recipe" then elseif action == "toggle_recipe" then
modem.transmit(ORDER_CHANNEL, BROADCAST_CHANNEL, { modem.transmit(ORDER_CHANNEL, BRIDGE_REPLY_CHANNEL, {
type = "toggle_recipe", type = "toggle_recipe",
commandId = cmd.commandId, commandId = cmd.commandId,
recipe = cmd.recipe, recipe = cmd.recipe,
}) })
elseif action == "enable_all" then elseif action == "enable_all" then
modem.transmit(ORDER_CHANNEL, BROADCAST_CHANNEL, { modem.transmit(ORDER_CHANNEL, BRIDGE_REPLY_CHANNEL, {
type = "enable_all", type = "enable_all",
commandId = cmd.commandId, commandId = cmd.commandId,
}) })
elseif action == "disable_all" then elseif action == "disable_all" then
modem.transmit(ORDER_CHANNEL, BROADCAST_CHANNEL, { modem.transmit(ORDER_CHANNEL, BRIDGE_REPLY_CHANNEL, {
type = "disable_all", type = "disable_all",
commandId = cmd.commandId, commandId = cmd.commandId,
}) })
elseif action == "sort_barrel" then elseif action == "sort_barrel" then
modem.transmit(ORDER_CHANNEL, BROADCAST_CHANNEL, { modem.transmit(ORDER_CHANNEL, BRIDGE_REPLY_CHANNEL, {
type = "sort_barrel", type = "sort_barrel",
commandId = cmd.commandId, commandId = cmd.commandId,
barrelName = cmd.barrelName, barrelName = cmd.barrelName,
}) })
elseif action == "craft" then elseif action == "craft" then
modem.transmit(ORDER_CHANNEL, BROADCAST_CHANNEL, { modem.transmit(ORDER_CHANNEL, BRIDGE_REPLY_CHANNEL, {
type = "craft", type = "craft",
commandId = cmd.commandId, commandId = cmd.commandId,
recipeIdx = cmd.recipeIdx, recipeIdx = cmd.recipeIdx,
}) })
elseif action == "recursive_craft" then
modem.transmit(ORDER_CHANNEL, BRIDGE_REPLY_CHANNEL, {
type = "recursive_craft",
commandId = cmd.commandId,
itemName = cmd.itemName,
count = cmd.count,
})
elseif action == "learn_crafting_recipe" then
modem.transmit(ORDER_CHANNEL, BRIDGE_REPLY_CHANNEL, {
type = "learn_crafting_recipe",
commandId = cmd.commandId,
output = cmd.output,
count = cmd.count,
grid = cmd.grid,
})
elseif action == "learn_smelting_recipe" then
modem.transmit(ORDER_CHANNEL, BRIDGE_REPLY_CHANNEL, {
type = "learn_smelting_recipe",
commandId = cmd.commandId,
input = cmd.input,
result = cmd.result,
furnaces = cmd.furnaces,
})
elseif action == "forget_recipe" then
modem.transmit(ORDER_CHANNEL, BRIDGE_REPLY_CHANNEL, {
type = "forget_recipe",
commandId = cmd.commandId,
recipe = cmd.recipe,
})
elseif action == "sync_disabled_recipes" then
modem.transmit(ORDER_CHANNEL, BRIDGE_REPLY_CHANNEL, {
type = "sync_disabled_recipes",
commandId = cmd.commandId,
disabledRecipes = cmd.disabledRecipes,
smeltingPaused = cmd.smeltingPaused,
})
elseif action == "reboot" then elseif action == "reboot" then
modem.transmit(ORDER_CHANNEL, BROADCAST_CHANNEL, { modem.transmit(ORDER_CHANNEL, BRIDGE_REPLY_CHANNEL, {
type = "reboot", type = "reboot",
commandId = cmd.commandId, commandId = cmd.commandId,
target = cmd.target or "all", target = cmd.target or "all",
@@ -212,6 +250,19 @@ local function modemListener()
if message.type == "state" then if message.type == "state" then
latestState = message latestState = message
end end
elseif channel == BRIDGE_REPLY_CHANNEL and type(message) == "table" then
-- Forward command results back to web server
local resultType = message.type
if resultType == "order_result" or resultType == "craft_result"
or resultType == "recursive_craft_result" or resultType == "find_item_result" then
pcall(httpPost, "/api/bridge/result", {
action = resultType,
commandId = message.commandId,
success = message.success,
message = message.message,
error = message.error,
})
end
end end
end end
end end