change - 添加系统状态监控

dev
WenJY 10 hours ago
parent 15edaebf44
commit 5543fcbc76

@ -15,7 +15,8 @@
"Bash(sed -i '' '/Console.WriteLine/d' ViewModels/Base/CrudPageViewModel.cs)",
"Bash(sed -i '' '/Console.WriteLine/d' Views/MainWindow.axaml.cs)",
"Bash(sed -i '' '/System.Console.WriteLine/d' ViewModels/Device/DeviceHostViewModel.cs)",
"Bash(sed -i '' '/System.Console.WriteLine/d' Views/Device/DeviceHostListView.axaml.cs)"
"Bash(sed -i '' '/System.Console.WriteLine/d' Views/Device/DeviceHostListView.axaml.cs)",
"Bash(git status *)"
]
}
}

@ -15,12 +15,6 @@
<Border Background="#0F1620" CornerRadius="10" Margin="0"
BorderBrush="#1B3A5C" BorderThickness="1">
<Border.Background>
<LinearGradientBrush StartPoint="0%,0%" EndPoint="100%,100%">
<GradientStop Color="#0F1620" Offset="0" />
<GradientStop Color="#0C1522" Offset="1" />
</LinearGradientBrush>
</Border.Background>
<Grid RowDefinitions="Auto,*,Auto" Margin="0">
<!-- Title Bar -->

@ -10,7 +10,7 @@
Background="#0A0E14"
Foreground="#BCC8D6">
<Grid RowDefinitions="Auto,Auto,*">
<Grid RowDefinitions="Auto,Auto,*,Auto">
<!-- Title Bar -->
<Border Grid.Row="0" Padding="16,8" Background="#0A0E14" BorderBrush="#1A2F4A" BorderThickness="0,0,0,1">
@ -68,5 +68,49 @@
<!-- Content Area -->
<Border Grid.Row="2" x:Name="ContentArea" />
<!-- System Status Bar -->
<Border Grid.Row="3" Padding="16,4" Background="#0C1622" BorderBrush="#1A2F4A" BorderThickness="0,1,0,0">
<Grid ColumnDefinitions="Auto,Auto,Auto,Auto,Auto,Auto,*,Auto">
<!-- CPU -->
<StackPanel Grid.Column="0" Orientation="Horizontal" Spacing="4" VerticalAlignment="Center">
<Ellipse x:Name="CpuStatusDot" Width="6" Height="6" Fill="#00E676" VerticalAlignment="Center" />
<TextBlock Text="CPU" FontSize="11" Foreground="#6B8CB5" VerticalAlignment="Center" />
<TextBlock x:Name="CpuUsageText" Text="0.0%" FontSize="11" Foreground="#BCC8D6" VerticalAlignment="Center" />
</StackPanel>
<Border Grid.Column="1" Width="1" Height="12" Background="#1A2F4A" Margin="10,0" VerticalAlignment="Center" />
<!-- Memory -->
<StackPanel Grid.Column="2" Orientation="Horizontal" Spacing="4" VerticalAlignment="Center">
<Ellipse x:Name="MemStatusDot" Width="6" Height="6" Fill="#00E676" VerticalAlignment="Center" />
<TextBlock Text="内存" FontSize="11" Foreground="#6B8CB5" VerticalAlignment="Center" />
<TextBlock x:Name="MemUsageText" Text="0 MB" FontSize="11" Foreground="#BCC8D6" VerticalAlignment="Center" />
</StackPanel>
<Border Grid.Column="3" Width="1" Height="12" Background="#1A2F4A" Margin="10,0" VerticalAlignment="Center" />
<!-- Network -->
<StackPanel Grid.Column="4" Orientation="Horizontal" Spacing="4" VerticalAlignment="Center">
<Ellipse x:Name="NetStatusDot" Width="6" Height="6" Fill="#00E676" VerticalAlignment="Center" />
<TextBlock Text="网络" FontSize="11" Foreground="#6B8CB5" VerticalAlignment="Center" />
<TextBlock x:Name="NetStatusText" Text="已连接" FontSize="11" Foreground="#BCC8D6" VerticalAlignment="Center" />
</StackPanel>
<!-- Uptime -->
<Border Grid.Column="5" Width="1" Height="12" Background="#1A2F4A" Margin="10,0" VerticalAlignment="Center" />
<StackPanel Grid.Column="6" Orientation="Horizontal" Spacing="4" VerticalAlignment="Center">
<TextBlock Text="运行时间" FontSize="11" Foreground="#6B8CB5" VerticalAlignment="Center" />
<TextBlock x:Name="UptimeText" Text="00:00:00" FontSize="11" Foreground="#BCC8D6" VerticalAlignment="Center" />
</StackPanel>
<!-- WCS Version -->
<StackPanel Grid.Column="7" Orientation="Horizontal" Spacing="12" VerticalAlignment="Center" Margin="24,0,0,0">
<TextBlock Text="WCS Core" FontSize="10" Foreground="#4B6A8A" VerticalAlignment="Center" />
<TextBlock Text="V1.0.0" FontSize="10" Foreground="#4FC3F7" VerticalAlignment="Center" />
</StackPanel>
</Grid>
</Border>
</Grid>
</Window>

