buzzer/data/www/script.js

63 lines
2 KiB
JavaScript
Raw Normal View History

const GLOBAL_TIMEOUT = 7000;
function play() {
console.log("Play...");
2023-01-02 22:53:52 +01:00
fetch("/play", { signal: AbortSignal.timeout(GLOBAL_TIMEOUT) })
.catch(handleError);
}
function volume(modifier) {
const body = new FormData();
body.set("modifier", modifier);
fetch(`/change-volume`, { method: "POST", body: body, signal: AbortSignal.timeout(GLOBAL_TIMEOUT) })
.then(response => response.json())
2023-01-02 22:53:52 +01:00
.then(handleStatus)
.catch(handleError);
}
2023-01-01 02:43:54 +01:00
function loadStatus() {
console.log("Status...");
fetch("/status", { signal: AbortSignal.timeout(GLOBAL_TIMEOUT) })
2023-01-01 02:43:54 +01:00
.then(response => response.json())
2023-01-02 22:53:52 +01:00
.then(handleStatus)
.catch(handleError);
2023-01-01 02:43:54 +01:00
}
function selectFile(name) {
console.log("Select file");
const body = new FormData();
body.set("fileName", name);
fetch("/select-file", { method: "POST", body: body, signal: AbortSignal.timeout(GLOBAL_TIMEOUT) })
2023-01-01 02:55:32 +01:00
.then(response => response.json())
2023-01-02 22:53:52 +01:00
.then(handleStatus)
.catch(handleError);
2023-01-01 02:55:32 +01:00
}
function handleStatus(data) {
2023-01-02 22:53:52 +01:00
document.body.classList.remove("w3-disabled");
2023-01-01 02:55:32 +01:00
console.log("data", data);
let dom = "";
data.files.forEach(element => {
if (element === data.selectedFile) {
dom += `<button class="w3-button w3-green" onclick="selectFile('${element}')">${element}</button>`;
}
else {
dom += `<button class="w3-button w3-blue" onclick="selectFile('${element}')">${element}</button>`;
}
});
document.getElementById("available-files").innerHTML = dom;
document.getElementById("volume-current").innerText = data.volume.current;
document.getElementById("volume-increase").disabled = !data.volume.canIncrease;
document.getElementById("volume-decrease").disabled = !data.volume.canDecrease;
2023-01-01 02:43:54 +01:00
}
2023-01-02 22:53:52 +01:00
function handleError() {
console.log("Lost connection :'(");
document.body.classList.add("w3-disabled");
}
2023-01-01 02:43:54 +01:00
(() => {
loadStatus();
2023-01-02 22:53:52 +01:00
setInterval(loadStatus, GLOBAL_TIMEOUT);
2023-01-01 02:43:54 +01:00
})();