1+ using One . Toolbox . ViewModels . Base ;
2+
3+ using System . Net . Mail ;
4+ using System . Net . NetworkInformation ;
5+
6+ using static System . Runtime . InteropServices . JavaScript . JSType ;
7+
8+ namespace One . Toolbox . ViewModels . NetSpeed ;
9+
10+ public partial class NetSpeedItemVM : BaseVM
11+ {
12+ private readonly NetworkInterface adapter ;
13+ private DateTime lastUpdate ;
14+
15+ [ ObservableProperty ]
16+ private string speedSentHuman ;
17+
18+ [ ObservableProperty ]
19+ private string speedReceivedHuman ;
20+
21+ public string InterfaceName { get ; set ; }
22+ public string PhysicalAddress { get ; private set ; }
23+ private long LastBytesSent { get ; set ; }
24+ private long LastBytesReceived { get ; set ; }
25+
26+ private int UpdateInterval { get ; set ; }
27+
28+ public Action < NetSpeedEventArgs > SpeedAction ;
29+
30+ public NetSpeedItemVM ( NetworkInterface networkInterface , int updateInterval = 1000 )
31+ {
32+ adapter = networkInterface ;
33+ UpdateInterval = updateInterval ;
34+
35+ InterfaceName = adapter . Name ;
36+ var macAddress = networkInterface . GetPhysicalAddress ( ) ;
37+
38+ PhysicalAddress = BitConverter . ToString ( macAddress . GetAddressBytes ( ) ) . Replace ( "-" , ":" ) ;
39+ }
40+
41+ public void Start ( CancellationToken cancellationToken = default )
42+ {
43+ lastUpdate = DateTime . Now ;
44+ _ = Task . Run ( async ( ) =>
45+ {
46+ while ( true )
47+ {
48+ try
49+ {
50+ if ( cancellationToken . IsCancellationRequested )
51+ return ;
52+
53+ UpdateSpeed ( ) ;
54+ await Task . Delay ( UpdateInterval ) ;
55+ }
56+ catch ( TaskCanceledException )
57+ {
58+ WriteDebugLog ( $ "Task canceled.") ;
59+
60+ break ;
61+ }
62+ catch ( Exception e )
63+ {
64+ WriteDebugLog ( e . ToString ( ) + "Failed to update net speed." ) ;
65+ }
66+ }
67+ } ) ;
68+ }
69+
70+ public override string ToString ( )
71+ {
72+ return $ "{ InterfaceName } ({ PhysicalAddress } )";
73+ }
74+
75+ public bool First { get ; set ; } = true ;
76+
77+ private void UpdateSpeed ( )
78+ {
79+ // Check if the interface is up (Up表示 网络接口已打开;它可以传输数据包。)
80+ if ( ! adapter . OperationalStatus . Equals ( OperationalStatus . Up ) )
81+ {
82+ WriteDebugLog ( $ "Net interface { ToString ( ) } is { adapter . OperationalStatus } ") ;
83+ return ;
84+ }
85+
86+ // Get the current bytes sent and received
87+ var bytesSent = adapter . GetIPStatistics ( ) . BytesSent ;
88+ var bytesReceived = adapter . GetIPStatistics ( ) . BytesReceived ;
89+
90+ //Debug.WriteLine($"BytesSent: {bytesSent}, BytesReceived: {bytesReceived}");
91+
92+ // Calculate the speed
93+
94+ var speedSent = ( bytesSent - LastBytesSent ) / ( DateTime . Now - lastUpdate ) . TotalSeconds ;
95+ var speedReceived = ( bytesReceived - LastBytesReceived ) / ( DateTime . Now - lastUpdate ) . TotalSeconds ;
96+
97+ // Update the last update time
98+ lastUpdate = DateTime . Now ;
99+
100+ // Update the last bytes sent and received
101+ LastBytesSent = bytesSent ;
102+ LastBytesReceived = bytesReceived ;
103+
104+ if ( First )
105+ {
106+ First = false ;
107+ return ;
108+ }
109+
110+ SpeedAction ? . Invoke ( new NetSpeedEventArgs ( Math . Round ( speedSent , 2 ) , Math . Round ( speedReceived , 2 ) ) ) ;
111+ SpeedSentHuman = HumanReadableSpeed ( speedSent ) ;
112+ SpeedReceivedHuman = HumanReadableSpeed ( speedReceived ) ;
113+
114+ //Debug.WriteLine($"Sent: {speedSent}, Received: {speedReceived}");
115+
116+ //Debug.WriteLine($"SentHuman: {SpeedSentHuman}, ReceivedHuman: {SpeedReceivedHuman}");
117+ }
118+
119+ public static string HumanReadableSpeed ( double bytesPerSecond )
120+ {
121+ if ( bytesPerSecond < 1024 )
122+ {
123+ return $ "{ bytesPerSecond : 0.00} B/s";
124+ }
125+ else if ( bytesPerSecond < 1024 * 1024 )
126+ {
127+ return $ "{ ( bytesPerSecond / 1024.0 ) : 0.00} KB/s";
128+ }
129+ else if ( bytesPerSecond < 1024 * 1024 * 1024 )
130+ {
131+ return $ "{ ( bytesPerSecond / 1024.0 / 1024.0 ) : 0.00} MB/s";
132+ }
133+ else
134+ {
135+ return $ "{ ( bytesPerSecond / 1024.0 / 1024.0 / 1024.0 ) : 0.00} GB/s";
136+ }
137+ }
138+ }
139+
140+ public class NetSpeedEventArgs
141+ {
142+ public double SpeedSent { get ; set ; }
143+ public double SpeedReceived { get ; set ; }
144+
145+ public NetSpeedEventArgs ( double speedSent , double speedReceived )
146+ {
147+ SpeedSent = speedSent ;
148+ SpeedReceived = speedReceived ;
149+ }
150+ }
0 commit comments