feat: Implement wireless home position sync and set functionality for turtles
This commit is contained in:
@@ -402,6 +402,75 @@ while true do
|
||||
stats.errors = stats.errors + 1
|
||||
addLog(" -> Server error", colors.red)
|
||||
end
|
||||
|
||||
elseif message.type == "request_home" then
|
||||
-- Turtle requesting its home position from server
|
||||
local turtleID = message.turtleID
|
||||
addLog("Turtle #" .. turtleID .. " requesting home", colors.cyan)
|
||||
|
||||
local success, result = pcall(function()
|
||||
local response = http.get(SERVER_URL .. "/api/turtle/" .. turtleID .. "/home")
|
||||
if response then
|
||||
local content = response.readAll()
|
||||
response.close()
|
||||
local data = textutils.unserializeJSON(content)
|
||||
return data
|
||||
end
|
||||
return nil
|
||||
end)
|
||||
|
||||
if success and result then
|
||||
modem.transmit(CHANNEL_RECEIVE, CHANNEL_RECEIVE, {
|
||||
type = "home_position",
|
||||
turtleID = turtleID,
|
||||
homePosition = result.homePosition
|
||||
})
|
||||
addLog(" -> Sent home position", colors.lime)
|
||||
else
|
||||
modem.transmit(CHANNEL_RECEIVE, CHANNEL_RECEIVE, {
|
||||
type = "home_position",
|
||||
turtleID = turtleID,
|
||||
homePosition = nil
|
||||
})
|
||||
addLog(" -> No home on server", colors.yellow)
|
||||
end
|
||||
|
||||
elseif message.type == "set_home" then
|
||||
-- Turtle setting its home position
|
||||
local turtleID = message.turtleID
|
||||
local position = message.position
|
||||
addLog("Turtle #" .. turtleID .. " setting home", colors.cyan)
|
||||
|
||||
local success, result = pcall(function()
|
||||
local response = http.post(
|
||||
SERVER_URL .. "/api/turtle/" .. turtleID .. "/home",
|
||||
textutils.serializeJSON({position = position}),
|
||||
{["Content-Type"] = "application/json"}
|
||||
)
|
||||
if response then
|
||||
local content = response.readAll()
|
||||
response.close()
|
||||
local data = textutils.unserializeJSON(content)
|
||||
return data
|
||||
end
|
||||
return nil
|
||||
end)
|
||||
|
||||
if success and result and result.success then
|
||||
modem.transmit(CHANNEL_RECEIVE, CHANNEL_RECEIVE, {
|
||||
type = "home_set_confirm",
|
||||
turtleID = turtleID,
|
||||
homePosition = result.homePosition
|
||||
})
|
||||
addLog(" -> Home saved to server", colors.lime)
|
||||
else
|
||||
modem.transmit(CHANNEL_RECEIVE, CHANNEL_RECEIVE, {
|
||||
type = "home_set_confirm",
|
||||
turtleID = turtleID,
|
||||
homePosition = position
|
||||
})
|
||||
addLog(" -> Server error, local only", colors.red)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user