Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tasks: using std::chrono ::steady_clock::time_point instead of millis #33

Merged
merged 1 commit into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jobs:
- test_logic
- test_savedconfig
- test_tasks
- test_chrono
steps:
- uses: actions/checkout@v4
with:
Expand Down
4 changes: 2 additions & 2 deletions include/Machine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ namespace fabomatic
bool active{false};
FabUser current_user{};

std::optional<std::chrono::milliseconds> usage_start_timestamp{std::nullopt}; // When did the machine start?
std::optional<std::chrono::milliseconds> logoff_timestamp{std::nullopt}; // When did the last user log off?
std::optional<std::chrono::steady_clock::time_point> usage_start_timestamp{std::nullopt}; // When did the machine start?
std::optional<std::chrono::steady_clock::time_point> logoff_timestamp{std::nullopt}; // When did the last user log off?
PowerState power_state{PowerState::PoweredOff};

/// @brief If true, machine needs maintenance
Expand Down
18 changes: 9 additions & 9 deletions include/Tasks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ namespace fabomatic::Tasks
using milliseconds = std::chrono::milliseconds;
using namespace std::chrono_literals;

[[nodiscard]] inline auto arduinoNow() -> milliseconds
[[nodiscard]] inline auto arduinoNow() -> const std::chrono::steady_clock::time_point
{
return milliseconds{::millis()};
return std::chrono::steady_clock::now();
}

class Scheduler;
Expand Down Expand Up @@ -69,7 +69,7 @@ namespace fabomatic::Tasks
[[nodiscard]] auto isActive() const -> bool;

/// @brief Current period of the task
[[nodiscard]] auto getPeriod() const -> milliseconds;
[[nodiscard]] auto getPeriod() const -> const milliseconds;

/// @brief Function to be called when task is run
/// @return Callback function
Expand All @@ -80,20 +80,20 @@ namespace fabomatic::Tasks

/// @brief Get the initial delay before the task is run at given period
/// @return Delay in milliseconds
[[nodiscard]] auto getDelay() const -> milliseconds;
[[nodiscard]] auto getDelay() const -> const milliseconds;

/// @brief Get the average tardiness, i.e. the average period between scheduled start and actual start of execution.
[[nodiscard]] auto getAvgTardiness() const -> milliseconds;
[[nodiscard]] auto getAvgTardiness() const -> const milliseconds;

/// @brief Gets the number of times the task has been run.
[[nodiscard]] auto getRunCounter() const -> unsigned long;

/// @brief Gets the total execution time of the task. Useful to spot slowest tasks
[[nodiscard]] auto getTotalRuntime() const -> milliseconds;
[[nodiscard]] auto getTotalRuntime() const -> const milliseconds;

/// @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 -> milliseconds;
[[nodiscard]] auto getNextRun() const -> const std::chrono::steady_clock::time_point;

[[nodiscard]] auto toString() const -> const std::string;

