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();
}
void DirectoryNode::printDirectoryTree(int level) const
{
for (int i = 0; i < level; i++)
@ -220,61 +222,60 @@ DirectoryNode *DirectoryNode::advanceToMP3(const uint16_t id)
return nullptr;
}
DirectoryNode *DirectoryNode::advanceToMP3(const String *currentGlobal)
DirectoryNode *DirectoryNode::advanceToMP3(const String *songName)
{
if (currentGlobal == nullptr) {
Serial.println("advanceToMP3: currentGlobal is null");
if (songName == nullptr) {
Serial.println("advanceToMP3: songName is null");
return nullptr;
}
// Check if the input is an absolute path (starts with '/') or just a filename
bool isAbsolutePath = currentGlobal->startsWith("/");
// First, check MP3 files in this directory
bool isAbsolutePath = songName->startsWith("/");
// First, search in the current directory's MP3 files
for (size_t i = 0; i < mp3Files.size(); i++)
{
if (isAbsolutePath) {
// For absolute paths, do exact match
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)
{
if (*songName == mp3Files[i]) {
setCurrentPlaying(&mp3Files[i]);
return this;
}
} else if (mp3Files[i].endsWith(*songName))
{
setCurrentPlaying(&mp3Files[i]);
return this;
}
}
// Recursively search subdirectories
// Then search in subdirectories
for (auto subdir : subdirectories)
{
// Check if the string matches a directory name (only for non-absolute paths)
if (!isAbsolutePath && subdir->getName() == *currentGlobal)
if (!isAbsolutePath && subdir->getName() == *songName)
{
subdir->advanceToFirstMP3InThisNode();
return subdir;
}
// Recursively search in subdirectory
DirectoryNode* result = subdir->advanceToMP3(currentGlobal);
if (result != nullptr && result->getCurrentPlaying() != nullptr)
{
return result;
// Search all files within subdir:
for (size_t i = 0; i < subdir->mp3Files.size(); i++)
{
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
Serial.println("advanceToMP3: No song found for: " + *currentGlobal);
Serial.println("advanceToMP3: No song found for: " + *songName);
return nullptr;
}
@ -448,6 +449,9 @@ DirectoryNode *DirectoryNode::advanceToNextMP3(const String *currentGlobal)
return this;
}
String DirectoryNode::getDirectoryStructureHTML() const
{
String html;

View File

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

View File

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