[SYS] Externalize utils function keep cmpToMainTopic (#2048)

And fill generateHash

Co-authored-by: Florian <1technophile@users.noreply.github.com>
This commit is contained in:
FlorianandFlorian authored and GitHub committed 2024-09-04 13:18:41 -05:00
1 parent 5e65e807b7
commit 2b958ae363
23 files changed
+163 -42

No files matched your search

+89
View File
@@ -609,5 +609,94 @@ bool SBBT_connect::processActions(std::vector<BLEAction>& actions) {
return result;
}
/*-----------------------SBCU HANDLING-----------------------*/
void SBCU_connect::notifyCB(NimBLERemoteCharacteristic* pChar, uint8_t* pData, size_t length, bool isNotify) {
if (m_taskHandle == nullptr) {
return; // unexpected notification
}
if (!BTProcessLock) {
Log.trace(F("Callback from %s characteristic" CR), pChar->getUUID().toString().c_str());
if (length) {
m_notifyVal = *pData;
} else {
Log.notice(F("Invalid notification data" CR));
return;
}
} else {
Log.trace(F("Callback process canceled by BTProcessLock" CR));
}
xTaskNotifyGive(m_taskHandle);
}
bool SBCU_connect::processActions(std::vector<BLEAction>& actions) {
NimBLEUUID serviceUUID("cba20d00-224d-11e6-9fb8-0002a5d5c51b");
NimBLEUUID charUUID("cba20002-224d-11e6-9fb8-0002a5d5c51b");
NimBLEUUID notifyCharUUID("cba20003-224d-11e6-9fb8-0002a5d5c51b");
static byte CLOSE[] = {0x57, 0x0f, 0x45, 0x01, 0x01, 0x01, 0x64};
static byte OPEN[] = {0x57, 0x0f, 0x45, 0x01, 0x01, 0x01, 0x00};
static byte MOVE[] = {0x57, 0x0f, 0x45, 0x01, 0x01, 0x01, 0x00};
static byte STOP[] = {0x57, 0x0f, 0x45, 0x01, 0x01, 0x00, 0x01};
bool result = false;
if (actions.size() > 0) {
for (auto& it : actions) {
if (NimBLEAddress(it.addr) == m_pClient->getPeerAddress()) {
NimBLERemoteCharacteristic* pChar = getCharacteristic(serviceUUID, charUUID);
NimBLERemoteCharacteristic* pNotifyChar = getCharacteristic(serviceUUID, notifyCharUUID);
int value = -1;
if (it.value_type == BLE_VAL_INT) {
value = std::stoi(it.value);
}
if (it.write && pChar && pNotifyChar) {
Log.trace(F("processing Switchbot %s" CR), it.value.c_str());
if (pNotifyChar->subscribe(true,
std::bind(&SBCU_connect::notifyCB,
this, std::placeholders::_1, std::placeholders::_2,
std::placeholders::_3, std::placeholders::_4),
true)) {
if (it.value == "open" && it.value_type == BLE_VAL_STRING) {
result = pChar->writeValue(OPEN, 7, false);
value = 100;
} else if (it.value == "close" && it.value_type == BLE_VAL_STRING) {
result = pChar->writeValue(CLOSE, 7, false);
value = 0;
} else if (it.value == "stop" && it.value_type == BLE_VAL_STRING) {
result = pChar->writeValue(STOP, 7, false);
} else if (it.value_type == BLE_VAL_INT) {
if (value >= 0 && value <= 100) {
byte posByte = (byte)value;
MOVE[6] = posByte;
result = pChar->writeValue(MOVE, 7, false);
}
}
if (result) {
m_taskHandle = xTaskGetCurrentTaskHandle();
if (ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(BLE_CNCT_TIMEOUT)) == pdFALSE) {
m_taskHandle = nullptr;
}
result = m_notifyVal == 0x01;
}
}
}
it.complete = result;
if (result || it.ttl <= 1) {
StaticJsonDocument<JSON_MSG_BUFFER> BLEdataBuffer;
JsonObject BLEdata = BLEdataBuffer.to<JsonObject>();
BLEdata["id"] = it.addr;
BLEdata["position"] = value;
buildTopicFromId(BLEdata, subjectBTtoMQTT);
enqueueJsonObject(BLEdata, QueueSemaphoreTimeOutTask);
}
}
}
}
return result;
}
# endif //ZgatewayBT
#endif //ESP32