Skip to content

Commit

Permalink
opt.: battery status
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit committed Dec 21, 2023
1 parent 14260fa commit 527e161
Show file tree
Hide file tree
Showing 8 changed files with 286 additions and 40 deletions.
36 changes: 18 additions & 18 deletions ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = Runner/Runner.entitlements;
CURRENT_PROJECT_VERSION = 686;
CURRENT_PROJECT_VERSION = 688;
DEVELOPMENT_TEAM = BA88US33G6;
ENABLE_BITCODE = NO;
INFOPLIST_FILE = "Runner/Info-$(CONFIGURATION).plist";
Expand All @@ -596,7 +596,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0.686;
MARKETING_VERSION = 1.0.688;
PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
Expand Down Expand Up @@ -720,7 +720,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = Runner/Runner.entitlements;
CURRENT_PROJECT_VERSION = 686;
CURRENT_PROJECT_VERSION = 688;
DEVELOPMENT_TEAM = BA88US33G6;
ENABLE_BITCODE = NO;
INFOPLIST_FILE = "Runner/Info-$(CONFIGURATION).plist";
Expand All @@ -730,7 +730,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0.686;
MARKETING_VERSION = 1.0.688;
PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
Expand All @@ -748,7 +748,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = Runner/Runner.entitlements;
CURRENT_PROJECT_VERSION = 686;
CURRENT_PROJECT_VERSION = 688;
DEVELOPMENT_TEAM = BA88US33G6;
ENABLE_BITCODE = NO;
INFOPLIST_FILE = "Runner/Info-$(CONFIGURATION).plist";
Expand All @@ -758,7 +758,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0.686;
MARKETING_VERSION = 1.0.688;
PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
Expand All @@ -779,7 +779,7 @@
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 686;
CURRENT_PROJECT_VERSION = 688;
DEVELOPMENT_TEAM = BA88US33G6;
GCC_C_LANGUAGE_STANDARD = gnu11;
GENERATE_INFOPLIST_FILE = YES;
Expand All @@ -792,7 +792,7 @@
"@executable_path/Frameworks",
"@executable_path/../../Frameworks",
);
MARKETING_VERSION = 1.0.686;
MARKETING_VERSION = 1.0.688;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox.StatusWidget;
Expand All @@ -818,7 +818,7 @@
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 686;
CURRENT_PROJECT_VERSION = 688;
DEVELOPMENT_TEAM = BA88US33G6;
GCC_C_LANGUAGE_STANDARD = gnu11;
GENERATE_INFOPLIST_FILE = YES;
Expand All @@ -831,7 +831,7 @@
"@executable_path/Frameworks",
"@executable_path/../../Frameworks",
);
MARKETING_VERSION = 1.0.686;
MARKETING_VERSION = 1.0.688;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox.StatusWidget;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -854,7 +854,7 @@
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 686;
CURRENT_PROJECT_VERSION = 688;
DEVELOPMENT_TEAM = BA88US33G6;
GCC_C_LANGUAGE_STANDARD = gnu11;
GENERATE_INFOPLIST_FILE = YES;
Expand All @@ -867,7 +867,7 @@
"@executable_path/Frameworks",
"@executable_path/../../Frameworks",
);
MARKETING_VERSION = 1.0.686;
MARKETING_VERSION = 1.0.688;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox.StatusWidget;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -890,7 +890,7 @@
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 686;
CURRENT_PROJECT_VERSION = 688;
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = BA88US33G6;
ENABLE_PREVIEWS = YES;
Expand All @@ -902,7 +902,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0.686;
MARKETING_VERSION = 1.0.688;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox.WatchEnd;
Expand Down Expand Up @@ -931,7 +931,7 @@
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 686;
CURRENT_PROJECT_VERSION = 688;
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = BA88US33G6;
ENABLE_PREVIEWS = YES;
Expand All @@ -943,7 +943,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0.686;
MARKETING_VERSION = 1.0.688;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox.WatchEnd;
PRODUCT_NAME = ServerBox;
Expand All @@ -969,7 +969,7 @@
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 686;
CURRENT_PROJECT_VERSION = 688;
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = BA88US33G6;
ENABLE_PREVIEWS = YES;
Expand All @@ -981,7 +981,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.0.686;
MARKETING_VERSION = 1.0.688;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.lollipopkit.toolbox.WatchEnd;
PRODUCT_NAME = ServerBox;
Expand Down
20 changes: 20 additions & 0 deletions lib/core/extension/listx.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,24 @@ extension ListX<T> on List<T> {
}
return list;
}

T? get firstOrNull => isEmpty ? null : first;

T? get lastOrNull => isEmpty ? null : last;

T? firstWhereOrNull(bool Function(T element) test) {
try {
return firstWhere(test);
} catch (_) {
return null;
}
}

T? lastWhereOrNull(bool Function(T element) test) {
try {
return lastWhere(test);
} catch (_) {
return null;
}
}
}
2 changes: 1 addition & 1 deletion lib/data/model/app/shell_func.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];

Expand Down
25 changes: 18 additions & 7 deletions lib/data/model/server/battery.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand All @@ -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);
}
Expand Down
14 changes: 7 additions & 7 deletions lib/data/model/server/server_status_update_req.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions lib/data/res/build_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
8 changes: 4 additions & 4 deletions lib/view/page/server/detail.dart
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ class _ServerDetailPageState extends State<ServerDetailPage>
children: [
Text(
disk.dev,
style: UIs.text12Bold,
style: UIs.text12,
textScaler: _textFactor,
),
Text(
Expand All @@ -485,7 +485,7 @@ class _ServerDetailPageState extends State<ServerDetailPage>
backgroundColor: DynamicColors.progress.resolve(context),
color: primaryColor,
),
Text('${disk.usedPercent}%', style: UIs.text13Grey)
Text('${disk.usedPercent}%', style: UIs.text12Grey)
],
),
)
Expand Down Expand Up @@ -560,7 +560,7 @@ class _ServerDetailPageState extends State<ServerDetailPage>
children: [
Text(
device,
style: UIs.text12Bold,
style: UIs.text12,
textScaler: _textFactor,
maxLines: 1,
overflow: TextOverflow.fade,
Expand Down Expand Up @@ -642,7 +642,7 @@ class _ServerDetailPageState extends State<ServerDetailPage>
children: [
Text('${battery.name}', style: UIs.text15),
Text(
'${battery.status.name} - ${battery.cycle} - ${battery.powerNow}',
'${battery.status.name} - ${battery.cycle}',
style: UIs.text13Grey,
),
],
Expand Down
Loading

0 comments on commit 527e161

Please sign in to comment.