Add low battery notification channel with threshold alerts

This commit is contained in:
shortwavesurfer2009 committed 2026-10-04 01:26:36 -04:00
1 parent 9334d556fe
commit 296eab20ca
23 files changed
+327 -1

No files matched your search

+27
View File
@@ -266,6 +266,10 @@ class MeshCoreConnector extends ChangeNotifier {
Stopwatch? _airtimeBumpStopwatch;
int _prevTotalAirSecs = 0;
int? _batteryMillivolts;
// Thresholds (percent) that trigger a low-battery notification once per
// crossing, re-arming 5 points above the threshold after recharge.
static const List<int> _lowBatteryThresholds = [25, 10];
final Set<int> _batteryAlertedThresholds = {};
double? _selfLatitude;
double? _selfLongitude;
final List<DirectRepeater> _directRepeaters = List.empty(growable: true);
@@ -3139,6 +3143,7 @@ class MeshCoreConnector extends ChangeNotifier {
_autoAddMaxHops = null;
_contactsStorageFull = false;
_batteryMillivolts = null;
_batteryAlertedThresholds.clear();
_repeaterBatterySnapshots.clear();
_batteryRequested = false;
_awaitingSelfInfo = false;
@@ -5357,6 +5362,7 @@ class MeshCoreConnector extends ChangeNotifier {
_batteryMillivolts = reader.readUInt16LE();
_storageUsedKb = reader.readUInt32LE();
_storageTotalKb = reader.readUInt32LE();
_checkLowBatteryThresholds();
final volts = (_batteryMillivolts! / 1000.0).toStringAsFixed(2);
_appDebugLogService?.info(
'Pulled battery: $volts V ($_batteryMillivolts mV)',
@@ -5371,6 +5377,27 @@ class MeshCoreConnector extends ChangeNotifier {
}
}
void _checkLowBatteryThresholds() {
final percent = batteryPercent;
if (percent == null) return;
for (final threshold in _lowBatteryThresholds) {
if (percent <= threshold &&
!_batteryAlertedThresholds.contains(threshold)) {
_batteryAlertedThresholds.add(threshold);
NotificationService().showLowBatteryNotification(
batteryPercent: percent,
threshold: threshold,
);
} else if (percent > threshold + 5 &&
_batteryAlertedThresholds.contains(threshold)) {
// Re-arm once the battery recovers 5 points above the threshold,
// so a recharge + discharge cycle alerts again without flapping.
_batteryAlertedThresholds.remove(threshold);
}
}
}
void _checkManualAddContacts() async {
// If manual add contacts is enabled, set auto add config and other params.
// and disable it after