Skip to content

Commit

Permalink
[BT] Regular BLE system parameter pub
Browse files Browse the repository at this point in the history
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
  • Loading branch information
1technophile committed Jan 25, 2023
1 parent c1afc02 commit bd5b369
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 34 deletions.
75 changes: 50 additions & 25 deletions main/ZgatewayBT.ino
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,38 @@ void BTConfig_update(JsonObject& data, const char* key, T& var) {
}
}

void stateBTMeasures(bool start) {
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
JsonObject jo = jsonBuffer.to<JsonObject>();
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);
Expand Down Expand Up @@ -144,31 +176,7 @@ void BTConfig_fromJson(JsonObject& BTdata, bool startup = false) {
// Disable Whitelist & Blacklist
BTConfig_update(BTdata, "ignoreWBlist", (BTConfig.ignoreWBlist));

StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
JsonObject jo = jsonBuffer.to<JsonObject>();
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<bool>()) {
// Erase config from NVS (non-volatile storage)
Expand All @@ -180,6 +188,23 @@ void BTConfig_fromJson(JsonObject& BTdata, bool startup = false) {
}

if (BTdata.containsKey("save") && BTdata["save"].as<bool>()) {
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
JsonObject jo = jsonBuffer.to<JsonObject>();
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);
Expand Down
12 changes: 3 additions & 9 deletions main/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit bd5b369

Please sign in to comment.