Skip to content

Commit

Permalink
[BT] Clean KnownBTAction function (#2062)
Browse files Browse the repository at this point in the history
Co-authored-by: Florian <[email protected]>
  • Loading branch information
1technophile and 1technophile authored Sep 17, 2024
1 parent 4b20976 commit 0b4c6ef
Showing 1 changed file with 39 additions and 44 deletions.
83 changes: 39 additions & 44 deletions main/ZgatewayBT.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char*>() && BTdata["model_id"] == "X1") {
strcpy(action.addr, (const char*)BTdata["id"]);
action.write = true;
std::string val = BTdata["cmd"].as<std::string>(); // 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<const char*>()) {
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<const char*>()) {
action.value_type = BLE_VAL_STRING;
std::string val = BTdata["cmd"].as<std::string>(); // 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<int>()) {
action.value_type = BLE_VAL_INT;
res = true;
} else if (BTdata.containsKey("tilt") && BTdata["tilt"].is<const char*>()) {
action.value_type = BLE_VAL_STRING;
} else {
Log.error(F("BLE value type invalid" CR));
return;
res = true;
}
std::string val = BTdata["tilt"].as<std::string>(); // 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<std::string>(); // 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<int>()) {
action.value_type = BLE_VAL_INT;
res = true;
} else if (BTdata.containsKey("position") && BTdata["position"].is<const char*>()) {
action.value_type = BLE_VAL_STRING;
} else {
Log.error(F("BLE value type invalid" CR));
return;
res = true;
}
std::string val = BTdata["position"].as<std::string>(); // 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<std::string>(); // 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;
}
}
}
Expand Down

0 comments on commit 0b4c6ef

Please sign in to comment.