Skip to content

Instantly share code, notes, and snippets.

@BK1031
Created July 10, 2024 21:18
Show Gist options
  • Save BK1031/c81fbbf456f265f95d2692f42685d1f8 to your computer and use it in GitHub Desktop.
Save BK1031/c81fbbf456f265f95d2692f42685d1f8 to your computer and use it in GitHub Desktop.
racecar_analytics dashboard data retrieval
import React from "react";
import useWebSocket, { ReadyState } from "react-use-websocket";
import axios from "axios";
function App() {
const [ecuSocketUrl] = React.useState("ws://localhost:9000/ecu/stream");
const { lastMessage: lastEcuMessage, readyState } = useWebSocket(ecuSocketUrl);
const [ecuData, setEcuData] = React.useState([{}]);
React.useEffect(() => {
if (lastEcuMessage !== null) {
setEcuData((prev) => [
...prev.slice(-50),
JSON.parse(lastEcuMessage.data),
]);
}
}, [lastEcuMessage]);
React.useEffect(() => {
getAllECUData();
}, []);
const getAllECUData = () => {
axios.get('http://localhost:9000/ecu')
.then(response => {
setEcuData(response.data.slice(-40));
})
.catch(error => {
console.error('Error fetching ECU data:', error);
});
};
// rest of component
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment