From 0b4c6ef10762529240810dd758357c5609ee566d Mon Sep 17 00:00:00 2001 From: Florian Date: Tue, 17 Sep 2024 10:58:21 -0400 Subject: [PATCH] [BT] Clean KnownBTAction function (#2062) Co-authored-by: Florian <1technophile@users.noreply.github.com> --- main/ZgatewayBT.ino | 83 +++++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 44 deletions(-) diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index 765f54c219..d594c448fe 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -1387,67 +1387,62 @@ void startBTActionTask() { # if BLEDecoder void KnownBTActions(JsonObject& BTdata) { - BLEAction action; - memset(&action, 0, sizeof(BLEAction)); - if (BTdata.containsKey("model_id") && BTdata["model_id"].is() && BTdata["model_id"] == "X1") { - strcpy(action.addr, (const char*)BTdata["id"]); - action.write = true; - std::string val = BTdata["cmd"].as(); // Fix #1694 - action.value = val; - action.ttl = 3; - createOrUpdateDevice(action.addr, device_flags_connect, - TheengsDecoder::BLE_ID_NUM::SBS1, 1); - BLEactions.push_back(action); - startBTActionTask(); + if (!BTdata.containsKey("id")) { + Log.error(F("BLE mac address missing" CR)); + gatewayState = GatewayState::ERROR; return; } + BLEAction action; + memset(&action, 0, sizeof(BLEAction)); + strcpy(action.addr, (const char*)BTdata["id"]); + action.write = true; + action.ttl = 3; + bool res = false; if (BTdata.containsKey("model_id") && BTdata["model_id"].is()) { - if (BTdata["model_id"] == "W270160X") { - if (!BTdata.containsKey("id")) { - Log.error(F("BLE mac address missing" CR)); - return; + if (BTdata["model_id"] == "X1") { + if (BTdata.containsKey("cmd") && BTdata["cmd"].is()) { + action.value_type = BLE_VAL_STRING; + std::string val = BTdata["cmd"].as(); // Fix #1694 + action.value = val; + createOrUpdateDevice(action.addr, device_flags_connect, + TheengsDecoder::BLE_ID_NUM::SBS1, 1); + res = true; } - strcpy(action.addr, (const char*)BTdata["id"]); - action.write = true; + } else if (BTdata["model_id"] == "W270160X") { if (BTdata.containsKey("tilt") && BTdata["tilt"].is()) { action.value_type = BLE_VAL_INT; + res = true; } else if (BTdata.containsKey("tilt") && BTdata["tilt"].is()) { action.value_type = BLE_VAL_STRING; - } else { - Log.error(F("BLE value type invalid" CR)); - return; + res = true; } - std::string val = BTdata["tilt"].as(); // Fix #1694 - action.value = val; - action.ttl = 3; - createOrUpdateDevice(action.addr, device_flags_connect, - TheengsDecoder::BLE_ID_NUM::SBBT, 1); - BLEactions.push_back(action); - startBTActionTask(); - return; - } else if (BTdata["model_id"] == "W070160X") { - if (!BTdata.containsKey("id")) { - Log.error(F("BLE mac address missing" CR)); - return; + if (res) { + std::string val = BTdata["tilt"].as(); // Fix #1694 + action.value = val; + createOrUpdateDevice(action.addr, device_flags_connect, + TheengsDecoder::BLE_ID_NUM::SBBT, 1); } - strcpy(action.addr, (const char*)BTdata["id"]); - action.write = true; + } else if (BTdata["model_id"] == "W070160X") { if (BTdata.containsKey("position") && BTdata["position"].is()) { action.value_type = BLE_VAL_INT; + res = true; } else if (BTdata.containsKey("position") && BTdata["position"].is()) { action.value_type = BLE_VAL_STRING; - } else { - Log.error(F("BLE value type invalid" CR)); - return; + res = true; } - std::string val = BTdata["position"].as(); // Fix #1694 - action.value = val; - action.ttl = 3; - createOrUpdateDevice(action.addr, device_flags_connect, - TheengsDecoder::BLE_ID_NUM::SBCU, 1); + if (res) { + std::string val = BTdata["position"].as(); // Fix #1694 + action.value = val; + createOrUpdateDevice(action.addr, device_flags_connect, + TheengsDecoder::BLE_ID_NUM::SBCU, 1); + } + } + if (res) { BLEactions.push_back(action); startBTActionTask(); - return; + } else { + Log.error(F("BLE action not recognized" CR)); + gatewayState = GatewayState::ERROR; } } }