feat: Implement eval command protocol and response handling in web bridge
This commit is contained in:
@@ -337,43 +337,44 @@ while true do
|
|||||||
-- Forward commands back to turtle
|
-- Forward commands back to turtle
|
||||||
for _, cmd in ipairs(commands) do
|
for _, cmd in ipairs(commands) do
|
||||||
stats.commandsSent = stats.commandsSent + 1
|
stats.commandsSent = stats.commandsSent + 1
|
||||||
addLog(" CMD: " .. cmd.command .. " -> Turtle #" .. turtleID, colors.yellow)
|
|
||||||
|
|
||||||
local commandPacket = {
|
local commandPacket
|
||||||
command = cmd.command,
|
|
||||||
param = cmd.param,
|
|
||||||
target = turtleID
|
|
||||||
}
|
|
||||||
|
|
||||||
print("📡 Transmitting command to Turtle #" .. turtleID)
|
if cmd.type == "eval" then
|
||||||
print(" Channel: " .. COMMAND_CHANNEL)
|
-- New eval protocol command
|
||||||
print(" Command: " .. cmd.command)
|
commandPacket = {
|
||||||
print(" Target: " .. turtleID)
|
type = "eval",
|
||||||
print(" Packet: " .. textutils.serialize(commandPacket))
|
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
|
-- Send command multiple times for reliability
|
||||||
for i = 1, 3 do
|
for i = 1, 3 do
|
||||||
modem.transmit(COMMAND_CHANNEL, CHANNEL_RECEIVE, commandPacket)
|
modem.transmit(COMMAND_CHANNEL, CHANNEL_RECEIVE, commandPacket)
|
||||||
print(" Transmission " .. i .. "/3 sent")
|
os.sleep(0.05)
|
||||||
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)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Acknowledge that we sent the commands
|
-- Acknowledge that we sent the commands
|
||||||
-- Wait longer to ensure turtle has received them
|
os.sleep(1.5)
|
||||||
os.sleep(1.5) -- Give turtle time to receive and process
|
|
||||||
if acknowledgeCommands(turtleID) then
|
if acknowledgeCommands(turtleID) then
|
||||||
addLog(" ACK: Commands acknowledged", colors.lime)
|
addLog(" ACK: Commands acknowledged", colors.lime)
|
||||||
else
|
else
|
||||||
addLog(" WARN: Failed to acknowledge", colors.orange)
|
addLog(" WARN: Failed to acknowledge", colors.orange)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Mark that we sent commands to this turtle
|
|
||||||
recentCommandSends[turtleID] = os.epoch("utc")
|
recentCommandSends[turtleID] = os.epoch("utc")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -510,6 +511,33 @@ while true do
|
|||||||
addLog(" -> Server error, local only", colors.red)
|
addLog(" -> Server error, local only", colors.red)
|
||||||
end
|
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
|
elseif message.type == "blocks_discovered" then
|
||||||
-- Turtle discovered new blocks - forward to server
|
-- Turtle discovered new blocks - forward to server
|
||||||
local turtleID = message.turtleID
|
local turtleID = message.turtleID
|
||||||
|
|||||||
Reference in New Issue
Block a user