From f8baadce3ace0aaad29164c1688142219a4de865 Mon Sep 17 00:00:00 2001 From: MayaTheShy Date: Fri, 20 Feb 2026 01:53:43 -0500 Subject: [PATCH] feat: Implement eval command protocol and response handling in web bridge --- webbridge.lua | 70 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 21 deletions(-) diff --git a/webbridge.lua b/webbridge.lua index ced76e8..81b020e 100644 --- a/webbridge.lua +++ b/webbridge.lua @@ -337,43 +337,44 @@ while true do -- Forward commands back to turtle for _, cmd in ipairs(commands) do stats.commandsSent = stats.commandsSent + 1 - addLog(" CMD: " .. cmd.command .. " -> Turtle #" .. turtleID, colors.yellow) - local commandPacket = { - command = cmd.command, - param = cmd.param, - target = turtleID - } + local commandPacket - print("📡 Transmitting command to Turtle #" .. turtleID) - print(" Channel: " .. COMMAND_CHANNEL) - print(" Command: " .. cmd.command) - print(" Target: " .. turtleID) - print(" Packet: " .. textutils.serialize(commandPacket)) + if cmd.type == "eval" then + -- New eval protocol command + commandPacket = { + type = "eval", + uuid = cmd.uuid, + code = cmd.code, + target = turtleID, + stateUpdate = cmd.stateUpdate + } + addLog(" EVAL: " .. (cmd.uuid or "?"):sub(1, 8) .. " -> T#" .. turtleID, colors.yellow) + else + -- Legacy command protocol + commandPacket = { + command = cmd.command, + param = cmd.param, + target = turtleID + } + addLog(" CMD: " .. cmd.command .. " -> T#" .. turtleID, colors.yellow) + end -- Send command multiple times for reliability for i = 1, 3 do modem.transmit(COMMAND_CHANNEL, CHANNEL_RECEIVE, commandPacket) - print(" Transmission " .. i .. "/3 sent") - os.sleep(0.05) -- Small delay between retransmissions - end - - -- Debug: show what we're sending - if not hasMonitor then - print(" ✅ Sent 3 times on channel " .. COMMAND_CHANNEL) + os.sleep(0.05) end end -- Acknowledge that we sent the commands - -- Wait longer to ensure turtle has received them - os.sleep(1.5) -- Give turtle time to receive and process + os.sleep(1.5) if acknowledgeCommands(turtleID) then addLog(" ACK: Commands acknowledged", colors.lime) else addLog(" WARN: Failed to acknowledge", colors.orange) end - -- Mark that we sent commands to this turtle recentCommandSends[turtleID] = os.epoch("utc") end end @@ -510,6 +511,33 @@ while true do addLog(" -> Server error, local only", colors.red) end + elseif message.type == "eval_response" then + -- Turtle eval response - forward to server + local turtleID = message.turtleID + local uuid = message.uuid + addLog("Eval response from T#" .. turtleID .. " uuid:" .. (uuid or "?"):sub(1, 8), colors.cyan) + + local success = pcall(function() + local response = http.post( + SERVER_URL .. "/api/turtle/eval-response", + textutils.serializeJSON({ + turtleID = turtleID, + uuid = uuid, + result = message.result, + error = message.error + }), + {["Content-Type"] = "application/json"} + ) + if response then + response.close() + end + end) + + if not success then + stats.errors = stats.errors + 1 + addLog(" -> Failed to forward eval response", colors.red) + end + elseif message.type == "blocks_discovered" then -- Turtle discovered new blocks - forward to server local turtleID = message.turtleID