eive-obsw/bsp_q7s/devices/startracker/ArcsecJsonParamBase.cpp

96 lines
2.6 KiB
C++
Raw Normal View History

#include "ArcsecJsonParamBase.h"
2022-01-18 11:41:19 +01:00
#include "ArcsecJsonKeys.h"
ArcsecJsonParamBase::ArcsecJsonParamBase(std::string setName) : setName(setName) {}
ReturnValue_t ArcsecJsonParamBase::create(std::string fullname, uint8_t* buffer) {
2022-01-18 11:41:19 +01:00
ReturnValue_t result = RETURN_OK;
result = init(fullname);
if (result != RETURN_OK) {
return result;
2022-01-18 11:41:19 +01:00
}
result = createCommand(buffer);
return result;
}
ReturnValue_t ArcsecJsonParamBase::getParam(const std::string name, std::string& value) {
2022-01-18 11:41:19 +01:00
for (json::iterator it = set.begin(); it != set.end(); ++it) {
if ((*it)[arcseckeys::NAME] == name) {
value = (*it)[arcseckeys::VALUE];
convertEmpty(value);
return RETURN_OK;
}
2022-01-18 11:41:19 +01:00
}
return PARAM_NOT_EXISTS;
}
void ArcsecJsonParamBase::convertEmpty(std::string& value) {
2022-01-18 11:41:19 +01:00
if (value == "") {
value = "0";
}
}
void ArcsecJsonParamBase::addfloat(const std::string value, uint8_t* buffer) {
2022-01-18 11:41:19 +01:00
float param = std::stof(value);
std::memcpy(buffer, &param, sizeof(param));
}
void ArcsecJsonParamBase::adduint8(const std::string value, uint8_t* buffer) {
2022-01-18 11:41:19 +01:00
uint8_t param = std::stoi(value);
std::memcpy(buffer, &param, sizeof(param));
}
void ArcsecJsonParamBase::addint16(const std::string value, uint8_t* buffer) {
2022-01-18 11:41:19 +01:00
int16_t param = std::stoi(value);
std::memcpy(buffer, &param, sizeof(param));
}
void ArcsecJsonParamBase::adduint16(const std::string value, uint8_t* buffer) {
2022-01-18 11:41:19 +01:00
uint16_t param = std::stoi(value);
std::memcpy(buffer, &param, sizeof(param));
}
void ArcsecJsonParamBase::adduint32(const std::string value, uint8_t* buffer) {
2022-01-18 11:41:19 +01:00
uint32_t param = std::stoi(value);
std::memcpy(buffer, &param, sizeof(param));
}
void ArcsecJsonParamBase::addSetParamHeader(uint8_t* buffer, uint8_t setId) {
2022-01-18 11:41:19 +01:00
*buffer = static_cast<uint8_t>(TMTC_SETPARAMREQ);
*(buffer + 1) = setId;
}
ReturnValue_t ArcsecJsonParamBase::init(const std::string filename) {
2022-01-18 11:41:19 +01:00
ReturnValue_t result = RETURN_OK;
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();
if (result != RETURN_OK) {
return result;
}
return RETURN_OK;
}
void ArcsecJsonParamBase::createJsonObject(const std::string fullname) {
2022-01-18 11:41:19 +01:00
json j;
std::ifstream file(fullname);
file >> j;
file.close();
properties = j[arcseckeys::PROPERTIES];
}
ReturnValue_t ArcsecJsonParamBase::initSet() {
2022-01-18 11:41:19 +01:00
for (json::iterator it = properties.begin(); it != properties.end(); ++it) {
if ((*it)["name"] == setName) {
set = (*it)["fields"];
return RETURN_OK;
}
2022-01-18 11:41:19 +01:00
}
return SET_NOT_EXISTS;
}