2019-11-14 19:42:02 +00:00
|
|
|
#include "playlist_manager.h"
|
|
|
|
#include <SD.h>
|
|
|
|
#include "spi_master.h"
|
2019-11-16 22:03:13 +00:00
|
|
|
#include <ArduinoJson.h>
|
2019-11-14 19:42:02 +00:00
|
|
|
|
|
|
|
PlaylistManager::PlaylistManager() {
|
2019-11-17 13:26:23 +00:00
|
|
|
scan_files();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlaylistManager::scan_files() {
|
2019-11-14 19:42:02 +00:00
|
|
|
SPIMaster::select_sd();
|
2019-11-17 13:26:23 +00:00
|
|
|
std::vector<String> folders;
|
2019-11-14 19:42:02 +00:00
|
|
|
File root = SD.open("/");
|
|
|
|
File entry;
|
|
|
|
while (entry = root.openNextFile()) {
|
|
|
|
String foldername = entry.name();
|
2019-11-17 13:26:23 +00:00
|
|
|
if (foldername.startsWith("/.")) continue;
|
2019-11-14 19:42:02 +00:00
|
|
|
foldername.remove(foldername.length());
|
2019-11-17 13:26:23 +00:00
|
|
|
folders.push_back(foldername);
|
|
|
|
_check_for_special_chars(foldername);
|
|
|
|
entry.close();
|
|
|
|
}
|
2019-11-16 22:03:13 +00:00
|
|
|
|
2019-11-17 13:26:23 +00:00
|
|
|
_map.clear();
|
|
|
|
if (!SD.exists("/_mapping.txt\n")) {
|
|
|
|
ERROR("WARNING: File /_mapping.txt not found.\n");
|
|
|
|
} else {
|
|
|
|
File f = SD.open("/_mapping.txt");
|
|
|
|
DEBUG("Reading /_mapping.txt...\n");
|
|
|
|
while (f.available()) {
|
|
|
|
char buffer[512];
|
|
|
|
size_t pos = f.readBytesUntil('\n', buffer, 511);
|
|
|
|
buffer[pos] = '\0';
|
2019-11-16 22:03:13 +00:00
|
|
|
|
2019-11-17 13:26:23 +00:00
|
|
|
String data = buffer;
|
|
|
|
uint8_t eq = data.indexOf('=');
|
|
|
|
if (eq>0 && eq<data.length()-1) {
|
|
|
|
String rfid_id = data.substring(0, eq);
|
|
|
|
String folder = data.substring(eq + 1);
|
|
|
|
TRACE(" Adding mapping: %s=>%s\n", rfid_id.c_str(), folder.c_str());
|
|
|
|
_map[rfid_id] = folder;
|
2019-11-28 05:29:51 +00:00
|
|
|
|
|
|
|
if (folder.charAt(0)=='/') {
|
|
|
|
bool found=false;
|
|
|
|
for (String f: folders) {
|
|
|
|
if (f.equals(folder)) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
INFO("WARNING: Found mapping for RFID id %s which maps to non-existing folder %s!\n", rfid_id.c_str(), folder.c_str());
|
2019-11-17 13:26:23 +00:00
|
|
|
}
|
2019-11-16 22:03:13 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-17 13:26:23 +00:00
|
|
|
}
|
|
|
|
f.close();
|
|
|
|
}
|
|
|
|
root.close();
|
|
|
|
|
|
|
|
_unmapped_folders.clear();
|
|
|
|
for (String folder: folders) {
|
|
|
|
bool found = false;
|
|
|
|
for(std::map<String, String>::iterator it = _map.begin(); it != _map.end(); it++) {
|
|
|
|
if (it->second.equals(folder)) {
|
|
|
|
found = true;
|
|
|
|
break;
|
2019-11-16 22:03:13 +00:00
|
|
|
}
|
2019-11-14 19:42:02 +00:00
|
|
|
}
|
2019-11-17 13:26:23 +00:00
|
|
|
if (!found) {
|
|
|
|
// Checking folder for media files
|
|
|
|
File dir = SD.open(folder);
|
|
|
|
while (entry = dir.openNextFile()) {
|
|
|
|
String filename = entry.name();
|
|
|
|
filename = filename.substring(folder.length() + 1);
|
|
|
|
if (!filename.startsWith(".") && filename.endsWith(".mp3")) {
|
|
|
|
found = true;
|
2019-11-14 19:42:02 +00:00
|
|
|
}
|
2019-11-17 13:26:23 +00:00
|
|
|
entry.close();
|
|
|
|
if (found) break;
|
|
|
|
}
|
|
|
|
if (found) {
|
|
|
|
_unmapped_folders.push_back(folder);
|
|
|
|
}
|
2019-11-14 19:42:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
SPIMaster::select_sd(false);
|
|
|
|
}
|
|
|
|
|
2019-11-17 13:26:23 +00:00
|
|
|
void PlaylistManager::_check_for_special_chars(String s) {
|
|
|
|
for(int i=0; i<s.length(); i++) {
|
|
|
|
char c = s.charAt(i);
|
|
|
|
if (c < 0x20 || c >= 0x7F) {
|
|
|
|
ERROR("WARNING: Folder / file '%s' contains non-ascii chars!\n", s.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 19:42:02 +00:00
|
|
|
Playlist* PlaylistManager::get_playlist_for_id(String id) {
|
|
|
|
if (!_map.count(id)) return NULL;
|
|
|
|
String folder = _map[id];
|
2019-11-16 22:03:13 +00:00
|
|
|
return get_playlist_for_folder(folder);
|
|
|
|
}
|
|
|
|
|
|
|
|
Playlist* PlaylistManager::get_playlist_for_folder(String folder) {
|
2019-11-14 19:42:02 +00:00
|
|
|
if (!_playlists.count(folder)) {
|
|
|
|
_playlists[folder] = new Playlist(folder);
|
|
|
|
}
|
|
|
|
return _playlists[folder];
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlaylistManager::dump_ids() {
|
|
|
|
for (std::map<String, String>::iterator it = _map.begin(); it!=_map.end(); it++) {
|
|
|
|
INFO(" %s -> %s\n", it->first.c_str(), it->second.c_str());
|
|
|
|
}
|
|
|
|
}
|
2019-11-16 22:03:13 +00:00
|
|
|
|
|
|
|
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++) {
|
2019-11-17 13:26:23 +00:00
|
|
|
folders[it->second] = it->first;
|
2019-11-16 22:03:13 +00:00
|
|
|
}
|
|
|
|
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>();
|
|
|
|
}
|
2019-11-16 23:35:23 +00:00
|
|
|
|
|
|
|
bool PlaylistManager::add_mapping(String id, String folder) {
|
2019-11-17 13:26:23 +00:00
|
|
|
DEBUG("Adding mapping: %s=>%s\n", id.c_str(), folder.c_str());
|
2019-11-16 23:35:23 +00:00
|
|
|
_map[id] = folder;
|
2019-11-17 13:26:23 +00:00
|
|
|
_save_mapping();
|
2019-11-16 23:35:23 +00:00
|
|
|
return true;
|
2019-11-17 13:26:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PlaylistManager::_save_mapping() {
|
|
|
|
SPIMaster::select_sd();
|
|
|
|
File f = SD.open("/_mapping.txt", "w");
|
2019-11-19 19:46:54 +00:00
|
|
|
String s = create_mapping_txt();
|
|
|
|
unsigned char* buf = new unsigned char[s.length()];
|
|
|
|
s.getBytes(buf, s.length());
|
|
|
|
f.write(buf, s.length()-1);
|
|
|
|
f.close();
|
|
|
|
SPIMaster::select_sd(false);
|
|
|
|
delete buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
String PlaylistManager::create_mapping_txt() {
|
2019-11-17 13:26:23 +00:00
|
|
|
String s;
|
|
|
|
for(std::map<String, String>::iterator it = _map.begin(); it != _map.end(); it++) {
|
|
|
|
s += it->first;
|
|
|
|
s += "=";
|
|
|
|
s += it->second;
|
|
|
|
s += '\n';
|
|
|
|
}
|
2019-11-19 19:46:54 +00:00
|
|
|
return s;
|
|
|
|
}
|