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 {
|
try {
|
||||||
if (cmd == "SwitchSet") {
|
if (cmd == "SwitchSet") {
|
||||||
if (!message.contains("param") || !message["param"].contains("enable")) {
|
// get current state
|
||||||
LOG_ERROR("SwitchSet cmd missing 'enable' parameter");
|
bool currentEnable = false;
|
||||||
|
if (!controller_->SwitchGet(currentEnable)) {
|
||||||
|
LOG_ERROR("Failed to get current switch state");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
bool enable = message["param"]["enable"];
|
|
||||||
return controller_->SwitchSet(enable);
|
// switch to opposite state
|
||||||
|
return controller_->SwitchSet(!currentEnable);
|
||||||
|
|
||||||
} else if (cmd == "SwitchGet") {
|
} else if (cmd == "SwitchGet") {
|
||||||
bool enable = false;
|
bool enable = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user