From 4a6749287776f1154ec2424e6a50289d5ab4a51a Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Wed, 4 Jan 2023 13:59:47 +0100 Subject: [PATCH] Rework status JSON --- data/www/script.js | 15 +++++++++------ src/main.cpp | 25 +++++++++++++++++++------ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/data/www/script.js b/data/www/script.js index ac62b40..9ebe19b 100644 --- a/data/www/script.js +++ b/data/www/script.js @@ -50,15 +50,18 @@ function handleStatus(data) { } let dom = ""; - data.files.forEach(element => { - if (element === data.selectedFile) { - dom += ``; - } - else { - dom += ``; + data.files.available.forEach((element, index) => { + let className = "w3-blue"; + if (index === data.files.selectedIndex) { + className = "w3-green"; } + dom += ``; }); + if (data.files.moreNotShown) { + dom += ``; + } 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; diff --git a/src/main.cpp b/src/main.cpp index b3f529a..3bcef17 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -98,29 +98,42 @@ void onStatus(AsyncWebServerRequest *request) Serial.println("Status"); AsyncResponseStream *response = request->beginResponseStream("application/json"); - DynamicJsonDocument root(1024); - root["selectedFile"] = selectedFile; + DynamicJsonDocument root(4096); + JsonObject files = root.createNestedObject("files"); + files["selectedIndex"] = -1; + files["moreNotShown"] = false; JsonObject volume = root.createNestedObject("volume"); volume["current"] = currentVolume; volume["canDecrease"] = currentVolume > 0; volume["canIncrease"] = currentVolume < 21; - JsonArray files = root.createNestedArray("files"); + JsonArray availableFiles = files.createNestedArray("available"); File music = SD.open("/"); File file = music.openNextFile(); + unsigned int index = 0; while (file) { String fileName = file.name(); if (fileIsValid(fileName)) - files.add(fileName); + { + availableFiles.add(fileName); + if (fileName == selectedFile) { + files["selectedIndex"] = index; + } + index++; + } file.close(); + + if (root.overflowed()) + { + files["moreNotShown"] = true; + break; + } file = music.openNextFile(); } - root.shrinkToFit(); serializeJson(root, *response); - request->send(response); }