Bt discovery fix (#880)
* launchDiscovery fix for esp32 (sending the discovery mqtt messages on the main core)
This commit is contained in:
1 parent
cd282596e0
commit
ebd6bb8031
4 files changed
+52
-18
No files matched your search
+45
-17
@@ -82,7 +82,8 @@ struct decompose {
|
|||||||
bool reverse;
|
bool reverse;
|
||||||
};
|
};
|
||||||
|
|
||||||
vector<BLEdevice> devices;
|
vector<BLEdevice*> devices;
|
||||||
|
int newDevices = 0;
|
||||||
|
|
||||||
static BLEdevice NO_DEVICE_FOUND = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false, false, false, UNKNOWN_MODEL};
|
static BLEdevice NO_DEVICE_FOUND = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, false, false, false, UNKNOWN_MODEL};
|
||||||
static bool oneWhite = false;
|
static bool oneWhite = false;
|
||||||
@@ -194,9 +195,9 @@ void createOrUpdateDevice(const char* mac, uint8_t flags, ble_sensor_model model
|
|||||||
BLEdevice* getDeviceByMac(const char* mac) {
|
BLEdevice* getDeviceByMac(const char* mac) {
|
||||||
Log.trace(F("getDeviceByMac %s" CR), mac);
|
Log.trace(F("getDeviceByMac %s" CR), mac);
|
||||||
|
|
||||||
for (vector<BLEdevice>::iterator p = devices.begin(); p != devices.end(); ++p) {
|
for (vector<BLEdevice*>::iterator it = devices.begin(); it != devices.end(); ++it) {
|
||||||
if ((strcmp(p->macAdr, mac) == 0)) {
|
if ((strcmp((*it)->macAdr, mac) == 0)) {
|
||||||
return &(*p);
|
return *it;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return &NO_DEVICE_FOUND;
|
return &NO_DEVICE_FOUND;
|
||||||
@@ -234,7 +235,8 @@ void createOrUpdateDevice(const char* mac, uint8_t flags, ble_sensor_model model
|
|||||||
device->isWhtL = flags & device_flags_isWhiteL;
|
device->isWhtL = flags & device_flags_isWhiteL;
|
||||||
device->isBlkL = flags & device_flags_isBlackL;
|
device->isBlkL = flags & device_flags_isBlackL;
|
||||||
if (model != UNKNOWN_MODEL) device->sensorModel = model;
|
if (model != UNKNOWN_MODEL) device->sensorModel = model;
|
||||||
devices.push_back(*device);
|
devices.push_back(device);
|
||||||
|
newDevices++;
|
||||||
} else {
|
} else {
|
||||||
Log.trace(F("update %s" CR), mac);
|
Log.trace(F("update %s" CR), mac);
|
||||||
|
|
||||||
@@ -263,7 +265,8 @@ void createOrUpdateDevice(const char* mac, uint8_t flags, ble_sensor_model model
|
|||||||
# define isDiscovered(device) device->isDisc
|
# define isDiscovered(device) device->isDisc
|
||||||
|
|
||||||
void dumpDevices() {
|
void dumpDevices() {
|
||||||
for (vector<BLEdevice>::iterator p = devices.begin(); p != devices.end(); ++p) {
|
for (vector<BLEdevice*>::iterator it = devices.begin(); it != devices.end(); ++it) {
|
||||||
|
BLEdevice* p = *it;
|
||||||
Log.trace(F("macAdr %s" CR), p->macAdr);
|
Log.trace(F("macAdr %s" CR), p->macAdr);
|
||||||
Log.trace(F("isDisc %d" CR), p->isDisc);
|
Log.trace(F("isDisc %d" CR), p->isDisc);
|
||||||
Log.trace(F("isWhtL %d" CR), p->isWhtL);
|
Log.trace(F("isWhtL %d" CR), p->isWhtL);
|
||||||
@@ -625,7 +628,8 @@ void notifyCB(
|
|||||||
JsonObject& BLEdata = getBTJsonObject();
|
JsonObject& BLEdata = getBTJsonObject();
|
||||||
String mac_adress = pBLERemoteCharacteristic->getRemoteService()->getClient()->getPeerAddress().toString().c_str();
|
String mac_adress = pBLERemoteCharacteristic->getRemoteService()->getClient()->getPeerAddress().toString().c_str();
|
||||||
mac_adress.toUpperCase();
|
mac_adress.toUpperCase();
|
||||||
for (vector<BLEdevice>::iterator p = devices.begin(); p != devices.end(); ++p) {
|
for (vector<BLEdevice*>::iterator it = devices.begin(); it != devices.end(); ++it) {
|
||||||
|
BLEdevice* p = *it;
|
||||||
if ((strcmp(p->macAdr, (char*)mac_adress.c_str()) == 0)) {
|
if ((strcmp(p->macAdr, (char*)mac_adress.c_str()) == 0)) {
|
||||||
if (p->sensorModel == LYWSD03MMC)
|
if (p->sensorModel == LYWSD03MMC)
|
||||||
BLEdata.set("model", "LYWSD03MMC");
|
BLEdata.set("model", "LYWSD03MMC");
|
||||||
@@ -657,7 +661,8 @@ void notifyCB(
|
|||||||
void BLEconnect() {
|
void BLEconnect() {
|
||||||
Log.notice(F("BLE Connect begin" CR));
|
Log.notice(F("BLE Connect begin" CR));
|
||||||
BLEDevice::init("");
|
BLEDevice::init("");
|
||||||
for (vector<BLEdevice>::iterator p = devices.begin(); p != devices.end(); ++p) {
|
for (vector<BLEdevice*>::iterator it = devices.begin(); it != devices.end(); ++it) {
|
||||||
|
BLEdevice* p = *it;
|
||||||
if (p->sensorModel == LYWSD03MMC || p->sensorModel == MHO_C401) {
|
if (p->sensorModel == LYWSD03MMC || p->sensorModel == MHO_C401) {
|
||||||
Log.trace(F("Model to connect found" CR));
|
Log.trace(F("Model to connect found" CR));
|
||||||
NimBLEClient* pClient;
|
NimBLEClient* pClient;
|
||||||
@@ -730,10 +735,6 @@ void coreTask(void* pvParameters) {
|
|||||||
// Launching a connect every BLEscanBeforeConnect
|
// Launching a connect every BLEscanBeforeConnect
|
||||||
if (!(scanCount % BLEscanBeforeConnect) || scanCount == 1)
|
if (!(scanCount % BLEscanBeforeConnect) || scanCount == 1)
|
||||||
BLEconnect();
|
BLEconnect();
|
||||||
# ifdef ZmqttDiscovery
|
|
||||||
if (disc)
|
|
||||||
launchDiscovery();
|
|
||||||
# endif
|
|
||||||
dumpDevices();
|
dumpDevices();
|
||||||
}
|
}
|
||||||
if (lowpowermode) {
|
if (lowpowermode) {
|
||||||
@@ -961,8 +962,22 @@ boolean valid_service_data(const char* data) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void launchDiscovery() {
|
// This function always should be called from the main core as it generates direct mqtt messages
|
||||||
for (vector<BLEdevice>::iterator p = devices.begin(); p != devices.end(); ++p) {
|
void launchBTDiscovery() {
|
||||||
|
if (newDevices == 0)
|
||||||
|
return;
|
||||||
|
# ifdef ESP32
|
||||||
|
if (!semaphoreCreateOrUpdateDevice.take(1000, "launchBTDiscovery"))
|
||||||
|
return;
|
||||||
|
newDevices = 0;
|
||||||
|
vector<BLEdevice*> localDevices = devices;
|
||||||
|
semaphoreCreateOrUpdateDevice.give();
|
||||||
|
for (vector<BLEdevice*>::iterator it = localDevices.begin(); it != localDevices.end(); ++it) {
|
||||||
|
# else
|
||||||
|
newDevices = 0;
|
||||||
|
for (vector<BLEdevice*>::iterator it = devices.begin(); it != devices.end(); ++it) {
|
||||||
|
# endif
|
||||||
|
BLEdevice* p = *it;
|
||||||
if (p->sensorModel != UNKNOWN_MODEL && !isDiscovered(p)) {
|
if (p->sensorModel != UNKNOWN_MODEL && !isDiscovered(p)) {
|
||||||
String macWOdots = String(p->macAdr);
|
String macWOdots = String(p->macAdr);
|
||||||
macWOdots.replace(":", "");
|
macWOdots.replace(":", "");
|
||||||
@@ -983,9 +998,14 @@ void launchDiscovery() {
|
|||||||
if (p->sensorModel == LYWSD03MMC || p->sensorModel == LYWSD03MMC_ATC) LYWSD03MMCDiscovery((char*)macWOdots.c_str(), "LYWSD03MMC");
|
if (p->sensorModel == LYWSD03MMC || p->sensorModel == LYWSD03MMC_ATC) LYWSD03MMCDiscovery((char*)macWOdots.c_str(), "LYWSD03MMC");
|
||||||
if (p->sensorModel == MHO_C401) MHO_C401Discovery((char*)macWOdots.c_str(), "MHO_C401");
|
if (p->sensorModel == MHO_C401) MHO_C401Discovery((char*)macWOdots.c_str(), "MHO_C401");
|
||||||
if (p->sensorModel == INODE_EM) INodeEMDiscovery((char*)macWOdots.c_str(), "INODE_EM");
|
if (p->sensorModel == INODE_EM) INodeEMDiscovery((char*)macWOdots.c_str(), "INODE_EM");
|
||||||
createOrUpdateDevice(p->macAdr, device_flags_isDisc, p->sensorModel);
|
p->isDisc = true; // we don't need the semaphore and all the search magic via createOrUpdateDevice
|
||||||
} else {
|
} else {
|
||||||
Log.trace(F("Device already discovered or UNKNOWN_MODEL" CR));
|
if (!isDiscovered(p)) {
|
||||||
|
Log.trace(F("Device UNKNOWN_MODEL %s" CR), p->macAdr);
|
||||||
|
p->isDisc = true;
|
||||||
|
} else {
|
||||||
|
Log.trace(F("Device already discovered %s" CR), p->macAdr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1416,8 +1436,16 @@ void MQTTtoBT(char* topicOri, JsonObject& BTdata) { // json object decoding
|
|||||||
WorBupdated |= updateWorB(BTdata, true);
|
WorBupdated |= updateWorB(BTdata, true);
|
||||||
WorBupdated |= updateWorB(BTdata, false);
|
WorBupdated |= updateWorB(BTdata, false);
|
||||||
|
|
||||||
if (WorBupdated)
|
if (WorBupdated) {
|
||||||
|
# ifdef ESP32
|
||||||
|
if (!semaphoreCreateOrUpdateDevice.take(1000, "dumpDevices")) {
|
||||||
|
dumpDevices();
|
||||||
|
semaphoreCreateOrUpdateDevice.give();
|
||||||
|
}
|
||||||
|
# else
|
||||||
dumpDevices();
|
dumpDevices();
|
||||||
|
# endif
|
||||||
|
}
|
||||||
|
|
||||||
// Scan interval set
|
// Scan interval set
|
||||||
if (BTdata.containsKey("interval")) {
|
if (BTdata.containsKey("interval")) {
|
||||||
|
|||||||
@@ -137,7 +137,7 @@ void createDiscovery(char* sensor_type,
|
|||||||
deviceid[12] = '\0';
|
deviceid[12] = '\0';
|
||||||
StaticJsonBuffer<JSON_MSG_BUFFER> jsonDeviceBuffer;
|
StaticJsonBuffer<JSON_MSG_BUFFER> jsonDeviceBuffer;
|
||||||
JsonObject& device = jsonDeviceBuffer.createObject();
|
JsonObject& device = jsonDeviceBuffer.createObject();
|
||||||
if (device_mac != "") {
|
if (device_mac[0] != 0) {
|
||||||
JsonArray& connections = device.createNestedArray("connections");
|
JsonArray& connections = device.createNestedArray("connections");
|
||||||
JsonArray& connection_mac = connections.createNestedArray();
|
JsonArray& connection_mac = connections.createNestedArray();
|
||||||
connection_mac.add("mac");
|
connection_mac.add("mac");
|
||||||
|
|||||||
@@ -29,6 +29,8 @@
|
|||||||
extern void setupBT();
|
extern void setupBT();
|
||||||
extern bool BTtoMQTT();
|
extern bool BTtoMQTT();
|
||||||
extern void MQTTtoBT(char* topicOri, JsonObject& RFdata);
|
extern void MQTTtoBT(char* topicOri, JsonObject& RFdata);
|
||||||
|
extern void emptyBTQueue();
|
||||||
|
extern void launchBTDiscovery();
|
||||||
|
|
||||||
#ifdef ESP32
|
#ifdef ESP32
|
||||||
extern int btQueueBlocked;
|
extern int btQueueBlocked;
|
||||||
|
|||||||
@@ -1319,6 +1319,10 @@ void loop() {
|
|||||||
PilighttoMQTT();
|
PilighttoMQTT();
|
||||||
#endif
|
#endif
|
||||||
#ifdef ZgatewayBT
|
#ifdef ZgatewayBT
|
||||||
|
# ifdef ZmqttDiscovery
|
||||||
|
if (disc)
|
||||||
|
launchBTDiscovery();
|
||||||
|
# endif
|
||||||
# ifndef ESP32
|
# ifndef ESP32
|
||||||
if (BTtoMQTT())
|
if (BTtoMQTT())
|
||||||
Log.trace(F("BTtoMQTT OK" CR));
|
Log.trace(F("BTtoMQTT OK" CR));
|
||||||
|
|||||||
Reference in new issue
Block a user