2021-07-16 21:51:30 +02:00
|
|
|
#include "NVMParameterBase.h"
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
ReturnValue_t NVMParameterBase::readJsonFile() {
|
2022-01-17 15:58:27 +01:00
|
|
|
if (std::filesystem::exists(fullName)) {
|
|
|
|
// Read JSON file content into object
|
|
|
|
std::ifstream i(fullName);
|
2022-03-03 19:13:43 +01:00
|
|
|
try {
|
|
|
|
i >> json;
|
|
|
|
} catch (nlohmann::json::exception& e) {
|
|
|
|
sif::warning << "Reading 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;
|
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
|
|
|
|
2022-01-17 15:58:27 +01:00
|
|
|
bool NVMParameterBase::getJsonFileExists() { return std::filesystem::exists(fullName); }
|
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
|
|
|
}
|