DirectoryNode structure
This commit is contained in:
194
src/DirectoryNode.cpp
Normal file
194
src/DirectoryNode.cpp
Normal file
@@ -0,0 +1,194 @@
|
||||
#include "DirectoryNode.h"
|
||||
|
||||
DirectoryNode::DirectoryNode(const String &nodeName)
|
||||
: name(nodeName), currentPlaying(nullptr) {}
|
||||
|
||||
DirectoryNode::~DirectoryNode()
|
||||
{
|
||||
for (DirectoryNode *childNode : subdirectories)
|
||||
{
|
||||
delete childNode;
|
||||
}
|
||||
}
|
||||
|
||||
const String &DirectoryNode::getName() const
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
const std::vector<DirectoryNode *> &DirectoryNode::getSubdirectories() const
|
||||
{
|
||||
return subdirectories;
|
||||
}
|
||||
|
||||
const std::vector<String> &DirectoryNode::getMP3Files() const
|
||||
{
|
||||
return mp3Files;
|
||||
}
|
||||
|
||||
void DirectoryNode::setCurrentPlaying(const String *mp3File)
|
||||
{
|
||||
currentPlaying = mp3File;
|
||||
}
|
||||
|
||||
const String *DirectoryNode::getCurrentPlaying() const
|
||||
{
|
||||
return currentPlaying;
|
||||
}
|
||||
|
||||
void DirectoryNode::addSubdirectory(DirectoryNode *subdirectory)
|
||||
{
|
||||
subdirectories.push_back(subdirectory);
|
||||
}
|
||||
|
||||
void DirectoryNode::addMP3File(const String &mp3File)
|
||||
{
|
||||
mp3Files.push_back(mp3File);
|
||||
}
|
||||
|
||||
void DirectoryNode::buildDirectoryTree(const char *currentPath)
|
||||
{
|
||||
File rootDir = SD.open(currentPath);
|
||||
while (true)
|
||||
{
|
||||
File entry = rootDir.openNextFile();
|
||||
if (!entry)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (entry.isDirectory())
|
||||
{
|
||||
DirectoryNode *newNode = new DirectoryNode(entry.name());
|
||||
subdirectories.push_back(newNode);
|
||||
newNode->buildDirectoryTree((String(currentPath) + entry.name()).c_str());
|
||||
}
|
||||
else if (String(entry.name()).endsWith(".mp3"))
|
||||
{
|
||||
mp3Files.push_back(entry.name());
|
||||
}
|
||||
entry.close();
|
||||
}
|
||||
rootDir.close();
|
||||
}
|
||||
|
||||
void DirectoryNode::printDirectoryTree(int level) const
|
||||
{
|
||||
for (int i = 0; i < level; i++)
|
||||
{
|
||||
Serial.print(" ");
|
||||
}
|
||||
Serial.println(name);
|
||||
|
||||
for (const String &mp3File : mp3Files)
|
||||
{
|
||||
for (int i = 0; i <= level; i++)
|
||||
{
|
||||
Serial.print(" ");
|
||||
}
|
||||
Serial.println(mp3File);
|
||||
}
|
||||
|
||||
for (DirectoryNode *childNode : subdirectories)
|
||||
{
|
||||
childNode->printDirectoryTree(level + 1);
|
||||
}
|
||||
}
|
||||
|
||||
DirectoryNode *DirectoryNode::findFirstDirectoryWithMP3s()
|
||||
{
|
||||
if (!mp3Files.empty())
|
||||
{
|
||||
// Found a directory with MP3 files
|
||||
return this;
|
||||
}
|
||||
|
||||
for (DirectoryNode *subdirectory : subdirectories)
|
||||
{
|
||||
DirectoryNode *result = subdirectory->findFirstDirectoryWithMP3s();
|
||||
if (result != nullptr)
|
||||
{
|
||||
// Found a directory with MP3 files in the subdirectories
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
// No directory with MP3 files found
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void DirectoryNode::advanceToNextMP3()
|
||||
{
|
||||
if (currentPlaying != nullptr)
|
||||
{
|
||||
for (size_t i = 0; i < mp3Files.size(); i++)
|
||||
{
|
||||
if (*currentPlaying == mp3Files[i])
|
||||
{
|
||||
// Found the current playing MP3 file
|
||||
if (i < mp3Files.size() - 1)
|
||||
{
|
||||
// Advance to the next MP3 file in the same directory
|
||||
currentPlaying = &mp3Files[i + 1];
|
||||
return;
|
||||
}
|
||||
// Reached the end of the MP3 files in the directory
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// If not playing or reached the end, set the first MP3 file as the current playing
|
||||
if (!mp3Files.empty())
|
||||
{
|
||||
currentPlaying = &mp3Files[0];
|
||||
}
|
||||
}
|
||||
|
||||
String DirectoryNode::getDirectoryStructureHTML() const
|
||||
{
|
||||
String html;
|
||||
html.reserve(1024); // Reserve memory for better performance
|
||||
|
||||
appendIndentation(html, 0);
|
||||
html += "<ul>\n";
|
||||
appendIndentation(html, 1);
|
||||
html += "<li>" + name + "</li>\n";
|
||||
|
||||
for (const String &mp3File : mp3Files)
|
||||
{
|
||||
appendIndentation(html, 2);
|
||||
html += "<li>" + mp3File + "</li>\n";
|
||||
}
|
||||
|
||||
for (DirectoryNode *childNode : subdirectories)
|
||||
{
|
||||
html += childNode->getDirectoryStructureHTML();
|
||||
}
|
||||
|
||||
appendIndentation(html, 0);
|
||||
html += "</ul>\n";
|
||||
|
||||
return html;
|
||||
}
|
||||
|
||||
void DirectoryNode::appendIndentation(String &html, int level) const
|
||||
{
|
||||
for (int i = 0; i < level; i++)
|
||||
{
|
||||
html += " ";
|
||||
}
|
||||
}
|
||||
|
||||
String DirectoryNode::getCurrentPlayingFilePath() const
|
||||
{
|
||||
if (currentPlaying != nullptr)
|
||||
{
|
||||
String filePath = "/" + name;
|
||||
if (!filePath.endsWith("/"))
|
||||
{
|
||||
filePath += "/";
|
||||
}
|
||||
filePath += *currentPlaying;
|
||||
return filePath;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
Reference in New Issue
Block a user