Skip to content

Commit c7ea1c8

Browse files
committed
fix: Unable to obtain system startup time
1 parent 4c6d764 commit c7ea1c8

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

lib/data/helper/ssh_decoder.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ class SSHDecoder {
99
///
1010
/// Tries in order:
1111
/// 1. UTF-8 (with allowMalformed for lenient parsing)
12+
/// - Windows PowerShell scripts now set UTF-8 output encoding by default
1213
/// 2. GBK (for Windows Chinese systems)
14+
/// - In some cases, Windows will still revert to GBK.
15+
/// - Only attempted if UTF-8 produces replacement characters (�)
1316
static String decode(
1417
List<int> bytes, {
1518
bool isWindows = false,
@@ -21,9 +24,15 @@ class SSHDecoder {
2124
try {
2225
final result = utf8.decode(bytes, allowMalformed: true);
2326
// Check if there are replacement characters indicating decode failure
27+
// For non-Windows systems, always use UTF-8 result
2428
if (!result.contains('�') || !isWindows) {
2529
return result;
2630
}
31+
// For Windows with replacement chars, log and try GBK fallback
32+
if (isWindows && result.contains('�')) {
33+
final contextInfo = context != null ? ' [$context]' : '';
34+
Loggers.app.info('UTF-8 decode has replacement chars$contextInfo, trying GBK fallback');
35+
}
2736
} catch (e) {
2837
final contextInfo = context != null ? ' [$context]' : '';
2938
Loggers.app.warning('UTF-8 decode failed$contextInfo: $e');

lib/data/model/app/scripts/cmd_types.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,19 @@ enum WindowsStatusCmdType implements ShellCmdType {
183183
'Get-WmiObject -Class Win32_Processor | '
184184
'Select-Object Name, LoadPercentage, NumberOfCores, NumberOfLogicalProcessors | ConvertTo-Json',
185185
),
186-
uptime('(Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime'),
186+
187+
/// Get system uptime by calculating time since last boot
188+
///
189+
/// Calculates uptime directly in PowerShell to avoid date format parsing issues:
190+
/// - Gets LastBootUpTime from Win32_OperatingSystem
191+
/// - Calculates difference from current time
192+
/// - Returns pre-formatted string: "X days, H:MM" or "H:MM" (if less than 1 day)
193+
/// - Uses ToString('00') for zero-padding to avoid quote escaping issues
194+
uptime(
195+
r'$up = (Get-Date) - (Get-CimInstance Win32_OperatingSystem).LastBootUpTime; '
196+
r'if ($up.Days -gt 0) { "$($up.Days) days, $($up.Hours):$($up.Minutes.ToString(''00''))" } '
197+
r'else { "$($up.Hours):$($up.Minutes.ToString(''00''))" }',
198+
),
187199
conn('(netstat -an | findstr ESTABLISHED | Measure-Object -Line).Count'),
188200
disk(
189201
'Get-WmiObject -Class Win32_LogicalDisk | '

lib/data/model/app/scripts/script_consts.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ exec 2>/dev/null
105105
# DO NOT delete this file while app is running
106106
107107
\$ErrorActionPreference = "SilentlyContinue"
108+
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
108109
109110
''';
110111
}

lib/data/model/server/server_status_update_req.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,11 @@ void _parseWindowsDiskData(ServerStatusUpdateReq req, Map<String, String> parsed
436436
/// Parse Windows uptime data
437437
void _parseWindowsUptimeData(ServerStatusUpdateReq req, Map<String, String> parsedOutput) {
438438
try {
439-
final uptime = WindowsParser.parseUpTime(WindowsStatusCmdType.uptime.findInMap(parsedOutput));
440-
if (uptime != null) {
439+
final uptimeRaw = WindowsStatusCmdType.uptime.findInMap(parsedOutput);
440+
if (uptimeRaw.isNotEmpty && uptimeRaw != 'null') {
441+
// PowerShell now returns pre-formatted uptime string (e.g., "28 days, 5:00" or "5:00")
442+
// No parsing needed - use it directly
443+
final uptime = uptimeRaw.trim();
441444
req.ss.more[StatusCmdType.uptime] = uptime;
442445
}
443446
} catch (e, s) {

0 commit comments

Comments
 (0)