From 347240026c85fe40f70e53eac8f4c298392faad5 Mon Sep 17 00:00:00 2001 From: Sucan126 <632190820@qq.com> Date: Tue, 9 Sep 2025 11:20:10 +0800 Subject: [PATCH] Refactor SwitchSet command handling in CustomRobot class. Updated logic to retrieve the current switch state before toggling, improving error handling and ensuring correct state management. --- src/custom_robot.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/custom_robot.cpp b/src/custom_robot.cpp index eba76c0..0cbff79 100644 --- a/src/custom_robot.cpp +++ b/src/custom_robot.cpp @@ -262,12 +262,15 @@ bool CustomRobot::processOacCmd(const std::string& cmd, const nlohmann::json& me try { if (cmd == "SwitchSet") { - if (!message.contains("param") || !message["param"].contains("enable")) { - LOG_ERROR("SwitchSet cmd missing 'enable' parameter"); + // get current state + bool currentEnable = false; + if (!controller_->SwitchGet(currentEnable)) { + LOG_ERROR("Failed to get current switch state"); return false; } - bool enable = message["param"]["enable"]; - return controller_->SwitchSet(enable); + + // switch to opposite state + return controller_->SwitchSet(!currentEnable); } else if (cmd == "SwitchGet") { bool enable = false;