mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 23:34:24 +01:00
opt.: battery status
This commit is contained in:
@@ -220,7 +220,7 @@ const _statusCmds = [
|
||||
'cat /sys/class/thermal/thermal_zone*/temp',
|
||||
'hostname',
|
||||
'cat /proc/diskstats',
|
||||
'cat /sys/class/power_supply/*/uevent',
|
||||
'for f in /sys/class/power_supply/*/uevent; do cat "\$f"; echo; done',
|
||||
'nvidia-smi -q -x',
|
||||
];
|
||||
|
||||
|
||||
@@ -17,14 +17,14 @@ class Battery {
|
||||
final BatteryStatus status;
|
||||
final String? name;
|
||||
final int? cycle;
|
||||
final int? powerNow;
|
||||
final String? tech;
|
||||
|
||||
const Battery({
|
||||
required this.status,
|
||||
this.percent,
|
||||
this.name,
|
||||
this.cycle,
|
||||
this.powerNow,
|
||||
this.tech,
|
||||
});
|
||||
|
||||
factory Battery.fromRaw(String raw) {
|
||||
@@ -38,16 +38,25 @@ class Battery {
|
||||
|
||||
final capacity = map['POWER_SUPPLY_CAPACITY']; // 30%
|
||||
final cycle = map['POWER_SUPPLY_CYCLE_COUNT']; // 30
|
||||
final powerNow = map['POWER_SUPPLY_POWER_NOW']; // 30W
|
||||
|
||||
var name = map['POWER_SUPPLY_MODEL_NAME'];
|
||||
name ??= map['POWER_SUPPLY_NAME'];
|
||||
|
||||
return Battery(
|
||||
percent: capacity == null ? null : int.tryParse(capacity),
|
||||
status: BatteryStatus.parse(map['POWER_SUPPLY_STATUS']),
|
||||
name: map['POWER_SUPPLY_MODEL_NAME'],
|
||||
name: name,
|
||||
cycle: cycle == null ? null : int.tryParse(cycle),
|
||||
powerNow: powerNow == null ? null : int.tryParse(powerNow),
|
||||
tech: map['POWER_SUPPLY_TECHNOLOGY'],
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Battery{$percent, $status, $name, $cycle}';
|
||||
}
|
||||
|
||||
bool get isLiPoly => tech == 'Li-poly';
|
||||
}
|
||||
|
||||
enum BatteryStatus {
|
||||
@@ -72,14 +81,16 @@ enum BatteryStatus {
|
||||
}
|
||||
|
||||
abstract final class Batteries {
|
||||
static List<Battery> parse(String raw) {
|
||||
static List<Battery> parse(String raw, [bool onlyLiPoly = false]) {
|
||||
final lines = raw.split('\n');
|
||||
final batteries = <Battery>[];
|
||||
final oneBatLines = <String>[];
|
||||
for (final line in lines) {
|
||||
if (line.isEmpty) {
|
||||
try {
|
||||
batteries.add(Battery.fromRaw(oneBatLines.join('\n')));
|
||||
final bat = Battery.fromRaw(oneBatLines.join('\n'));
|
||||
if (onlyLiPoly && !bat.isLiPoly) continue;
|
||||
batteries.add(bat);
|
||||
} catch (e, s) {
|
||||
Loggers.parse.warning(e, s);
|
||||
}
|
||||
|
||||
@@ -24,12 +24,10 @@ class ServerStatusUpdateReq {
|
||||
}
|
||||
|
||||
Future<ServerStatus> getStatus(ServerStatusUpdateReq req) async {
|
||||
switch (req.system) {
|
||||
case SystemType.linux:
|
||||
return _getLinuxStatus(req);
|
||||
case SystemType.bsd:
|
||||
return _getBsdStatus(req);
|
||||
}
|
||||
return switch (req.system) {
|
||||
SystemType.linux => _getLinuxStatus(req),
|
||||
SystemType.bsd => _getBsdStatus(req),
|
||||
};
|
||||
}
|
||||
|
||||
// Wrap each operation with a try-catch, so that if one operation fails,
|
||||
@@ -130,7 +128,9 @@ Future<ServerStatus> _getLinuxStatus(ServerStatusUpdateReq req) async {
|
||||
|
||||
try {
|
||||
final battery = StatusCmdType.battery.find(segments);
|
||||
final batteries = Batteries.parse(battery);
|
||||
|
||||
/// Only collect li-poly batteries
|
||||
final batteries = Batteries.parse(battery, true);
|
||||
req.ss.batteries.clear();
|
||||
if (batteries.isNotEmpty) {
|
||||
req.ss.batteries.addAll(batteries);
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
class BuildData {
|
||||
static const String name = "ServerBox";
|
||||
static const int build = 686;
|
||||
static const int build = 688;
|
||||
static const String engine = "3.16.4";
|
||||
static const String buildAt = "2023-12-20 17:26:32";
|
||||
static const int modifications = 2;
|
||||
static const String buildAt = "2023-12-21 16:19:00";
|
||||
static const int modifications = 6;
|
||||
static const int script = 33;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user