Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
103 changes: 103 additions & 0 deletions gs/backend/websocket_poc/ws_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import asyncio
import json
from datetime import datetime

import uvicorn
from fastapi import FastAPI, WebSocket, WebSocketDisconnect

app = FastAPI(title="WebSocket Terminal")


@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket) -> None:
"""
Handle a WebSocket connection for the Raspberry Pi terminal.

Receives commands from the client, executes them asynchronously on
the Raspberry Pi shell, and streams stdout/stderr back to the client
in real time, including status messages.
"""

await websocket.accept()
print("New connection established.")

try:
while True:
# Receive command from client
command = await websocket.receive_text()
print(f"Received command: {command}")

timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

# Step 1: Send loading indicator
await websocket.send_text(
json.dumps({"type": "status", "message": "...", "timestamp": timestamp, "source": "raspberry-pi"})
)

# Step 2: Run command on Raspberry Pi shell
process = await asyncio.create_subprocess_shell(
command, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
)

# Step 3: Read and send output line by line
while True:
line = await process.stdout.readline()
if not line:
break
output = line.decode().strip()
if output:
await websocket.send_text(
json.dumps(
{
"type": "command",
"message": output,
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"source": "raspberry-pi",
}
)
)

# Step 4: Capture and send any errors
err = await process.stderr.read()
if err:
await websocket.send_text(
json.dumps(
{
"type": "error",
"message": err.decode().strip(),
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"source": "raspberry-pi",
}
)
)

# Step 5: Notify completion
await websocket.send_text(
json.dumps(
{
"type": "status",
"message": f"Command '{command}' completed.",
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"source": "raspberry-pi",
}
)
)

except WebSocketDisconnect:
print("Client disconnected.")
except Exception as e:
print(f"Error: {e}")
await websocket.send_text(
json.dumps(
{
"type": "error",
"message": str(e),
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"source": "server",
}
)
)


if __name__ == "__main__":
uvicorn.run("backend.websocket_poc.ws_server:app", host="0.0.0.0", port=9067, reload=True)
20 changes: 0 additions & 20 deletions gs/frontend/mcc/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions gs/frontend/mcc/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Dashboard from "./pages/Dashboard";
import AROAdmin from "./pages/AROAdmin";
import LiveSession from "./pages/LiveSession";
import Login from "./pages/Login";
import WebsocketTerminal from "./pages/WebsocketTerminal";

/**
* @brief App component displaying the main application
Expand All @@ -22,6 +23,7 @@ function App() {
<Route path="/telemetry-data" element={<AROAdmin />} />
<Route path="/aro-requests" element={<LiveSession />} />
<Route path="/login" element={<Login />} />
<Route path="/websocket-terminal" element={<WebsocketTerminal />} />
</Routes>
</>
);
Expand Down
Loading
Loading