Rework status JSON

This commit is contained in:
Gabriel Augendre 2023-01-04 13:59:47 +01:00
parent 4368d6ad2d
commit 4a67492877
2 changed files with 28 additions and 12 deletions

View file

@ -50,15 +50,18 @@ function handleStatus(data) {
}
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>`;
data.files.available.forEach((element, index) => {
let className = "w3-blue";
if (index === data.files.selectedIndex) {
className = "w3-green";
}
dom += `<button class="w3-button ${className}" onclick="selectFile('${element}')">${element}</button>`;
});
if (data.files.moreNotShown) {
dom += `<button class="w3-button w3-gray" disabled="" title="D'autres fichiers sont disponibles">...</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;

View file

@ -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);
}