2023-03-08 14:50:25 +01:00
|
|
|
#include <mission/memory/NvmParameterBase.h>
|
2021-07-16 21:51:30 +02:00
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
2022-09-16 11:43:11 +02:00
|
|
|
#include "fsfw/filesystem/HasFileSystemIF.h"
|
2022-01-17 15:58:27 +01:00
|
|
|
#include "fsfw/serviceinterface/ServiceInterface.h"
|
|
|
|
|
|
|
|
NVMParameterBase::NVMParameterBase(std::string fullName) : fullName(fullName) {}
|
2021-07-16 21:51:30 +02:00
|
|
|
|
2023-02-21 15:34:16 +01:00
|
|
|
NVMParameterBase::NVMParameterBase() {}
|
|
|
|
|
2021-07-16 21:51:30 +02:00
|
|
|
ReturnValue_t NVMParameterBase::readJsonFile() {
|
2023-03-08 14:50:25 +01:00
|
|
|
std::error_code e;
|
|
|
|
if (std::filesystem::exists(fullName, e)) {
|
2022-01-17 15:58:27 +01:00
|
|
|
// Read JSON file content into object
|
|
|
|
std::ifstream i(fullName);
|
2022-03-03 19:13:43 +01:00
|
|
|
try {
|
|
|
|
i >> json;
|
2023-03-08 14:59:50 +01:00
|
|
|
} catch (nlohmann::json::exception& nlohmannE) {
|
|
|
|
sif::warning << "Reading JSON file failed with error " << nlohmannE.what() << std::endl;
|
2022-08-24 17:27:47 +02:00
|
|
|
return returnvalue::FAILED;
|
2022-03-03 19:13:43 +01:00
|
|
|
}
|
2022-08-24 17:27:47 +02:00
|
|
|
return returnvalue::OK;
|
2022-01-17 15:58:27 +01:00
|
|
|
}
|
|
|
|
return HasFileSystemIF::FILE_DOES_NOT_EXIST;
|
2021-07-16 21:51:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t NVMParameterBase::writeJsonFile() {
|
2022-01-17 15:58:27 +01:00
|
|
|
std::ofstream o(fullName);
|
2022-03-03 19:13:43 +01:00
|
|
|
try {
|
|
|
|
o << std::setw(4) << json << std::endl;
|
|
|
|
} catch (nlohmann::json::exception& e) {
|
|
|
|
sif::warning << "Writing JSON file failed with error " << e.what() << std::endl;
|
2022-08-24 17:27:47 +02:00
|
|
|
return returnvalue::FAILED;
|
2022-03-03 19:13:43 +01:00
|
|
|
}
|
2022-08-24 17:27:47 +02:00
|
|
|
return returnvalue::OK;
|
2021-07-16 21:51:30 +02:00
|
|
|
}
|
|
|
|
|
2022-01-17 15:58:27 +01:00
|
|
|
void NVMParameterBase::setFullName(std::string fullName) { this->fullName = fullName; }
|
2021-07-16 21:51:30 +02:00
|
|
|
|
2022-01-17 15:58:27 +01:00
|
|
|
std::string NVMParameterBase::getFullName() const { return fullName; }
|
2021-07-16 21:51:30 +02:00
|
|
|
|
2023-03-08 14:50:25 +01:00
|
|
|
bool NVMParameterBase::getJsonFileExists() {
|
|
|
|
std::error_code e;
|
|
|
|
return std::filesystem::exists(fullName, e);
|
|
|
|
}
|
2021-07-16 21:51:30 +02:00
|
|
|
|
|
|
|
void NVMParameterBase::printKeys() const {
|
2022-01-17 15:58:27 +01:00
|
|
|
sif::info << "Printing keys for JSON file " << fullName << std::endl;
|
|
|
|
for (const auto& key : keys) {
|
|
|
|
sif::info << key << std::endl;
|
|
|
|
}
|
2021-07-16 21:51:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void NVMParameterBase::print() const {
|
2022-01-17 15:58:27 +01:00
|
|
|
sif::info << "Printing JSON file " << fullName << std::endl;
|
|
|
|
for (const auto& key : keys) {
|
|
|
|
sif::info << key << ": " << json[key] << std::endl;
|
|
|
|
}
|
2021-07-16 21:51:30 +02:00
|
|
|
}
|