fix: sensors (#350)

This commit is contained in:
lollipopkit
2024-05-10 20:15:30 +08:00
parent ed5bcb17ed
commit f9efd6acfa
11 changed files with 118 additions and 222 deletions

View File

@@ -1,6 +1,3 @@
import 'dart:convert';
import 'package:toolbox/core/extension/listx.dart';
import 'package:toolbox/data/res/logger.dart';
final class SensorAdaptor {
@@ -26,78 +23,52 @@ final class SensorAdaptor {
};
}
final class SensorTemp {
final double? current;
final double? max;
final double? min;
const SensorTemp({this.current, this.max, this.min});
@override
String toString() {
return 'SensorTemp{current: $current, max: $max, min: $min}';
}
@override
operator ==(Object other) {
if (identical(this, other)) return true;
return other is SensorTemp &&
other.current == current &&
other.max == max &&
other.min == min;
}
@override
int get hashCode => current.hashCode ^ max.hashCode ^ min.hashCode;
}
final class SensorItem {
final String device;
final SensorAdaptor adapter;
final Map<String, SensorTemp> props;
final String val;
const SensorItem({
required this.device,
required this.adapter,
required this.props,
required this.val,
});
static List<SensorItem> parse(String raw) {
final rmErrRaw = raw.split('\n')
..removeWhere((element) => element.contains('ERROR:'));
final map = json.decode(rmErrRaw.join('\n')) as Map<String, dynamic>;
final items = <SensorItem>[];
for (final key in map.keys) {
try {
final adapter = SensorAdaptor.parse(map[key]['Adapter'] as String);
final props = <String, SensorTemp>{};
for (final subKey in map[key].keys) {
if (subKey == 'Adapter') {
continue;
}
final subMap = map[key][subKey] as Map<String, dynamic>;
final currentKey =
subMap.keys.toList().firstWhereOrNull((e) => e.endsWith('input'));
final current = subMap[currentKey] as double?;
final maxKey = subMap.keys
.toList()
.firstWhereOrNull((e) => e.endsWith('max') || e.endsWith('crit'));
final max = subMap[maxKey] as double?;
final minKey =
subMap.keys.toList().firstWhereOrNull((e) => e.endsWith('min'));
final min = subMap[minKey] as double?;
if (current == null && max == null && min == null) {
continue;
}
props[subKey] = SensorTemp(current: current, max: max, min: min);
}
items.add(SensorItem(device: key, adapter: adapter, props: props));
} catch (e, s) {
Loggers.parse.warning(e, s);
final eachSensorLines = <List<String>>[[]];
final lines = raw.split('\n');
var emptyLinesCount = 0;
for (var i = 0; i < lines.length; i++) {
final line = lines[i];
if (line.isEmpty) {
eachSensorLines.add([]);
emptyLinesCount++;
continue;
}
eachSensorLines.last.add(line);
}
return items;
if (emptyLinesCount + 1 != eachSensorLines.length) {
Loggers.app.warning('Empty lines count not match');
}
final sensors = <SensorItem>[];
for (final sensorLines in eachSensorLines) {
// At least 3 lines: [device, adapter, temp]
final len = sensorLines.length;
if (len < 3) continue;
final device = sensorLines.first;
final adapter =
SensorAdaptor.parse(sensorLines[1].split(':').last.trim());
final line = sensorLines[2];
final parts = line.split(':');
if (parts.length < 2) {
continue;
}
final val = parts[1].trim();
sensors.add(SensorItem(device: device, adapter: adapter, val: val));
}
return sensors;
}
}