feat: Add pocket computer communication and command handling
This commit is contained in:
132
webbridge.lua
132
webbridge.lua
@@ -6,6 +6,7 @@ local SERVER_URL = "http://10.10.10.6:4200" -- Change to your server address
|
|||||||
local CHANNEL_RECEIVE = 101
|
local CHANNEL_RECEIVE = 101
|
||||||
local STATUS_CHANNEL = 102
|
local STATUS_CHANNEL = 102
|
||||||
local COMMAND_CHANNEL = 100
|
local COMMAND_CHANNEL = 100
|
||||||
|
local POCKET_CHANNEL = 103 -- Pocket computer communication
|
||||||
|
|
||||||
-- Find peripherals
|
-- Find peripherals
|
||||||
local modem = peripheral.find("modem")
|
local modem = peripheral.find("modem")
|
||||||
@@ -24,6 +25,7 @@ end
|
|||||||
|
|
||||||
modem.open(CHANNEL_RECEIVE)
|
modem.open(CHANNEL_RECEIVE)
|
||||||
modem.open(STATUS_CHANNEL)
|
modem.open(STATUS_CHANNEL)
|
||||||
|
modem.open(POCKET_CHANNEL)
|
||||||
|
|
||||||
-- Track turtles and stats
|
-- Track turtles and stats
|
||||||
local turtles = {}
|
local turtles = {}
|
||||||
@@ -286,7 +288,7 @@ else
|
|||||||
print("Server: " .. SERVER_URL)
|
print("Server: " .. SERVER_URL)
|
||||||
end
|
end
|
||||||
|
|
||||||
addLog("Listening on channels " .. STATUS_CHANNEL .. " and " .. CHANNEL_RECEIVE, colors.lightBlue)
|
addLog("Listening on channels " .. STATUS_CHANNEL .. ", " .. CHANNEL_RECEIVE .. ", " .. POCKET_CHANNEL, colors.lightBlue)
|
||||||
|
|
||||||
-- Start polling timer
|
-- Start polling timer
|
||||||
local POLL_INTERVAL = 2 -- Poll every 2 seconds (reduced frequency for better reliability)
|
local POLL_INTERVAL = 2 -- Poll every 2 seconds (reduced frequency for better reliability)
|
||||||
@@ -460,6 +462,134 @@ while true do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
elseif channel == POCKET_CHANNEL then
|
||||||
|
-- Handle pocket computer requests
|
||||||
|
stats.messagesReceived = stats.messagesReceived + 1
|
||||||
|
|
||||||
|
if type(message) == "table" and message.from then
|
||||||
|
local pocketID = message.from
|
||||||
|
|
||||||
|
if message.type == "turtle_command" then
|
||||||
|
-- Forward command to turtle
|
||||||
|
local turtleID = message.turtleID
|
||||||
|
addLog("Pocket #" .. pocketID .. " -> Turtle #" .. turtleID .. ": " .. message.command, colors.magenta)
|
||||||
|
|
||||||
|
modem.transmit(COMMAND_CHANNEL, CHANNEL_RECEIVE, {
|
||||||
|
command = message.command,
|
||||||
|
param = message.param,
|
||||||
|
target = turtleID
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Send acknowledgment
|
||||||
|
modem.transmit(POCKET_CHANNEL, POCKET_CHANNEL, {
|
||||||
|
type = "command_ack",
|
||||||
|
to = pocketID
|
||||||
|
})
|
||||||
|
|
||||||
|
elseif message.type == "player_position" then
|
||||||
|
-- Forward player position to server
|
||||||
|
addLog("Pocket #" .. pocketID .. " GPS update", colors.cyan)
|
||||||
|
|
||||||
|
local success = pcall(function()
|
||||||
|
http.post(
|
||||||
|
SERVER_URL .. "/api/player/update",
|
||||||
|
textutils.serializeJSON({
|
||||||
|
playerID = message.playerID,
|
||||||
|
position = message.position,
|
||||||
|
timestamp = message.timestamp
|
||||||
|
}),
|
||||||
|
{["Content-Type"] = "application/json"}
|
||||||
|
)
|
||||||
|
end)
|
||||||
|
|
||||||
|
if not success then
|
||||||
|
addLog(" -> Failed to send player position", colors.red)
|
||||||
|
end
|
||||||
|
|
||||||
|
elseif message.type == "server_stats_request" then
|
||||||
|
-- Fetch server stats and send to pocket
|
||||||
|
addLog("Pocket #" .. pocketID .. " requesting stats", colors.yellow)
|
||||||
|
|
||||||
|
local success, result = pcall(function()
|
||||||
|
local response = http.get(SERVER_URL .. "/api/stats")
|
||||||
|
if response then
|
||||||
|
local content = response.readAll()
|
||||||
|
response.close()
|
||||||
|
return textutils.unserializeJSON(content)
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end)
|
||||||
|
|
||||||
|
if success and result then
|
||||||
|
modem.transmit(POCKET_CHANNEL, POCKET_CHANNEL, {
|
||||||
|
type = "server_stats",
|
||||||
|
to = pocketID,
|
||||||
|
data = result
|
||||||
|
})
|
||||||
|
else
|
||||||
|
modem.transmit(POCKET_CHANNEL, POCKET_CHANNEL, {
|
||||||
|
type = "error",
|
||||||
|
to = pocketID,
|
||||||
|
error = "Failed to fetch server stats"
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
elseif message.type == "webbridge_control" then
|
||||||
|
-- Handle webbridge control commands
|
||||||
|
addLog("Pocket #" .. pocketID .. " control: " .. message.command, colors.orange)
|
||||||
|
|
||||||
|
if message.command == "ping" then
|
||||||
|
modem.transmit(POCKET_CHANNEL, POCKET_CHANNEL, {
|
||||||
|
type = "webbridge_log",
|
||||||
|
to = pocketID,
|
||||||
|
text = "Pong! Webbridge online"
|
||||||
|
})
|
||||||
|
elseif message.command == "status" then
|
||||||
|
local turtleCount = 0
|
||||||
|
for _ in pairs(turtles) do
|
||||||
|
turtleCount = turtleCount + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
modem.transmit(POCKET_CHANNEL, POCKET_CHANNEL, {
|
||||||
|
type = "webbridge_status",
|
||||||
|
to = pocketID,
|
||||||
|
data = {
|
||||||
|
messages = stats.messagesReceived,
|
||||||
|
commands = stats.commandsSent,
|
||||||
|
turtles = turtleCount,
|
||||||
|
uptime = os.epoch("utc") - stats.startTime
|
||||||
|
}
|
||||||
|
})
|
||||||
|
elseif message.command == "restart" then
|
||||||
|
modem.transmit(POCKET_CHANNEL, POCKET_CHANNEL, {
|
||||||
|
type = "webbridge_log",
|
||||||
|
to = pocketID,
|
||||||
|
text = "Restarting webbridge..."
|
||||||
|
})
|
||||||
|
sleep(1)
|
||||||
|
os.reboot()
|
||||||
|
elseif message.command == "logs" then
|
||||||
|
-- Send recent log entries
|
||||||
|
for i = math.max(1, #activityLog - 5), #activityLog do
|
||||||
|
if activityLog[i] then
|
||||||
|
modem.transmit(POCKET_CHANNEL, POCKET_CHANNEL, {
|
||||||
|
type = "webbridge_log",
|
||||||
|
to = pocketID,
|
||||||
|
text = activityLog[i].text
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
elseif message.type == "web_interface_request" then
|
||||||
|
-- Send web interface info
|
||||||
|
modem.transmit(POCKET_CHANNEL, POCKET_CHANNEL, {
|
||||||
|
type = "webbridge_log",
|
||||||
|
to = pocketID,
|
||||||
|
text = "Web: " .. SERVER_URL
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user