From 6d6fbae5f6ce69c031b01331a7b753a7d8c4a59e Mon Sep 17 00:00:00 2001 From: lains <383502+lains@users.noreply.github.com> Date: Wed, 15 May 2024 15:12:35 +0200 Subject: [PATCH] Allowing to forge unhorodated frames' timestamp based on system time --- inc/domain/TicFrameParser.h | 17 ++++++++++++++--- src/domain/TicFrameParser.cpp | 23 +++++++++++++---------- src/main.cpp | 7 ++++++- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/inc/domain/TicFrameParser.h b/inc/domain/TicFrameParser.h index 0950c97..6034919 100644 --- a/inc/domain/TicFrameParser.h +++ b/inc/domain/TicFrameParser.h @@ -68,7 +68,8 @@ class TicFrameParser { public: /* Types */ typedef void(*FOnNewPowerDataFunc)(const TicEvaluatedPower& power, const TimeOfDay& timestamp, unsigned int frameId, void* context); /*!< The prototype of callbacks invoked on new power data */ - typedef void(*FOnDayOverFunc)(void* context); /*!< The prototype of callbacks invoked we we switch to the next day */ + typedef void(*FOnDayOverFunc)(void* context); /*!< The prototype of callbacks invoked when we switch to the next day */ + typedef TimeOfDay(*FCurrentTimerGetterFunc)(void* context); /*!< The prototype of function to invoked to get the current TimeOfDay */ /* Methods */ /** @@ -86,10 +87,18 @@ class TicFrameParser { /** * @brief Set the method to invoke when we detect a switch to the next day * - * @param power The method to invoke + * @param dayOverFunc The method to invoke * @param context A context provided to the method */ - void onDayOverInvoke(FOnDayOverFunc dayOverFunc, void* context); + void invokeWhenDayOver(FOnDayOverFunc dayOverFunc, void* context); + + /** + * @brief Set the method to invoke to get the current TimeOfDay + * + * @param currentTimeGetter The method to invoke + * @param context A context provided to the method + */ + void setCurrentTimeGetter(FCurrentTimerGetterFunc currentTimeGetter, void* context); protected: void onNewMeasurementAvailable(); @@ -215,6 +224,8 @@ class TicFrameParser { void* onNewPowerDataContext; /*!< A context pointer passed to onNewFrameBytes() and onFrameComplete() at invokation */ FOnDayOverFunc onDayOverFunc; /*!< Pointer to a function invoked when we detect switching to the next day */ void* onDayOverFuncContext; /*!< A context pointer passed as argument to the above method */ + FCurrentTimerGetterFunc currentTimeGetterFunc; /*!< Pointer to a function to invoke to get the current TimeOfDay */ + void* currentTimeGetterFuncContext; /*!< A context pointer passed as argument to the above method */ unsigned int nbFramesParsed; /*!< Total number of complete frames parsed */ TIC::DatasetExtractor de; /*!< The encapsulated dataset extractor instance (programmed to call us back on newly decoded datasets) */ TicMeasurements lastFrameMeasurements; /*!< Gathers all interesting measurement of the last frame */ diff --git a/src/domain/TicFrameParser.cpp b/src/domain/TicFrameParser.cpp index a9afbb0..ff4ba01 100644 --- a/src/domain/TicFrameParser.cpp +++ b/src/domain/TicFrameParser.cpp @@ -121,8 +121,10 @@ void std::swap(TicMeasurements& first, TicMeasurements& second) { TicFrameParser::TicFrameParser(FOnNewPowerDataFunc onNewPowerData, void* onNewPowerDataContext) : onNewPowerData(onNewPowerData), onNewPowerDataContext(onNewPowerDataContext), - onDayOverFunc([](void* context) { }), + onDayOverFunc(nullptr), onDayOverFuncContext(nullptr), + currentTimeGetterFunc(nullptr), + currentTimeGetterFuncContext(nullptr), nbFramesParsed(0), de(ticFrameParserUnWrapDatasetExtractor, this), lastFrameMeasurements(), @@ -220,15 +222,11 @@ void TicFrameParser::guessFrameArrivalTime() { Stm32DebugOutput::get().send("\n"); #endif } - unsigned int emulatedSecond = (this->nbFramesParsed * 3) % 60; /* Assume 1 historical TIC frame every 3 seconds */ - unsigned int emulatedHorodateRemainder = (this->nbFramesParsed / 20); /* Counts total remainder as minutes */ - unsigned int emulatedMinute = emulatedHorodateRemainder % 60; - emulatedHorodateRemainder = emulatedHorodateRemainder / 60; /* Now count total remainder as hours */ - unsigned int emulatedHour = emulatedHorodateRemainder % 24; - /* Note: we discard days and month for now */ - this->lastFrameMeasurements.timestamp = TimeOfDay(emulatedHour, emulatedMinute, emulatedSecond); + if (this->currentTimeGetterFunc) { + this->lastFrameMeasurements.timestamp = this->currentTimeGetterFunc(this->currentTimeGetterFuncContext); + } #ifdef EMBEDDED_DEBUG_CONSOLE - Stm32DebugOutput::get().send("Injecting timestamp in historical frame: "); + Stm32DebugOutput::get().send("Using the following systemtime instead of (missing) frame horodate: "); Stm32DebugOutput::get().send(static_cast(this->lastFrameMeasurements.timestamp.hour)); Stm32DebugOutput::get().send(":"); Stm32DebugOutput::get().send(static_cast(this->lastFrameMeasurements.timestamp.minute)); @@ -238,11 +236,16 @@ void TicFrameParser::guessFrameArrivalTime() { #endif } -void TicFrameParser::onDayOverInvoke(FOnDayOverFunc dayOverFunc, void* context) { +void TicFrameParser::invokeWhenDayOver(FOnDayOverFunc dayOverFunc, void* context) { this->onDayOverFunc = dayOverFunc; this->onDayOverFuncContext = context; } +void TicFrameParser::setCurrentTimeGetter(FCurrentTimerGetterFunc currentTimeGetter, void* context) { + this->currentTimeGetterFunc = currentTimeGetter; + this->currentTimeGetterFuncContext = context; +} + void TicFrameParser::onRefPowerInfo(uint32_t power) { //FIXME: Todo } diff --git a/src/main.cpp b/src/main.cpp index 9d4eb0f..029ed5a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -147,7 +147,12 @@ int main(void) { TicProcessingContext* ticContext = static_cast(context); ticContext->currentTime.startNewDayAtMidnight(); }; - ticParser.onDayOverInvoke(performAtMidnight, static_cast(&ticContext)); + ticParser.invokeWhenDayOver(performAtMidnight, static_cast(&ticContext)); + auto currentTimeGetter = [](void* context) -> TimeOfDay { + TicProcessingContext* ticContext = static_cast(context); + return ticContext->currentTime.time; + }; + ticParser.setCurrentTimeGetter(currentTimeGetter, static_cast(&ticContext)); powerHistory.setContext(&ticContext);