Fixed advent mode and persistence stuff.

This commit is contained in:
Fabian Schlenz 2019-11-29 21:20:19 +01:00
parent 4eef69516e
commit 51bef05465
3 changed files with 48 additions and 35 deletions

View File

@ -142,7 +142,8 @@ void Controller::_check_rfid() {
if (time.tm_mon == 11) { // tm_mon is "months since january", so 11 means december.
pl->advent_shuffle(time.tm_mday);
} else {
// TODO
DEBUG("Album is in advent mode, but it isn't december (yet). Not playing.\n");
return;
}
} else if (data.indexOf("[random]") != -1 && pl->is_fresh()) {
pl->shuffle();

View File

@ -317,12 +317,19 @@ void Playlist::shuffle(uint8_t random_offset) {
}
void Playlist::advent_shuffle(uint8_t day) {
if (day > 24) day = 24;
TRACE("advent_shuffle running...\n");
// Not enough songs till the current day? Play all songs in the default order.
if (day > _files.size()) {
return;
}
// We are in the "different playlist every day" mode. So we don't persist it in order to not miss changes.
persistence = PERSIST_NONE;
if (day > _files.size()) return;
_files.insert(_files.begin(), _files[day - 1]);
_files.erase(_files.begin() + day - 1, _files.end());
_files.erase(_files.begin() + day, _files.end());
}
void Playlist::reset() {

View File

@ -109,7 +109,6 @@ Playlist* PlaylistManager::get_playlist_for_folder(String folder) {
p = new Playlist(folder);
_playlists[folder] = p;
if (p->persistence == PERSIST_PERMANENTLY) {
// TODO Load persistence from file
String search = folder;
search += "=";
SPIMaster::select_sd();
@ -197,38 +196,44 @@ String PlaylistManager::create_mapping_txt() {
}
void PlaylistManager::persist(Playlist* p) {
if (p->persistence != PERSIST_PERMANENTLY) return;
if (p->persistence == PERSIST_NONE) {
_playlists.erase(p->path());
return;
} else if (p->persistence == PERSIST_PERMANENTLY) {
String search = p->path();
search += '=';
bool old_file_existed = false;
SPIMaster::select_sd();
if (SD.exists("_positions.txt")) {
SD.rename("/_positions.txt", "/_positions.temp.txt");
old_file_existed = true;
}
File dst = SD.open("/_positions.txt", "w");
if (old_file_existed) {
File src = SD.open("/_positions.temp.txt", "r");
while (true) {
String line = src.readStringUntil('\n');
line.trim();
if (line.startsWith(search)) continue;
dst.println(line);
String search = p->path();
search += '=';
bool old_file_existed = false;
SPIMaster::select_sd();
if (SD.exists("_positions.txt")) {
SD.rename("/_positions.txt", "/_positions.temp.txt");
old_file_existed = true;
}
File dst = SD.open("/_positions.txt", "w");
if (old_file_existed) {
File src = SD.open("/_positions.temp.txt", "r");
while (true) {
String line = src.readStringUntil('\n');
line.trim();
if (line.startsWith(search)) continue;
dst.println(line);
}
src.close();
SD.remove("/_positions.temp.txt");
}
src.close();
SD.remove("/_positions.temp.txt");
dst.print(search);
dst.print(p->get_current_track_id());
dst.print(',');
dst.println(p->get_position());
dst.close();
SPIMaster::select_sd(false);
_playlists.erase(p->path());
}
dst.print(search);
dst.print(p->get_current_track_id());
dst.print(',');
dst.println(p->get_position());
dst.close();
SPIMaster::select_sd(false);
}