Massive changes. Added a quite nice webinterface with live data using WebSockets. Removed the FTP server (wasn't that useful anyways). JSON creating using ArduinoJson instead of String concatenation. Ans, and, and.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include "playlist_manager.h"
|
||||
#include <SD.h>
|
||||
#include "spi_master.h"
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
PlaylistManager::PlaylistManager() {
|
||||
SPIMaster::select_sd();
|
||||
@@ -8,12 +9,40 @@ PlaylistManager::PlaylistManager() {
|
||||
File entry;
|
||||
while (entry = root.openNextFile()) {
|
||||
String foldername = entry.name();
|
||||
if (!entry.isDirectory() || foldername.startsWith("/.")) continue;
|
||||
// Remove trailing slash
|
||||
foldername.remove(foldername.length());
|
||||
TRACE("Looking at %s...", foldername.c_str());
|
||||
if (!entry.isDirectory() || foldername.startsWith("/.")) continue;
|
||||
DEBUG(" Checking %s...\n", foldername.c_str());
|
||||
bool non_ascii_chars = false;
|
||||
for(int i=0; i<foldername.length(); i++) {
|
||||
char c = foldername.charAt(i);
|
||||
if (c < 0x20 || c >= 0x7F) {
|
||||
non_ascii_chars = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (non_ascii_chars) {
|
||||
ERROR("WARNING: Folder '%s' contains non-ascii chars!\n", foldername.c_str());
|
||||
}
|
||||
|
||||
|
||||
if (!SD.exists(foldername + "/ids.txt")) {
|
||||
TRACE("No ids.txt -> ignoring\n");
|
||||
TRACE("No ids.txt -> checking for media files...\n");
|
||||
|
||||
File file;
|
||||
bool media_files = false;
|
||||
while(file = entry.openNextFile()) {
|
||||
String filename = file.name();
|
||||
filename = filename.substring(foldername.length() + 1);
|
||||
if (!filename.startsWith(".") && filename.endsWith(".mp3")) {
|
||||
media_files = true;
|
||||
}
|
||||
file.close();
|
||||
if (media_files) break;
|
||||
}
|
||||
if (media_files) {
|
||||
_unmapped_folders.push_back(foldername);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
File f = SD.open(foldername + "/ids.txt");
|
||||
@@ -47,6 +76,10 @@ PlaylistManager::PlaylistManager() {
|
||||
Playlist* PlaylistManager::get_playlist_for_id(String id) {
|
||||
if (!_map.count(id)) return NULL;
|
||||
String folder = _map[id];
|
||||
return get_playlist_for_folder(folder);
|
||||
}
|
||||
|
||||
Playlist* PlaylistManager::get_playlist_for_folder(String folder) {
|
||||
if (!_playlists.count(folder)) {
|
||||
_playlists[folder] = new Playlist(folder);
|
||||
}
|
||||
@@ -58,3 +91,22 @@ void PlaylistManager::dump_ids() {
|
||||
INFO(" %s -> %s\n", it->first.c_str(), it->second.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
String PlaylistManager::json() {
|
||||
DynamicJsonDocument json(10240);
|
||||
json["_type"] = "playlist_manager";
|
||||
JsonObject folders = json.createNestedObject("folders");
|
||||
for (std::map<String, String>::iterator it = _map.begin(); it!=_map.end(); it++) {
|
||||
folders[it->second] = folders[it->first];
|
||||
}
|
||||
JsonArray started = json.createNestedArray("started");
|
||||
for (std::map<String, Playlist*>::iterator it = _playlists.begin(); it!=_playlists.end(); it++) {
|
||||
if (it->second->is_fresh()) continue;
|
||||
started.add(it->first);
|
||||
}
|
||||
JsonArray unmapped = json.createNestedArray("unmapped");
|
||||
for(String folder : _unmapped_folders) {
|
||||
unmapped.add(folder);
|
||||
}
|
||||
return json.as<String>();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user