From 13c58ea1b6086ddc43c289fbf8f9615f3162bb6a Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Mon, 2 Jan 2023 23:05:18 +0100 Subject: [PATCH] Add button to stop playing --- data/www/index.html | 1 + data/www/script.js | 16 ++++++++++++++-- src/main.cpp | 7 +++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/data/www/index.html b/data/www/index.html index 9173da1..6f3710e 100644 --- a/data/www/index.html +++ b/data/www/index.html @@ -15,6 +15,7 @@
+
diff --git a/data/www/script.js b/data/www/script.js index 8799441..4341b07 100644 --- a/data/www/script.js +++ b/data/www/script.js @@ -1,4 +1,5 @@ -const GLOBAL_TIMEOUT = 7000; +const GLOBAL_TIMEOUT = 10000; +let connectionOk = true; function play() { console.log("Play..."); @@ -6,6 +7,12 @@ function play() { .catch(handleError); } +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); @@ -35,7 +42,11 @@ function selectFile(name) { function handleStatus(data) { document.body.classList.remove("w3-disabled"); - console.log("data", data); + if (!connectionOk) { + connectionOk = true; + location.reload(); + } + let dom = ""; data.files.forEach(element => { if (element === data.selectedFile) { @@ -54,6 +65,7 @@ function handleStatus(data) { function handleError() { console.log("Lost connection :'("); document.body.classList.add("w3-disabled"); + connectionOk = false; } (() => { diff --git a/src/main.cpp b/src/main.cpp index 40f3a18..82adafb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -74,6 +74,12 @@ void play() audio.connecttoFS(SD, path.c_str()); } +void onStop(AsyncWebServerRequest *request) +{ + audio.stopSong(); + request->send(200); +} + void onPlay(AsyncWebServerRequest *request) { play(); @@ -237,6 +243,7 @@ void setup() // Server server.on("/play", HTTP_GET, onPlay); + server.on("/stop", HTTP_GET, onStop); server.on("/status", HTTP_GET, onStatus); server.on("/select-file", HTTP_POST, onSelectFile); server.on("/change-volume", HTTP_POST, onChangeVolume);