Skip to content

Commit

Permalink
refactor(connections): 🎉 add ros connection control functions
Browse files Browse the repository at this point in the history
  • Loading branch information
gokhangunduz committed Dec 20, 2023
1 parent 6af1737 commit 40b9c23
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ui",
"version": "0.23.5",
"version": "0.23.6",
"private": true,
"scripts": {
"dev": "react-scripts start",
Expand Down
49 changes: 23 additions & 26 deletions src/components/Connections/Connections.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,34 @@ export default function Connections(): ReactElement {
const [isRosConnected, setIsRosConnected] = useState<boolean | null>(null);

useEffect(() => {
const rosClient: ROSLIB.Ros = new ROSLIB.Ros({
url: responseRobot?.bridgeIngressEndpoint,
});
let rosClient: ROSLIB.Ros | null = null;

if (isRosConnected === null) {
if (
isRobotReady &&
isSettedCookie &&
responseRobot?.bridgeIngressEndpoint
) {
rosClient?.on("connection", function () {
setIsRosConnected(true);
});
const establishRosConnection = () => {
rosClient = new ROSLIB.Ros({
url: responseRobot?.bridgeIngressEndpoint,
});

rosClient?.on("error", function (error) {
setIsRosConnected(false);
});
rosClient.on("connection", () => setIsRosConnected(true));
rosClient.on("error", () => setIsRosConnected(false));
};

const closeRosConnection = () => {
if (rosClient) {
rosClient.close();
}
} else {
rosClient?.close();
};

if (
isRobotReady &&
isSettedCookie &&
responseRobot?.bridgeIngressEndpoint
) {
closeRosConnection();
establishRosConnection();
}

return () => {
rosClient?.close();
};
}, [
isRobotReady,
isSettedCookie,
isRosConnected,
responseRobot?.bridgeIngressEndpoint,
]);
return closeRosConnection;
}, [isRobotReady, isSettedCookie, responseRobot?.bridgeIngressEndpoint]);

useEffect(() => {
console.log("isRosConnected", isRosConnected);
Expand Down

0 comments on commit 40b9c23

Please sign in to comment.