Skip to content
Snippets Groups Projects

Resend Data capabilities

Merged Zoe Pfister requested to merge 29-resending-of-data-if-network-connection-was-lost into develop
Files
9
//
// Created by cynthya on 1/5/23.
//
#include "ResendManager.h"
#include "Utilities.h"
void ResendManager::init() {
createResendDirectory();
// log
uint lastResendFileId = getLastResendFileId();
nextResendFileId = lastResendFileId + 1;
esp_log_write(ESP_LOG_INFO, "ResendManager", "nextResendFileId: %d\n", nextResendFileId);
}
uint ResendManager::getLastResendFileId() const { // get the next file id to be resend
auto filesInDirectory = getFilesInDirectory(resendDirectoryPath);
// convert the file names to uint
std::list<uint> fileUintIDs;
try {
for (const auto &fileName : filesInDirectory) {
fileUintIDs.emplace_back(std::stoul(fileName.c_str()));
}
} catch (std::invalid_argument &e) {
esp_log_write(ESP_LOG_ERROR, "getLastResendFileId", "Failed to convert file name to uint\n");
throw;
}
// get the max
uint max = 0;
for (const auto &fileID : fileUintIDs) {
if (fileID > max) {
max = fileID;
}
}
return max;
}
void ResendManager::createResendDirectory() const { // create directory if it doesn't exist
createDirectory("/resend");
}
void ResendManager::storeForResend(const String &messageToBeSend) {
// create file
String filename = String(nextResendFileId);
writeFile(messageToBeSend, resendDirectoryPath + "/" + filename);
ResendManager::incrementCount();
}
std::optional<ResendPointType> ResendManager::loadNextToBeResendMessage() {
// get first file in resend directory
auto filename = getFirstFileNameInDirectory(resendDirectoryPath.c_str());
if (filename.has_value()) {
// read file
auto message = readFile(resendDirectoryPath + "/" + filename.value().c_str());
return ResendPointType{message, filename.value()};
}
return std::nullopt;
}
void ResendManager::deleteResendMessage(const String &filename) {
auto filePath = resendDirectoryPath + "/" + filename;
if (SD.remove(filePath.c_str())) {
esp_log_write(ESP_LOG_INFO, "ResendManager", "Deleted file: %s\n", filePath.c_str());
} else {
esp_log_write(ESP_LOG_ERROR, "ResendManager", "Failed to delete file: %s\n", filePath.c_str());
throw;
}
}
Loading