From a91ad1d4ca2db39bbbc11943e6067e489a126d04 Mon Sep 17 00:00:00 2001 From: MayaTheShy Date: Mon, 16 Feb 2026 00:56:59 -0500 Subject: [PATCH] fix: Refactor message handling to improve status updates and server communication --- webbridge.lua | 68 ++++++++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/webbridge.lua b/webbridge.lua index f2e318b..b299820 100644 --- a/webbridge.lua +++ b/webbridge.lua @@ -60,41 +60,47 @@ end while true do local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message") - print("Received modem message on channel " .. channel) - - if type(message) == "table" then - print(" Message type: " .. (message.type or "no type")) - print(" Turtle ID: " .. tostring(message.turtleID)) - else - print(" Message is not a table, it's: " .. type(message)) - end - - if channel == STATUS_CHANNEL and type(message) == "table" and message.type == "status" then - -- Status update from turtle - print("✓ Valid status from Turtle " .. (message.turtleID or "?")) + -- Only process messages on our channels + if channel == STATUS_CHANNEL or channel == CHANNEL_RECEIVE then + print("Received modem message on channel " .. channel) - -- Update local cache - turtles[message.turtleID] = message - - -- Forward to web server - local success = sendToServer(message) - if success then - print(" -> Sent to server") + if type(message) == "table" then + print(" Message type: " .. (message.type or "no type")) + print(" Turtle ID: " .. tostring(message.turtleID)) - -- Check for commands from server - local commands = pollCommands(message.turtleID) - - -- Forward commands back to turtle - for _, cmd in ipairs(commands) do - print(" -> Command for Turtle " .. message.turtleID .. ": " .. cmd.command) - modem.transmit(100, 101, { - command = cmd.command, - param = cmd.param, - target = message.turtleID - }) + if message.type == "status" then + -- Status update from turtle + print("✓ Valid status from Turtle " .. (message.turtleID or "?")) + + -- Update local cache + turtles[message.turtleID] = message + + -- Forward to web server + local success = sendToServer(message) + if success then + print(" -> Sent to server") + + -- Check for commands from server + local commands = pollCommands(message.turtleID) + + -- Forward commands back to turtle + for _, cmd in ipairs(commands) do + print(" -> Command for Turtle " .. message.turtleID .. ": " .. cmd.command) + modem.transmit(100, 101, { + command = cmd.command, + param = cmd.param, + target = message.turtleID + }) + end + else + print(" -> Failed to send to server") + end + else + print(" Ignoring non-status message") end else - print(" -> Failed to send to server") + -- Ignore non-table messages (from other mods/systems) + -- Silently skip to avoid spam end end