buzzer/data/www/script.js

134 lines
4.4 KiB
JavaScript
Raw Permalink Normal View History

const GLOBAL_TIMEOUT = 3500;
2023-01-02 23:05:18 +01:00
let connectionOk = true;
2023-01-02 23:16:07 +01:00
let statusTimeout = null;
2023-01-04 15:55:59 +01:00
let selectedFile = "";
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
2023-01-04 15:55:59 +01:00
async function loadStatus() {
console.log("Status...");
2023-01-04 15:55:59 +01:00
return 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-04 15:55:59 +01:00
if (data.files.selected != selectedFile) {
selectedFile = data.files.selected;
document.querySelectorAll(".w3-green").forEach(element => {
element.classList.remove("w3-green");
element.classList.add("w3-blue");
});
const previouslySelected = document.querySelector(`[data-name='${selectedFile}']`);
if (previouslySelected) {
previouslySelected.classList.remove("w3-blue");
previouslySelected.classList.add("w3-green");
2023-01-01 02:55:32 +01:00
}
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;
document.getElementById("remainingTimeBeforeSleep").innerText = secondsToHumanDuration(data.remainingSecondsBeforeSleep);
const playingStatus = document.getElementById("playingStatus");
if (data.playing) {
playingStatus.classList.remove("w3-hide");
} else {
playingStatus.classList.add("w3-hide");
}
2023-01-02 23:16:07 +01:00
statusTimeout = setTimeout(loadStatus, GLOBAL_TIMEOUT);
2023-01-01 02:43:54 +01:00
}
2023-01-04 15:55:59 +01:00
async function listFiles(cursor=0) {
console.log("List files...");
return fetch(`/list-files?cursor=${cursor}`, { signal: AbortSignal.timeout(GLOBAL_TIMEOUT) })
.then(response => response.json())
.then(data => {
let dom = "";
data.files.forEach(element => {
if (!element) {
// Filter out null
return;
}
let className = "w3-blue";
if (element === selectedFile) {
className = "w3-green";
}
dom += `<button class="w3-button ${className}" data-name="${element}" onclick="selectFile('${element}')">${element}</button>`;
});
if (data.files.moreNotShown) {
dom += `<button class="w3-button w3-gray" disabled="" title="D'autres fichiers sont disponibles">...</button>`;
}
const availableFilesNode = document.getElementById("available-files");
if (cursor == 0) {
availableFilesNode.innerHTML = dom;
}
else {
availableFilesNode.innerHTML += dom;
}
if (data.next) {
return listFiles(data.next);
}
})
.catch(handleError);
}
2023-01-04 14:00:03 +01:00
function handleError(error) {
console.error(error);
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
}
function secondsToHumanDuration(seconds) {
console.log({seconds});
return new Date(seconds * 1000).toISOString().slice(11, 19);
}
2023-01-01 02:43:54 +01:00
(() => {
2023-01-04 15:55:59 +01:00
loadStatus().then(() => listFiles());
2023-01-02 23:16:07 +01:00
statusTimeout = setTimeout(loadStatus, GLOBAL_TIMEOUT);
2023-01-01 02:43:54 +01:00
})();