Skip to content
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

cmake_minimum_required(VERSION 3.16)
project(robometry LANGUAGES C CXX
VERSION 1.2.8)
VERSION 1.3.0)

include(GNUInstallDirs)
include(FeatureSummary)
Expand Down
17 changes: 15 additions & 2 deletions src/librobometry/include/robometry/BufferManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -501,12 +501,24 @@ class BufferManager {

/**
* @brief Set the saveCallback function. Thanks to this function you can save additional data
* type along with the matfile salve by telemetry
* @param[in] saveCallback The saveCallback function
* type along with the matfile saved by telemetry. This callback is called after saving the matfile.
* The callback receives as input the full path of the matfile just saved and the triggering method.
* The callback must return true on success, false otherwise.
* @param[in] saveCallback The saveCallback function.
* @return true on success, false otherwise.
*/
bool setSaveCallback(std::function<bool(const std::string&, const SaveCallbackSaveMethod& method)> saveCallback);

/**
* @brief Set the preSaveCallback function. Thanks to this function you can decide whether to proceed
* with the saving of the matfile or not. This callback is called before saving the matfile.
* The callback receives as input the triggering method.
* The callback must return true to proceed with the saving, false otherwise.
* @param[in] preSaveCallback The preSaveCallback function.
* @return true on success, false otherwise.
*/
bool setPreSaveCallback(std::function<bool(const SaveCallbackSaveMethod& method)> preSaveCallback);


private:
static double DefaultClock();
Expand Down Expand Up @@ -547,6 +559,7 @@ class BufferManager {

std::function<double(void)> m_nowFunction{DefaultClock};
std::function<bool(const std::string&, const SaveCallbackSaveMethod& method)> m_saveCallback{};
std::function<bool(const SaveCallbackSaveMethod& method)> m_preSaveCallback {};

std::thread m_save_thread;
matioCpp::CellArray m_description_cell_array;
Expand Down
44 changes: 34 additions & 10 deletions src/librobometry/src/BufferManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@ robometry::BufferManager::~BufferManager() {
}
if (m_bufferConfig.auto_save) {
std::string fileName;
saveToFile(fileName);
if (m_saveCallback)
{
m_saveCallback(fileName, SaveCallbackSaveMethod::last_call);
bool should_save = true;
if (m_preSaveCallback) {
should_save = m_preSaveCallback(SaveCallbackSaveMethod::last_call);
}
if (should_save) {
saveToFile(fileName);
if (m_saveCallback) {
m_saveCallback(fileName, SaveCallbackSaveMethod::last_call);
}
}
}
}
Expand Down Expand Up @@ -223,7 +228,18 @@ bool robometry::BufferManager::setSaveCallback(std::function<bool (const std::st
return true;
}

double robometry::BufferManager::DefaultClock() {
bool robometry::BufferManager::setPreSaveCallback(std::function<bool(const SaveCallbackSaveMethod& method)> preSaveCallback)
{
if (preSaveCallback == nullptr) {
std::cout << "Not valid preSaveCallback function." << std::endl;
return false;
}
m_preSaveCallback = preSaveCallback;
return true;
}

double robometry::BufferManager::DefaultClock()
{
return std::chrono::duration<double>(std::chrono::system_clock::now().time_since_epoch()).count();
}

Expand All @@ -238,11 +254,19 @@ void robometry::BufferManager::periodicSave()
{
if (!m_tree->empty()) // if there are channels
{
std::string fileName;
saveToFile(fileName, false);
if (m_saveCallback)
{
m_saveCallback(fileName, SaveCallbackSaveMethod::periodic);
bool should_save = true;
if (m_preSaveCallback) {
should_save = m_preSaveCallback(SaveCallbackSaveMethod::periodic);
}
if (should_save) {
std::string fileName;
saveToFile(fileName, false);
if (m_saveCallback) {
m_saveCallback(fileName, SaveCallbackSaveMethod::periodic);
}
}
else {
std::cout << "Pre-save callback prevented the periodic save." << std::endl;
}
}
}
Expand Down
34 changes: 22 additions & 12 deletions test/BufferManagerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,20 +424,30 @@ TEST_CASE("Buffer Manager Test")

SECTION("Callback")
{
robometry::BufferManager bm;
robometry::BufferConfig bufferConfig;
bufferConfig.n_samples = n_samples;
bufferConfig.filename = "buffer_manager_test_callback";
bufferConfig.auto_save = true;

REQUIRE(bm.addChannel({ "int_channel", {1}}));
bm.setSaveCallback(testCallback);
REQUIRE(bm.configure(bufferConfig));
bool called = false;

for (int i = 0; i < 10; i++) {
bm.push_back(i, "int_channel");
std::this_thread::sleep_for(std::chrono::milliseconds(10));
{
robometry::BufferManager bm;
robometry::BufferConfig bufferConfig;
bufferConfig.n_samples = n_samples;
bufferConfig.filename = "buffer_manager_test_callback";
bufferConfig.auto_save = true;

REQUIRE(bm.addChannel({ "int_channel", {1}}));
REQUIRE(bm.setSaveCallback(testCallback));
REQUIRE(bm.setPreSaveCallback([&called](const robometry::SaveCallbackSaveMethod& /**method*/)
{
called = true;
return true;
}));
REQUIRE(bm.configure(bufferConfig));

for (int i = 0; i < 10; i++) {
bm.push_back(i, "int_channel");
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
REQUIRE(called);
}

SECTION("Unit of measure") {
Expand Down
Loading