35 lines
1.2 KiB
JavaScript
35 lines
1.2 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 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));
|
|
}); |