|
|
|
#include <HTTPClient.h>
|
|
|
|
#include <FirebaseJson.h>
|
|
|
|
|
|
|
|
#define PLAYER_UPDATE_INTERVAL_MS (500)
|
|
|
|
#define UI_ENTRIES_TO_LOAD (20)
|
|
|
|
|
|
|
|
static unsigned long playerLastUpdateTime = 0;
|
|
|
|
static ViewMode lastViewMode = (ViewMode)-1;
|
|
|
|
|
|
|
|
String httpURL(String path) {
|
|
|
|
return String(OWNTONE_SERVER_ADDRESS) + "/api/" + path;
|
|
|
|
}
|
|
|
|
|
|
|
|
String httpGET(String path) {
|
|
|
|
HTTPClient http;
|
|
|
|
|
|
|
|
Serial.println(httpURL(path));
|
|
|
|
|
|
|
|
http.begin(httpURL(path));
|
|
|
|
http.GET();
|
|
|
|
String out = http.getString();
|
|
|
|
http.end();
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
void apiDebug(AppState *state) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void apiLoop(AppState *state) {
|
|
|
|
bool playerNeedsUpdate = (state->sinceBoot - playerLastUpdateTime) > PLAYER_UPDATE_INTERVAL_MS;
|
|
|
|
playerNeedsUpdate = playerNeedsUpdate || (playerLastUpdateTime > state->sinceBoot);
|
|
|
|
if (playerNeedsUpdate) {
|
|
|
|
String playerText = httpGET(String("player"));
|
|
|
|
FirebaseJson json;
|
|
|
|
json.setJsonData(playerText);
|
|
|
|
FirebaseJsonData result;
|
|
|
|
json.get(result, "volume");
|
|
|
|
|
|
|
|
state->volume = result.to<int>();
|
|
|
|
|
|
|
|
playerLastUpdateTime = state->sinceBoot;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool viewNeedsUpdate = (state->viewMode != lastViewMode);
|
|
|
|
if (viewNeedsUpdate) {
|
|
|
|
String parameters = "?limit=" + String(UI_ENTRIES_TO_LOAD);
|
|
|
|
lastViewMode = state->viewMode;
|
|
|
|
if (state->viewMode == ViewModeArtists) {
|
|
|
|
String artistsText = httpGET(String("library/artists" + parameters));
|
|
|
|
FirebaseJson json;
|
|
|
|
json.setJsonData(artistsText);
|
|
|
|
FirebaseJsonData result;
|
|
|
|
json.get(result, "items");
|
|
|
|
FirebaseJsonArray arr;
|
|
|
|
result.get<FirebaseJsonArray>(arr);
|
|
|
|
Serial.println(arr.size());
|
|
|
|
|
|
|
|
if(state->uiEntries != NULL) {
|
|
|
|
free(state->uiEntries);
|
|
|
|
}
|
|
|
|
state->uiEntries = (UIEntry *)malloc(UI_ENTRIES_TO_LOAD * sizeof(UIEntry));
|
|
|
|
state->uiEntriesCount = UI_ENTRIES_TO_LOAD;
|
|
|
|
|
|
|
|
FirebaseJsonData arrayIndexResult;
|
|
|
|
for (size_t i = 0; i < arr.size(); i++) {
|
|
|
|
Serial.println("Processing item...");
|
|
|
|
String path = "/[" + String(i) + "]/";
|
|
|
|
String namePath = path + "name";
|
|
|
|
String idPath = path + "id";
|
|
|
|
|
|
|
|
Serial.println("name...");
|
|
|
|
arr.get(arrayIndexResult, namePath);
|
|
|
|
String text = arrayIndexResult.to<String>().c_str();
|
|
|
|
|
|
|
|
Serial.println("id...");
|
|
|
|
arr.get(arrayIndexResult, idPath);
|
|
|
|
int id = arrayIndexResult.to<int>();
|
|
|
|
|
|
|
|
Serial.println("setting...");
|
|
|
|
state->uiEntries[i].text = text;
|
|
|
|
state->uiEntries[i].id = id;
|
|
|
|
|
|
|
|
Serial.println(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void apiSetup(AppState *state) {
|
|
|
|
|
|
|
|
}
|