Re-publish BTtoMQTT Theengs discovery topics on MQTT re-connect (#1290)
* Re-publish BTtoMQTT Theengs discovery topics on MQTT re-connect - Improved version of #1274, this should re-publish more - Handle return value of (MQTT) client.loop() to detect connection loss faster Signed-off-by: Sami Salonen <ssalonen@gmail.com>
This commit is contained in:
1 parent
8bc65d62dc
commit
941b1e109f
3 files changed
+20
-21
No files matched your search
+5
-4
@@ -853,8 +853,9 @@ boolean valid_service_data(const char* data, int size) {
|
||||
|
||||
# ifdef ZmqttDiscovery
|
||||
// This function always should be called from the main core as it generates direct mqtt messages
|
||||
void launchBTDiscovery() {
|
||||
if (newDevices == 0)
|
||||
// When overrideDiscovery=true, we publish discovery messages of known devices (even if no new)
|
||||
void launchBTDiscovery(bool overrideDiscovery) {
|
||||
if (!overrideDiscovery && newDevices == 0)
|
||||
return;
|
||||
if (xSemaphoreTake(semaphoreCreateOrUpdateDevice, pdMS_TO_TICKS(1000)) == pdFALSE) {
|
||||
Log.error(F("Semaphore NOT taken" CR));
|
||||
@@ -866,8 +867,8 @@ void launchBTDiscovery() {
|
||||
for (vector<BLEdevice*>::iterator it = localDevices.begin(); it != localDevices.end(); ++it) {
|
||||
BLEdevice* p = *it;
|
||||
Log.trace(F("Device mac %s" CR), p->macAdr);
|
||||
// Do not launch discovery for the devices already discovered or that are not unique by their MAC Address (Ibeacon, GAEN and Microsoft Cdp)
|
||||
if (!isDiscovered(p) &&
|
||||
// Do not launch discovery for the devices already discovered (unless we have overrideDiscovery) or that are not unique by their MAC Address (Ibeacon, GAEN and Microsoft Cdp)
|
||||
if ((overrideDiscovery || !isDiscovered(p)) &&
|
||||
p->sensorModel_id != TheengsDecoder::BLE_ID_NUM::IBEACON &&
|
||||
p->sensorModel_id != TheengsDecoder::BLE_ID_NUM::MS_CDP &&
|
||||
p->sensorModel_id != TheengsDecoder::BLE_ID_NUM::GAEN) {
|
||||
|
||||
+1
-1
@@ -30,7 +30,7 @@ extern void setupBT();
|
||||
extern bool BTtoMQTT();
|
||||
extern void MQTTtoBT(char* topicOri, JsonObject& RFdata);
|
||||
extern void emptyBTQueue();
|
||||
extern void launchBTDiscovery();
|
||||
extern void launchBTDiscovery(bool overrideDiscovery);
|
||||
|
||||
#ifdef ESP32
|
||||
extern int btQueueBlocked;
|
||||
|
||||
+14
-16
@@ -180,7 +180,7 @@ char WifiManager_ssid[MAC_NAME_MAX_LEN];
|
||||
char ota_hostname[MAC_NAME_MAX_LEN];
|
||||
#endif
|
||||
bool connectedOnce = false; //indicate if we have been connected once to MQTT
|
||||
bool justReconnected = false; //indicate when we have just re-connected to MQTT
|
||||
bool connected = false; //indicate whether we are currently connected. Used to detected re-connection
|
||||
int failure_number_ntwk = 0; // number of failure connecting to network
|
||||
int failure_number_mqtt = 0; // number of failure connecting to MQTT
|
||||
#ifdef ZmqttDiscovery
|
||||
@@ -1374,23 +1374,22 @@ void loop() {
|
||||
if ((Ethernet.hardwareStatus() != EthernetW5100 && Ethernet.linkStatus() == LinkON) || (Ethernet.hardwareStatus() == EthernetW5100)) { //we are able to detect disconnection only on w5200 and w5500
|
||||
#endif
|
||||
failure_number_ntwk = 0;
|
||||
if (client.connected()) {
|
||||
if (client.loop()) { // MQTT client is still connected
|
||||
digitalWrite(LED_INFO, LED_INFO_ON);
|
||||
failure_number_ntwk = 0;
|
||||
|
||||
client.loop();
|
||||
// We have just re-connected if connected was previously false
|
||||
bool justReconnected = !connected;
|
||||
|
||||
#ifdef ZmqttDiscovery
|
||||
if (disc) {
|
||||
if (!connectedOnce || (discovery_republish_on_reconnect && !justReconnected)) {
|
||||
// at first connection we publish the discovery payloads
|
||||
// or, when we have just re-connected (only when discovery_republish_on_reconnect is enabled)
|
||||
pubMqttDiscovery();
|
||||
}
|
||||
// at first connection we publish the discovery payloads
|
||||
// or, when we have just re-connected (only when discovery_republish_on_reconnect is enabled)
|
||||
bool publishDiscovery = disc && (!connectedOnce || (discovery_republish_on_reconnect && justReconnected));
|
||||
if (publishDiscovery) {
|
||||
pubMqttDiscovery();
|
||||
}
|
||||
#endif
|
||||
connectedOnce = true;
|
||||
justReconnected = true;
|
||||
connected = true;
|
||||
#if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
|
||||
if (now > (timer_sys_measures + (TimeBetweenReadingSYS * 1000)) || !timer_sys_measures) {
|
||||
timer_sys_measures = millis();
|
||||
@@ -1460,7 +1459,7 @@ void loop() {
|
||||
#ifdef ZgatewayBT
|
||||
# ifdef ZmqttDiscovery
|
||||
if (disc)
|
||||
launchBTDiscovery();
|
||||
launchBTDiscovery(publishDiscovery);
|
||||
# endif
|
||||
# ifndef ESP32
|
||||
if (BTtoMQTT())
|
||||
@@ -1496,13 +1495,12 @@ void loop() {
|
||||
RTL_433Loop();
|
||||
#endif
|
||||
} else {
|
||||
// MQTT disconnected, we reset the justReconnected flag
|
||||
justReconnected = false;
|
||||
// MQTT disconnected
|
||||
connected = false;
|
||||
connectMQTT();
|
||||
}
|
||||
} else { // disconnected from network
|
||||
// Network disconnected, we reset the justReconnected flag
|
||||
justReconnected = false;
|
||||
connected = false;
|
||||
Log.warning(F("Network disconnected:" CR));
|
||||
digitalWrite(LED_ERROR, LED_ERROR_ON);
|
||||
delay(2000); // add a delay to avoid ESP32 crash and reset
|
||||
|
||||
Reference in new issue
Block a user