eive-obsw/mission/memory/NVMParameterBase.cpp

55 lines
1.6 KiB
C++
Raw Permalink Normal View History

2021-07-16 21:51:30 +02:00
#include "NVMParameterBase.h"
#include <fstream>
2022-01-17 15:58:27 +01:00
#include "fsfw/memory/HasFileSystemIF.h"
#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;
return HasReturnvaluesIF::RETURN_FAILED;
}
2022-01-17 15:58:27 +01:00
return HasReturnvaluesIF::RETURN_OK;
}
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;
return HasReturnvaluesIF::RETURN_FAILED;
}
2022-01-17 15:58:27 +01:00
return HasReturnvaluesIF::RETURN_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
}