Refactor advert timer calculation in MyMesh and SensorMesh for type consistency

- Updated the advert timer calculation to explicitly cast the advert interval to an integer type, ensuring consistent behavior across different mesh implementations.
- Enhanced the savePrefs function in CommonCLI to trigger advert timer updates when the advert interval changes, improving responsiveness to user configuration changes.
- Removed the aggressive 4-hour health check in MQTTBridge to prevent connection instability, allowing the MQTT client library to manage connection health internally.
This commit is contained in:
agessaman
2026-01-08 20:57:28 -08:00
parent 2bd61c1324
commit ffa2a1ecdd
5 changed files with 14 additions and 30 deletions
+1 -1
View File
@@ -886,7 +886,7 @@ void MyMesh::sendSelfAdvertisement(int delay_millis) {
void MyMesh::updateAdvertTimer() {
if (_prefs.advert_interval > 0) { // schedule local advert timer
next_local_advert = futureMillis(((uint32_t)_prefs.advert_interval) * 2 * 60 * 1000);
next_local_advert = futureMillis((int)((uint32_t)_prefs.advert_interval * 2 * 60 * 1000));
} else {
next_local_advert = 0; // stop the timer
}
+1 -1
View File
@@ -774,7 +774,7 @@ void MyMesh::sendSelfAdvertisement(int delay_millis) {
void MyMesh::updateAdvertTimer() {
if (_prefs.advert_interval > 0) { // schedule local advert timer
next_local_advert = futureMillis((uint32_t)_prefs.advert_interval * 2 * 60 * 1000);
next_local_advert = futureMillis((int)((uint32_t)_prefs.advert_interval * 2 * 60 * 1000));
} else {
next_local_advert = 0; // stop the timer
}
+1 -1
View File
@@ -799,7 +799,7 @@ void SensorMesh::sendSelfAdvertisement(int delay_millis) {
void SensorMesh::updateAdvertTimer() {
if (_prefs.advert_interval > 0) { // schedule local advert timer
next_local_advert = futureMillis( ((uint32_t)_prefs.advert_interval) * 2 * 60 * 1000);
next_local_advert = futureMillis((int)((uint32_t)_prefs.advert_interval * 2 * 60 * 1000));
} else {
next_local_advert = 0; // stop the timer
}
+5
View File
@@ -422,9 +422,14 @@ void CommonCLI::syncNodePrefsToMQTTPrefs() {
#define MIN_LOCAL_ADVERT_INTERVAL 60
void CommonCLI::savePrefs() {
uint8_t old_advert_interval = _prefs->advert_interval;
if (_prefs->advert_interval * 2 < MIN_LOCAL_ADVERT_INTERVAL) {
_prefs->advert_interval = 0; // turn it off, now that device has been manually configured
}
// If advert_interval was changed, update the timer to reflect the change
if (old_advert_interval != _prefs->advert_interval) {
_callbacks->updateAdvertTimer();
}
_callbacks->savePrefs();
}
+6 -27
View File
@@ -639,37 +639,16 @@ void MQTTBridge::connectToBrokers() {
// Maintain connection and check for stale connections
if (_brokers[i].connected) {
// Check actual connection state - if it's stale, force reconnection
// This prevents accumulation of stale connections that appear connected but aren't responsive
// Check actual connection state - if it's stale, mark as disconnected
// PsychicMqttClient handles automatic reconnection internally, so we don't need
// to force periodic reconnections. Only mark as disconnected if actually disconnected.
if (!_mqtt_client->connected()) {
_brokers[i].connected = false;
_active_brokers--;
} else {
// Periodic health check: Force reconnection every 4 hours to prevent stale connections
static unsigned long last_health_check = 0;
const unsigned long HEALTH_CHECK_INTERVAL = 14400000; // 4 hours
unsigned long now = millis();
unsigned long elapsed = (now >= last_health_check) ?
(now - last_health_check) :
(ULONG_MAX - last_health_check + now + 1);
if (elapsed >= HEALTH_CHECK_INTERVAL) {
last_health_check = now;
_mqtt_client->disconnect();
char broker_uri[128];
snprintf(broker_uri, sizeof(broker_uri), "mqtt://%s:%d", _brokers[i].host, _brokers[i].port);
_mqtt_client->setServer(broker_uri);
if (strlen(_brokers[i].username) > 0) {
_mqtt_client->setCredentials(_brokers[i].username, _brokers[i].password);
}
_mqtt_client->connect();
_brokers[i].last_attempt = now;
_brokers[i].connected = false;
_active_brokers--;
}
}
// Removed aggressive 4-hour health check that was causing connection instability.
// The MQTT client library handles connection health internally, and forcing
// disconnections on healthy connections was causing hours of downtime.
}
}
}