From 80cc6f4431800828154f704f6c799f93f671a23f Mon Sep 17 00:00:00 2001 From: Pascal Brunot Date: Sun, 14 Jul 2024 17:03:20 +0200 Subject: [PATCH] Tasks: time_point typedef to limit explicit std::chrono::clock dependencies --- include/Machine.hpp | 6 ++++-- include/Tasks.hpp | 10 ++++++---- include/mock/MockMrfc522.hpp | 3 ++- src/Tasks.cpp | 11 +++++------ 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/include/Machine.hpp b/include/Machine.hpp index c59b767e..c1c73704 100644 --- a/include/Machine.hpp +++ b/include/Machine.hpp @@ -3,6 +3,8 @@ #include "FabUser.hpp" #include "MachineConfig.hpp" +#include "Tasks.hpp" + #include #include #include @@ -107,8 +109,8 @@ namespace fabomatic bool active{false}; FabUser current_user{}; - std::optional usage_start_timestamp{std::nullopt}; // When did the machine start? - std::optional logoff_timestamp{std::nullopt}; // When did the last user log off? + std::optional usage_start_timestamp{std::nullopt}; // When did the machine start? + std::optional logoff_timestamp{std::nullopt}; // When did the last user log off? PowerState power_state{PowerState::PoweredOff}; /// @brief If true, machine needs maintenance diff --git a/include/Tasks.hpp b/include/Tasks.hpp index 6bb9309b..a894faab 100644 --- a/include/Tasks.hpp +++ b/include/Tasks.hpp @@ -12,7 +12,9 @@ namespace fabomatic::Tasks using milliseconds = std::chrono::milliseconds; using namespace std::chrono_literals; - [[nodiscard]] inline auto arduinoNow() -> const std::chrono::steady_clock::time_point + typedef std::chrono::steady_clock::time_point time_point; + + [[nodiscard]] inline auto arduinoNow() -> const time_point { return std::chrono::steady_clock::now(); } @@ -93,7 +95,7 @@ namespace fabomatic::Tasks /// @brief When shall the task be run again /// @return time_point of the next run or time_point::max() if the task will not run. - [[nodiscard]] auto getNextRun() const -> const std::chrono::steady_clock::time_point; + [[nodiscard]] auto getNextRun() const -> const Tasks::time_point &; [[nodiscard]] auto toString() const -> const std::string; @@ -102,8 +104,8 @@ namespace fabomatic::Tasks const std::string id; milliseconds period; milliseconds delay; - std::chrono::steady_clock::time_point last_run; - std::chrono::steady_clock::time_point next_run; + Tasks::time_point last_run; + Tasks::time_point next_run; milliseconds average_tardiness; milliseconds total_runtime; std::function callback; diff --git a/include/mock/MockMrfc522.hpp b/include/mock/MockMrfc522.hpp index 7b8d030e..0bc71fab 100644 --- a/include/mock/MockMrfc522.hpp +++ b/include/mock/MockMrfc522.hpp @@ -11,6 +11,7 @@ #include "MFRC522DriverPinSimple.h" #include "MFRC522DriverSPI.h" #include "MFRC522v2.h" +#include "Tasks.hpp" namespace fabomatic { @@ -19,7 +20,7 @@ namespace fabomatic { private: std::optional uid{std::nullopt}; - std::optional stop_uid_simulate_time{std::nullopt}; + std::optional stop_uid_simulate_time{std::nullopt}; std::optional getSimulatedUid() const; public: diff --git a/src/Tasks.cpp b/src/Tasks.cpp index 93f90213..8ec0aabf 100644 --- a/src/Tasks.cpp +++ b/src/Tasks.cpp @@ -139,13 +139,12 @@ namespace fabomatic::Tasks { std::stringstream ss; ss << "Task " << getId() << ", active=" << active - << ",Period=" << period << ", Delay=" << delay + << ",Period(ms)=" << period << ", Delay(ms)=" << delay << ",Last run=" << last_run.time_since_epoch() << ",Next_run=" << next_run.time_since_epoch() - << ",Avg tardiness=" << average_tardiness - << ",total_runtime " << total_runtime - << ",run_counter=" << run_counter - << ",clock=" << millis(); + << ",Avg tardiness (ms)=" << average_tardiness + << ",total_runtime (ms)= " << total_runtime + << ",run_counter=" << run_counter; return ss.str(); } @@ -256,7 +255,7 @@ namespace fabomatic::Tasks return total_runtime; } - auto Task::getNextRun() const -> const std::chrono::steady_clock::time_point + auto Task::getNextRun() const -> const Tasks::time_point & { return next_run; }