Skip to content

Commit

Permalink
improved readability
Browse files Browse the repository at this point in the history
according to code-style

Contributes to CURA-10561
  • Loading branch information
jellespijker committed Oct 12, 2023
1 parent 3f593af commit e0bb17c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ NLOHMANN_JSON_SERIALIZE_ENUM(

} // namespace botcmd

namespace miracle_jtp::kKeyStr
namespace miracle_jtp::k_key_str
{

constexpr static std::string_view a{ "a" };
Expand All @@ -60,9 +60,9 @@ constexpr static std::string_view value{ "value" };
constexpr static std::string_view x{ "x" };
constexpr static std::string_view y{ "y" };
constexpr static std::string_view z{ "z" };
constexpr static std::array<const std::string_view, 5> kParamPointNames{ x, y, z, a, b };
constexpr static std::array<const std::string_view, 5> k_param_point_names{ x, y, z, a, b };

} // namespace miracle_jtp::kKeyStr
} // namespace miracle_jtp::k_key_str


} // namespace dulcificum
Expand Down
38 changes: 19 additions & 19 deletions src/miracle_jtp/mgjtp_command_to_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ nlohmann::json getCommandMetadata(const Command& cmd)
if (cmd.type == CommandType::kMove)
{
auto move = static_cast<const Move&>(cmd);
jmetadata["relative"] = zipListsToJson(kKeyStr::kParamPointNames, move.is_point_relative);
jmetadata["relative"] = zipListsToJson(k_key_str::k_param_point_names, move.is_point_relative);
return jmetadata;
}
return jmetadata;
Expand All @@ -51,54 +51,54 @@ nlohmann::json getCommandParameters(const Command& cmd)
if (cmd.type == CommandType::kMove)
{
const auto move = static_cast<const Move&>(cmd);
jparams = zipListsToJson(kKeyStr::kParamPointNames, move.point);
jparams[kKeyStr::feedrate] = move.feedrate;
jparams = zipListsToJson(k_key_str::k_param_point_names, move.point);
jparams[k_key_str::feedrate] = move.feedrate;
return jparams;
}
if (cmd.type == CommandType::kComment)
{
const auto com = static_cast<const Comment&>(cmd);
jparams[kKeyStr::comment] = com.comment;
jparams[k_key_str::comment] = com.comment;
return jparams;
}
if (cmd.type == CommandType::kActiveFanDuty)
{
const auto dut = static_cast<const FanDuty&>(cmd);
jparams[kKeyStr::index] = dut.index;
jparams[kKeyStr::value] = dut.duty;
jparams[k_key_str::index] = dut.index;
jparams[k_key_str::value] = dut.duty;
return jparams;
}
if (cmd.type == CommandType::kActiveFanEnable)
{
const auto fan = static_cast<const FanToggle&>(cmd);
jparams[kKeyStr::index] = fan.index;
jparams[kKeyStr::value] = fan.is_on;
jparams[k_key_str::index] = fan.index;
jparams[k_key_str::value] = fan.is_on;
return jparams;
}
if (cmd.type == CommandType::kSetTemperature)
{
const auto dcmd = static_cast<const SetTemperature&>(cmd);
jparams[kKeyStr::index] = dcmd.index;
jparams[kKeyStr::temperature] = dcmd.temperature;
jparams[k_key_str::index] = dcmd.index;
jparams[k_key_str::temperature] = dcmd.temperature;
return jparams;
}
if (cmd.type == CommandType::kWaitForTemperature)
{
const auto dcmd = static_cast<const WaitForTemperature&>(cmd);
jparams[kKeyStr::index] = dcmd.index;
jparams[k_key_str::index] = dcmd.index;
return jparams;
}
if (cmd.type == CommandType::kChangeTool)
{
const auto dcmd = static_cast<const ChangeTool&>(cmd);
jparams = zipListsIgnoreNan(kKeyStr::kParamPointNames, dcmd.position);
jparams[kKeyStr::index] = dcmd.index;
jparams = zipListsIgnoreNan(k_key_str::k_param_point_names, dcmd.position);
jparams[k_key_str::index] = dcmd.index;
return jparams;
}
if (cmd.type == CommandType::kDelay)
{
const auto dcmd = static_cast<const Delay&>(cmd);
jparams[kKeyStr::seconds] = dcmd.seconds;
jparams[k_key_str::seconds] = dcmd.seconds;
return jparams;
}
return jparams;
Expand All @@ -107,12 +107,12 @@ nlohmann::json getCommandParameters(const Command& cmd)
nlohmann::json toJson(const Command& cmd)
{
nlohmann::json jcmd;
jcmd[kKeyStr::function] = cmd.type;
jcmd[kKeyStr::metadata] = getCommandMetadata(cmd);
jcmd[kKeyStr::parameters] = getCommandParameters(cmd);
jcmd[kKeyStr::tags] = cmd.tags;
jcmd[k_key_str::function] = cmd.type;
jcmd[k_key_str::metadata] = getCommandMetadata(cmd);
jcmd[k_key_str::parameters] = getCommandParameters(cmd);
jcmd[k_key_str::tags] = cmd.tags;
nlohmann::json jout;
jout[kKeyStr::command] = jcmd;
jout[k_key_str::command] = jcmd;
return jout;
}
} // namespace dulcificum::miracle_jtp
54 changes: 27 additions & 27 deletions src/miracle_jtp/mgjtp_json_to_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ using namespace botcmd;
std::shared_ptr<Move> toMove(const nlohmann::json& jmove)
{
auto move = std::make_shared<Move>();
const auto& jparams = jmove.at(kKeyStr::parameters);
move->feedrate = jparams.at(kKeyStr::feedrate);
const auto& jrelative = jmove.at(kKeyStr::metadata).at("relative");
for (size_t param_ii = 0; param_ii < kKeyStr::kParamPointNames.size(); param_ii++)
const auto& jparams = jmove.at(k_key_str::parameters);
move->feedrate = jparams.at(k_key_str::feedrate);
const auto& jrelative = jmove.at(k_key_str::metadata).at("relative");
for (size_t param_ii = 0; param_ii < k_key_str::k_param_point_names.size(); param_ii++)
{
const auto& key = kKeyStr::kParamPointNames[param_ii];
const auto& key = k_key_str::k_param_point_names[param_ii];
double pval = jparams.at(key);
move->point[param_ii] = pval;
bool is_rel = jrelative.at(key);
move->is_point_relative[param_ii] = is_rel;
}
std::vector<Tag> tags = jmove.at(kKeyStr::tags);
std::vector<Tag> tags = jmove.at(k_key_str::tags);
move->tags = std::move(tags);
return move;
}
Expand All @@ -29,47 +29,47 @@ CommandPtr toParamOnlyCommand(const CommandType type, const nlohmann::json& jpar
if (type == CommandType::kComment)
{
auto com = std::make_shared<Comment>();
com->comment = jparam.at(kKeyStr::comment);
com->comment = jparam.at(k_key_str::comment);
return com;
}
if (type == CommandType::kActiveFanDuty)
{
auto cmd = std::make_shared<FanDuty>();
cmd->index = jparam[kKeyStr::index];
cmd->duty = jparam[kKeyStr::value];
cmd->index = jparam[k_key_str::index];
cmd->duty = jparam[k_key_str::value];
return cmd;
}
if (type == CommandType::kActiveFanEnable)
{
auto cmd = std::make_shared<FanToggle>();
cmd->index = jparam[kKeyStr::index];
cmd->is_on = jparam[kKeyStr::value];
cmd->index = jparam[k_key_str::index];
cmd->is_on = jparam[k_key_str::value];
return cmd;
}
if (type == CommandType::kSetTemperature)
{
auto cmd = std::make_shared<SetTemperature>();
cmd->index = jparam[kKeyStr::index];
cmd->temperature = jparam[kKeyStr::temperature];
cmd->index = jparam[k_key_str::index];
cmd->temperature = jparam[k_key_str::temperature];
return cmd;
}
if (type == CommandType::kWaitForTemperature)
{
auto cmd = std::make_shared<WaitForTemperature>();
cmd->index = jparam[kKeyStr::index];
cmd->index = jparam[k_key_str::index];
return cmd;
}
if (type == CommandType::kChangeTool)
{
auto cmd = std::make_shared<SetTemperature>();
cmd->index = jparam[kKeyStr::index];
cmd->temperature = jparam[kKeyStr::temperature];
cmd->index = jparam[k_key_str::index];
cmd->temperature = jparam[k_key_str::temperature];
return cmd;
}
if (type == CommandType::kDelay)
{
auto cmd = std::make_shared<Delay>();
cmd->seconds = jparam[kKeyStr::seconds];
cmd->seconds = jparam[k_key_str::seconds];
return cmd;
}
return spawnCommandPtr(type);
Expand All @@ -78,25 +78,25 @@ CommandPtr toParamOnlyCommand(const CommandType type, const nlohmann::json& jpar
CommandPtr toChangeTool(const nlohmann::json& jcmd)
{
auto cmd = std::make_shared<ChangeTool>();
auto jparam = jcmd.at(kKeyStr::parameters);
cmd->index = jparam.at(kKeyStr::index);
for (size_t param_ii = 0; param_ii < kKeyStr::kParamPointNames.size(); param_ii++)
auto jparam = jcmd.at(k_key_str::parameters);
cmd->index = jparam.at(k_key_str::index);
for (size_t param_ii = 0; param_ii < k_key_str::k_param_point_names.size(); param_ii++)
{
const auto& key = kKeyStr::kParamPointNames[param_ii];
const auto& key = k_key_str::k_param_point_names[param_ii];
if (jparam.contains(key))
{
cmd->position[param_ii] = jparam.at(key);
}
}
std::vector<Tag> tags = jcmd.at(kKeyStr::tags);
std::vector<Tag> tags = jcmd.at(k_key_str::tags);
cmd->tags = std::move(tags);
return cmd;
}

CommandPtr toCommand(const nlohmann::json& jin)
{
auto jcmd = jin[kKeyStr::command];
CommandType type = jcmd[kKeyStr::function];
auto jcmd = jin[k_key_str::command];
CommandType type = jcmd[k_key_str::function];
if (type == CommandType::kMove)
{
return toMove(jcmd);
Expand All @@ -105,11 +105,11 @@ CommandPtr toCommand(const nlohmann::json& jin)
{
return toChangeTool(jcmd);
}
if (jcmd.contains(kKeyStr::parameters))
if (jcmd.contains(k_key_str::parameters))
{
auto jparam = jcmd[kKeyStr::parameters];
auto jparam = jcmd[k_key_str::parameters];
auto cmd = toParamOnlyCommand(type, jparam);
std::vector<Tag> tags = jcmd.at(kKeyStr::tags);
std::vector<Tag> tags = jcmd.at(k_key_str::tags);
cmd->tags = std::move(tags);
return cmd;
}
Expand Down

0 comments on commit e0bb17c

Please sign in to comment.