Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BT] Enable the publication of non decoded servicedata when several are advertised #1667

Merged
merged 1 commit into from
Jun 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions main/ZgatewayBT.ino
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ public:
}
};

void PublishDeviceData(JsonObject& BLEdata, bool processBLEData = true);
void PublishDeviceData(JsonObject& BLEdata);

atomic_int forceBTScan;

Expand Down Expand Up @@ -732,16 +732,33 @@ void procBLETask(void* pvParameters) {
for (int j = 0; j < serviceDataCount; j++) {
std::string service_data = convertServiceData(advertisedDevice->getServiceData(j));
Log.trace(F("Service data: %s" CR), service_data.c_str());
BLEdata["servicedata"] = (char*)service_data.c_str();
std::string serviceDatauuid = advertisedDevice->getServiceDataUUID(j).toString();
Log.trace(F("Service data UUID: %s" CR), (char*)serviceDatauuid.c_str());
BLEdata["servicedatauuid"] = (char*)serviceDatauuid.c_str();
process_bledata(BLEdata); // this will force to resolve all the service data
if (j) {
// if we have more than one service data, we need to create a new BLEdata object to publish the data to MQTT
// we can't use the same BLEdata object because its servicedata would be overwritten by the next service data
// the decoder does not handle multiple servicedata in the same message
// note that we keep the decoded data during the different iterations of the loop,
// the goal is to avoid issue with certain controllers that do not handle multiple messages with undecoded data (OpenHAB for example)
JsonObject& BLEdataTemp = getBTJsonObject();
bool ret = BLEdataTemp.set(BLEdata);
if (!ret) {
Log.error(F("BLEdataTemp.set(BLEdata) failed" CR));
} else {
BLEdataTemp.remove("servicedata");
BLEdataTemp["servicedata"] = (char*)service_data.c_str();
BLEdataTemp.remove("servicedatauuid");
BLEdataTemp["servicedatauuid"] = (char*)serviceDatauuid.c_str();
PublishDeviceData(BLEdataTemp);
}
} else {
BLEdata["servicedata"] = (char*)service_data.c_str();
BLEdata["servicedatauuid"] = (char*)serviceDatauuid.c_str();
PublishDeviceData(BLEdata);
}
}

PublishDeviceData(BLEdata, false);
} else {
PublishDeviceData(BLEdata); // PublishDeviceData has its own logic whether it needs to publish the json or not
PublishDeviceData(BLEdata);
}
} else {
Log.trace(F("Filtered MAC device" CR));
Expand Down Expand Up @@ -1159,9 +1176,9 @@ void launchBTDiscovery(bool overrideDiscovery) {
}
# endif

void PublishDeviceData(JsonObject& BLEdata, bool processBLEData) {
void PublishDeviceData(JsonObject& BLEdata) {
if (abs((int)BLEdata["rssi"] | 0) < abs(BTConfig.minRssi)) { // process only the devices close enough
if (processBLEData) process_bledata(BLEdata);
process_bledata(BLEdata);
if (!BTConfig.pubRandomMACs && (BLEdata["type"].as<string>()).compare("RMAC") == 0) {
return;
}
Expand Down