lektion 03 erwitert - morse code senden
This commit is contained in:
1 parent
f449d2e797
commit
93917f637d
6 files changed
+292
-73
No files matched your search
@@ -1,10 +1,18 @@
|
||||
// JavaScript reagiert auf die Knöpfe und spricht mit dem Python-Webserver.
|
||||
const statusAnzeige = document.querySelector("#led-status");
|
||||
const meldung = document.querySelector("#message");
|
||||
const knoepfe = document.querySelectorAll("button[data-state]");
|
||||
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) {
|
||||
knoepfe.forEach(knopf => knopf.disabled = true);
|
||||
bedienungSperren(true);
|
||||
meldung.textContent = "Bitte warten ...";
|
||||
|
||||
try {
|
||||
@@ -14,11 +22,11 @@ async function ledSchalten(zustand) {
|
||||
body: JSON.stringify({ state: zustand })
|
||||
});
|
||||
|
||||
const daten = await antwort.json();
|
||||
if (!antwort.ok) {
|
||||
throw new Error("Der Server konnte die LED nicht schalten.");
|
||||
throw new Error(daten.error || "Der Server konnte die LED nicht schalten.");
|
||||
}
|
||||
|
||||
const daten = await antwort.json();
|
||||
statusAnzeige.textContent = daten.is_on ? "AN" : "AUS";
|
||||
statusAnzeige.classList.toggle("is-on", daten.is_on);
|
||||
statusAnzeige.classList.toggle("is-off", !daten.is_on);
|
||||
@@ -26,10 +34,51 @@ async function ledSchalten(zustand) {
|
||||
} catch (fehler) {
|
||||
meldung.textContent = "Fehler: " + fehler.message;
|
||||
} finally {
|
||||
knoepfe.forEach(knopf => knopf.disabled = false);
|
||||
bedienungSperren(false);
|
||||
}
|
||||
}
|
||||
|
||||
knoepfe.forEach(knopf => {
|
||||
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();
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user