From df342e230476dd5c5b790d018ba5b2727966f273 Mon Sep 17 00:00:00 2001 From: Tongg Date: Mon, 7 Apr 2025 17:11:08 +0800 Subject: [PATCH] feat: Network detection --- Language/en_US.xaml | 3 + Language/zh_CN.xaml | 3 + MainWindow.xaml.cs | 2 + Model/Helper/NetworkSpeedHelper.cs | 114 ++++++++++++++++++++++++ SparkClient.csproj | 1 + Views/Dialog/MessageBoxHasCheck.xaml | 74 +++++++++++++++ Views/Dialog/MessageBoxHasCheck.xaml.cs | 35 ++++++++ 7 files changed, 232 insertions(+) create mode 100644 Model/Helper/NetworkSpeedHelper.cs create mode 100644 Views/Dialog/MessageBoxHasCheck.xaml create mode 100644 Views/Dialog/MessageBoxHasCheck.xaml.cs diff --git a/Language/en_US.xaml b/Language/en_US.xaml index fbd1a3f..f02025f 100644 --- a/Language/en_US.xaml +++ b/Language/en_US.xaml @@ -246,4 +246,7 @@ RunModel Factory Laboratory + + Cancel the detection + The current network transmission efficiency is low, which may affect the detection efficiency! diff --git a/Language/zh_CN.xaml b/Language/zh_CN.xaml index bcb4969..e9dc363 100644 --- a/Language/zh_CN.xaml +++ b/Language/zh_CN.xaml @@ -246,4 +246,7 @@ 工厂模式 实验室模式 + 取消检测 + 当前网络传输效率低,可能影响检测效率! + \ No newline at end of file diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 986891c..f8bb663 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -65,6 +65,8 @@ public partial class MainWindow this.WindowState = WindowState.Maximized; WindowManager.MainWindow = this; + + NetworkSpeedHelper.StartMonitoring(); } #region 重写窗体操作按钮 diff --git a/Model/Helper/NetworkSpeedHelper.cs b/Model/Helper/NetworkSpeedHelper.cs new file mode 100644 index 0000000..26fc314 --- /dev/null +++ b/Model/Helper/NetworkSpeedHelper.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/SparkClient.csproj b/SparkClient.csproj index e17ea37..397b10f 100644 --- a/SparkClient.csproj +++ b/SparkClient.csproj @@ -42,6 +42,7 @@ + diff --git a/Views/Dialog/MessageBoxHasCheck.xaml b/Views/Dialog/MessageBoxHasCheck.xaml new file mode 100644 index 0000000..1128e5c --- /dev/null +++ b/Views/Dialog/MessageBoxHasCheck.xaml @@ -0,0 +1,74 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Views/Dialog/MessageBoxHasCheck.xaml.cs b/Views/Dialog/MessageBoxHasCheck.xaml.cs new file mode 100644 index 0000000..81dc27b --- /dev/null +++ b/Views/Dialog/MessageBoxHasCheck.xaml.cs @@ -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; + } +} \ No newline at end of file