create api endpoint for playlist

pull/1/head
Thomas Ballmann 3 years ago
parent 28c5fc569c
commit f37c30f7b1

@ -29,6 +29,7 @@ const char *jsonAppVersion = "/dist/version.json";
AppConfig appConfig; AppConfig appConfig;
void setupApiDevice(); void setupApiDevice();
void setupApiPlaylist();
void setupApiSettings(); void setupApiSettings();
void setupApiWifi(); void setupApiWifi();
void setupApiCache(); void setupApiCache();
@ -89,6 +90,7 @@ void setupApp()
server.serveStatic("/fs/", SPIFFS, "/"); server.serveStatic("/fs/", SPIFFS, "/");
setupApiDevice(); setupApiDevice();
setupApiPlaylist();
setupApiSettings(); setupApiSettings();
setupApiWifi(); setupApiWifi();
setupApiCache(); setupApiCache();
@ -130,8 +132,8 @@ void setupApp()
doc["device"]["heap"]["total"] = ESP.getHeapSize(); doc["device"]["heap"]["total"] = ESP.getHeapSize();
doc["device"]["heap"]["free"] = ESP.getFreeHeap(); doc["device"]["heap"]["free"] = ESP.getFreeHeap();
doc["playlist"]["current"] = PlaylistGetCurrentFace(); doc["playlist"]["current"] = playlistGetCurrentFace();
doc["playlist"]["remaining"] = (PlaylistGetRemainingTimeMs() / 1000) + 3; // + face rendering time 3s doc["playlist"]["remaining"] = (playlistGetRemainingTimeMs() / 1000) + 3; // + face rendering time 3s
doc["firmware"]["created"] = FW_CREATED; doc["firmware"]["created"] = FW_CREATED;
doc["firmware"]["rev"] = FW_GIT_REV; doc["firmware"]["rev"] = FW_GIT_REV;
@ -163,7 +165,7 @@ void loopApp()
Serial.println("loop app update display"); Serial.println("loop app update display");
// stop playlist to show the new image // stop playlist to show the new image
PlaylistResetTimer(); playlistResetTimer();
updateDisplayRequired = false; updateDisplayRequired = false;
displayFlush(); displayFlush();
@ -294,7 +296,7 @@ void setupApiDevice()
bool dither = strcmp(filename.c_str(), "dithering") == 0; bool dither = strcmp(filename.c_str(), "dithering") == 0;
ImageNew(0, 0, 0, 0, dither); ImageNew(0, 0, 0, 0, dither);
PlaylistResetTimer(); playlistResetTimer();
} }
ImageWriteBuffer(data, len); ImageWriteBuffer(data, len);
@ -352,6 +354,21 @@ void setupApiDevice()
}); });
} }
/**
* api playlist endpoint
*/
void setupApiPlaylist()
{
server.on("/api/playlist/show", HTTP_GET, [](AsyncWebServerRequest *request) {
if (request->hasParam("face")) {
playlistShow(request->getParam("face")->value().c_str());
}
request->send(200, "application/json", "{}");
});
}
/** /**
* api wifi endpoint * api wifi endpoint
*/ */

@ -61,43 +61,58 @@ void loopPlaylist()
faces[i].loop(); faces[i].loop();
} }
if (PlaylistGetRemainingTimeMs() <= 0) // && autoplayEnabled if (playlistGetRemainingTimeMs() <= 0) // && autoplayEnabled
{ {
PlaylistResetTimer(); playlistResetTimer();
PlaylistNextFace(); playlistNextFace();
Serial.println("switch face: " + faces[currentFaceIndex].name); Serial.println("switch face: " + faces[currentFaceIndex].name);
faces[currentFaceIndex].show(); faces[currentFaceIndex].show();
} }
} }
void PlaylistNextFace() void playlistNextFace()
{ {
currentFaceIndex++; currentFaceIndex++;
// wrap around at the ends
if (currentFaceIndex < 0) if (currentFaceIndex < 0)
{ {
currentFaceIndex = faceCount - 1; currentFaceIndex = 0;
} }
// wrap around at the ends
if (currentFaceIndex >= faceCount) if (currentFaceIndex >= faceCount)
{ {
currentFaceIndex = 0; currentFaceIndex = 0;
} }
} }
String PlaylistGetCurrentFace() String playlistGetCurrentFace()
{ {
return faces[currentFaceIndex].name; return faces[currentFaceIndex].name;
} }
int32_t PlaylistGetRemainingTimeMs() int32_t playlistGetRemainingTimeMs()
{ {
return timer - (millis() - lastSwitch); return timer - (millis() - lastSwitch);
} }
void PlaylistResetTimer() void playlistResetTimer()
{ {
lastSwitch = millis(); lastSwitch = millis();
}
void playlistShow(const char name[])
{
for (uint8_t i = 0; i < faceCount; i++)
{
if (faces[i].name.equalsIgnoreCase(name))
{
Serial.println("switch to face " + faces[i].name);
currentFaceIndex = i -1;
lastSwitch = 0;
break;
}
}
} }

@ -6,9 +6,10 @@
void setupPlaylist(); void setupPlaylist();
void loopPlaylist(); void loopPlaylist();
void PlaylistNextFace(); void playlistNextFace();
String PlaylistGetCurrentFace(); String playlistGetCurrentFace();
int32_t PlaylistGetRemainingTimeMs(); int32_t playlistGetRemainingTimeMs();
void PlaylistResetTimer(); void playlistResetTimer();
void playlistShow(const char name[]);
#endif #endif
Loading…
Cancel
Save