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
4 files
+ 184
144
Compare changes
  • Side-by-side
  • Inline
Files
4
@@ -3,152 +3,62 @@
//
#include "ResendManager.h"
File openDirectory(const String &dirname);
std::optional<String> getLastFileInDirectory(const String &dirname) {
// log
Serial.println("getLastFileInDirectory");
File root = openDirectory(dirname);
root.rewindDirectory();
File file = root.openNextFile();
while (file) {
File nextFile = root.openNextFile();
if (!nextFile) {
break;
}
file = nextFile;
}
// log
if (file) {
Serial.println("getLastFileInDirectory: file found: " + String(file.name()));
return file.name();
} else {
Serial.println("getLastFileInDirectory: no file found");
return std::nullopt;
}
}
File openDirectory(const String &dirname) {
File root = SD.open(dirname);
if (!root) {
Serial.println("Failed to open directory");
throw;
}
if (!root.isDirectory()) {
Serial.println("Not a directory");
throw;
}
return root;
}
std::optional<String> getFirstFileNameInDirectory(const String &dirname) {
File root = openDirectory(dirname);
root.rewindDirectory();
File file = root.openNextFile();
if (file) {
Serial.println("getFirstFileNameInDirectory: file found: " + String(file.name()));
return file.name();
} else {
Serial.println("getFirstFileNameInDirectory: no file found");
return std::nullopt;
}
}
#include "Utilities.h"
void ResendManager::init() {
// log
Serial.println("createResendDirectory");
createResendDirectory();
// log
Serial.println("getLastResendFileId");
uint lastResendFileId = getLastResendFileId();
nextResendFileId = lastResendFileId + 1;
// log
Serial.println("createResendDirectory");
createResendDirectory();
// log
Serial.println("getLastResendFileId");
uint lastResendFileId = getLastResendFileId();
nextResendFileId = lastResendFileId + 1;
Serial.println("nextResendFileId: " + String(nextResendFileId));
}
uint ResendManager::getLastResendFileId() const { // get the next file id to be resend
auto lastResendFileName = getLastFileInDirectory(resendDirectoryPath.c_str());
if (lastResendFileName.has_value()) {
// if there was a file, get the id from the filename
Serial.println("Last resend file name: " + lastResendFileName.value());
uint lastResendFileId = std::stoul(lastResendFileName.value().c_str());
return lastResendFileId;
} else {
// if there was no file, we start at 0
return 0;
}
auto lastResendFileName = getLastFileInDirectory(resendDirectoryPath.c_str());
if (lastResendFileName.has_value()) {
// if there was a file, get the id from the filename
Serial.println("Last resend file name: " + lastResendFileName.value());
uint lastResendFileId = std::stoul(lastResendFileName.value().c_str());
return lastResendFileId;
} else {
// if there was no file, we start at 0
return 0;
}
}
void ResendManager::createResendDirectory() const { // create directory if it doesn't exist
if (!SD.exists("/resend")) {
SD.mkdir("/resend");
Serial.println("Created directory");
} else {
Serial.println("Resend directory already exists");
}
createDirectory("/resend");
}
void ResendManager::storeForResend(const String &messageToBeSend) {
// create file
String filename = String(nextResendFileId);
writeFile(messageToBeSend, resendDirectoryPath + "/" + filename);
ResendManager::incrementCount();
}
// TODO: Move to a file utils class
void ResendManager::writeFile(const String &messageToBeSend, const String &filePath) const {
File file = SD.open(filePath, FILE_WRITE);
if (!file) {
Serial.println("Failed to open file for writing");
throw;
}
if (file.print(messageToBeSend)) {
Serial.println("File written " + filePath);
} else {
Serial.println("Write failed " + filePath);
throw;
}
file.close();
}
String readFile(const String &filePath) {
Serial.println("Reading file: " + filePath);
File file = SD.open(filePath);
if (!file) {
Serial.println("Failed to open file for reading");
throw;
}
String ret;
while (file.available()) {
ret += (char)file.read();
}
file.close();
return ret;
// 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());
// get first file in resend directory
auto filename = getFirstFileNameInDirectory(resendDirectoryPath.c_str());
if (filename.has_value()) {
if (filename.has_value()) {
// read file
auto message = readFile(resendDirectoryPath + "/" + filename.value().c_str());
return ResendPointType{message, filename.value()};
}
return std::nullopt;
// 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())) {
Serial.println("File deleted " + filePath);
} else {
Serial.println("Delete failed " + filePath);
throw;
}
auto filePath = resendDirectoryPath + "/" + filename;
if (SD.remove(filePath.c_str())) {
Serial.println("File deleted " + filePath);
} else {
Serial.println("Delete failed " + filePath);
throw;
}
}
Loading