-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Race tab should use live data from socket
- Loading branch information
1 parent
502f99d
commit 092b0b9
Showing
8 changed files
with
115 additions
and
50 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import axios from "axios"; | ||
import { | ||
type PropsWithChildren, | ||
createContext, | ||
useContext, | ||
useEffect, | ||
useState, | ||
} from "react"; | ||
|
||
import { CONNECTIONTYPES, useAppState } from "@/contexts/AppStateContext"; | ||
import { socketIO } from "@/contexts/SocketContext"; | ||
import { ILapData } from "@shared/helios-types"; | ||
|
||
interface ILapDataContextReturn { | ||
lapData: ILapData[]; | ||
} | ||
|
||
const lapDataContext = createContext<ILapDataContextReturn>({ | ||
lapData: [], | ||
}); | ||
|
||
export function LapDataContextProvider({ | ||
children, | ||
}: PropsWithChildren): JSX.Element { | ||
const { currentAppState } = useAppState(); | ||
const [lapData, setLapData] = useState<ILapData[]>([]); | ||
|
||
const onLapData = (data: ILapData) => { | ||
setLapData((prev) => [...prev, data]); | ||
}; | ||
|
||
const fetchLapData = async () => { | ||
try { | ||
const response = await axios.get( | ||
`https://aedes.calgarysolarcar.ca:3001/laps`, | ||
); | ||
return response.data; // Assuming the API returns an array of lap data | ||
} catch (error) { | ||
return { error: "Error fetching lap data" }; | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
fetchLapData() | ||
.then((response) => { | ||
const formattedData = response.data.map( | ||
(lapPacket: { data: ILapData }) => ({ | ||
ampHours: parseFloat(lapPacket.data.ampHours.toFixed(2)), | ||
averagePackCurrent: parseFloat( | ||
lapPacket.data.averagePackCurrent.toFixed(2), | ||
), | ||
averageSpeed: parseFloat(lapPacket.data.averageSpeed.toFixed(2)), | ||
batterySecondsRemaining: parseFloat( | ||
lapPacket.data.batterySecondsRemaining.toFixed(2), | ||
), | ||
distance: parseFloat(lapPacket.data.distance.toFixed(2)), | ||
lapTime: parseFloat(lapPacket.data.lapTime.toFixed(2)), | ||
netPowerOut: parseFloat(lapPacket.data.netPowerOut.toFixed(2)), | ||
timeStamp: new Date(lapPacket.data.timeStamp).toLocaleDateString( | ||
"en-US", | ||
), | ||
totalPowerIn: parseFloat(lapPacket.data.totalPowerIn.toFixed(2)), | ||
totalPowerOut: parseFloat(lapPacket.data.totalPowerOut.toFixed(2)), | ||
}), | ||
); | ||
|
||
setLapData(formattedData); | ||
}) | ||
.catch((error) => { | ||
throw new Error(error); | ||
}); | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (currentAppState.connectionType === CONNECTIONTYPES.NETWORK) { | ||
socketIO.on("lapData", onLapData); | ||
return () => { | ||
socketIO.off("lapData", onLapData); | ||
}; | ||
} | ||
}, [currentAppState.connectionType, currentAppState.playbackSwitch]); | ||
|
||
return ( | ||
<lapDataContext.Provider value={{ lapData }}> | ||
{children} | ||
</lapDataContext.Provider> | ||
); | ||
} | ||
|
||
export function useLapData(): ILapDataContextReturn { | ||
return useContext(lapDataContext); | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import { type Server, type Socket } from "socket.io"; | ||
|
||
import type { ITelemetryData } from "@shared/helios-types"; | ||
import type { ILapData, ITelemetryData } from "@shared/helios-types"; | ||
|
||
export interface SocketIOType { | ||
broadcastCarLatency(latency: number): void; | ||
broadcastPacket(packet: ITelemetryData): void; | ||
initializeSocketListeners(socket: Socket): void; | ||
io: Server; | ||
broadcastLapData(lapData: ILapData): void; | ||
} |