Created
July 10, 2024 21:18
-
-
Save BK1031/c81fbbf456f265f95d2692f42685d1f8 to your computer and use it in GitHub Desktop.
racecar_analytics dashboard data retrieval
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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