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 = ""; let dom = "";
data.files.forEach(element => { data.files.available.forEach((element, index) => {
if (element === data.selectedFile) { let className = "w3-blue";
dom += `<button class="w3-button w3-green" onclick="selectFile('${element}')">${element}</button>`; if (index === data.files.selectedIndex) {
} className = "w3-green";
else {
dom += `<button class="w3-button w3-blue" onclick="selectFile('${element}')">${element}</button>`;
} }
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("available-files").innerHTML = dom;
document.getElementById("volume-current").innerText = data.volume.current; document.getElementById("volume-current").innerText = data.volume.current;
document.getElementById("volume-increase").disabled = !data.volume.canIncrease; document.getElementById("volume-increase").disabled = !data.volume.canIncrease;
document.getElementById("volume-decrease").disabled = !data.volume.canDecrease; document.getElementById("volume-decrease").disabled = !data.volume.canDecrease;

View file

@ -98,29 +98,42 @@ void onStatus(AsyncWebServerRequest *request)
Serial.println("Status"); Serial.println("Status");
AsyncResponseStream *response = request->beginResponseStream("application/json"); AsyncResponseStream *response = request->beginResponseStream("application/json");
DynamicJsonDocument root(1024); DynamicJsonDocument root(4096);
root["selectedFile"] = selectedFile; JsonObject files = root.createNestedObject("files");
files["selectedIndex"] = -1;
files["moreNotShown"] = false;
JsonObject volume = root.createNestedObject("volume"); JsonObject volume = root.createNestedObject("volume");
volume["current"] = currentVolume; volume["current"] = currentVolume;
volume["canDecrease"] = currentVolume > 0; volume["canDecrease"] = currentVolume > 0;
volume["canIncrease"] = currentVolume < 21; volume["canIncrease"] = currentVolume < 21;
JsonArray files = root.createNestedArray("files"); JsonArray availableFiles = files.createNestedArray("available");
File music = SD.open("/"); File music = SD.open("/");
File file = music.openNextFile(); File file = music.openNextFile();
unsigned int index = 0;
while (file) while (file)
{ {
String fileName = file.name(); String fileName = file.name();
if (fileIsValid(fileName)) if (fileIsValid(fileName))
files.add(fileName); {
availableFiles.add(fileName);
if (fileName == selectedFile) {
files["selectedIndex"] = index;
}
index++;
}
file.close(); file.close();
if (root.overflowed())
{
files["moreNotShown"] = true;
break;
}
file = music.openNextFile(); file = music.openNextFile();
} }
root.shrinkToFit();
serializeJson(root, *response); serializeJson(root, *response);
request->send(response); request->send(response);
} }