Make_WirelessThroughput.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Net.Sockets;
  10. using System.Text;
  11. using System.Text.RegularExpressions;
  12. using System.Threading;
  13. using System.Web.Services.Description;
  14. using System.Web.UI.WebControls.WebParts;
  15. using System.Windows.Forms;
  16. using UAS_MES_NEW.DataOperate;
  17. using UAS_MES_NEW.Entity;
  18. namespace UAS_MES_NEW.Make
  19. {
  20. public partial class Make_WirelessThroughput : Form
  21. {
  22. public Make_WirelessThroughput()
  23. {
  24. InitializeComponent();
  25. }
  26. StringBuilder SQL = new StringBuilder();
  27. DataTable dt;
  28. DataHelper dh;
  29. private void Make_WirelessThroughput_Load(object sender, EventArgs e)
  30. {
  31. dh = SystemInf.dh;
  32. }
  33. private void Start_Click(object sender, EventArgs e)
  34. {
  35. if (IsCheckSet()) return;
  36. ShowMsg(1, $"开始测试");
  37. var tester = new CameraIperfTester(
  38. cameraIp: ProductList.Text.Trim(),
  39. cameraPort: 23,
  40. username: Account.Text.Trim(),
  41. password: ProductList.Text.Trim(),
  42. iperfServerIp: IPList.Text.Trim(),
  43. iperfServerPort : 5201
  44. );
  45. string Msg = tester.TelnetConnect();
  46. if (Msg.Substring(0,2) != "OK")
  47. {
  48. ShowMsg(0, $"Telnet登录失败,{Msg}");
  49. return;
  50. }
  51. ShowMsg(1, $"Telnet登录成功");
  52. Msg = tester.StartIperfTest(@"D:\MesCoder\Company\慧眼\吞吐量测试\iperf3.exe", @"D:\MesCoder\Company\慧眼\吞吐量测试\iperf_logs");
  53. if (Msg.Substring(0, 2) != "OK")
  54. {
  55. ShowMsg(0, $"iperf3启动失败,{Msg}");
  56. return;
  57. }
  58. }
  59. private void SN_KeyDown(object sender, KeyEventArgs e)
  60. {
  61. if (e.KeyCode != Keys.Enter) return;
  62. UpdateSN("L", SN.Text.Trim());
  63. }
  64. private bool IsCheckSet()
  65. {
  66. if (string.IsNullOrEmpty(IPList.Text))
  67. {
  68. ShowMsg(0, "请选择本地iperf 服务IP地址");
  69. return true;
  70. }
  71. if (string.IsNullOrEmpty(ProductList.Text))
  72. {
  73. ShowMsg(0, "请选择产品固定IP地址");
  74. return true;
  75. }
  76. if (string.IsNullOrEmpty(Account.Text))
  77. {
  78. ShowMsg(0, "请输入Telnet登录账号");
  79. return true;
  80. }
  81. if (string.IsNullOrEmpty(Password.Text))
  82. {
  83. ShowMsg(0, "请输入Telnet登录密码");
  84. return true;
  85. }
  86. if (string.IsNullOrEmpty(TestTime.Text))
  87. {
  88. ShowMsg(0, "请输入测试时长");
  89. return true;
  90. }
  91. //if (Locat1.Checked == false || Locat2.Checked == false || Locat3.Checked == false)
  92. //{
  93. // ShowMsg(0, "请选择固件位置");
  94. // return true;
  95. //}
  96. //if (Radio1.Checked == false || Radio1.Checked == false )
  97. //{
  98. // ShowMsg(0, "请选择测试类型");
  99. // return true;
  100. //}
  101. return false;
  102. }
  103. private void UpdateSN(string type, string sn)
  104. {
  105. if (type == "C")
  106. {
  107. serialNumber.Text = "";
  108. workOrder.Text = "";
  109. productCode.Text = "";
  110. productName.Text = "";
  111. }
  112. else if (type == "L")
  113. {
  114. SQL.Clear();
  115. SQL.Append($@"SELECT ms_sncode,ma_code,pr_code,pr_spec FROM makeserial,make,product
  116. WHERE ms_sncode = '{sn}' AND ms_makecode = ma_code AND ms_prodcode = pr_code");
  117. dt = (DataTable)dh.ExecuteSql(SQL.ToString(), "select");
  118. if (dt.Rows.Count > 0)
  119. {
  120. serialNumber.Text = dt.Rows[0]["ms_sncode"].ToString();
  121. workOrder.Text = dt.Rows[0]["ma_code"].ToString();
  122. productCode.Text = dt.Rows[0]["pr_code"].ToString();
  123. productName.Text = dt.Rows[0]["pr_spec"].ToString();
  124. }
  125. else
  126. {
  127. UpdateSN("C", sn);
  128. }
  129. }
  130. }
  131. public class CameraIperfTester
  132. {
  133. string cameraIp;
  134. int cameraPort;
  135. string username;
  136. string password;
  137. string iperfServerIp;
  138. int iperfServerPort;
  139. public CameraIperfTester(string cameraIp, int cameraPort, string username, string password, string iperfServerIp, int iperfServerPort)
  140. {
  141. this.cameraIp = cameraIp;
  142. this.cameraPort = cameraPort;
  143. this.username = username;
  144. this.password = password;
  145. this.iperfServerIp = iperfServerIp;
  146. this.iperfServerPort = iperfServerPort;
  147. }
  148. public string StartIperfTest(string iperfPath,string logDirectory)
  149. {
  150. return "OK,测试";
  151. }
  152. public string TelnetConnect()
  153. {
  154. try
  155. {
  156. using (TcpClient client = new TcpClient())
  157. {
  158. IAsyncResult connectResult = client.BeginConnect(cameraIp, cameraPort, null, null);
  159. bool connected = connectResult.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(5000)); // 5秒连接超时
  160. if (!connected)
  161. {
  162. client.Close();
  163. return "NG,连接超时";
  164. }
  165. try
  166. {
  167. client.EndConnect(connectResult);
  168. }
  169. catch (SocketException ex)
  170. {
  171. return $"NG,连接失败: {ex.Message}";
  172. }
  173. using (NetworkStream stream = client.GetStream())
  174. {
  175. // 3S读取超时
  176. stream.ReadTimeout = 3000;
  177. StringBuilder fullResponse = new StringBuilder();
  178. string welcomeResponse = ReadStream(stream);
  179. fullResponse.Append(welcomeResponse);
  180. WriteStream(stream, username);
  181. string usernameResponse = ReadStream(stream);
  182. fullResponse.Append(usernameResponse);
  183. WriteStream(stream, password);
  184. string loginResponse = ReadStream(stream);
  185. fullResponse.Append(loginResponse);
  186. return $"OK,{fullResponse.ToString()}";
  187. }
  188. }
  189. }
  190. catch (Exception ex)
  191. {
  192. return $"NG,{ex.Message}";
  193. }
  194. }
  195. private void WriteStream(NetworkStream stream, string text)
  196. {
  197. byte[] data = Encoding.ASCII.GetBytes(text + "\r\n");
  198. stream.Write(data, 0, data.Length);
  199. stream.Flush();
  200. }
  201. private string ReadStream(NetworkStream stream)
  202. {
  203. StringBuilder sb = new StringBuilder();
  204. byte[] buffer = new byte[1024];
  205. int totalBytesRead = 0;
  206. int currentBytesRead = 0;
  207. DateTime lastReadTime = DateTime.UtcNow;
  208. TimeSpan idleTimeout = TimeSpan.FromMilliseconds(800); // 空闲超时
  209. try
  210. {
  211. do
  212. {
  213. currentBytesRead = stream.Read(buffer, 0, buffer.Length);
  214. if (currentBytesRead > 0)
  215. {
  216. sb.Append(Encoding.ASCII.GetString(buffer, 0, currentBytesRead));
  217. totalBytesRead += currentBytesRead;
  218. lastReadTime = DateTime.UtcNow;
  219. }
  220. } while ((DateTime.UtcNow - lastReadTime) < idleTimeout);
  221. }
  222. catch (IOException ioEx)
  223. {
  224. }
  225. catch (ObjectDisposedException)
  226. {
  227. throw;
  228. }
  229. catch (SocketException sockEx)
  230. {
  231. throw new IOException($"NG,网络错误: {sockEx.Message}", sockEx);
  232. }
  233. return sb.ToString();
  234. }
  235. }
  236. private void ShowMsg(int type, string msg)
  237. {
  238. msg = msg.Replace("\r", "").Replace("\n", "");
  239. string msgTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  240. string showMsg = $"{msgTime}: {msg}\n";
  241. if (type == 0)
  242. {
  243. OperatResult.AppendText(showMsg, Color.Red);
  244. }
  245. else if (type == 1)
  246. {
  247. OperatResult.AppendText(showMsg, Color.Green);
  248. }
  249. }
  250. private void Radio1_Click(object sender, EventArgs e)
  251. {
  252. if (Radio1.Checked)
  253. {
  254. Radio2.Checked = false;
  255. }
  256. }
  257. private void Radio2_Click(object sender, EventArgs e)
  258. {
  259. if (Radio2.Checked)
  260. {
  261. Radio1.Checked = false;
  262. }
  263. }
  264. private void Locat1_Click(object sender, EventArgs e)
  265. {
  266. if (Locat1.Checked)
  267. {
  268. Locat2.Checked = false;
  269. Locat3.Checked = false;
  270. }
  271. }
  272. private void Locat2_Click(object sender, EventArgs e)
  273. {
  274. if (Locat2.Checked)
  275. {
  276. Locat1.Checked = false;
  277. Locat3.Checked = false;
  278. }
  279. }
  280. private void Locat3_Click(object sender, EventArgs e)
  281. {
  282. if (Locat3.Checked)
  283. {
  284. Locat1.Checked = false;
  285. Locat2.Checked = false;
  286. }
  287. }
  288. private void Account_KeyDown(object sender, KeyEventArgs e)
  289. {
  290. if (e.KeyCode != Keys.Enter) return;
  291. Password.Focus();
  292. Password.SelectAll();
  293. }
  294. private void Password_KeyDown(object sender, KeyEventArgs e)
  295. {
  296. if (e.KeyCode != Keys.Enter) return;
  297. TestTime.Focus();
  298. TestTime.SelectAll();
  299. }
  300. }
  301. }