added DME, fixed sync and bugs. Rewrote setDisplay logic

This commit is contained in:
PxlLoewe
2025-05-19 22:23:15 -07:00
parent 46c58ae127
commit 61e7caf6c8
23 changed files with 776 additions and 242 deletions

View File

@@ -1,4 +1,4 @@
import { CSSProperties } from "react";
import { CSSProperties, useEffect } from "react";
import MrtImage from "./MRT.png";
import { useButtons } from "./useButtons";
import { useSounds } from "./useSounds";

View File

@@ -1,115 +1,23 @@
import { useSession } from "next-auth/react";
import { ConnectedAircraft } from "@repo/db";
import { usePilotConnectionStore } from "_store/pilot/connectionStore";
import { useMrtStore } from "_store/pilot/MrtStore";
import { pilotSocket } from "pilot/socket";
import { editConnectedAircraftAPI } from "querys/aircrafts";
import { Station } from "@repo/db";
import { DisplayLineProps } from "pilot/_components/mrt/Mrt";
export const fmsStatusDescription: { [key: string]: string } = {
NaN: "Keine Daten",
"0": "Prio. Sprechwunsch",
"1": "Frei auf Funk",
"2": "Einsatzbereit am LRZ",
"3": "Auf dem Weg",
"4": "Am Einsatzort",
"5": "Sprechwunsch",
"6": "Nicht einsatzbereit",
"7": "Patient aufgenommen",
"8": "Am Transportziel",
"9": "Fremdanmeldung",
E: "Indent/Abbruch/Einsatzbefehl abgebrochen",
C: "Anmelden zur Übernahme des Einsatzes",
F: "Kommen über Draht",
H: "Fahren auf Wache",
J: "Sprechaufforderung",
L: "Lagebericht abgeben",
P: "Einsatz mit Polizei/Pause machen",
U: "Ungültiger Status",
c: "Status korrigieren",
d: "Transportziel angeben",
h: "Zielklinik verständigt",
o: "Warten, alle Abfrageplätze belegt",
u: "Verstanden",
};
export const getSendingLines = (station: Station): DisplayLineProps[] => {
return [
{
textLeft: `VAR#.${station?.bosCallsign}123`,
style: { fontWeight: "bold" },
textSize: "2",
},
{ textLeft: "ILS VAR#", textSize: "3" },
{
textMid: "sending...",
style: { fontWeight: "bold" },
textSize: "4",
},
{
textLeft: "Status wird gesendet...",
textSize: "1",
},
];
};
export const getHomeLines = (
station: Station,
fmsStatus: string,
): DisplayLineProps[] => {
return [
{
textLeft: `VAR#.${station?.bosCallsign}`,
style: { fontWeight: "bold" },
textSize: "2",
},
{ textLeft: "ILS VAR#", textSize: "3" },
{
textLeft: fmsStatus,
style: { fontWeight: "extrabold" },
textSize: "4",
},
{
textLeft: fmsStatusDescription[fmsStatus],
textSize: "1",
},
];
};
export const getNewStatusLines = (
station: Station,
fmsStatus: string,
): DisplayLineProps[] => {
return [
{
textLeft: `VAR#.${station?.bosCallsign}`,
style: { fontWeight: "bold" },
textSize: "2",
},
{ textLeft: "ILS VAR#", textSize: "3" },
{
textLeft: fmsStatus,
style: { fontWeight: "extrabold" },
textSize: "4",
},
{
textLeft: fmsStatusDescription[fmsStatus],
textSize: "1",
},
];
};
import { useEffect } from "react";
export const useButtons = () => {
const user = useSession().data?.user;
const station = usePilotConnectionStore((state) => state.selectedStation);
const connectedAircraft = usePilotConnectionStore(
(state) => state.connectedAircraft,
);
const connectionStatus = usePilotConnectionStore((state) => state.status);
const { page, setLines } = useMrtStore((state) => state);
const { page, setPage } = useMrtStore((state) => state);
const handleButton =
(button: "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "0") =>
() => {
if (connectionStatus !== "connected") return;
if (!station) return;
if (!connectedAircraft?.id) return;
if (
@@ -125,16 +33,32 @@ export const useButtons = () => {
button === "0"
) {
if (page !== "home") return;
setLines(getSendingLines(station));
setPage({ page: "sending-status", station });
setTimeout(async () => {
await editConnectedAircraftAPI(connectedAircraft!.id, {
fmsStatus: button,
});
setLines(getNewStatusLines(station, button));
setPage({
page: "home",
station,
fmsStatus: button,
});
}, 1000);
}
};
useEffect(() => {
pilotSocket.on("connect", () => {
if (!station) return;
setPage({ page: "home", fmsStatus: "6", station });
});
pilotSocket.on("aircraft-update", () => {
if (!station) return;
setPage({ page: "new-status", station });
});
}, [setPage, station]);
return { handleButton };
};

View File

@@ -1,66 +1,53 @@
"use client";
import { usePilotConnectionStore } from "_store/pilot/connectionStore";
import { useMrtStore } from "_store/pilot/MrtStore";
import { editConnectedAircraftAPI } from "querys/aircrafts";
import { useEffect, useRef } from "react";
const MRTstatusSound = new Audio("/sounds/MRT-status.mp3");
const MrtMessageReceivedSound = new Audio("/sounds/MRT-message-received.mp3");
export const useSounds = () => {
const mrtState = useMrtStore((state) => state);
const { connectedAircraft, selectedStation } = usePilotConnectionStore(
(state) => state,
);
const fmsStatus = connectedAircraft?.fmsStatus || "NaN";
const previousFmsStatus = useRef(fmsStatus || "6");
const timeout = useRef<NodeJS.Timeout>(null);
const setPage = useMrtStore((state) => state.setPage);
const MRTstatusSoundRef = useRef<HTMLAudioElement>(null);
const MrtMessageReceivedSoundRef = useRef<HTMLAudioElement>(null);
useEffect(() => {
const handleSoundEnd = () => {
mrtState.setPage("home");
};
if (typeof window !== "undefined") {
MRTstatusSoundRef.current = new Audio("/sounds/MRT-status.mp3");
MrtMessageReceivedSoundRef.current = new Audio(
"/sounds/MRT-message-received.mp3",
);
MRTstatusSoundRef.current.onended = () => {
if (!selectedStation || !connectedAircraft?.fmsStatus) return;
setPage({
page: "home",
station: selectedStation,
fmsStatus: connectedAircraft?.fmsStatus,
});
};
MrtMessageReceivedSoundRef.current.onended = () => {
if (!selectedStation || !connectedAircraft?.fmsStatus) return;
setPage({
page: "home",
station: selectedStation,
fmsStatus: connectedAircraft?.fmsStatus,
});
};
}
}, [connectedAircraft?.fmsStatus, selectedStation, setPage]);
const playSound = (sound: HTMLAudioElement) => {
sound.play();
sound.addEventListener("ended", handleSoundEnd);
};
const fmsStatus = connectedAircraft?.fmsStatus || "NaN";
useEffect(() => {
if (!connectedAircraft) return;
if (mrtState.page === "new-status") {
if (fmsStatus === "J") {
playSound(MrtMessageReceivedSound);
timeout.current = setTimeout(() => {
editConnectedAircraftAPI(connectedAircraft.id, {
fmsStatus: previousFmsStatus.current,
});
}, 5000);
} else if (previousFmsStatus.current !== fmsStatus) {
playSound(MRTstatusSound);
if (fmsStatus === "J" || fmsStatus === "c") {
MrtMessageReceivedSoundRef.current?.play();
} else {
handleSoundEnd();
}
if (!timeout.current) {
previousFmsStatus.current = fmsStatus || "6";
MRTstatusSoundRef.current?.play();
}
}
return () => {
if (timeout.current) clearTimeout(timeout.current);
[MRTstatusSound, MrtMessageReceivedSound].forEach((sound) => {
sound.removeEventListener("ended", handleSoundEnd);
sound.pause();
sound.currentTime = 0;
});
};
}, [
mrtState,
fmsStatus,
connectedAircraft,
selectedStation,
previousFmsStatus,
timeout,
]);
}, [mrtState, fmsStatus, connectedAircraft, selectedStation]);
};