Skip to content

Commit

Permalink
[BT] Enable the publication of non decoded servicedata when several a…
Browse files Browse the repository at this point in the history
…re advertised (#1667)

Before the service data were overiden by the last one, with this modification several mqtt messages will be published, one per service data
  • Loading branch information
1technophile authored Jun 7, 2023
1 parent 53f2677 commit d3a53ce
Showing 1 changed file with 26 additions and 9 deletions.
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

0 comments on commit d3a53ce

Please sign in to comment.