From 5ef8977fadf1a086f7755664f22560954535c18b Mon Sep 17 00:00:00 2001 From: MayaTheShy Date: Thu, 19 Feb 2026 23:54:49 -0500 Subject: [PATCH] feat: Add pocket computer communication and command handling --- webbridge.lua | 132 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 131 insertions(+), 1 deletion(-) diff --git a/webbridge.lua b/webbridge.lua index e086b86..d2b6a2a 100644 --- a/webbridge.lua +++ b/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 STATUS_CHANNEL = 102 local COMMAND_CHANNEL = 100 +local POCKET_CHANNEL = 103 -- Pocket computer communication -- Find peripherals local modem = peripheral.find("modem") @@ -24,6 +25,7 @@ end modem.open(CHANNEL_RECEIVE) modem.open(STATUS_CHANNEL) +modem.open(POCKET_CHANNEL) -- Track turtles and stats local turtles = {} @@ -286,7 +288,7 @@ else print("Server: " .. SERVER_URL) 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 local POLL_INTERVAL = 2 -- Poll every 2 seconds (reduced frequency for better reliability) @@ -460,6 +462,134 @@ while true do 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