feat: Network detection

master
Tongg 8 months ago
parent f3fdfbfa1f
commit df342e2304
  1. 3
      Language/en_US.xaml
  2. 3
      Language/zh_CN.xaml
  3. 2
      MainWindow.xaml.cs
  4. 114
      Model/Helper/NetworkSpeedHelper.cs
  5. 1
      SparkClient.csproj
  6. 74
      Views/Dialog/MessageBoxHasCheck.xaml
  7. 35
      Views/Dialog/MessageBoxHasCheck.xaml.cs

@ -246,4 +246,7 @@
<sys:String x:Key="CustomSettingPageRunModeSet">RunModel</sys:String> <sys:String x:Key="CustomSettingPageRunModeSet">RunModel</sys:String>
<sys:String x:Key="CustomSettingPageFactory">Factory</sys:String> <sys:String x:Key="CustomSettingPageFactory">Factory</sys:String>
<sys:String x:Key="CustomSettingPageLaboratory">Laboratory</sys:String> <sys:String x:Key="CustomSettingPageLaboratory">Laboratory</sys:String>
<sys:String x:Key="NetworkSpeedCheckedText">Cancel the detection</sys:String>
<sys:String x:Key="NetworkSpeedCheckedMessage">The current network transmission efficiency is low, which may affect the detection efficiency!</sys:String>
</ResourceDictionary> </ResourceDictionary>

@ -246,4 +246,7 @@
<sys:String x:Key="CustomSettingPageFactory">工厂模式</sys:String> <sys:String x:Key="CustomSettingPageFactory">工厂模式</sys:String>
<sys:String x:Key="CustomSettingPageLaboratory">实验室模式</sys:String> <sys:String x:Key="CustomSettingPageLaboratory">实验室模式</sys:String>
<sys:String x:Key="NetworkSpeedCheckedText">取消检测</sys:String>
<sys:String x:Key="NetworkSpeedCheckedMessage">当前网络传输效率低,可能影响检测效率!</sys:String>
</ResourceDictionary> </ResourceDictionary>

@ -65,6 +65,8 @@ public partial class MainWindow
this.WindowState = WindowState.Maximized; this.WindowState = WindowState.Maximized;
WindowManager.MainWindow = this; WindowManager.MainWindow = this;
NetworkSpeedHelper.StartMonitoring();
} }
#region 重写窗体操作按钮 #region 重写窗体操作按钮

@ -0,0 +1,114 @@
using log4net;
using SparkClient.Views.Dialog;
namespace SparkClient.Model.Helper;
using System.Management;
public class NetworkSpeedHelper
{
private static readonly ILog Logger = LogManager.GetLogger(typeof(NetworkSpeedHelper));
private static int _cycle = 300000;
private static bool _ignore = false;
public static bool Ignore {
get => _ignore;
set
{
if (_ignore)
{
DisposeTimer();
}
}
}
private static Timer _monitorTimer;
private static object _lock = new object();
public static void StartMonitoring()
{
Logger.Info($"周期网络检测开始,周期: {_cycle}");
lock (_lock)
{
if (_monitorTimer == null)
{
_monitorTimer = new Timer(CheckSpeedCallback,
null,
_cycle,
Timeout.Infinite);
}
}
}
private static void CheckSpeedCallback(object state)
{
try
{
// 检查停止标志
if (Ignore)
{
DisposeTimer();
return;
}
// 执行网络测速
var speed = InternetSpeedDetection();
// 触发阈值报警
if (speed < 866)
{
ShowSpeedAlert(speed);
}
// 重置定时器(单次触发模式)
lock (_lock)
{
if (!Ignore && _monitorTimer != null)
{
_monitorTimer.Change(_cycle, Timeout.Infinite);
}
}
}
catch (Exception ex)
{
Logger.Info($"监控异常: {ex.Message}");
}
}
private static void DisposeTimer()
{
lock (_lock)
{
Logger.Info("网络检测取消!");
_monitorTimer?.Dispose();
_monitorTimer = null;
}
}
private static void ShowSpeedAlert(double speed)
{
var result = MessageBoxHasCheck.ShowMessageDialog() ;
NetworkSpeedHelper.Ignore = result;
}
private static double InternetSpeedDetection()
{
try {
// 连接到WMI命名空间
var searcher = new ManagementObjectSearcher(
"SELECT Name, Speed FROM Win32_NetworkAdapter " +
"WHERE NetConnectionStatus = 2");
foreach (ManagementObject obj in searcher.Get()) {
string name = obj["Name"].ToString();
if (obj["Speed"] != null) {
long speedBps = Convert.ToInt64(obj["Speed"]);
double speedMbps = speedBps / 1_000_000.0;
return speedMbps;
}
}
return 0;
} catch (Exception ex)
{
return 0;
}
}
}

