fix: Keep the conencted robots in a global list
ref: N25B-142
This commit is contained in:
23
src/App.tsx
23
src/App.tsx
@@ -1,4 +1,5 @@
|
|||||||
import { Routes, Route, Link } from 'react-router'
|
import { Routes, Route, Link } from 'react-router'
|
||||||
|
import { useState, useEffect } from 'react'
|
||||||
import './App.css'
|
import './App.css'
|
||||||
import TemplatePage from './pages/TemplatePage/Template.tsx'
|
import TemplatePage from './pages/TemplatePage/Template.tsx'
|
||||||
import Home from './pages/Home/Home.tsx'
|
import Home from './pages/Home/Home.tsx'
|
||||||
@@ -8,7 +9,16 @@ import Logging from './pages/Logging/Logging.tsx'
|
|||||||
import VisProg from "./pages/VisProgPage/VisProg.tsx";
|
import VisProg from "./pages/VisProgPage/VisProg.tsx";
|
||||||
|
|
||||||
function App(){
|
function App(){
|
||||||
|
|
||||||
|
// Define what our conencted robot should include
|
||||||
|
type Robot = {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
port: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
const [connectedRobots, setConnectedRobots] = useState<Robot[]>([]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
{/* Should not use inline styles like this */}
|
{/* Should not use inline styles like this */}
|
||||||
@@ -19,7 +29,16 @@ function App(){
|
|||||||
<Route path="/ServerComms" element = {<ServerComms />} />
|
<Route path="/ServerComms" element = {<ServerComms />} />
|
||||||
<Route path="/visprog" element={<VisProg />} />
|
<Route path="/visprog" element={<VisProg />} />
|
||||||
<Route path="/logging" element = {<Logging />} />
|
<Route path="/logging" element = {<Logging />} />
|
||||||
<Route path="/ConnectedRobots" element = {<ConnectedRobots />} />
|
<Route
|
||||||
|
path="/ConnectedRobots"
|
||||||
|
// Pass the useState to the page to update from there
|
||||||
|
element={
|
||||||
|
<ConnectedRobots
|
||||||
|
connectedRobots={connectedRobots}
|
||||||
|
setConnectedRobots={setConnectedRobots}
|
||||||
|
/>
|
||||||
|
}
|
||||||
|
/>
|
||||||
</Routes>
|
</Routes>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -8,15 +8,22 @@ type Robot = {
|
|||||||
port: number;
|
port: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function ConnectedRobots() {
|
// Define the expected arguments
|
||||||
const [connectedRobots, setConnectedRobots] = useState<Robot[]>([]);
|
type ConnectedRobotsProps = {
|
||||||
|
connectedRobots: Robot[];
|
||||||
|
setConnectedRobots: React.Dispatch<React.SetStateAction<Robot[]>>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function ConnectedRobots({
|
||||||
|
connectedRobots, setConnectedRobots}: ConnectedRobotsProps) {
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const eventSource = new EventSource("http://localhost:8000/sse");
|
const eventSource = new EventSource("http://localhost:8000/sse");
|
||||||
eventSource.onmessage = (event) => {
|
eventSource.onmessage = (event) => {
|
||||||
try {
|
try {
|
||||||
|
console.log("message received :", event.data)
|
||||||
const data = JSON.parse(event.data);
|
const data = JSON.parse(event.data);
|
||||||
|
|
||||||
// Example: data = { event: "robot_connected", id: "pepper_robot1", name: "Pepper", port: 1234 }
|
// Example: data = { event: "robot_connected", id: "pepper_robot1", name: "Pepper", port: 1234 }
|
||||||
if (data.event === "robot_connected") {
|
if (data.event === "robot_connected") {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user