diff --git a/mission/tmtc/TmStore.cpp b/mission/tmtc/TmStore.cpp index a2aec09c..55a47347 100644 --- a/mission/tmtc/TmStore.cpp +++ b/mission/tmtc/TmStore.cpp @@ -61,8 +61,6 @@ ReturnValue_t TmStore::storePacket(PusTmReader& reader) { create_directory(baseDir); } if (not mostRecentFile) { - // TODO: Find most recent file by iterating through all files and remembering the file - // with the most recent timestamp. for (auto const& file : directory_iterator(baseDir)) { if (file.is_directory()) { continue; @@ -72,13 +70,21 @@ ReturnValue_t TmStore::storePacket(PusTmReader& reader) { if (pathStr.find(baseName) == std::string::npos) { continue; } - float seconds = 0.0; - char* prefix = nullptr; - int count = sscanf(pathStr.c_str(), - "%s_%4" SCNu32 "-%2" SCNu32 "-%2" SCNu32 "T%2" SCNu32 ":%2" SCNu32 ":%fZ", - prefix, &tod.year, &tod.month, &tod.day, &tod.hour, &tod.minute, &seconds); - tod.second = std::floor(seconds); - static_cast(count); + unsigned int underscorePos = pathStr.find_last_of('_'); + std::string stampStr = pathStr.substr(underscorePos + 1); + int count = + sscanf(stampStr.c_str(), + "%4" SCNu32 "-%2" SCNu32 "-%2" SCNu32 "T%2" SCNu32 ":%2" SCNu32 ":%2" SCNu32 "Z", + &tod.year, &tod.month, &tod.day, &tod.hour, &tod.minute, &tod.second); + if (count != 6) { + continue; + } + timeval tv{}; + Clock::convertTimeOfDayToTimeval(&tod, &tv); + if (not mostRecentTv || tv > mostRecentTv.value()) { + mostRecentTv = tv; + mostRecentFile = file.path(); + } } } // TODO: Need to find the file of the most recent file. diff --git a/mission/tmtc/TmStore.h b/mission/tmtc/TmStore.h index 7402921e..a2638d2b 100644 --- a/mission/tmtc/TmStore.h +++ b/mission/tmtc/TmStore.h @@ -7,6 +7,8 @@ #include #include +#include + struct PacketFilter { std::optional> apid; std::optional> services; @@ -37,7 +39,8 @@ class TmStore : public SystemObject { uint8_t intervalFactor; char NAME_BUF[524] = {}; std::array fileBuf{}; - std::optional mostRecentFile; + std::optional mostRecentTv; + std::optional mostRecentFile; SdCardMountedIF& sdcMan; };