@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.NetworkInformation;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
@ -15,6 +17,10 @@ public partial class MainWindow : Window
{
private readonly NavigationViewModel _navVm;
private readonly List<Popup> _openPopups = new();
private readonly Process _currentProcess;
private TimeSpan _lastCpuTime;
private DateTime _lastCpuCheck;
private readonly DateTime _startTime;
public MainWindow(NavigationViewModel navigationVm)
{
@ -26,11 +32,19 @@ public partial class MainWindow : Window
BuildMenu();
_navVm.LoadDefaultPage();
_currentProcess = Process.GetCurrentProcess();
_lastCpuTime = _currentProcess.TotalProcessorTime;
_lastCpuCheck = DateTime.UtcNow;
_startTime = DateTime.UtcNow;
// 时钟
var timer = new System.Timers.Timer(1000);
timer.Elapsed += (_, _) =>
Avalonia.Threading.Dispatcher.UIThread.Post(() =>
ClockText.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
{
ClockText.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
RefreshSystemStatus();
});
timer.Start();
ClockText.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
@ -38,6 +52,72 @@ public partial class MainWindow : Window
LogoutBtn.Click += (_, _) => Close();
}
private void RefreshSystemStatus()
{
// CPU usage
var now = DateTime.UtcNow;
var currentCpuTime = _currentProcess.TotalProcessorTime;
var cpuUsedMs = (currentCpuTime - _lastCpuTime).TotalMilliseconds;
var elapsedMs = (now - _lastCpuCheck).TotalMilliseconds;
_lastCpuTime = currentCpuTime;
_lastCpuCheck = now;
var cpuPercent = elapsedMs > 0
? cpuUsedMs / elapsedMs / Environment.ProcessorCount * 100.0
: 0.0;
CpuUsageText.Text = $"{cpuPercent:F1}%";
// CPU indicator color
var cpuColor = cpuPercent switch
{
< 50 => "#00E676",
< 80 => "#FFB74D",
_ => "#FF5252"
};
CpuStatusDot.Fill = Brush.Parse(cpuColor);
// Memory usage
var memBytes = _currentProcess.WorkingSet64;
var memMB = memBytes / 1024.0 / 1024.0;
MemUsageText.Text = memMB >= 1024
? $"{memMB / 1024.0:F1} GB"
: $"{memMB:F0} MB";
// Memory indicator color
var memColor = memMB switch
{
< 512 => "#00E676",
< 1024 => "#FFB74D",
_ => "#FF5252"
};
MemStatusDot.Fill = Brush.Parse(memColor);
// Network status
var isNetworkAvailable = NetworkInterface.GetIsNetworkAvailable();
var activeInterface = false;
if (isNetworkAvailable)
{
foreach (var ni in NetworkInterface.GetAllNetworkInterfaces())
{
if (ni.OperationalStatus == OperationalStatus.Up
&& ni.NetworkInterfaceType != NetworkInterfaceType.Loopback)
{
activeInterface = true;
break;
}
}
}
NetStatusDot.Fill = Brush.Parse(activeInterface ? "#00E676" : "#FF5252");
NetStatusText.Text = activeInterface ? "已连接" : "未连接";
// Uptime
var uptime = DateTime.UtcNow - _startTime;
UptimeText.Text = uptime.TotalDays >= 1
? $"{(int)uptime.TotalDays}d {uptime.Hours:D2}:{uptime.Minutes:D2}:{uptime.Seconds:D2}"
: $"{uptime.Hours:D2}:{uptime.Minutes:D2}:{uptime.Seconds:D2}";
}
private void BuildMenu()
{
MenuContainer.Children.Clear();

Loading…
Cancel
Save