Skip to content

Commit 9836d37

Browse files
committed
Basically what's happening is the app fires off a new noise floor request every 5 seconds, but it doesn't check if the last one is still running. So sometimes two requests end up overlapping. When the older one eventually times out, it goes "oh something's wrong" and shuts down the whole polling loop, even though everything was actually working fine.
1 parent b1c1cf7 commit 9836d37

File tree

2 files changed

+29246
-4
lines changed

2 files changed

+29246
-4
lines changed

lib/services/meshcore/connection.dart

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ class MeshCoreConnection {
9494
// Noise floor tracking
9595
int? _lastNoiseFloor; // dBm or null if not supported
9696
Timer? _noiseFloorTimer;
97+
bool _isFetchingNoiseFloor = false;
98+
int _noiseFloorFailCount = 0;
9799

98100
// Battery tracking
99101
int? _lastBatteryMilliVolts; // millivolts or null if not supported
@@ -944,6 +946,8 @@ class MeshCoreConnection {
944946
// Check if firmware supports noise floor (v1.11.0+)
945947
// For now, we'll try and handle errors gracefully
946948
_noiseFloorTimer?.cancel();
949+
_isFetchingNoiseFloor = false;
950+
_noiseFloorFailCount = 0;
947951

948952
// Get initial reading immediately
949953
_fetchNoiseFloor();
@@ -956,21 +960,29 @@ class MeshCoreConnection {
956960
}
957961

958962
Future<void> _fetchNoiseFloor() async {
963+
if (_isFetchingNoiseFloor) return; // Skip if previous fetch still in flight
964+
_isFetchingNoiseFloor = true;
959965
try {
960966
debugLog('[CONN] Fetching noise floor...');
961967
await getNoiseFloor();
968+
_noiseFloorFailCount = 0; // Reset on success
962969
} catch (e) {
963-
// Firmware may not support noise floor - don't spam logs
964-
debugLog('[CONN] Noise floor fetch failed: $e');
965-
// Stop polling if consistently failing
966-
_stopNoiseFloorPolling();
970+
_noiseFloorFailCount++;
971+
debugLog('[CONN] Noise floor fetch failed ($_noiseFloorFailCount/3): $e');
972+
if (_noiseFloorFailCount >= 3) {
973+
debugLog('[CONN] Noise floor polling stopped after 3 consecutive failures');
974+
_stopNoiseFloorPolling();
975+
}
976+
} finally {
977+
_isFetchingNoiseFloor = false;
967978
}
968979
}
969980

970981
/// Stop noise floor polling
971982
void _stopNoiseFloorPolling() {
972983
_noiseFloorTimer?.cancel();
973984
_noiseFloorTimer = null;
985+
_isFetchingNoiseFloor = false;
974986
debugLog('[CONN] Stopped noise floor polling');
975987
}
976988

0 commit comments

Comments
 (0)