fixed absolute / relative song name handling

This commit is contained in:
Stefan Ostermann 2025-07-28 17:37:28 +02:00
parent a1d486dd2d
commit 78167ecc20
3 changed files with 40 additions and 32 deletions

View File

@ -130,6 +130,8 @@ void DirectoryNode::buildDirectoryTree(const char *currentPath)
rootDir.close(); rootDir.close();
} }
void DirectoryNode::printDirectoryTree(int level) const void DirectoryNode::printDirectoryTree(int level) const
{ {
for (int i = 0; i < level; i++) for (int i = 0; i < level; i++)
@ -220,61 +222,60 @@ DirectoryNode *DirectoryNode::advanceToMP3(const uint16_t id)
return nullptr; return nullptr;
} }
DirectoryNode *DirectoryNode::advanceToMP3(const String *currentGlobal)
DirectoryNode *DirectoryNode::advanceToMP3(const String *songName)
{ {
if (currentGlobal == nullptr) { if (songName == nullptr) {
Serial.println("advanceToMP3: currentGlobal is null"); Serial.println("advanceToMP3: songName is null");
return nullptr; return nullptr;
} }
// Check if the input is an absolute path (starts with '/') or just a filename // Check if the input is an absolute path (starts with '/') or just a filename
bool isAbsolutePath = currentGlobal->startsWith("/"); bool isAbsolutePath = songName->startsWith("/");
// First, check MP3 files in this directory // First, search in the current directory's MP3 files
for (size_t i = 0; i < mp3Files.size(); i++) for (size_t i = 0; i < mp3Files.size(); i++)
{ {
if (isAbsolutePath) { if (isAbsolutePath) {
// For absolute paths, do exact match if (*songName == mp3Files[i]) {
if (*currentGlobal == mp3Files[i])
{
setCurrentPlaying(&mp3Files[i]);
return this;
}
} else {
// For filenames, extract filename from full path and compare
String filename = mp3Files[i];
int lastSlash = filename.lastIndexOf('/');
if (lastSlash != -1) {
filename = filename.substring(lastSlash + 1);
}
if (*currentGlobal == filename)
{
setCurrentPlaying(&mp3Files[i]); setCurrentPlaying(&mp3Files[i]);
return this; return this;
} }
} else if (mp3Files[i].endsWith(*songName))
{
setCurrentPlaying(&mp3Files[i]);
return this;
} }
} }
// Recursively search subdirectories // Then search in subdirectories
for (auto subdir : subdirectories) for (auto subdir : subdirectories)
{ {
// Check if the string matches a directory name (only for non-absolute paths) if (!isAbsolutePath && subdir->getName() == *songName)
if (!isAbsolutePath && subdir->getName() == *currentGlobal)
{ {
subdir->advanceToFirstMP3InThisNode(); subdir->advanceToFirstMP3InThisNode();
return subdir; return subdir;
} }
// Recursively search in subdirectory // Search all files within subdir:
DirectoryNode* result = subdir->advanceToMP3(currentGlobal); for (size_t i = 0; i < subdir->mp3Files.size(); i++)
if (result != nullptr && result->getCurrentPlaying() != nullptr) {
{
return result; if (isAbsolutePath) {
if (*songName == subdir->mp3Files[i]) {
subdir->setCurrentPlaying(&subdir->mp3Files[i]);
return subdir;
}
} else if (subdir->mp3Files[i].endsWith(*songName))
{
subdir->setCurrentPlaying(&subdir->mp3Files[i]);
return subdir;
}
} }
} }
// If we get here, no matching song was found // If we get here, no matching song was found
Serial.println("advanceToMP3: No song found for: " + *currentGlobal); Serial.println("advanceToMP3: No song found for: " + *songName);
return nullptr; return nullptr;
} }
@ -448,6 +449,9 @@ DirectoryNode *DirectoryNode::advanceToNextMP3(const String *currentGlobal)
return this; return this;
} }
String DirectoryNode::getDirectoryStructureHTML() const String DirectoryNode::getDirectoryStructureHTML() const
{ {
String html; String html;

View File

@ -47,7 +47,7 @@ public:
void addMP3File(const String& mp3File); void addMP3File(const String& mp3File);
void buildDirectoryTree(const char* currentPath); void buildDirectoryTree(const char* currentPath);
void printDirectoryTree(int level = 0) const; void printDirectoryTree(int level = 0) const;
DirectoryNode* advanceToMP3(const String* currentGlobal); DirectoryNode* advanceToMP3(const String* songName);
DirectoryNode* advanceToNextMP3(const String* currentGlobal); DirectoryNode* advanceToNextMP3(const String* currentGlobal);
DirectoryNode* goToPreviousMP3(uint32_t thresholdSeconds = 3); DirectoryNode* goToPreviousMP3(uint32_t thresholdSeconds = 3);
DirectoryNode* findPreviousMP3Globally(const String* currentGlobal); DirectoryNode* findPreviousMP3Globally(const String* currentGlobal);

View File

@ -555,7 +555,7 @@ void saveMappingToFile(const String filename) {
File file = SD.open(filename, FILE_WRITE); File file = SD.open(filename, FILE_WRITE);
if (file) { if (file) {
for (const auto &pair : rfid_map) { for (const auto &pair : rfid_map) {
file.println(pair.first + "=" + pair.second); file.println(pair.first+ "=" + pair.second);
} }
file.close(); file.close();
Serial.println("Mapping saved to file."); Serial.println("Mapping saved to file.");
@ -569,6 +569,8 @@ void editMapping(AsyncWebServerRequest *request) {
if (request->hasParam("rfid", true) && request->hasParam("song", true)) { if (request->hasParam("rfid", true) && request->hasParam("song", true)) {
String rfid = request->getParam("rfid", true)->value(); String rfid = request->getParam("rfid", true)->value();
String song = request->getParam("song", true)->value(); String song = request->getParam("song", true)->value();
rfid.trim();
song.trim();
rfid_map[rfid] = song; rfid_map[rfid] = song;
saveMappingToFile(getSysDir(mapping_file)); saveMappingToFile(getSysDir(mapping_file));
request->send(200, "text/plain", "Mapping updated"); request->send(200, "text/plain", "Mapping updated");
@ -590,6 +592,8 @@ std::map<String, String> readDataFromFile(String filename) {
// Extract key and value // Extract key and value
String key = line.substring(0, separatorIndex).c_str(); String key = line.substring(0, separatorIndex).c_str();
String value = line.substring(separatorIndex + 1).c_str(); String value = line.substring(separatorIndex + 1).c_str();
key.trim();
value.trim();
Serial.println("found rfid mapping for "+value); Serial.println("found rfid mapping for "+value);
// Add key-value pair to the map // Add key-value pair to the map
rfid_map[key] = value; rfid_map[key] = value;