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:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user