From a64acab7c895ffee307fe14d6cf076a463343b24 Mon Sep 17 00:00:00 2001 From: Florian Date: Thu, 26 Jan 2023 08:18:44 -0600 Subject: [PATCH] [BT] Regular BLE system parameter pub (#1422) Publish BT config parameter at start so as to update the switch statuses in the controller Also publish regularly in case of controller restart Also move btqsum, btqsnd and btqavg to BTtoMQTT topic instead of SYStoMQTT as they are more relevant here --- main/ZgatewayBT.ino | 75 ++++++++++++++++++++++++++++++--------------- main/main.ino | 12 ++------ 2 files changed, 53 insertions(+), 34 deletions(-) diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index 2ceef40..2c6fd8d 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -111,6 +111,38 @@ void BTConfig_update(JsonObject& data, const char* key, T& var) { } } +void stateBTMeasures(bool start) { + StaticJsonDocument jsonBuffer; + JsonObject jo = jsonBuffer.to(); + jo["bleconnect"] = BTConfig.bleConnect; + jo["interval"] = BTConfig.BLEinterval; + jo["activescan"] = BTConfig.activeScan; + jo["scanbcnct"] = BTConfig.BLEscanBeforeConnect; + jo["onlysensors"] = BTConfig.pubOnlySensors; + jo["hasspresence"] = BTConfig.presenceEnable; + jo["presenceTopic"] = BTConfig.presenceTopic; + jo["presenceUseBeaconUuid"] = BTConfig.presenceUseBeaconUuid; + jo["minrssi"] = -abs(BTConfig.minRssi); // Always export as negative value + jo["extDecoderEnable"] = BTConfig.extDecoderEnable; + jo["extDecoderTopic"] = BTConfig.extDecoderTopic; + jo["filterConnectable"] = BTConfig.filterConnectable; + jo["pubadvdata"] = BTConfig.pubAdvData; + jo["pubBeaconUuidForTopic"] = BTConfig.pubBeaconUuidForTopic; + jo["ignoreWBlist"] = BTConfig.ignoreWBlist; + jo["btqblck"] = btQueueBlocked; + jo["btqsum"] = btQueueLengthSum; + jo["btqsnd"] = btQueueLengthCount; + jo["btqavg"] = (btQueueLengthCount > 0 ? btQueueLengthSum / (float)btQueueLengthCount : 0); + + if (start) { + Log.notice(F("BT sys: ")); + serializeJsonPretty(jsonBuffer, Serial); + Serial.println(); + return; // Do not try to erase/write/send config at startup + } + pub(subjectBTtoMQTT, jo); +} + void BTConfig_fromJson(JsonObject& BTdata, bool startup = false) { // Attempts to connect to eligible devices or not BTConfig_update(BTdata, "bleconnect", BTConfig.bleConnect); @@ -144,31 +176,7 @@ void BTConfig_fromJson(JsonObject& BTdata, bool startup = false) { // Disable Whitelist & Blacklist BTConfig_update(BTdata, "ignoreWBlist", (BTConfig.ignoreWBlist)); - StaticJsonDocument jsonBuffer; - JsonObject jo = jsonBuffer.to(); - jo["bleconnect"] = BTConfig.bleConnect; - jo["interval"] = BTConfig.BLEinterval; - jo["activescan"] = BTConfig.activeScan; - jo["scanbcnct"] = BTConfig.BLEscanBeforeConnect; - jo["onlysensors"] = BTConfig.pubOnlySensors; - jo["hasspresence"] = BTConfig.presenceEnable; - jo["presenceTopic"] = BTConfig.presenceTopic; - jo["presenceUseBeaconUuid"] = BTConfig.presenceUseBeaconUuid; - jo["minrssi"] = -abs(BTConfig.minRssi); // Always export as negative value - jo["extDecoderEnable"] = BTConfig.extDecoderEnable; - jo["extDecoderTopic"] = BTConfig.extDecoderTopic; - jo["filterConnectable"] = BTConfig.filterConnectable; - jo["pubadvdata"] = BTConfig.pubAdvData; - jo["pubBeaconUuidForTopic"] = BTConfig.pubBeaconUuidForTopic; - jo["ignoreWBlist"] = BTConfig.ignoreWBlist; - - if (startup) { - Log.notice(F("BT config: ")); - serializeJsonPretty(jsonBuffer, Serial); - Serial.println(); - return; // Do not try to erase/write/send config at startup - } - pub(subjectBTtoMQTT, jo); + stateBTMeasures(startup); if (BTdata.containsKey("erase") && BTdata["erase"].as()) { // Erase config from NVS (non-volatile storage) @@ -180,6 +188,23 @@ void BTConfig_fromJson(JsonObject& BTdata, bool startup = false) { } if (BTdata.containsKey("save") && BTdata["save"].as()) { + StaticJsonDocument jsonBuffer; + JsonObject jo = jsonBuffer.to(); + jo["bleconnect"] = BTConfig.bleConnect; + jo["interval"] = BTConfig.BLEinterval; + jo["activescan"] = BTConfig.activeScan; + jo["scanbcnct"] = BTConfig.BLEscanBeforeConnect; + jo["onlysensors"] = BTConfig.pubOnlySensors; + jo["hasspresence"] = BTConfig.presenceEnable; + jo["presenceTopic"] = BTConfig.presenceTopic; + jo["presenceUseBeaconUuid"] = BTConfig.presenceUseBeaconUuid; + jo["minrssi"] = -abs(BTConfig.minRssi); // Always export as negative value + jo["extDecoderEnable"] = BTConfig.extDecoderEnable; + jo["extDecoderTopic"] = BTConfig.extDecoderTopic; + jo["filterConnectable"] = BTConfig.filterConnectable; + jo["pubadvdata"] = BTConfig.pubAdvData; + jo["pubBeaconUuidForTopic"] = BTConfig.pubBeaconUuidForTopic; + jo["ignoreWBlist"] = BTConfig.ignoreWBlist; // Save config into NVS (non-volatile storage) String conf = ""; serializeJson(jsonBuffer, conf); diff --git a/main/main.ino b/main/main.ino index 42d8335..52a6e78 100644 --- a/main/main.ino +++ b/main/main.ino @@ -1492,6 +1492,9 @@ void loop() { if (now > (timer_sys_measures + (TimeBetweenReadingSYS * 1000)) || !timer_sys_measures) { timer_sys_measures = millis(); stateMeasures(); +# ifdef ZgatewayBT + stateBTMeasures(false); +# endif } #endif #ifdef ZsensorBME280 @@ -1562,12 +1565,7 @@ void loop() { if (disc) launchBTDiscovery(publishDiscovery); # endif -# ifndef ESP32 - if (BTtoMQTT()) - Log.trace(F("BTtoMQTT OK" CR)); -# else emptyBTQueue(); -# endif #endif #ifdef ZgatewaySRFB SRFBtoMQTT(); @@ -1705,10 +1703,6 @@ void stateMeasures() { # ifdef ZgatewayBT # ifdef ESP32 SYSdata["lowpowermode"] = (int)lowpowermode; - SYSdata["btqblck"] = btQueueBlocked; - SYSdata["btqsum"] = btQueueLengthSum; - SYSdata["btqsnd"] = btQueueLengthCount; - SYSdata["btqavg"] = (btQueueLengthCount > 0 ? btQueueLengthSum / (float)btQueueLengthCount : 0); # endif SYSdata["interval"] = BTConfig.BLEinterval; SYSdata["scanbcnct"] = BTConfig.BLEscanBeforeConnect;