You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
135 lines
3.7 KiB
135 lines
3.7 KiB
using System.Diagnostics; |
|
using System.Windows; |
|
using System.Windows.Threading; |
|
using log4net; |
|
using BrilliantSightClient.Views.Dialog; |
|
|
|
namespace BrilliantSightClient.Model.Helper; |
|
using System.Management; |
|
public class NetworkSpeedHelper |
|
{ |
|
|
|
private static readonly ILog Logger = LogManager.GetLogger(typeof(NetworkSpeedHelper)); |
|
private static int _cycle = 300000; |
|
private static double _value = 866; |
|
private static bool _ignore = false; |
|
public static bool Ignore { |
|
get => _ignore; |
|
set |
|
{ |
|
if (value) |
|
{ |
|
DisposeTimer(); |
|
} |
|
_ignore = value; |
|
} |
|
} |
|
|
|
private static Timer _monitorTimer; |
|
private static object _lock = new object(); |
|
|
|
public static void StartMonitoring() |
|
{ |
|
var configCycle = ConfigurationHelper.ReadConfigValue("InternetCheckCycle"); |
|
var configMbps = ConfigurationHelper.ReadConfigValue("InternetCheckMbps"); |
|
|
|
_cycle = int.TryParse(configCycle, out int cycle)? cycle: 300000; |
|
_value = double.TryParse(configMbps, out double valueMbps)? valueMbps: 866; |
|
|
|
Logger.Info($"周期网络检测开始,周期: {_cycle};阈值:{_value}"); |
|
|
|
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 < _value) |
|
{ |
|
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) |
|
{ |
|
Application.Current.Dispatcher.Invoke(() => |
|
{ |
|
try |
|
{ |
|
var result = MessageBoxHasCheck.ShowMessageDialog(); |
|
NetworkSpeedHelper.Ignore = result; |
|
} |
|
catch (Exception ex) |
|
{ |
|
Debug.WriteLine($"对话框操作异常: {ex.Message}"); |
|
} |
|
}, DispatcherPriority.Normal); |
|
} |
|
|
|
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; |
|
} |
|
} |
|
} |