buzzer/data/www/script.js

83 lines
2.6 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 = "";
2023-01-04 13:59:47 +01:00
data.files.available.forEach((element, index) => {
let className = "w3-blue";
if (index === data.files.selectedIndex) {
className = "w3-green";
2023-01-01 02:55:32 +01:00
}
2023-01-04 13:59:47 +01:00
dom += `<button class="w3-button ${className}" onclick="selectFile('${element}')">${element}</button>`;
2023-01-01 02:55:32 +01:00
});
2023-01-04 13:59:47 +01:00
if (data.files.moreNotShown) {
dom += `<button class="w3-button w3-gray" disabled="" title="D'autres fichiers sont disponibles">...</button>`;
}
2023-01-01 02:55:32 +01:00
document.getElementById("available-files").innerHTML = dom;
2023-01-04 13:59:47 +01:00
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() {
2023-01-02 23:22:18 +01:00
clearTimeout(statusTimeout);
2023-01-02 22:53:52 +01:00
console.log("Lost connection :'(");
document.body.classList.add("w3-disabled");
2023-01-02 23:05:18 +01:00
connectionOk = false;
2023-01-02 23:22:18 +01:00
statusTimeout = setTimeout(loadStatus, GLOBAL_TIMEOUT);
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
})();