[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
This commit is contained in:
1 parent
150603555c
commit
a64acab7c8
2 files changed
+47
-28
No files matched your search
+44
-19
@@ -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);
|
||||
@@ -144,6 +176,18 @@ void BTConfig_fromJson(JsonObject& BTdata, bool startup = false) {
|
||||
// Disable Whitelist & Blacklist
|
||||
BTConfig_update(BTdata, "ignoreWBlist", (BTConfig.ignoreWBlist));
|
||||
|
||||
stateBTMeasures(startup);
|
||||
|
||||
if (BTdata.containsKey("erase") && BTdata["erase"].as<bool>()) {
|
||||
// Erase config from NVS (non-volatile storage)
|
||||
preferences.begin(Gateway_Short_Name, false);
|
||||
preferences.remove("BTConfig");
|
||||
preferences.end();
|
||||
Log.notice(F("BT config erased" CR));
|
||||
return; // Erase prevails on save, so skipping save
|
||||
}
|
||||
|
||||
if (BTdata.containsKey("save") && BTdata["save"].as<bool>()) {
|
||||
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
|
||||
JsonObject jo = jsonBuffer.to<JsonObject>();
|
||||
jo["bleconnect"] = BTConfig.bleConnect;
|
||||
@@ -161,25 +205,6 @@ void BTConfig_fromJson(JsonObject& BTdata, bool startup = false) {
|
||||
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);
|
||||
|
||||
if (BTdata.containsKey("erase") && BTdata["erase"].as<bool>()) {
|
||||
// Erase config from NVS (non-volatile storage)
|
||||
preferences.begin(Gateway_Short_Name, false);
|
||||
preferences.remove("BTConfig");
|
||||
preferences.end();
|
||||
Log.notice(F("BT config erased" CR));
|
||||
return; // Erase prevails on save, so skipping save
|
||||
}
|
||||
|
||||
if (BTdata.containsKey("save") && BTdata["save"].as<bool>()) {
|
||||
// Save config into NVS (non-volatile storage)
|
||||
String conf = "";
|
||||
serializeJson(jsonBuffer, conf);
|
||||
|
||||
+3
-9
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user