Skip to content

Commit

Permalink
Merge branch 'main' into idf5-arduino3
Browse files Browse the repository at this point in the history
  • Loading branch information
PBrunot committed May 30, 2024
2 parents 1e112f9 + efb8e5d commit 9b6eb45
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 21 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name: Build firmware images

on:
workflow_dispatch:
pull_request:
workflow_run:
workflows: [Test suite based on Wokwi CLI]
types:
Expand Down
4 changes: 2 additions & 2 deletions include/BufferedMsg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ namespace fabomatic
static constexpr auto MAGIC_NUMBER = 1;

public:
auto push_back(const std::string &message, const std::string &topic, bool wait) -> void;
auto push_front(const std::string &message, const std::string &topic, bool wait) -> void;
auto push_back(const BufferedMsg &message) -> void;
auto push_front(const BufferedMsg &message) -> void;
auto getMessage() -> const BufferedMsg;
auto count() const -> size_t;
auto toJson(JsonDocument &doc, const std::string &element_name) const -> void;
Expand Down
27 changes: 16 additions & 11 deletions src/BufferedMsg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,41 @@
namespace fabomatic
{

auto Buffer::push_back(const std::string &message, const std::string &topic, bool wait) -> void
auto Buffer::push_back(const BufferedMsg &message) -> void
{
if constexpr (conf::debug::ENABLE_BUFFERING)
{
BufferedMsg msg{message, topic, wait};
msg_queue.push_back(msg);
msg_queue.push_back(message);
if (msg_queue.size() > MAX_MESSAGES)
{
msg_queue.pop_front();
ESP_LOGW(TAG, "Removing first message from the buffer");
}
has_changed = true;

ESP_LOGI(TAG, "Buffered %s on %s, %u messages queued", message.c_str(), topic.c_str(), msg_queue.size());
ESP_LOGI(TAG, "Buffered %s on %s, %lu messages queued",
message.mqtt_message.c_str(),
message.mqtt_topic.c_str(),
msg_queue.size());
}
}

auto Buffer::push_front(const std::string &message, const std::string &topic, bool wait) -> void
auto Buffer::push_front(const BufferedMsg &message) -> void
{
if constexpr (conf::debug::ENABLE_BUFFERING)
{
BufferedMsg msg{message, topic, wait};
msg_queue.push_front(msg);
msg_queue.push_front(message);
if (msg_queue.size() > MAX_MESSAGES)
{
msg_queue.pop_back();
ESP_LOGW(TAG, "Removing last message from the buffer");
}
has_changed = true;

ESP_LOGI(TAG, "Buffered %s on %s, %u messages queued", message.c_str(), topic.c_str(), msg_queue.size());
ESP_LOGI(TAG, "Buffered %s on %s, %lu messages queued",
message.mqtt_message.c_str(),
message.mqtt_topic.c_str(),
msg_queue.size());
}
}

Expand Down Expand Up @@ -89,9 +93,10 @@ namespace fabomatic

for (const auto &elem : json_obj["messages"].as<JsonArray>())
{
buff.push_back(elem["msg"].as<std::string>(),
elem["tp"].as<std::string>(),
elem["wait"].as<bool>());
const auto &msg = BufferedMsg{elem["msg"].as<std::string>(),
elem["tp"].as<std::string>(),
elem["wait"].as<bool>()};
buff.push_back(msg);
}

ESP_LOGD(TAG, "Buffer::fromJsonElement() : data loaded successfully");
Expand Down
15 changes: 10 additions & 5 deletions src/FabBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ namespace fabomatic
// Do not send twice if response did not arrive
if (query.buffered() && !published)
{
buffer.push_back(query.payload(), topic, query.waitForReply());
const auto &msg = BufferedMsg{query.payload(), topic, query.waitForReply()};
buffer.push_back(msg);
}

if (published)
Expand Down Expand Up @@ -440,7 +441,8 @@ namespace fabomatic

if (query.buffered())
{
buffer.push_back(query.payload(), topic, query.waitForReply());
const auto &msg = BufferedMsg{query.payload(), topic, query.waitForReply()};
buffer.push_back(msg);
}

return std::make_unique<RespT>(false);
Expand Down Expand Up @@ -489,7 +491,8 @@ namespace fabomatic

if (query.buffered())
{
buffer.push_back(query.payload(), topic, query.waitForReply());
const auto &msg = BufferedMsg{query.payload(), topic, query.waitForReply()};
buffer.push_back(msg);
}
return false;
}
Expand Down Expand Up @@ -602,7 +605,8 @@ namespace fabomatic
// If it has been published but not answered, do not try again
if (result == PublishResult::PublishedWithoutAnswer)
{
buffer.push_front(msg.mqtt_message, msg.mqtt_topic, msg.wait_for_answer);
const auto &bmsg = BufferedMsg{msg.mqtt_message, msg.mqtt_topic, msg.wait_for_answer};
buffer.push_front(bmsg);
}
break;
}
Expand All @@ -613,7 +617,8 @@ namespace fabomatic
{
// Will try again
ESP_LOGW(TAG, "Retransmitting buffered message failed!");
buffer.push_front(msg.mqtt_message, msg.mqtt_topic, msg.wait_for_answer);
const auto &bmsg = BufferedMsg{msg.mqtt_message, msg.mqtt_topic, msg.wait_for_answer};
buffer.push_front(bmsg);
break;
}
}
Expand Down
9 changes: 6 additions & 3 deletions test/test_savedconfig/test_savedconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ namespace fabomatic::tests
for (const auto &msg : messages)
{
TEST_ASSERT_EQUAL_MESSAGE(msg_count, buff.count(), "Push_back: Buffer count is correct");
buff.push_front(msg.mqtt_message, msg.mqtt_topic, msg.wait_for_answer);
const auto &bmsg = BufferedMsg{msg.mqtt_message, msg.mqtt_topic, msg.wait_for_answer};
buff.push_front(bmsg);
msg_count++;
}

