fix: Improve message processing and command transmission in turtle system
This commit is contained in:
28
turtle.lua
28
turtle.lua
@@ -565,27 +565,33 @@ local commands = {
|
|||||||
|
|
||||||
-- Process incoming messages
|
-- Process incoming messages
|
||||||
local function processMessage(message)
|
local function processMessage(message)
|
||||||
print("Received message on modem")
|
if type(message) ~= "table" then
|
||||||
print(" Type: " .. type(message))
|
return
|
||||||
|
|
||||||
if type(message) == "table" then
|
|
||||||
print(" Has command: " .. tostring(message.command ~= nil))
|
|
||||||
if message.command then
|
|
||||||
print(" Command: " .. message.command)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if type(message) == "table" and message.command then
|
-- Check if message is targeted to this turtle
|
||||||
print("Processing command: " .. message.command)
|
local myID = os.getComputerID()
|
||||||
|
if message.target and message.target ~= myID then
|
||||||
|
-- This command is for a different turtle, ignore it
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
print("Received command message")
|
||||||
|
print(" Target: " .. tostring(message.target))
|
||||||
|
print(" My ID: " .. myID)
|
||||||
|
|
||||||
|
if message.command then
|
||||||
|
print(" Command: " .. message.command)
|
||||||
|
|
||||||
local handler = commands[message.command]
|
local handler = commands[message.command]
|
||||||
if handler then
|
if handler then
|
||||||
|
print(" Executing command...")
|
||||||
local success, result = handler(message.param)
|
local success, result = handler(message.param)
|
||||||
|
|
||||||
modem.transmit(CHANNEL_SEND, CHANNEL_RECEIVE, {
|
modem.transmit(CHANNEL_SEND, CHANNEL_RECEIVE, {
|
||||||
status = success and "ok" or "error",
|
status = success and "ok" or "error",
|
||||||
message = result or "",
|
message = result or "",
|
||||||
turtleID = os.getComputerID()
|
turtleID = myID
|
||||||
})
|
})
|
||||||
|
|
||||||
broadcastStatus()
|
broadcastStatus()
|
||||||
|
|||||||
@@ -301,11 +301,18 @@ while true do
|
|||||||
stats.commandsSent = stats.commandsSent + 1
|
stats.commandsSent = stats.commandsSent + 1
|
||||||
addLog("CMD: " .. cmd.command .. " -> Turtle #" .. turtleID, colors.yellow)
|
addLog("CMD: " .. cmd.command .. " -> Turtle #" .. turtleID, colors.yellow)
|
||||||
|
|
||||||
modem.transmit(COMMAND_CHANNEL, CHANNEL_RECEIVE, {
|
local commandPacket = {
|
||||||
command = cmd.command,
|
command = cmd.command,
|
||||||
param = cmd.param,
|
param = cmd.param,
|
||||||
target = turtleID
|
target = turtleID
|
||||||
})
|
}
|
||||||
|
|
||||||
|
modem.transmit(COMMAND_CHANNEL, CHANNEL_RECEIVE, commandPacket)
|
||||||
|
|
||||||
|
-- Debug: show what we're sending
|
||||||
|
if not hasMonitor then
|
||||||
|
print(" Sent to channel " .. COMMAND_CHANNEL .. ": target=" .. turtleID)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user