Expand All @@ -102,8 +102,8 @@ namespace fabomatic::Tasks
const std::string id;
milliseconds period;
milliseconds delay;
milliseconds last_run;
milliseconds next_run;
std::chrono::steady_clock::time_point last_run;
std::chrono::steady_clock::time_point next_run;
milliseconds average_tardiness;
milliseconds total_runtime;
std::function<void()> callback;
Expand Down
2 changes: 1 addition & 1 deletion include/mock/MockMrfc522.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace fabomatic
{
private:
std::optional<card::uid_t> uid{std::nullopt};
std::optional<std::chrono::milliseconds> stop_uid_simulate_time{std::nullopt};
std::optional<std::chrono::steady_clock::time_point> stop_uid_simulate_time{std::nullopt};
std::optional<card::uid_t> getSimulatedUid() const;

public:
Expand Down
5 changes: 4 additions & 1 deletion src/Machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,10 @@ namespace fabomatic
sstream << ", MaintenanceNeeded:" << maintenanceNeeded;
sstream << ", " << config.value().toString();
sstream << ", Active:" << active;
sstream << ", Last logoff:" << (logoff_timestamp.has_value() ? logoff_timestamp.value().count() : 0);
if (logoff_timestamp)
{
sstream << ", Last logoff:" << logoff_timestamp.value().time_since_epoch();
}
sstream << ", GracePeriod (s):" << getGracePeriod().count();
sstream << ")";

Expand Down
18 changes: 9 additions & 9 deletions src/Tasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ namespace fabomatic::Tasks
std::stringstream ss;
ss << "Task " << getId() << ", active=" << active
<< ",Period=" << period << ", Delay=" << delay
<< ",Last run=" << last_run.count()
<< ",Next_run=" << next_run.count()
<< ",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
Expand All @@ -155,8 +155,8 @@ namespace fabomatic::Tasks
if (isActive() && time_to_run)
{
run_counter++;
auto last_period = arduinoNow() - last_run;
average_tardiness = (average_tardiness * (run_counter - 1) + last_period) / run_counter;
auto last_duration = std::chrono::duration_cast<milliseconds>(arduinoNow() - last_run);
average_tardiness = (average_tardiness * (run_counter - 1) + last_duration) / run_counter;
last_run = arduinoNow();

callback();
Expand Down Expand Up @@ -212,7 +212,7 @@ namespace fabomatic::Tasks
return active;
}

auto Task::getPeriod() const -> milliseconds
auto Task::getPeriod() const -> const milliseconds
{
return period;
}
Expand All @@ -227,7 +227,7 @@ namespace fabomatic::Tasks
return id;
}

auto Task::getAvgTardiness() const -> milliseconds
auto Task::getAvgTardiness() const -> const milliseconds
{
if (average_tardiness > period)
{
Expand All @@ -241,7 +241,7 @@ namespace fabomatic::Tasks
return run_counter;
}

auto Task::getDelay() const -> milliseconds
auto Task::getDelay() const -> const milliseconds
{
return delay;
}
Expand All @@ -251,12 +251,12 @@ namespace fabomatic::Tasks
delay = new_delay;
}

auto Task::getTotalRuntime() const -> milliseconds
auto Task::getTotalRuntime() const -> const milliseconds
{
return total_runtime;
}

auto Task::getNextRun() const -> milliseconds
auto Task::getNextRun() const -> const std::chrono::steady_clock::time_point
{
return next_run;
}
Expand Down
66 changes: 66 additions & 0 deletions test/test_chrono/test_chrono.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <chrono>
#include <string>
#include <functional>

#include <Arduino.h>
#define UNITY_INCLUDE_PRINT_FORMATTED
#include <unity.h>
#include "Tasks.hpp"
#include "Logging.hpp"
#include "Espressif.hpp"

[[maybe_unused]] static const char *TAG4 = "test_chrono";

using namespace std::chrono_literals;

namespace fabomatic::tests
{
using Task = fabomatic::Tasks::Task;
using Scheduler = fabomatic::Tasks::Scheduler;

void tearDown(void)
{
}

void setUp(void)
{
// set stuff up here
}

void test_steady_clock(void)
{
static constexpr auto nb_tests = 100;

auto cpt = 0;
TEST_ASSERT_TRUE_MESSAGE(std::chrono::steady_clock::is_steady, "Steady clock available");
auto previous_val = std::chrono::steady_clock::now();
while (cpt < nb_tests)
{
auto val = std::chrono::steady_clock::now();
auto duration = (val - previous_val);
auto count = duration.count();
std::stringstream ss{};
ss << "Duration = " << duration << ", tse=" << val.time_since_epoch();
auto log = ss.str().c_str();
ESP_LOGI(TAG4, "%s", log);
TEST_ASSERT_GREATER_THAN_MESSAGE(0, count, "Duration");
::delay(10);
previous_val = val;
cpt++;
}
}

} // namespace fabomatic::tests

void setup()
{
delay(1000);
esp_log_level_set(TAG4, LOG_LOCAL_LEVEL);
UNITY_BEGIN();
RUN_TEST(fabomatic::tests::test_steady_clock);
UNITY_END(); // stop unit testing
}

void loop()
{
}