@@ -21,12 +21,116 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2121OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222SOFTWARE.
2323*/
24+ using InternetTest . Commands ;
25+ using InternetTest . Models ;
26+ using System . Net ;
27+ using System . Net . NetworkInformation ;
28+ using System . Threading . Tasks ;
29+ using System . Windows ;
30+ using System . Windows . Input ;
31+
2432namespace InternetTest . ViewModels ;
2533
2634public class PingPageViewModel : ViewModelBase
2735{
28- public PingPageViewModel ( )
36+ private string _min = string . Empty ;
37+ public string Min { get => _min ; set { _min = value ; OnPropertyChanged ( nameof ( Min ) ) ; } }
38+
39+ private string _max = string . Empty ;
40+ public string Max { get => _max ; set { _max = value ; OnPropertyChanged ( nameof ( Max ) ) ; } }
41+
42+ private string _avg = string . Empty ;
43+ public string Avg { get => _avg ; set { _avg = value ; OnPropertyChanged ( nameof ( Avg ) ) ; } }
44+
45+ private string _startTime = string . Empty ;
46+ public string StartTime { get => _startTime ; set { _startTime = value ; OnPropertyChanged ( nameof ( StartTime ) ) ; } }
47+
48+ private string _duration = string . Empty ;
49+ public string Duration { get => _duration ; set { _duration = value ; OnPropertyChanged ( nameof ( Duration ) ) ; } }
50+
51+ private string _host = string . Empty ;
52+ public string Host { get => _host ; set { _host = value ; OnPropertyChanged ( nameof ( Host ) ) ; } }
53+
54+ private string _query = string . Empty ;
55+ public string Query { get => _query ; set { _query = value ; OnPropertyChanged ( nameof ( Query ) ) ; } }
56+
57+ private int _received ;
58+ public int Received { get => _received ; set { _received = value ; OnPropertyChanged ( nameof ( Received ) ) ; } }
59+
60+ private int _sent ;
61+ public int Sent { get => _sent ; set { _sent = value ; OnPropertyChanged ( nameof ( Sent ) ) ; } }
62+
63+ private int _lost ;
64+ public int Lost { get => _lost ; set { _lost = value ; OnPropertyChanged ( nameof ( Lost ) ) ; } }
65+
66+ private string _lossPercentage = string . Empty ;
67+ public string LossPercentage { get => _lossPercentage ; set { _lossPercentage = value ; OnPropertyChanged ( nameof ( LossPercentage ) ) ; } }
68+
69+ private int _requestAmount = 4 ;
70+ public int RequestAmount { get => _requestAmount ; set { _requestAmount = value ; OnPropertyChanged ( nameof ( RequestAmount ) ) ; } }
71+
72+ private bool _empty = true ;
73+
74+ public bool Empty { get => _empty ; set { _empty = value ; OnPropertyChanged ( nameof ( Empty ) ) ; } }
75+
76+ public ICommand PingCommand => new RelayCommand ( o => Ping ( ) ) ;
77+
78+ private readonly Settings _settings ;
79+ public PingPageViewModel ( Settings settings )
80+ {
81+ _settings = settings ;
82+
83+ Query = settings . TestSite ?? "google.com" ;
84+ }
85+
86+ private async Task Ping ( )
2987 {
88+ Query = Query . Replace ( "https://" , "" ) . Replace ( "http://" , "" ) ;
89+
90+ if ( Query is null or { Length : 0 } || string . IsNullOrWhiteSpace ( Query ) )
91+ {
92+ MessageBox . Show ( Properties . Resources . EnterIP , Properties . Resources . Error , MessageBoxButton . OK , MessageBoxImage . Information ) ;
93+ return ;
94+ }
95+
96+ Sent = 0 ;
97+ Received = 0 ;
98+ Lost = 0 ;
99+ StartTime = DateTime . Now . ToString ( "HH:mm:ss" ) ;
100+ Empty = false ;
101+
102+ long [ ] times = new long [ RequestAmount ] ;
103+ for ( int i = 0 ; i < RequestAmount ; i ++ )
104+ {
105+ try
106+ {
107+ var ping = await new Ping ( ) . SendPingAsync ( Query ) ;
108+ times [ i ] = ping . RoundtripTime ;
109+ Sent ++ ;
110+
111+ if ( ping . Status == IPStatus . Success )
112+ {
113+ Received ++ ;
114+ Lost = Sent - Received ;
115+ LossPercentage = $ "{ ( double ) Lost / Sent * 100 : 0.00} %";
116+ Min = $ "{ times . Min ( ) } ms";
117+ Max = $ "{ times . Max ( ) } ms";
118+ Avg = $ "{ times . Average ( ) } ms";
119+ Duration = $ "{ times . Sum ( ) } ms";
120+ }
121+ else
122+ {
123+ Lost ++ ;
124+ LossPercentage = $ "{ ( double ) Lost / Sent * 100 : 0.00} %";
125+ }
126+ }
127+ catch ( Exception ex )
128+ {
129+ MessageBox . Show ( ex . Message , Properties . Resources . Error , MessageBoxButton . OK , MessageBoxImage . Error ) ;
130+ return ;
131+ }
132+ }
30133
134+ Host = Dns . GetHostEntry ( Query ) . HostName ;
31135 }
32136}
0 commit comments