Fix review findings in low battery alerts
- Respect the notifications-enabled setting - Notify only the lowest newly crossed threshold - Ignore implausible readings (boards without a battery sensor report 0 mV) - Wait for the device identity so the saved chemistry is used - Require two consecutive low readings before alerting - Re-arm and clear alerts on any link reset, not just explicit disconnect - Cancel stale notifications on re-arm and link reset - Scope notification ids per device and name the device in the body - Use the connector's shared NotificationService
This commit is contained in:
1 parent
7d55bf03df
commit
b6c1d73bf0
22 files changed
+138
-52
No files matched your search
@@ -302,6 +302,18 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
// crossing, re-arming 5 points above the threshold after recharge.
|
||||
static const List<int> _lowBatteryThresholds = [25, 10];
|
||||
final Set<int> _batteryAlertedThresholds = {};
|
||||
// Consecutive readings that newly crossed a threshold. Alerts need
|
||||
// [_lowBatteryConfirmReadings] in a row so a single TX-load voltage sag
|
||||
// doesn't notify.
|
||||
int _lowBatteryReadingStreak = 0;
|
||||
// Device key the posted low-battery notifications were scoped to, kept so
|
||||
// they can still be cancelled after the session's identity is cleared.
|
||||
String? _lowBatteryAlertDeviceKey;
|
||||
static const int _lowBatteryConfirmReadings = 2;
|
||||
// Readings below this are treated as "no battery sensor" (boards without
|
||||
// one report 0 mV). Any single Li cell, including LiFePO4 (2600 mV empty),
|
||||
// is protection-cut well above 2 V, so lower values are not real readings.
|
||||
static const int _minPlausibleBatteryMillivolts = 2000;
|
||||
double? _selfLatitude;
|
||||
double? _selfLongitude;
|
||||
final List<DirectRepeater> _directRepeaters = List.empty(growable: true);
|
||||
@@ -3722,7 +3734,6 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
_allowedRepeatFreqRanges = null;
|
||||
_autoAddMaxHops = null;
|
||||
_contactsStorageFull = false;
|
||||
_batteryAlertedThresholds.clear();
|
||||
_repeaterBatterySnapshots.clear();
|
||||
|
||||
_activeTransport = MeshCoreTransportType.bluetooth;
|
||||
@@ -6492,24 +6503,61 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
}
|
||||
|
||||
void _checkLowBatteryThresholds() {
|
||||
final millivolts = _batteryMillivolts;
|
||||
if (millivolts == null || millivolts < _minPlausibleBatteryMillivolts) {
|
||||
return;
|
||||
}
|
||||
// Until the device identity (and so its saved chemistry) is known, the
|
||||
// percentage falls back to NMC and can read low for e.g. LiFePO4 packs.
|
||||
// Skip evaluation; the next battery poll re-checks once identity is set.
|
||||
if (batteryDeviceKey == null || _appSettingsService == null) return;
|
||||
final percent = batteryPercent;
|
||||
if (percent == null) return;
|
||||
|
||||
// Only the lowest newly crossed threshold notifies; higher ones crossed
|
||||
// in the same reading are marked as alerted silently.
|
||||
final newlyCrossed = <int>[];
|
||||
for (final threshold in _lowBatteryThresholds) {
|
||||
if (percent <= threshold &&
|
||||
!_batteryAlertedThresholds.contains(threshold)) {
|
||||
_batteryAlertedThresholds.add(threshold);
|
||||
NotificationService().showLowBatteryNotification(
|
||||
batteryPercent: percent,
|
||||
threshold: threshold,
|
||||
);
|
||||
newlyCrossed.add(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);
|
||||
final alertDeviceKey = _lowBatteryAlertDeviceKey;
|
||||
if (alertDeviceKey != null) {
|
||||
unawaited(
|
||||
_notificationService.clearLowBatteryNotifications(alertDeviceKey, [
|
||||
threshold,
|
||||
]),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (newlyCrossed.isEmpty) {
|
||||
_lowBatteryReadingStreak = 0;
|
||||
return;
|
||||
}
|
||||
// Debounce: wait for consecutive low readings before alerting.
|
||||
if (++_lowBatteryReadingStreak < _lowBatteryConfirmReadings) return;
|
||||
_lowBatteryReadingStreak = 0;
|
||||
_batteryAlertedThresholds.addAll(newlyCrossed);
|
||||
final deviceKey = batteryDeviceKey!;
|
||||
_lowBatteryAlertDeviceKey = deviceKey;
|
||||
final lowestNewlyCrossed = newlyCrossed.reduce((a, b) => a < b ? a : b);
|
||||
if (!(_appSettingsService?.settings.notificationsEnabled ?? false)) {
|
||||
return;
|
||||
}
|
||||
unawaited(
|
||||
_notificationService.showLowBatteryNotification(
|
||||
deviceKey: deviceKey,
|
||||
deviceName: deviceDisplayName,
|
||||
batteryPercent: percent,
|
||||
threshold: lowestNewlyCrossed,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _checkManualAddContacts() async {
|
||||
@@ -8595,6 +8643,18 @@ class MeshCoreConnector extends ChangeNotifier {
|
||||
_hasReceivedDeviceInfo = false;
|
||||
_batteryMillivolts = null;
|
||||
_batteryRequested = false;
|
||||
final alertDeviceKey = _lowBatteryAlertDeviceKey;
|
||||
if (alertDeviceKey != null && _batteryAlertedThresholds.isNotEmpty) {
|
||||
unawaited(
|
||||
_notificationService.clearLowBatteryNotifications(
|
||||
alertDeviceKey,
|
||||
_batteryAlertedThresholds.toList(),
|
||||
),
|
||||
);
|
||||
}
|
||||
_batteryAlertedThresholds.clear();
|
||||
_lowBatteryAlertDeviceKey = null;
|
||||
_lowBatteryReadingStreak = 0;
|
||||
_pathHashByteWidth = 1;
|
||||
_maxContacts = _defaultMaxContacts;
|
||||
_maxChannels = _defaultMaxChannels;
|
||||
|
||||
+5
-2
@@ -2682,10 +2682,13 @@
|
||||
"@notification_lowBatteryTitle": {
|
||||
"description": "Title of the low battery notification"
|
||||
},
|
||||
"notification_lowBatteryBody": "Device battery is at {percent}%",
|
||||
"notification_lowBatteryBody": "{deviceName} battery is at {percent}%",
|
||||
"@notification_lowBatteryBody": {
|
||||
"description": "Body of the low battery notification",
|
||||
"description": "Body of the low battery notification; deviceName is the connected radio's node name",
|
||||
"placeholders": {
|
||||
"deviceName": {
|
||||
"type": "String"
|
||||
},
|
||||
"percent": {
|
||||
"type": "int"
|
||||
}
|
||||
|
||||
@@ -8114,11 +8114,11 @@ abstract class AppLocalizations {
|
||||
/// **'Low battery'**
|
||||
String get notification_lowBatteryTitle;
|
||||
|
||||
/// Body of the low battery notification
|
||||
/// Body of the low battery notification; deviceName is the connected radio's node name
|
||||
///
|
||||
/// In en, this message translates to:
|
||||
/// **'Device battery is at {percent}%'**
|
||||
String notification_lowBatteryBody(int percent);
|
||||
/// **'{deviceName} battery is at {percent}%'**
|
||||
String notification_lowBatteryBody(String deviceName, int percent);
|
||||
|
||||
/// No description provided for @notification_messagesCount.
|
||||
///
|
||||
|
||||
@@ -4731,8 +4731,8 @@ class AppLocalizationsBg extends AppLocalizations {
|
||||
String get notification_lowBatteryTitle => 'Low battery';
|
||||
|
||||
@override
|
||||
String notification_lowBatteryBody(int percent) {
|
||||
return 'Device battery is at $percent%';
|
||||
String notification_lowBatteryBody(String deviceName, int percent) {
|
||||
return '$deviceName battery is at $percent%';
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -4742,8 +4742,8 @@ class AppLocalizationsDe extends AppLocalizations {
|
||||
String get notification_lowBatteryTitle => 'Low battery';
|
||||
|
||||
@override
|
||||
String notification_lowBatteryBody(int percent) {
|
||||
return 'Device battery is at $percent%';
|
||||
String notification_lowBatteryBody(String deviceName, int percent) {
|
||||
return '$deviceName battery is at $percent%';
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -4666,8 +4666,8 @@ class AppLocalizationsEn extends AppLocalizations {
|
||||
String get notification_lowBatteryTitle => 'Low battery';
|
||||
|
||||
@override
|
||||
String notification_lowBatteryBody(int percent) {
|
||||
return 'Device battery is at $percent%';
|
||||
String notification_lowBatteryBody(String deviceName, int percent) {
|
||||
return '$deviceName battery is at $percent%';
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -4733,8 +4733,8 @@ class AppLocalizationsEs extends AppLocalizations {
|
||||
String get notification_lowBatteryTitle => 'Low battery';
|
||||
|
||||
@override
|
||||
String notification_lowBatteryBody(int percent) {
|
||||
return 'Device battery is at $percent%';
|
||||
String notification_lowBatteryBody(String deviceName, int percent) {
|
||||
return '$deviceName battery is at $percent%';
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -4752,8 +4752,8 @@ class AppLocalizationsFr extends AppLocalizations {
|
||||
String get notification_lowBatteryTitle => 'Low battery';
|
||||
|
||||
@override
|
||||
String notification_lowBatteryBody(int percent) {
|
||||
return 'Device battery is at $percent%';
|
||||
String notification_lowBatteryBody(String deviceName, int percent) {
|
||||
return '$deviceName battery is at $percent%';
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -4730,8 +4730,8 @@ class AppLocalizationsHu extends AppLocalizations {
|
||||
String get notification_lowBatteryTitle => 'Low battery';
|
||||
|
||||
@override
|
||||
String notification_lowBatteryBody(int percent) {
|
||||
return 'Device battery is at $percent%';
|
||||
String notification_lowBatteryBody(String deviceName, int percent) {
|
||||
return '$deviceName battery is at $percent%';
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -4739,8 +4739,8 @@ class AppLocalizationsIt extends AppLocalizations {
|
||||
String get notification_lowBatteryTitle => 'Low battery';
|
||||
|
||||
@override
|
||||
String notification_lowBatteryBody(int percent) {
|
||||
return 'Device battery is at $percent%';
|
||||
String notification_lowBatteryBody(String deviceName, int percent) {
|
||||
return '$deviceName battery is at $percent%';
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -4502,8 +4502,8 @@ class AppLocalizationsJa extends AppLocalizations {
|
||||
String get notification_lowBatteryTitle => 'Low battery';
|
||||
|
||||
@override
|
||||
String notification_lowBatteryBody(int percent) {
|
||||
return 'Device battery is at $percent%';
|
||||
String notification_lowBatteryBody(String deviceName, int percent) {
|
||||
return '$deviceName battery is at $percent%';
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -4513,8 +4513,8 @@ class AppLocalizationsKo extends AppLocalizations {
|
||||
String get notification_lowBatteryTitle => 'Low battery';
|
||||
|
||||
@override
|
||||
String notification_lowBatteryBody(int percent) {
|
||||
return 'Device battery is at $percent%';
|
||||
String notification_lowBatteryBody(String deviceName, int percent) {
|
||||
return '$deviceName battery is at $percent%';
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -4708,8 +4708,8 @@ class AppLocalizationsNl extends AppLocalizations {
|
||||
String get notification_lowBatteryTitle => 'Low battery';
|
||||
|
||||
@override
|
||||
String notification_lowBatteryBody(int percent) {
|
||||
return 'Device battery is at $percent%';
|
||||
String notification_lowBatteryBody(String deviceName, int percent) {
|
||||
return '$deviceName battery is at $percent%';
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -4759,8 +4759,8 @@ class AppLocalizationsPl extends AppLocalizations {
|
||||
String get notification_lowBatteryTitle => 'Low battery';
|
||||
|
||||
@override
|
||||
String notification_lowBatteryBody(int percent) {
|
||||
return 'Device battery is at $percent%';
|
||||
String notification_lowBatteryBody(String deviceName, int percent) {
|
||||
return '$deviceName battery is at $percent%';
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -4735,8 +4735,8 @@ class AppLocalizationsPt extends AppLocalizations {
|
||||
String get notification_lowBatteryTitle => 'Low battery';
|
||||
|
||||
@override
|
||||
String notification_lowBatteryBody(int percent) {
|
||||
return 'Device battery is at $percent%';
|
||||
String notification_lowBatteryBody(String deviceName, int percent) {
|
||||
return '$deviceName battery is at $percent%';
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -4743,8 +4743,8 @@ class AppLocalizationsRu extends AppLocalizations {
|
||||
String get notification_lowBatteryTitle => 'Low battery';
|
||||
|
||||
@override
|
||||
String notification_lowBatteryBody(int percent) {
|
||||
return 'Device battery is at $percent%';
|
||||
String notification_lowBatteryBody(String deviceName, int percent) {
|
||||
return '$deviceName battery is at $percent%';
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -4716,8 +4716,8 @@ class AppLocalizationsSk extends AppLocalizations {
|
||||
String get notification_lowBatteryTitle => 'Low battery';
|
||||
|
||||
@override
|
||||
String notification_lowBatteryBody(int percent) {
|
||||
return 'Device battery is at $percent%';
|
||||
String notification_lowBatteryBody(String deviceName, int percent) {
|
||||
return '$deviceName battery is at $percent%';
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -4714,8 +4714,8 @@ class AppLocalizationsSl extends AppLocalizations {
|
||||
String get notification_lowBatteryTitle => 'Low battery';
|
||||
|
||||
@override
|
||||
String notification_lowBatteryBody(int percent) {
|
||||
return 'Device battery is at $percent%';
|
||||
String notification_lowBatteryBody(String deviceName, int percent) {
|
||||
return '$deviceName battery is at $percent%';
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -4690,8 +4690,8 @@ class AppLocalizationsSv extends AppLocalizations {
|
||||
String get notification_lowBatteryTitle => 'Low battery';
|
||||
|
||||
@override
|
||||
String notification_lowBatteryBody(int percent) {
|
||||
return 'Device battery is at $percent%';
|
||||
String notification_lowBatteryBody(String deviceName, int percent) {
|
||||
return '$deviceName battery is at $percent%';
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -4742,8 +4742,8 @@ class AppLocalizationsUk extends AppLocalizations {
|
||||
String get notification_lowBatteryTitle => 'Low battery';
|
||||
|
||||
@override
|
||||
String notification_lowBatteryBody(int percent) {
|
||||
return 'Device battery is at $percent%';
|
||||
String notification_lowBatteryBody(String deviceName, int percent) {
|
||||
return '$deviceName battery is at $percent%';
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -4410,8 +4410,8 @@ class AppLocalizationsZh extends AppLocalizations {
|
||||
String get notification_lowBatteryTitle => 'Low battery';
|
||||
|
||||
@override
|
||||
String notification_lowBatteryBody(int percent) {
|
||||
return 'Device battery is at $percent%';
|
||||
String notification_lowBatteryBody(String deviceName, int percent) {
|
||||
return '$deviceName battery is at $percent%';
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -600,10 +600,13 @@ class NotificationService {
|
||||
|
||||
/// Posts a low-battery alert for the connected device.
|
||||
///
|
||||
/// Uses a per-threshold notification id so each alert level updates its own
|
||||
/// notification instead of stacking. Bypasses the rate-limit queue, which
|
||||
/// only handles message and advert notifications.
|
||||
/// Uses a per-device, per-threshold notification id so each alert level
|
||||
/// updates its own notification instead of stacking, and alerts for
|
||||
/// different radios don't overwrite each other. Bypasses the rate-limit
|
||||
/// queue, which only handles message and advert notifications.
|
||||
Future<void> showLowBatteryNotification({
|
||||
required String deviceKey,
|
||||
required String deviceName,
|
||||
required int batteryPercent,
|
||||
required int threshold,
|
||||
}) async {
|
||||
@@ -632,9 +635,9 @@ class NotificationService {
|
||||
|
||||
try {
|
||||
await _notifications.show(
|
||||
id: 'low_battery:$threshold'.hashCode,
|
||||
id: _lowBatteryNotificationId(deviceKey, threshold),
|
||||
title: _l10n.notification_lowBatteryTitle,
|
||||
body: _l10n.notification_lowBatteryBody(batteryPercent),
|
||||
body: _l10n.notification_lowBatteryBody(deviceName, batteryPercent),
|
||||
notificationDetails: notificationDetails,
|
||||
payload: 'low_battery',
|
||||
);
|
||||
@@ -699,6 +702,26 @@ class NotificationService {
|
||||
await _updateBadge(totalUnreadCount);
|
||||
}
|
||||
|
||||
static int _lowBatteryNotificationId(String deviceKey, int threshold) =>
|
||||
'low_battery:$deviceKey:$threshold'.hashCode;
|
||||
|
||||
/// Cancel a device's low-battery notifications for the given thresholds.
|
||||
Future<void> clearLowBatteryNotifications(
|
||||
String deviceKey,
|
||||
Iterable<int> thresholds,
|
||||
) async {
|
||||
if (!await _ensureInitialized()) return;
|
||||
for (final threshold in thresholds) {
|
||||
try {
|
||||
await _notifications.cancel(
|
||||
id: _lowBatteryNotificationId(deviceKey, threshold),
|
||||
);
|
||||
} catch (e) {
|
||||
debugPrint('Failed to clear low battery notification: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Cancel advert notifications for the given contact public key hexes.
|
||||
Future<void> clearAdvertNotifications(List<String> contactIds) async {
|
||||
if (!await _ensureInitialized()) return;
|
||||
|
||||
Reference in new issue
Block a user