2021-12-07 16:30:17 +01:00
|
|
|
#include "ArcsecJsonParamBase.h"
|
2022-01-17 15:58:27 +01:00
|
|
|
|
2021-12-07 16:30:17 +01:00
|
|
|
#include "ArcsecJsonKeys.h"
|
|
|
|
|
|
|
|
ArcsecJsonParamBase::ArcsecJsonParamBase(std::string setName) : setName(setName) {}
|
|
|
|
|
|
|
|
ReturnValue_t ArcsecJsonParamBase::create(std::string fullname, uint8_t* buffer) {
|
2022-08-24 17:27:47 +02:00
|
|
|
ReturnValue_t result = returnvalue::OK;
|
2022-01-17 15:58:27 +01:00
|
|
|
result = init(fullname);
|
2022-08-24 17:27:47 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-02-05 18:08:54 +01:00
|
|
|
sif::warning << "ArcsecJsonParamBase::create: Failed to init parameter command for set "
|
|
|
|
<< setName << std::endl;
|
2021-12-07 16:30:17 +01:00
|
|
|
return result;
|
2022-01-17 15:58:27 +01:00
|
|
|
}
|
|
|
|
result = createCommand(buffer);
|
2022-08-24 17:27:47 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-02-05 18:08:54 +01:00
|
|
|
sif::warning << "ArcsecJsonParamBase::create: Failed to create parameter command for set "
|
|
|
|
<< setName << std::endl;
|
2022-01-28 12:43:26 +01:00
|
|
|
}
|
2022-01-17 15:58:27 +01:00
|
|
|
return result;
|
2021-12-07 16:30:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t ArcsecJsonParamBase::getParam(const std::string name, std::string& value) {
|
2022-01-17 15:58:27 +01:00
|
|
|
for (json::iterator it = set.begin(); it != set.end(); ++it) {
|
|
|
|
if ((*it)[arcseckeys::NAME] == name) {
|
|
|
|
value = (*it)[arcseckeys::VALUE];
|
|
|
|
convertEmpty(value);
|
2022-08-24 17:27:47 +02:00
|
|
|
return returnvalue::OK;
|
2021-12-07 16:30:17 +01:00
|
|
|
}
|
2022-01-17 15:58:27 +01:00
|
|
|
}
|
|
|
|
return PARAM_NOT_EXISTS;
|
2021-12-07 16:30:17 +01:00
|
|
|
}
|
|
|
|
|
2021-12-13 09:40:37 +01:00
|
|
|
void ArcsecJsonParamBase::convertEmpty(std::string& value) {
|
2022-01-17 15:58:27 +01:00
|
|
|
if (value == "") {
|
|
|
|
value = "0";
|
|
|
|
}
|
2021-12-13 09:40:37 +01:00
|
|
|
}
|
|
|
|
|
2021-12-07 16:30:17 +01:00
|
|
|
void ArcsecJsonParamBase::addfloat(const std::string value, uint8_t* buffer) {
|
2022-01-17 15:58:27 +01:00
|
|
|
float param = std::stof(value);
|
|
|
|
std::memcpy(buffer, ¶m, sizeof(param));
|
2021-12-07 16:30:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArcsecJsonParamBase::adduint8(const std::string value, uint8_t* buffer) {
|
2022-01-17 15:58:27 +01:00
|
|
|
uint8_t param = std::stoi(value);
|
|
|
|
std::memcpy(buffer, ¶m, sizeof(param));
|
2021-12-07 16:30:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArcsecJsonParamBase::addint16(const std::string value, uint8_t* buffer) {
|
2022-01-17 15:58:27 +01:00
|
|
|
int16_t param = std::stoi(value);
|
|
|
|
std::memcpy(buffer, ¶m, sizeof(param));
|
2021-12-07 16:30:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArcsecJsonParamBase::adduint16(const std::string value, uint8_t* buffer) {
|
2022-01-17 15:58:27 +01:00
|
|
|
uint16_t param = std::stoi(value);
|
|
|
|
std::memcpy(buffer, ¶m, sizeof(param));
|
2021-12-07 16:30:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArcsecJsonParamBase::adduint32(const std::string value, uint8_t* buffer) {
|
2022-01-17 15:58:27 +01:00
|
|
|
uint32_t param = std::stoi(value);
|
|
|
|
std::memcpy(buffer, ¶m, sizeof(param));
|
2021-12-07 16:30:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArcsecJsonParamBase::addSetParamHeader(uint8_t* buffer, uint8_t setId) {
|
2022-01-17 15:58:27 +01:00
|
|
|
*buffer = static_cast<uint8_t>(TMTC_SETPARAMREQ);
|
|
|
|
*(buffer + 1) = setId;
|
2021-12-07 16:30:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t ArcsecJsonParamBase::init(const std::string filename) {
|
2022-08-24 17:27:47 +02:00
|
|
|
ReturnValue_t result = returnvalue::OK;
|
2022-01-17 15:58:27 +01:00
|
|
|
if (not std::filesystem::exists(filename)) {
|
|
|
|
sif::warning << "ArcsecJsonParamBase::init: JSON file " << filename << " does not exist"
|
|
|
|
<< std::endl;
|
|
|
|
return JSON_FILE_NOT_EXISTS;
|
|
|
|
}
|
|
|
|
createJsonObject(filename);
|
|
|
|
result = initSet();
|
2022-08-24 17:27:47 +02:00
|
|
|
if (result != returnvalue::OK) {
|
2022-01-17 15:58:27 +01:00
|
|
|
return result;
|
|
|
|
}
|
2022-08-24 17:27:47 +02:00
|
|
|
return returnvalue::OK;
|
2021-12-07 16:30:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArcsecJsonParamBase::createJsonObject(const std::string fullname) {
|
2022-01-17 15:58:27 +01:00
|
|
|
json j;
|
|
|
|
std::ifstream file(fullname);
|
|
|
|
file >> j;
|
|
|
|
file.close();
|
|
|
|
properties = j[arcseckeys::PROPERTIES];
|
2021-12-07 16:30:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t ArcsecJsonParamBase::initSet() {
|
2022-01-17 15:58:27 +01:00
|
|
|
for (json::iterator it = properties.begin(); it != properties.end(); ++it) {
|
|
|
|
if ((*it)["name"] == setName) {
|
|
|
|
set = (*it)["fields"];
|
2022-08-24 17:27:47 +02:00
|
|
|
return returnvalue::OK;
|
2021-12-07 16:30:17 +01:00
|
|
|
}
|
2022-01-17 15:58:27 +01:00
|
|
|
}
|
2022-01-29 12:27:26 +01:00
|
|
|
sif::warning << "ArcsecJsonParamBase::initSet: Set " << setName << "not present in json file"
|
2022-02-05 18:08:54 +01:00
|
|
|
<< std::endl;
|
2022-01-17 15:58:27 +01:00
|
|
|
return SET_NOT_EXISTS;
|
2021-12-07 16:30:17 +01:00
|
|
|
}
|