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

50 lines
1.7 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,需判断)
///
/// 🟢 1.4.5 优化:仅从 status=Full 的行中提取容量(充满状态下 BMS 最准确)
class CapacityExtractor {
static int? extractCapacity(List<String> lines) {
// 🟢 只保留包含 tbat= 且 status=Full 的行
final fullLines = lines.where((line) {
return line.contains('tbat=') && line.contains('status=Full');
}).toList();
if (fullLines.isEmpty) {
// 今天没有充满电的记录,返回 null 表示无有效数据
return null;
}
// 模式一:learned_cap21 系列)
final learnedReg = RegExp(r'learned_cap=(\d+)');
for (String line in fullLines.reversed) {
final match = learnedReg.firstMatch(line);
if (match != null) {
return int.parse(match.group(1)!) ~/ 1000;
}
}
// 模式二:remaining_cap 最大值(20 系列,或 21 系列回退)
final remainingReg = RegExp(r'remaining_cap=(\d+)');
int? maxRemaining;
for (String line in fullLines) {
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
if (maxRemaining > 100000) {
return maxRemaining ~/ 1000;
} else {
return maxRemaining;
}
}
}