From e3ba6f722596b09fdbcff7fb7a9607801e2c6f95 Mon Sep 17 00:00:00 2001 From: Sucan126 <632190820@qq.com> Date: Tue, 23 Sep 2025 19:09:13 +0800 Subject: [PATCH] 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. --- src/custom_robot.cpp | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/src/custom_robot.cpp b/src/custom_robot.cpp index 5426ab7..b651f81 100644 --- a/src/custom_robot.cpp +++ b/src/custom_robot.cpp @@ -525,25 +525,12 @@ bool CustomRobot::processOacCmd(const std::string& cmd, const nlohmann::json& me try { if (cmd == "SwitchSet") { - // get current state - bool currentEnable = false; - if (!controller_->SwitchGet(currentEnable)) { - LOG_ERROR("Failed to get current switch state"); + if (message.find("param") == message.end() || message["param"].find("enable") == message["param"].end()) { + LOG_ERROR("Missing 'enable' parameter for Switch command"); return false; } - LOG_INFO("Current switch state: " + std::string(currentEnable ? "true" : "false")); - // switch to opposite state - bool enable = !currentEnable; + bool enable = message["param"]["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") { if (!message.contains("param")) { LOG_ERROR("Move cmd missing 'param'");