@ -42,6 +42,7 @@
<PackageReference Include="Rougamo.Fody" Version="5.0.0" /> <PackageReference Include="Rougamo.Fody" Version="5.0.0" />
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlcipher" Version="2.1.10" /> <PackageReference Include="SQLitePCLRaw.bundle_e_sqlcipher" Version="2.1.10" />
<PackageReference Include="System.IO.Ports" Version="9.0.1" /> <PackageReference Include="System.IO.Ports" Version="9.0.1" />
<PackageReference Include="System.Management" Version="9.0.3" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

@ -0,0 +1,74 @@
<Window x:Class="SparkClient.Views.Dialog.MessageBoxHasCheck"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:SparkClient.Views.Dialog"
mc:Ignorable="d"
WindowStyle="None"
AllowsTransparency="True"
Background="Transparent"
ResizeMode="NoResize"
Title="MessageBox" Height="Auto" Width="600">
<Window.Resources>
<Style x:Key="CloseHoverImageStyle" TargetType="Image">
<Setter Property="Source" Value="pack://application:,,,/Resource/Images/UIResource/01-8.png"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource AncestorType=Border}}" Value="True">
<Setter Property="Source" Value="pack://application:,,,/Resource/Images/UIResource/01-8-1.png"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid MouseLeftButtonDown="Grid_MouseLeftButtonDown">
<Border CornerRadius="20"
BorderThickness="1"
BorderBrush="LightGray"
Background="White">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="1" Width="600" TextWrapping = "Wrap" Padding="30 0" HorizontalAlignment="Center"
Text="{StaticResource NetworkSpeedCheckedMessage}"
VerticalAlignment="Center" TextAlignment="Center" FontSize="18" x:Name="TextBlockCenterBox" Margin="0 20"
/>
<CheckBox Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Center" Content="{StaticResource NetworkSpeedCheckedText}"
FontSize="16" x:Name="CheckBoxCancelCheck"/>
<Grid Grid.Row="3" x:Name="GridButtons" Margin="0 20">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button x:Name="ConfirmButton" Grid.Column="1" Padding="0" Margin="0 0 0 20" Height="50" Click="ConfirmButton_OnClick"
BorderThickness="0" Background="Transparent" FocusVisualStyle="{x:Null}">
<Border
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="0 0 0 0"
Padding="10, 10"
Width="120"
BorderThickness="0"
CornerRadius="20">
<Border.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="#40b1ff" Offset="0"/>
<GradientStop Color="#3c9ce6" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<TextBlock FontSize="18" Text="{StaticResource Confirm}" TextAlignment="Center" Foreground="#ffffff"></TextBlock>
</Border>
</Button>
</Grid>
</Grid>
</Border>
</Grid>
</Window>

@ -0,0 +1,35 @@
using System.Windows;
using System.Windows.Input;
namespace SparkClient.Views.Dialog;
public partial class MessageBoxHasCheck : Window
{
public MessageBoxHasCheck()
{
InitializeComponent();
}
private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ButtonState == MouseButtonState.Pressed)
{
this.DragMove();
}
}
private void ConfirmButton_OnClick(object sender, RoutedEventArgs e)
{
this.Close();
}
public static bool ShowMessageDialog()
{
var dialog = new MessageBoxHasCheck();
dialog.ShowDialog();
var data = dialog.CheckBoxCancelCheck.IsChecked == true;
return data;
}
}
Loading…
Cancel
Save