buzzer/data/www/script.js

78 lines
2.4 KiB
JavaScript
Raw Normal View History

2023-01-02 23:05:18 +01:00
const GLOBAL_TIMEOUT = 10000;
let connectionOk = true;
2023-01-02 23:16:07 +01:00
let statusTimeout = null;
function play() {
console.log("Play...");
2023-01-02 22:53:52 +01:00
fetch("/play", { signal: AbortSignal.timeout(GLOBAL_TIMEOUT) })
.catch(handleError);
}
2023-01-02 23:05:18 +01:00
function stop() {
console.log("Stop...");
fetch("/stop", { 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 23:16:07 +01:00
clearTimeout(statusTimeout);
2023-01-02 22:53:52 +01:00
document.body.classList.remove("w3-disabled");
2023-01-02 23:05:18 +01:00
if (!connectionOk) {
connectionOk = true;
location.reload();
}
2023-01-01 02:55:32 +01:00
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-02 23:16:07 +01:00
statusTimeout = setTimeout(loadStatus, GLOBAL_TIMEOUT);
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-02 23:05:18 +01:00
connectionOk = false;
2023-01-02 22:53:52 +01:00
}
2023-01-01 02:43:54 +01:00
(() => {
loadStatus();
2023-01-02 23:16:07 +01:00
statusTimeout = setTimeout(loadStatus, GLOBAL_TIMEOUT);
2023-01-01 02:43:54 +01:00
})();