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.

This commit is contained in:
2025-09-09 11:20:10 +08:00
parent f46bb3b7c9
commit 347240026c

View File

@@ -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;