refactor(custom_robot): Simplify SwitchSet command processing
- Removed redundant state retrieval for the SwitchSet command. - Added error handling for missing 'enable' parameter in the command message. - Streamlined the logic for setting the switch state based on the provided parameter.
This commit is contained in:
@@ -525,25 +525,12 @@ bool CustomRobot::processOacCmd(const std::string& cmd, const nlohmann::json& me
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
if (cmd == "SwitchSet") {
|
if (cmd == "SwitchSet") {
|
||||||
// get current state
|
if (message.find("param") == message.end() || message["param"].find("enable") == message["param"].end()) {
|
||||||
bool currentEnable = false;
|
LOG_ERROR("Missing 'enable' parameter for Switch command");
|
||||||
if (!controller_->SwitchGet(currentEnable)) {
|
|
||||||
LOG_ERROR("Failed to get current switch state");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
LOG_INFO("Current switch state: " + std::string(currentEnable ? "true" : "false"));
|
bool enable = message["param"]["enable"];
|
||||||
// switch to opposite state
|
|
||||||
bool enable = !currentEnable;
|
|
||||||
return controller_->SwitchSet(enable);
|
return controller_->SwitchSet(enable);
|
||||||
|
|
||||||
} else if (cmd == "SwitchGet") {
|
|
||||||
bool enable = false;
|
|
||||||
bool result = controller_->SwitchGet(enable);
|
|
||||||
if (result) {
|
|
||||||
LOG_INFO("SwitchGet result: enable=" + std::string(enable ? "true" : "false"));
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
|
|
||||||
} else if (cmd == "Move") {
|
} else if (cmd == "Move") {
|
||||||
if (!message.contains("param")) {
|
if (!message.contains("param")) {
|
||||||
LOG_ERROR("Move cmd missing 'param'");
|
LOG_ERROR("Move cmd missing 'param'");
|
||||||
|
|||||||
Reference in New Issue
Block a user