MainWindow.xaml.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.Configuration;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. using System.Xml.Linq;
  18. namespace EapClientSimulator
  19. {
  20. /// <summary>
  21. /// MainWindow.xaml 的交互逻辑
  22. /// </summary>
  23. public partial class MainWindow : Window
  24. {
  25. public MainWindow()
  26. {
  27. InitializeComponent();
  28. lp1.OnSendMessage += Lp1_OnSendMessage;
  29. lp2.OnSendMessage += Lp2_OnSendMessage;
  30. lp3.OnSendMessage += Lp3_OnSendMessage;
  31. ureport.OnSendMessage += Ureport_OnSendMessage;
  32. ueventReport.OnSendMessage += UeventReport_OnSendMessage;
  33. uevent.OnSendMessage += Uevent_OnSendMessage;
  34. ualarm.OnSendMessage += Ualarm_OnSendMessage;
  35. ustatus.OnSendMessage += Ustatus_OnSendMessage;
  36. }
  37. private void Ustatus_OnSendMessage(object sender, string e)
  38. {
  39. ShowMessage(e);
  40. }
  41. private void Ualarm_OnSendMessage(object sender, string e)
  42. {
  43. ShowMessage(e);
  44. }
  45. private void Uevent_OnSendMessage(object sender, string e)
  46. {
  47. ShowMessage(e);
  48. }
  49. private void UeventReport_OnSendMessage(object sender, string e)
  50. {
  51. ShowMessage(e);
  52. }
  53. private void Ureport_OnSendMessage(object sender, string e)
  54. {
  55. ShowMessage(e);
  56. }
  57. private void Lp3_OnSendMessage(object sender, string e)
  58. {
  59. ShowMessage(e);
  60. }
  61. private void Lp2_OnSendMessage(object sender, string e)
  62. {
  63. ShowMessage(e);
  64. }
  65. private void Lp1_OnSendMessage(object sender, string e)
  66. {
  67. ShowMessage(e);
  68. }
  69. private void btnConnect_Click(object sender, RoutedEventArgs e)
  70. {
  71. if(GlobalData.Client==null)
  72. {
  73. GlobalData.Client = new EapClient();
  74. GlobalData.Client.OnDisConnected += Client_OnDisConnected;
  75. GlobalData.Client.OnReceiveData += Client_OnReceiveData;
  76. }
  77. if(GlobalData.Client.Connect(txtIp.Text,int.Parse(txtPort.Text)))
  78. {
  79. btnConnect.IsEnabled = false;
  80. btnDisConnect.IsEnabled = true;
  81. gp1.IsEnabled = true;
  82. gp2.IsEnabled = true;
  83. gp3.IsEnabled = true;
  84. gp4.IsEnabled = true;
  85. gp5.IsEnabled = true;
  86. gp6.IsEnabled = true;
  87. gp7.IsEnabled = true;
  88. gp8.IsEnabled = true;
  89. gp10.IsEnabled = true;
  90. lstReceivedData.Items.Clear();
  91. }
  92. else
  93. {
  94. MessageBox.Show($"connect failed {txtIp.Text}:{txtPort.Text}");
  95. }
  96. }
  97. private void Client_OnReceiveData(object sender, string e)
  98. {
  99. ShowMessage(e);
  100. }
  101. private object locker = new object();
  102. private void ShowMessage(string msg)
  103. {
  104. string content = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff ") + msg;
  105. lock (locker)
  106. {
  107. try
  108. {
  109. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  110. {
  111. if (lstReceivedData.Items.Count >= 1000)
  112. {
  113. lstReceivedData.Items.Clear();
  114. }
  115. lstReceivedData.Items.Insert(0,content);
  116. lstReceivedData.SelectedIndex = 0;
  117. //if (lstReceivedData.Items.Count >= 2)
  118. //{
  119. //lstReceivedData.ScrollIntoView(lstReceivedData.Items[lstReceivedData.Items.Count - 2]);
  120. //}
  121. }));
  122. }
  123. catch
  124. {
  125. }
  126. }
  127. }
  128. private void Client_OnDisConnected(object sender, string e)
  129. {
  130. try
  131. {
  132. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  133. {
  134. btnDisConnect.IsEnabled = false;
  135. btnConnect.IsEnabled = true;
  136. gp1.IsEnabled = false;
  137. gp2.IsEnabled = false;
  138. gp3.IsEnabled = false;
  139. gp4.IsEnabled = false;
  140. gp5.IsEnabled = false;
  141. gp6.IsEnabled = false;
  142. gp7.IsEnabled = false;
  143. gp8.IsEnabled= false;
  144. gp10.IsEnabled = false;
  145. }));
  146. }
  147. catch
  148. {
  149. }
  150. }
  151. private void btnDisConnect_Click(object sender, RoutedEventArgs e)
  152. {
  153. GlobalData.Client.Close();
  154. btnDisConnect.IsEnabled = false;
  155. btnConnect.IsEnabled = true;
  156. }
  157. }
  158. }