85 lines
2.7 KiB
JavaScript
85 lines
2.7 KiB
JavaScript
// JavaScript reagiert auf die Knöpfe und spricht mit dem Python-Webserver.
|
|
const statusAnzeige = document.querySelector("#led-status");
|
|
const meldung = document.querySelector("#message");
|
|
const ledKnoepfe = document.querySelectorAll("button[data-state]");
|
|
const morseEingabe = document.querySelector("#morse-text");
|
|
const morseKnopf = document.querySelector("#morse-send");
|
|
const alleKnoepfe = document.querySelectorAll("button");
|
|
|
|
function bedienungSperren(gesperrt) {
|
|
alleKnoepfe.forEach(knopf => knopf.disabled = gesperrt);
|
|
morseEingabe.disabled = gesperrt;
|
|
}
|
|
|
|
async function ledSchalten(zustand) {
|
|
bedienungSperren(true);
|
|
meldung.textContent = "Bitte warten ...";
|
|
|
|
try {
|
|
const antwort = await fetch("/api/led", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ state: zustand })
|
|
});
|
|
|
|
const daten = await antwort.json();
|
|
if (!antwort.ok) {
|
|
throw new Error(daten.error || "Der Server konnte die LED nicht schalten.");
|
|
}
|
|
|
|
statusAnzeige.textContent = daten.is_on ? "AN" : "AUS";
|
|
statusAnzeige.classList.toggle("is-on", daten.is_on);
|
|
statusAnzeige.classList.toggle("is-off", !daten.is_on);
|
|
meldung.textContent = "";
|
|
} catch (fehler) {
|
|
meldung.textContent = "Fehler: " + fehler.message;
|
|
} finally {
|
|
bedienungSperren(false);
|
|
}
|
|
}
|
|
|
|
async function morseSenden() {
|
|
const text = morseEingabe.value.trim();
|
|
if (!text) {
|
|
meldung.textContent = "Bitte gib zuerst einen Text ein.";
|
|
morseEingabe.focus();
|
|
return;
|
|
}
|
|
|
|
bedienungSperren(true);
|
|
meldung.textContent = "Morsecode wird gesendet. Bitte warten ...";
|
|
|
|
try {
|
|
const antwort = await fetch("/api/morse", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ text: text })
|
|
});
|
|
|
|
const daten = await antwort.json();
|
|
if (!antwort.ok) {
|
|
throw new Error(daten.error || "Der Morsecode konnte nicht gesendet werden.");
|
|
}
|
|
|
|
statusAnzeige.textContent = "AUS";
|
|
statusAnzeige.classList.remove("is-on");
|
|
statusAnzeige.classList.add("is-off");
|
|
meldung.textContent = daten.message;
|
|
} catch (fehler) {
|
|
meldung.textContent = "Fehler: " + fehler.message;
|
|
} finally {
|
|
bedienungSperren(false);
|
|
}
|
|
}
|
|
|
|
ledKnoepfe.forEach(knopf => {
|
|
knopf.addEventListener("click", () => ledSchalten(knopf.dataset.state));
|
|
});
|
|
|
|
morseKnopf.addEventListener("click", morseSenden);
|
|
morseEingabe.addEventListener("keydown", ereignis => {
|
|
if (ereignis.key === "Enter") {
|
|
morseSenden();
|
|
}
|
|
});
|