Expand All @@ -265,7 +266,8 @@ namespace fabomatic::tests
for (const auto &msg : messages)
{
TEST_ASSERT_EQUAL_MESSAGE(msg_count, buff.count(), "(1) Push_front: Buffer count is correct");
buff.push_back(msg.mqtt_message, msg.mqtt_topic, msg.wait_for_answer);
const auto &bmsg = BufferedMsg{msg.mqtt_message, msg.mqtt_topic, msg.wait_for_answer};
buff.push_back(bmsg);
msg_count++;
}

Expand Down Expand Up @@ -293,7 +295,8 @@ namespace fabomatic::tests
for (const auto &msg : messages)
{
TEST_ASSERT_EQUAL_MESSAGE(msg_count, buff.count(), "(2) Push_front: Buffer count is correct");
buff.push_back(msg.mqtt_message, msg.mqtt_topic, msg.wait_for_answer);
const auto &bmsg = BufferedMsg{msg.mqtt_message, msg.mqtt_topic, msg.wait_for_answer};
buff.push_back(bmsg);
msg_count++;
}

Expand Down
13 changes: 13 additions & 0 deletions test/test_tasks/test_tasks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <unity.h>
#include "Tasks.hpp"
#include "Logging.hpp"
#include "Espressif.hpp"

using namespace std::chrono_literals;

Expand Down Expand Up @@ -128,6 +129,17 @@ namespace fabomatic::tests
execute();
TEST_ASSERT_EQUAL_MESSAGE(NB_TASKS, task_counter, "Restarted tasks did not run immediately");
}

void test_esp32()
{
auto result = fabomatic::esp32::esp_serial();
TEST_ASSERT_GREATER_OR_EQUAL_UINT32_MESSAGE(0, result.length(), "ESP32 serial must be non empty");
auto mem_free = fabomatic::esp32::getFreeHeap();
TEST_ASSERT_GREATER_OR_EQUAL_UINT32_MESSAGE(0, mem_free, "ESP32 mem free must be > 0");
fabomatic::esp32::showHeapStats();
TEST_ASSERT_TRUE_MESSAGE(fabomatic::esp32::setupWatchdog(30s), "Watchdog setup");
fabomatic::esp32::removeWatchdog();
}
} // namespace fabomatic::tests

void setup()
Expand All @@ -137,6 +149,7 @@ void setup()
UNITY_BEGIN();
RUN_TEST(fabomatic::tests::test_execute_runs_all_tasks);
RUN_TEST(fabomatic::tests::test_stop_start_tasks);
RUN_TEST(fabomatic::tests::test_esp32);
UNITY_END(); // stop unit testing
}

Expand Down

0 comments on commit 9b6eb45

Please sign in to comment.