From 8c0e261f449a80ef2a2da023d0be0ac915bcd4af Mon Sep 17 00:00:00 2001 From: Irini Kosmidou Date: Wed, 31 Aug 2022 10:58:21 +0200 Subject: [PATCH] ScexDeviceHandler: fileId includes date and time --- mission/devices/ScexDeviceHandler.cpp | 42 +++++++++++++++++---------- mission/devices/ScexDeviceHandler.h | 2 +- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/mission/devices/ScexDeviceHandler.cpp b/mission/devices/ScexDeviceHandler.cpp index 0c3049c8..bd9211a6 100644 --- a/mission/devices/ScexDeviceHandler.cpp +++ b/mission/devices/ScexDeviceHandler.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -168,7 +169,7 @@ ReturnValue_t ScexDeviceHandler::handleValidReply(size_t remSize, DeviceCommandI ReturnValue_t result = OK; auto multiFileHandler = [&](std::string cmdName) { if ((helper.getPacketCounter() == 1) or (not fileNameSet)) { - fileId = random_string(6); + fileId = date_time_string(); std::ostringstream oss("/tmp/scex-", std::ostringstream::ate); oss << cmdName << fileId << ".bin"; fileName = oss.str(); @@ -230,7 +231,7 @@ ReturnValue_t ScexDeviceHandler::interpretDeviceReply(DeviceCommandId_t id, cons ReturnValue_t status = OK; auto oneFileHandler = [&](std::string cmdName) { - fileId = random_string(6); + fileId = date_time_string(); std::ostringstream oss("/tmp/scex-", std::ostringstream::ate); oss << cmdName << fileId << ".bin"; fileName = oss.str(); @@ -298,23 +299,32 @@ ReturnValue_t ScexDeviceHandler::initializeLocalDataPool(localpool::DataPool& lo return OK; } -std::string ScexDeviceHandler::random_string(std::string::size_type length) { - static auto& chrs = - "0123456789" - "abcdefghijklmnopqrstuvwxyz" - "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; +std::string ScexDeviceHandler::date_time_string() { + using namespace std; + string date_time; + time_t now = time(0); + tm* ltm = localtime(&now); + ostringstream oss(std::ostringstream::ate); - thread_local static std::mt19937 rg{std::random_device{}()}; - thread_local static std::uniform_int_distribution pick(0, - sizeof(chrs) - 2); + if (ltm->tm_hour < 10) { + oss << 1900 + ltm->tm_year << 1 + ltm->tm_mon << ltm->tm_mday << "_0" << ltm->tm_hour; + } else { + oss << 1900 + ltm->tm_year << 1 + ltm->tm_mon << ltm->tm_mday << "_" << ltm->tm_hour; + } + if (ltm->tm_min < 10) { + oss << 0 << ltm->tm_min; + } else { + oss << ltm->tm_min; + } + if (ltm->tm_sec < 10) { + oss << 0 << ltm->tm_sec; + } else { + oss << ltm->tm_sec; + } - std::string s; + date_time = oss.str(); - s.reserve(length); - - while (length--) s += chrs[pick(rg)]; - - return s; + return date_time; } void ScexDeviceHandler::modeChanged() {} diff --git a/mission/devices/ScexDeviceHandler.h b/mission/devices/ScexDeviceHandler.h index 69436e0b..39d333f5 100644 --- a/mission/devices/ScexDeviceHandler.h +++ b/mission/devices/ScexDeviceHandler.h @@ -28,7 +28,7 @@ class ScexDeviceHandler : public DeviceHandlerBase { SdCardMountedIF *sdcMan = nullptr; Countdown finishCountdown = Countdown(LONG_CD); - std::string random_string(std::string::size_type length); + std::string date_time_string(); // DeviceHandlerBase private function implementation void doStartUp() override;