diff --git a/packages/client/src/components/global/SocketManager.tsx b/packages/client/src/components/global/SocketManager.tsx index dd705c9b..3cdeeec3 100644 --- a/packages/client/src/components/global/SocketManager.tsx +++ b/packages/client/src/components/global/SocketManager.tsx @@ -17,7 +17,10 @@ import { socketURL } from "@shared/helios-types"; interface ClientToServerEvents { ping: (cb: (val: number) => void) => void; - setLapCoords: (coords: CoordInfoUpdate) => void; + setLapCoords: ( + coords: CoordInfoUpdate, + callback: (response: CoordUpdateResponse) => void, + ) => void; } interface ServerToClientEvents { diff --git a/packages/client/src/components/molecules/LogoStatusMolecules/Settings/SetFinishLineLocation.tsx b/packages/client/src/components/molecules/LogoStatusMolecules/Settings/SetFinishLineLocation.tsx new file mode 100644 index 00000000..e0f0ca86 --- /dev/null +++ b/packages/client/src/components/molecules/LogoStatusMolecules/Settings/SetFinishLineLocation.tsx @@ -0,0 +1,102 @@ +import { useTheme } from "next-themes"; +import React from "react"; + +import { socketIO } from "@/components/global/SocketManager"; +import { usePacketStore } from "@/stores/usePacket"; +import { helios, heliosCompliment, sand } from "@/styles/colors"; +import { notifications } from "@mantine/notifications"; +import { Button, TextField } from "@mui/material"; +import { CoordUpdateResponse } from "@shared/helios-types"; + +const SetFinishLineLocation = () => { + const { currentPacket } = usePacketStore(); + const { resolvedTheme } = useTheme(); + const [password, setPassword] = React.useState(""); + + const handleSetFinishLineLocation = () => { + socketIO.emit( + "setLapCoords", + { + lat: String(currentPacket?.Telemetry?.GpsLatitude ?? 0), + long: String(currentPacket?.Telemetry?.GpsLongitude ?? 0), + password: password, + }, + (response: CoordUpdateResponse) => { + if ("error" in response) { + notifications.show({ + color: "red", + message: response.error, + title: "Error Setting Finish Line Location", + }); + return; + } + notifications.show({ + color: "green", + message: "Finish Line Location Updated Successfully", + title: "Success", + }); + }, + ); + }; + + return ( +
+
+ +
+
+ + setPassword(e.target.value)} + sx={{ + "& .MuiInputBase-input": { + color: resolvedTheme === "dark" ? "white" : "black", + }, + "& .MuiInputBase-input::label": { + color: resolvedTheme === "dark" ? sand : helios, + }, + "& .MuiInputLabel-root, & .MuiInputLabel-root.Mui-focused": { + color: resolvedTheme === "dark" ? sand : helios, + }, + "& .MuiOutlinedInput-root .MuiOutlinedInput-notchedOutline, & .MuiOutlinedInput-root.Mui-focused .MuiOutlinedInput-notchedOutline": + { + borderColor: resolvedTheme === "dark" ? sand : helios, + }, + }} + type="password" + value={password} + /> +
+
+ ); +}; + +export default SetFinishLineLocation; diff --git a/packages/client/src/components/molecules/LogoStatusMolecules/SettingsComponent.tsx b/packages/client/src/components/molecules/LogoStatusMolecules/SettingsComponent.tsx index 3d4f3655..fb4b46eb 100644 --- a/packages/client/src/components/molecules/LogoStatusMolecules/SettingsComponent.tsx +++ b/packages/client/src/components/molecules/LogoStatusMolecules/SettingsComponent.tsx @@ -1,9 +1,9 @@ import { useTheme } from "next-themes"; import { useCallback, useState } from "react"; +import SetFinishLineLocation from "@/components/molecules/LogoStatusMolecules/Settings/SetFinishLineLocation"; import { APPUNITS, CONNECTIONTYPES, useAppState } from "@/stores/useAppState"; import { helios, heliosCompliment, sand } from "@/styles/colors"; -import { TimeInput } from "@mantine/dates"; import SettingsIcon from "@mui/icons-material/Settings"; import Modal from "@mui/material/Modal"; import ToggleButton from "@mui/material/ToggleButton"; @@ -242,6 +242,7 @@ function SettingsComponent() { + diff --git a/packages/server/src/controllers/LapController/LapController.ts b/packages/server/src/controllers/LapController/LapController.ts index 936814f0..2d870a5d 100644 --- a/packages/server/src/controllers/LapController/LapController.ts +++ b/packages/server/src/controllers/LapController/LapController.ts @@ -132,7 +132,7 @@ export class LapController implements LapControllerType { ): CoordUpdateResponse { logger.info(JSON.stringify(newCoordInfo)); const { lat, long, password } = newCoordInfo; - if (password !== process.env.LAP_POSITION_PASSWORD) { + if (password !== process.env.FINISH_LINE_UPDATE_PASSWORD) { logger.error("Invalid Password: " + password); return { error: "Invalid Password", invalidFields: ["password"] }; } diff --git a/packages/server/src/datasources/SocketIO/SocketIO.ts b/packages/server/src/datasources/SocketIO/SocketIO.ts index f69a1f95..2c6f041e 100644 --- a/packages/server/src/datasources/SocketIO/SocketIO.ts +++ b/packages/server/src/datasources/SocketIO/SocketIO.ts @@ -68,17 +68,30 @@ export class SocketIO implements SocketIOType { socket.on("ping", (callback: () => void) => { callback(); }); - socket.on("setLapCoords", (newCoordInfo: CoordInfoUpdate) => { - const { lat, long, password } = newCoordInfo; - logger.info("UPDATED COORDS: "); - logger.info("lat: ", lat, "long: ", long); - const res = - this.backendController.lapController.setFinishLineLocation( - newCoordInfo, - ); + socket.on( + "setLapCoords", + ( + newCoordInfo: CoordInfoUpdate, + callback?: (response: CoordUpdateResponse) => void, + ) => { + const { lat, long } = newCoordInfo; - this.broadcastLapCoords(res); - }); + logger.info("UPDATED COORDS: "); + logger.info("lat: ", lat, "long: ", long); + const res = + this.backendController.lapController.setFinishLineLocation( + newCoordInfo, + ); + // Only broadcast if successful + if (!("error" in res)) { + this.broadcastLapCoords(res); + } + + if (typeof callback === "function") { + callback(res); + } + }, + ); socket.on("disconnect", () => { logger.info("Client disconnected"); });