Files
MEIZU-Battery-Healthy/lib/logic/capacity_extractor.dart
T

42 lines
1.4 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/// 容量提取工具
/// 支持两种日志格式:
/// 1. learned_cap21 系列):单位 µAh,除以 1000 得到 mAh
/// 2. remaining_cap20 系列):单位 mAh,直接使用(但 21 系列的 remaining_cap 也是 µAh,需判断)
class CapacityExtractor {
static int? extractCapacity(List<String> lines) {
final validLines = lines.where((line) => line.contains('tbat=')).toList();
if (validLines.isEmpty) return null;
// 模式一:learned_cap21 系列)
final learnedReg = RegExp(r'learned_cap=(\d+)');
for (String line in validLines.reversed) {
final match = learnedReg.firstMatch(line);
if (match != null) {
return int.parse(match.group(1)!) ~/ 1000;
}
}
// 模式二:remaining_cap 最大值
final remainingReg = RegExp(r'remaining_cap=(\d+)');
int? maxRemaining;
for (String line in validLines) {
final match = remainingReg.firstMatch(line);
if (match != null) {
final value = int.parse(match.group(1)!);
if (maxRemaining == null || value > maxRemaining) {
maxRemaining = value;
}
}
}
if (maxRemaining == null) return null;
// 🟢 判断单位:如果数值大于 100000(约 100 mAh),则认为是 µAh,需除以 1000
// 否则认为是 mAh,直接使用
if (maxRemaining > 100000) {
return maxRemaining ~/ 1000;
} else {
return maxRemaining;
}
}
}