尝试针对魅族20系设计新的计算逻辑
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
/// 容量提取工具
|
||||
/// 支持两种日志格式:
|
||||
/// 1. learned_cap(21 系列):单位 µAh,除以 1000 得到 mAh
|
||||
/// 2. remaining_cap(20 系列):单位 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_cap(21 系列)
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
import 'dart:developer' as dev;
|
||||
import '../utils/time_utils.dart';
|
||||
|
||||
/// 充电日志解析器
|
||||
class ChargeParser {
|
||||
/// 解析充电数据,返回解析结果
|
||||
static ParseResult parse(List<String> lines, DateTime? chargingStartTime) {
|
||||
dev.log('[ChargeParser] 开始解析,共 ${lines.length} 行', name: 'BatteryHealth');
|
||||
|
||||
final validLines = _collectValidLines(lines, chargingStartTime);
|
||||
if (validLines.isEmpty) {
|
||||
dev.log('[ChargeParser] 无有效时间窗口内的数据行', name: 'BatteryHealth');
|
||||
return ParseResult.empty();
|
||||
}
|
||||
|
||||
// 按时间戳从新到旧排序
|
||||
validLines.sort((a, b) => b['timestamp'].compareTo(a['timestamp']));
|
||||
final latest = validLines.first;
|
||||
final latestLine = latest['line'] as String;
|
||||
final latestType = latest['type'] as String;
|
||||
|
||||
dev.log(
|
||||
'[ChargeParser] 最新有效数据: $latestType, 时间: ${latest['timestamp']}',
|
||||
name: 'BatteryHealth',
|
||||
);
|
||||
|
||||
if (latestType == 'wls') {
|
||||
return _parseWireless(latestLine);
|
||||
} else {
|
||||
return _parseWired(latestLine);
|
||||
}
|
||||
}
|
||||
|
||||
static List<Map<String, dynamic>> _collectValidLines(
|
||||
List<String> lines,
|
||||
DateTime? chargingStartTime,
|
||||
) {
|
||||
final result = <Map<String, dynamic>>[];
|
||||
for (String line in lines) {
|
||||
final timestamp = TimeUtils.parseLogTimestamp(line);
|
||||
if (!TimeUtils.isLogTimestampValid(timestamp, chargingStartTime))
|
||||
continue;
|
||||
|
||||
final isUsb =
|
||||
line.contains('chg_usb') &&
|
||||
(line.contains('ibus=') || line.contains('real_type='));
|
||||
final isWls = line.contains('chg_wls') && line.contains('wls_online=1');
|
||||
|
||||
if (isUsb || isWls) {
|
||||
result.add({
|
||||
'line': line,
|
||||
'timestamp': timestamp,
|
||||
'type': isWls ? 'wls' : 'usb',
|
||||
});
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static ParseResult _parseWireless(String line) {
|
||||
final typeReg = RegExp(r'wls_type=(\w+)');
|
||||
final voltReg = RegExp(r'wls_volt=(\d+)');
|
||||
final currReg = RegExp(r'wls_curr=(\d+)');
|
||||
|
||||
final wlsType = typeReg.firstMatch(line)?.group(1);
|
||||
final volt = int.parse(voltReg.firstMatch(line)?.group(1) ?? '0');
|
||||
final curr = int.parse(currReg.firstMatch(line)?.group(1) ?? '0');
|
||||
|
||||
return ParseResult(
|
||||
isWireless: true,
|
||||
chargeType: wlsType ?? '无线',
|
||||
power: (volt * curr) / 1000000.0,
|
||||
volt: volt / 1000.0,
|
||||
curr: curr / 1000.0,
|
||||
wiredLevel: null,
|
||||
);
|
||||
}
|
||||
|
||||
static ParseResult _parseWired(String line) {
|
||||
final ibusReg = RegExp(r'ibus=(\d+)');
|
||||
final vbusReg = RegExp(r'vbus=(\d+)');
|
||||
final levelReg = RegExp(r'wired_level=(\d+)');
|
||||
final typeReg = RegExp(r'real_type=(\w+)');
|
||||
|
||||
final ibus = int.parse(ibusReg.firstMatch(line)?.group(1) ?? '0');
|
||||
final vbus = int.parse(vbusReg.firstMatch(line)?.group(1) ?? '0');
|
||||
final realType = typeReg.firstMatch(line)?.group(1);
|
||||
final level = int.parse(levelReg.firstMatch(line)?.group(1) ?? '0');
|
||||
|
||||
String chargeType;
|
||||
if (realType == 'PPS') {
|
||||
chargeType = 'PPS';
|
||||
} else if (realType == 'SDP' || realType == 'FLOAT') {
|
||||
chargeType = 'SDP';
|
||||
} else if (level >= 3) {
|
||||
chargeType = 'PPS';
|
||||
} else {
|
||||
chargeType = 'SDP';
|
||||
}
|
||||
|
||||
return ParseResult(
|
||||
isWireless: false,
|
||||
chargeType: chargeType,
|
||||
power: (vbus * ibus) / 1000000000.0,
|
||||
volt: vbus / 1000000.0,
|
||||
curr: ibus / 1000000.0,
|
||||
wiredLevel: chargeType == 'PPS' ? level : null,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ParseResult {
|
||||
final bool isWireless;
|
||||
final String chargeType;
|
||||
final double power;
|
||||
final double volt;
|
||||
final double curr;
|
||||
final int? wiredLevel;
|
||||
|
||||
ParseResult({
|
||||
required this.isWireless,
|
||||
required this.chargeType,
|
||||
required this.power,
|
||||
required this.volt,
|
||||
required this.curr,
|
||||
this.wiredLevel,
|
||||
});
|
||||
|
||||
factory ParseResult.empty() {
|
||||
return ParseResult(
|
||||
isWireless: false,
|
||||
chargeType: '',
|
||||
power: 0.0,
|
||||
volt: 0.0,
|
||||
curr: 0.0,
|
||||
wiredLevel: null,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user