lektion 03 angepasst
This commit is contained in:
1 parent
c60910e505
commit
1293a0f6a0
5 files changed
+247
-187
No files matched your search
@@ -0,0 +1,35 @@
|
||||
// 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]");
|
||||
|
||||
async function ledSchalten(zustand) {
|
||||
knoepfe.forEach(knopf => knopf.disabled = true);
|
||||
meldung.textContent = "Bitte warten ...";
|
||||
|
||||
try {
|
||||
const antwort = await fetch("/api/led", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ state: zustand })
|
||||
});
|
||||
|
||||
if (!antwort.ok) {
|
||||
throw new 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);
|
||||
meldung.textContent = "";
|
||||
} catch (fehler) {
|
||||
meldung.textContent = "Fehler: " + fehler.message;
|
||||
} finally {
|
||||
knoepfe.forEach(knopf => knopf.disabled = false);
|
||||
}
|
||||
}
|
||||
|
||||
knoepfe.forEach(knopf => {
|
||||
knopf.addEventListener("click", () => ledSchalten(knopf.dataset.state));
|
||||
});
|
||||
Reference in new issue
Block a user