Make_WirelessThroughput.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. using DevExpress.Utils.Drawing.Helpers;
  2. using DevExpress.XtraReports;
  3. using NPOI.SS.Formula.Functions;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Diagnostics;
  9. using System.Drawing;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Net;
  13. using System.Net.Sockets;
  14. using System.Text;
  15. using System.Text.RegularExpressions;
  16. using System.Threading;
  17. using System.Web.Services.Description;
  18. using System.Web.UI.WebControls.WebParts;
  19. using System.Windows.Forms;
  20. using UAS_MES_NEW.DataOperate;
  21. using UAS_MES_NEW.Entity;
  22. using UAS_MES_NEW.PublicMethod;
  23. namespace UAS_MES_NEW.Make
  24. {
  25. public partial class Make_WirelessThroughput : Form
  26. {
  27. public Make_WirelessThroughput()
  28. {
  29. InitializeComponent();
  30. }
  31. StringBuilder SQL = new StringBuilder();
  32. DataTable dt;
  33. DataHelper dh;
  34. string curIp, telnetPort,testType;
  35. List<string> cmdList = new List<string>();
  36. private void Make_WirelessThroughput_Load(object sender, EventArgs e)
  37. {
  38. dh = SystemInf.dh;
  39. try
  40. {
  41. IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
  42. foreach (IPAddress ip in hostEntry.AddressList)
  43. {
  44. if (ip.AddressFamily == AddressFamily.InterNetwork)
  45. {
  46. curIp = ip.ToString();
  47. }
  48. }
  49. }
  50. catch (Exception ex)
  51. {
  52. ShowMsg(0, $"获取iperf服务器地址或产品IP失败,请手动填写,{ex.Message}");
  53. }
  54. IPList.Text = "5021";
  55. telnetPort = "23";
  56. Locat1.Visible = false;
  57. Locat2.Visible = false;
  58. Locat3.Visible = false;
  59. }
  60. private void SN_KeyDown(object sender, KeyEventArgs e)
  61. {
  62. if (e.KeyCode != Keys.Enter) return;
  63. if (IPList.Text.IndexOf(':') > 0) IPList.Text = IPList.Text.Replace(":", ":");
  64. else IPList.Text = IPList.Text.Trim();
  65. SN.Text = SN.Text.Trim().Replace(";", ";");
  66. string sn = SN.Text.Trim();
  67. SQL.Clear();
  68. SQL.Append($@"SELECT pr_test_account,pr_test_password,pr_test_start,pr_test_cmd1,pr_test_cmd2,pr_test_cmd3 FROM makeserial,product
  69. WHERE ms_sncode = '{sn}' AND ms_prodcode = pr_code ORDER BY ms_id DESC");
  70. dt = (DataTable)dh.ExecuteSql(SQL.ToString(), "select");
  71. if (dt.Rows.Count == 0)
  72. {
  73. ShowMsg(0, $"序列号:{sn}未维护吞吐量基础信息");
  74. }
  75. else
  76. {
  77. Account.Text = dt.Rows[0]["pr_test_account"].ToString();
  78. Password.Text = dt.Rows[0]["pr_test_password"].ToString();
  79. cmdList.Add(dt.Rows[0]["pr_test_start"].ToString());
  80. if(testType == "2.4G")
  81. {
  82. cmdList.Add(dt.Rows[0]["pr_test_cmd1"].ToString());
  83. }
  84. else
  85. {
  86. cmdList.Add(dt.Rows[0]["pr_test_cmd2"].ToString());
  87. }
  88. cmdList.Add(dt.Rows[0]["pr_test_cmd3"].ToString());
  89. }
  90. if (IsCheckSet()) return;
  91. UpdateSN("L", SN.Text.Trim());
  92. ShowMsg(1, $"开始测试");
  93. var tester = new CameraIperfTester(
  94. timeOut: TestTime.Text.Trim(),
  95. cameraIp: ProductList.Text.Trim(),
  96. cameraPort: Convert.ToInt32(telnetPort),
  97. username: Account.Text.Trim(),
  98. password: Password.Text.Trim(),
  99. iperfServerIp: curIp,
  100. iperfServerPort: Convert.ToInt32(IPList.Text.Trim())
  101. );
  102. string parentPath = Path.GetDirectoryName(ExePath.Text.Trim());
  103. string logsPath = Path.Combine(parentPath, "iperf_logs");
  104. if (!Directory.Exists(logsPath))
  105. {
  106. Directory.CreateDirectory(logsPath);
  107. }
  108. string Msg = tester.TestStart(SN.Text.Trim(), ExePath.Text.Trim(), logsPath, cmdList);
  109. if (Msg.StartsWith("NG"))
  110. {
  111. ShowMsg(0, Msg);
  112. return;
  113. }
  114. string upRate = Msg.Split('|')[0].Split(',')[1];
  115. string downRate = Msg.Split('|')[1];
  116. ShowMsg(1, $"OK,上行速率: {upRate},下行速率: {downRate}");
  117. ResMax.Text = upRate;
  118. ResMin.Text = downRate;
  119. if (testType == "2.4G")
  120. {
  121. CheckPassStation(SN.Text, "PASS", upRate, downRate, testType);
  122. }
  123. else
  124. {
  125. CheckPassStation(SN.Text, "PASS", upRate, downRate, testType);
  126. }
  127. }
  128. private void CheckPassStation(string sn, string testRes, string upRate, string downRate, string resType)
  129. {
  130. List<string> param = new List<string>() { };
  131. string outMsg = "";
  132. param.Add(workOrder.Text);
  133. param.Add(sn);
  134. param.Add(User.CurrentStepCode);
  135. param.Add(testRes);
  136. param.Add("");
  137. param.Add("");
  138. param.Add("ThruputCmd");
  139. param.Add($"upRate/{upRate};downRate/{downRate}");
  140. param.Add(outMsg);
  141. string[] paramList = param.ToArray();
  142. dh.CallProcedure("cs_insert_testrejects", ref paramList);
  143. if (paramList[8].Substring(0, 2) == "OK")
  144. {
  145. ShowMsg(1, $"序列号{sn}采集成功:测试结果为{testRes}");
  146. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, workOrder.Text, User.UserLineCode, User.UserSourceCode, "无线吞吐量", "无线吞吐量过站成功", sn, "");
  147. }
  148. dt = (DataTable)dh.ExecuteSql($"SELECT mp_id FROM makeprocess WHERE mp_sncode = '{sn}' AND instr(mp_stepname, '吞吐量') > 0", "select");
  149. if (dt.Rows.Count > 0)
  150. {
  151. ShowMsg(1, $"序列号:{sn}已经过站记录");
  152. return;
  153. }
  154. dt = (DataTable)dh.ExecuteSql($"select sm_id from steptestmain where INSTR(sm_sourcetype, 'Thruput') > 0 and sm_sn = '{sn}' order by sm_indate desc", "select");
  155. string sm_id = dt.Rows[0]["sm_id"].ToString();
  156. Dictionary<string, string> testArr = new Dictionary<string, string>();
  157. testArr.Add("测试项", $"{resType}吞吐量测试");
  158. testArr.Add("参数名称", $"{resType}上行测试值");
  159. testArr.Add("结果", "0");
  160. testArr.Add("结果描述", "OK");
  161. testArr.Add("产测指标门限", "");
  162. testArr.Add("测试时长", "10");
  163. testArr.Add("值", upRate);
  164. foreach (var item in testArr)
  165. {
  166. dh.ExecuteSql($@"INSERT INTO steptestdetail (sd_id,sd_smid,sd_sn,sd_makecode,sd_machinecode,sd_indate,
  167. sd_class,sd_length,sd_actvalue,sd_detno) VALUES
  168. ( steptestdetail_seq.NEXTVAL,'{sm_id}','{sn}','{workOrder.Text.Trim()}', 'ZTE',sysdate,
  169. '{item.Key}','100','{item.Value}',1 )", "insert");
  170. }
  171. Dictionary<string, string> testArr1 = new Dictionary<string, string>();
  172. testArr1.Add("测试项", $"{resType}吞吐量测试");
  173. testArr1.Add("参数名称", $"{resType}下行测试值");
  174. testArr1.Add("结果", "0");
  175. testArr1.Add("结果描述", "OK");
  176. testArr1.Add("产测指标门限", "");
  177. testArr1.Add("测试时长", "10");
  178. testArr1.Add("值", downRate);
  179. foreach (var item in testArr1)
  180. {
  181. dh.ExecuteSql($@"INSERT INTO steptestdetail (sd_id,sd_smid,sd_sn,sd_makecode,sd_machinecode,sd_indate,
  182. sd_class,sd_length,sd_actvalue,sd_detno) VALUES
  183. ( steptestdetail_seq.NEXTVAL,'{sm_id}','{sn}','{workOrder.Text.Trim()}', 'ZTE',sysdate,
  184. '{item.Key}','100','{item.Value}',2 )", "insert");
  185. }
  186. string oWO, oWOId, oErrMsg = "";
  187. if (LogicHandler.CheckStepSNAndMacode(workOrder.Text, User.UserSourceCode, sn, User.UserCode, out oWO, out oWOId, out oErrMsg))
  188. {
  189. if (LogicHandler.SetStepResult(oWO, User.UserSourceCode, sn, "无线吞吐量", "OK", User.UserCode, out oErrMsg))
  190. {
  191. ShowMsg(1, $"序列号{sn},过站记录成功");
  192. }
  193. else
  194. {
  195. ShowMsg(0, $"序列号{sn},处理过站NG:{oErrMsg}");
  196. }
  197. }
  198. else
  199. {
  200. ShowMsg(0, $"序列号{sn},过站核对NG:{oErrMsg}");
  201. }
  202. }
  203. private bool IsCheckSet()
  204. {
  205. if (string.IsNullOrEmpty(IPList.Text))
  206. {
  207. ShowMsg(0, "请选择本地iperf 服务IP地址");
  208. return true;
  209. }
  210. if (string.IsNullOrEmpty(ProductList.Text))
  211. {
  212. ShowMsg(0, "请选择产品固定IP地址");
  213. return true;
  214. }
  215. if (string.IsNullOrEmpty(Account.Text))
  216. {
  217. ShowMsg(0, "请输入Telnet登录账号");
  218. return true;
  219. }
  220. if (string.IsNullOrEmpty(Password.Text))
  221. {
  222. ShowMsg(0, "请输入Telnet登录密码");
  223. return true;
  224. }
  225. if (string.IsNullOrEmpty(ExePath.Text))
  226. {
  227. ShowMsg(0, "请输入启动iperf3文件");
  228. return true;
  229. }
  230. if (!File.Exists(ExePath.Text))
  231. {
  232. Console.WriteLine("iperf3启动文件不存在");
  233. }
  234. if (string.IsNullOrEmpty(TestTime.Text))
  235. {
  236. ShowMsg(0, "请输入测试时长");
  237. return true;
  238. }
  239. //if (comboBox1.SelectedIndex == -1)
  240. //{
  241. // ShowMsg(0, "请选择指令模式");
  242. // return true;
  243. //}
  244. //if (Locat1.Checked == false || Locat2.Checked == false || Locat3.Checked == false)
  245. //{
  246. // ShowMsg(0, "请选择固件位置");
  247. // return true;
  248. //}
  249. if (Radio1.Checked == false || Radio1.Checked == false)
  250. {
  251. ShowMsg(0, "请选择测试类型");
  252. return true;
  253. }
  254. return false;
  255. }
  256. private void UpdateSN(string type, string sn)
  257. {
  258. if (type == "C")
  259. {
  260. serialNumber.Text = "";
  261. workOrder.Text = "";
  262. productCode.Text = "";
  263. productName.Text = "";
  264. }
  265. else if (type == "L")
  266. {
  267. SQL.Clear();
  268. SQL.Append($@"SELECT ms_sncode,ma_code,pr_code,pr_spec FROM makeserial,make,product
  269. WHERE ms_sncode = '{sn}' AND ms_makecode = ma_code AND ms_prodcode = pr_code");
  270. dt = (DataTable)dh.ExecuteSql(SQL.ToString(), "select");
  271. if (dt.Rows.Count > 0)
  272. {
  273. serialNumber.Text = dt.Rows[0]["ms_sncode"].ToString();
  274. workOrder.Text = dt.Rows[0]["ma_code"].ToString();
  275. productCode.Text = dt.Rows[0]["pr_code"].ToString();
  276. productName.Text = dt.Rows[0]["pr_spec"].ToString();
  277. }
  278. else
  279. {
  280. UpdateSN("C", sn);
  281. }
  282. }
  283. }
  284. public class CameraIperfTester
  285. {
  286. int timeOut;
  287. string cameraIp, iperfServerIp, username, password;
  288. int cameraPort, iperfServerPort;
  289. string SN, iperfPath, logDirectory;
  290. private TcpClient _telnetClient;
  291. private NetworkStream _telnetStream;
  292. string sVal1, sVal2, rVal1, rVal2;
  293. public CameraIperfTester(string timeOut, string cameraIp, int cameraPort, string username, string password, string iperfServerIp, int iperfServerPort)
  294. {
  295. this.timeOut = Convert.ToInt32(timeOut) * 1000;
  296. this.cameraIp = cameraIp;
  297. this.cameraPort = cameraPort;
  298. this.username = username;
  299. this.password = password;
  300. this.iperfServerIp = iperfServerIp;
  301. this.iperfServerPort = iperfServerPort;
  302. }
  303. public string TestStart(string sn, string iP, string lD,List<string> cmdList)
  304. {
  305. StringBuilder Msg = new StringBuilder();
  306. Msg.Append(TelnetConnect());
  307. if (Msg.ToString().StartsWith("NG")) return Msg.ToString();
  308. SN = sn;
  309. iperfPath = iP;
  310. logDirectory = lD;
  311. Msg.Clear();
  312. Msg.Append(StartIperfTest(cmdList));
  313. return Msg.ToString();
  314. }
  315. public string TelnetConnect()
  316. {
  317. try
  318. {
  319. _telnetClient = new TcpClient();
  320. IAsyncResult connectResult = _telnetClient.BeginConnect(cameraIp, cameraPort, null, null);
  321. bool connected = connectResult.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(timeOut));
  322. if (!connected)
  323. {
  324. _telnetClient.Close();
  325. return "NG,连接超时";
  326. }
  327. try
  328. {
  329. _telnetClient.EndConnect(connectResult);
  330. }
  331. catch (SocketException ex)
  332. {
  333. return $"NG,连接失败: {ex.Message}";
  334. }
  335. _telnetStream = _telnetClient.GetStream();
  336. _telnetStream.ReadTimeout = timeOut;
  337. StringBuilder fullResponse = new StringBuilder();
  338. string welcomeResponse = ReadStream(_telnetStream);
  339. if (welcomeResponse.StartsWith("NG")) return welcomeResponse;
  340. fullResponse.Append(welcomeResponse);
  341. WriteStream(_telnetStream, username);
  342. string usernameResponse = ReadStream(_telnetStream);
  343. if (usernameResponse.StartsWith("NG")) return usernameResponse;
  344. fullResponse.Append(usernameResponse);
  345. WriteStream(_telnetStream, password);
  346. string loginResponse = ReadStream(_telnetStream);
  347. if (loginResponse.StartsWith("NG")) return loginResponse;
  348. fullResponse.Append(loginResponse);
  349. return $"OK,{fullResponse.ToString()}";
  350. }
  351. catch (Exception ex)
  352. {
  353. Close();
  354. return $"NG,{ex.Message}";
  355. }
  356. }
  357. public string StartIperfTest(List<string> cmdList)
  358. {
  359. try
  360. {
  361. StringBuilder streamTxt = new StringBuilder();
  362. string upRate, downRate, cmdLog, upLog, downLog;
  363. WriteStream(_telnetStream, cmdList[2]);
  364. streamTxt.Append(ReadStream(_telnetStream));
  365. WriteStream(_telnetStream, cmdList[0].Trim().Replace("Port", iperfServerPort.ToString()));
  366. streamTxt.Append(ReadStream(_telnetStream));
  367. cmdLog = Path.Combine(logDirectory, $"{SN}_CMD.log");
  368. File.WriteAllText(cmdLog, streamTxt.ToString());
  369. upLog = Path.Combine(logDirectory, $"{SN}_up.log");
  370. downLog = Path.Combine(logDirectory, $"{SN}_down.log");
  371. RunIperfClient(cmdList[1].Trim().Replace("IP", cameraIp).Replace("Port", iperfServerPort.ToString()) + " -R", iperfPath, upLog);
  372. RunIperfClient(cmdList[1].Trim().Replace("IP", cameraIp).Replace("Port", iperfServerPort.ToString()), iperfPath, downLog);
  373. upRate = ParseIperfBitrate(upLog);
  374. downRate = ParseIperfBitrate(downLog);
  375. Close();
  376. if (string.IsNullOrEmpty(upRate) || string.IsNullOrEmpty(downRate))
  377. {
  378. return $"NG,产前维护指令测试失败,请核对指令或使用吞吐量日志解析";
  379. }
  380. return $"OK,{upRate}|{downRate}";
  381. /*if (cmdType == "0")
  382. {
  383. WriteStream(_telnetStream, "killall iperf3");
  384. streamTxt.Append(ReadStream(_telnetStream));
  385. Thread.Sleep(500);
  386. WriteStream(_telnetStream, $"iperf3 -s -p {iperfServerPort} -i 1&");
  387. streamTxt.Append(ReadStream(_telnetStream));
  388. Thread.Sleep(500);
  389. cmdLog = Path.Combine(logDirectory, $"{SN}_CMD.log");
  390. File.WriteAllText(cmdLog, streamTxt.ToString());
  391. upLog = Path.Combine(logDirectory, $"{SN}_up.log");
  392. downLog = Path.Combine(logDirectory, $"{SN}_down.log");
  393. RunIperfClient($"-c {cameraIp} -p {iperfServerPort} -t 10 -w 1M -i 1 -P 8 -R", iperfPath, upLog);
  394. RunIperfClient($"-c {cameraIp} -p {iperfServerPort} -t 10 -w 1M -i 1 -P 8", iperfPath, downLog);
  395. upRate = ParseIperfBitrate(upLog);
  396. downRate = ParseIperfBitrate(downLog);
  397. Close();
  398. if(string.IsNullOrEmpty(upRate) || string.IsNullOrEmpty(downRate))
  399. {
  400. return $"NG,指令一无法获测试结果,请核对指令或使用吞吐量日志解析";
  401. }
  402. return $"OK,{upRate}|{downRate}";
  403. }
  404. else
  405. {
  406. WriteStream(_telnetStream, "killall iperf3_mstar");
  407. streamTxt.Append(ReadStream(_telnetStream));
  408. Thread.Sleep(500);
  409. WriteStream(_telnetStream, "cp /mnt/tf/usb1_1/iperf3_mstar /var/tmp/ -f");
  410. streamTxt.Append(ReadStream(_telnetStream));
  411. Thread.Sleep(500);
  412. WriteStream(_telnetStream, "chmod a+x /var/tmp/iperf3_mstar");
  413. streamTxt.Append(ReadStream(_telnetStream));
  414. Thread.Sleep(500);
  415. WriteStream(_telnetStream, $"/var/tmp/iperf3_mstar -s -p {iperfServerPort} -i 1 &");
  416. streamTxt.Append(ReadStream(_telnetStream));
  417. cmdLog = Path.Combine(logDirectory, $"{SN}_CMD.log");
  418. File.WriteAllText(cmdLog, streamTxt.ToString());
  419. upLog = Path.Combine(logDirectory, $"{SN}_up.log");
  420. downLog = Path.Combine(logDirectory, $"{SN}_down.log");
  421. RunIperfClient($"/var/tmp/iperf3 -c {cameraIp} -p {iperfServerPort} -t 6 -w 1M -i 1 -l 1518 -P 8 -R", iperfPath, upLog);
  422. RunIperfClient($"/var/tmp/iperf3 -c {cameraIp} -p {iperfServerPort} -t 6 -w 1M -i 1 -l 1518 -P 8", iperfPath, downLog);
  423. upRate = ParseIperfBitrate(upLog);
  424. downRate = ParseIperfBitrate(downLog);
  425. Close();
  426. if (string.IsNullOrEmpty(upRate) || string.IsNullOrEmpty(downRate))
  427. {
  428. return $"NG,指令二获取测试结果,请核对指令或使用吞吐量日志解析";
  429. }
  430. return $"OK,{upRate}|{downRate}";
  431. }*/
  432. }
  433. catch (Exception ex)
  434. {
  435. return $"NG,{ex.Message}";
  436. }
  437. }
  438. private void RunIperfClient(string args, string path, string logPath)
  439. {
  440. var psi = new ProcessStartInfo
  441. {
  442. FileName = path,
  443. Arguments = args,
  444. RedirectStandardOutput = true,
  445. RedirectStandardError = true,
  446. UseShellExecute = false,
  447. CreateNoWindow = true,
  448. WorkingDirectory = Path.GetDirectoryName(path)
  449. };
  450. using (var process = Process.Start(psi))
  451. {
  452. StringBuilder outputBuilder = new StringBuilder();
  453. StringBuilder errorBuilder = new StringBuilder();
  454. process.OutputDataReceived += (sender, e) =>
  455. {
  456. if (!string.IsNullOrEmpty(e.Data))
  457. {
  458. outputBuilder.AppendLine(e.Data);
  459. }
  460. };
  461. process.ErrorDataReceived += (sender, e) =>
  462. {
  463. if (!string.IsNullOrEmpty(e.Data))
  464. {
  465. errorBuilder.AppendLine(e.Data);
  466. }
  467. };
  468. process.BeginOutputReadLine();
  469. process.BeginErrorReadLine();
  470. process.WaitForExit();
  471. File.WriteAllText(logPath, outputBuilder.ToString() + errorBuilder.ToString());
  472. }
  473. }
  474. public string ParseIperfBitrate(string logPath)
  475. {
  476. if (!File.Exists(logPath)) return "0.0";
  477. var lines = File.ReadAllLines(logPath);
  478. List<string> logItem = new List<string> { };
  479. foreach (var line in lines)
  480. {
  481. if (line.Trim().StartsWith("[SUM]") && line.Contains("receiver"))
  482. {
  483. logItem.Add(line);
  484. }
  485. }
  486. //if (logItem.Count > 0 && logItem[logItem.Count - 2].Contains("sender"))
  487. //{
  488. // var match = Regex.Match(logItem[logItem.Count - 2], @"(\d+\.\d+\s+MBytes).*?(\d+\.\d+\s+Mbits/sec)");
  489. // if (match.Success)
  490. // {
  491. // sVal1 = match.Groups[1].Value;
  492. // sVal2 = match.Groups[2].Value;
  493. // }
  494. //}
  495. if (logItem.Count > 0 && logItem[logItem.Count - 1].Contains("receiver"))
  496. {
  497. //var match = Regex.Match(logItem[logItem.Count - 1], @"(\d+\.\d+\s+MBytes).*?(\d+\.\d+\s+Mbits/sec)");
  498. var match = Regex.Match(logItem[logItem.Count - 1], @"(\d+\.?\d*)\s*MBytes\s*(\d+\.?\d*)\s*Mbits/sec");
  499. if (match.Success)
  500. {
  501. rVal1 = match.Groups[1].Value + "MBytes";
  502. rVal2 = match.Groups[2].Value + "Mbits/sec";
  503. }
  504. }
  505. //double num1 = (double.Parse(sVal1.Split(' ')[0]) + double.Parse(rVal1.Split(' ')[0])) / 2;
  506. //double num2 = (double.Parse(sVal2.Split(' ')[0]) + double.Parse(rVal2.Split(' ')[0])) / 2;
  507. return rVal2;
  508. }
  509. private string WriteStream(NetworkStream stream, string data)
  510. {
  511. try
  512. {
  513. byte[] dataBytes = Encoding.UTF8.GetBytes(data + "\r\n");
  514. stream.Write(dataBytes, 0, dataBytes.Length);
  515. stream.Flush();
  516. return $"OK";
  517. }
  518. catch (Exception ex)
  519. {
  520. Close();
  521. return $"NG,写入数据时发生错误: {ex.Message}";
  522. }
  523. }
  524. private string ReadStream(NetworkStream stream)
  525. {
  526. try
  527. {
  528. StringBuilder response = new StringBuilder();
  529. byte[] buffer = new byte[1024];
  530. int bytesRead;
  531. while (stream.DataAvailable || response.Length == 0)
  532. {
  533. bytesRead = stream.Read(buffer, 0, buffer.Length);
  534. if (bytesRead > 0)
  535. {
  536. response.Append(Encoding.UTF8.GetString(buffer, 0, bytesRead));
  537. }
  538. else
  539. {
  540. break;
  541. }
  542. Thread.Sleep(100);
  543. }
  544. return response.ToString();
  545. }
  546. catch (IOException ioEx) when (ioEx.InnerException is SocketException socketEx && socketEx.SocketErrorCode == SocketError.TimedOut)
  547. {
  548. Close();
  549. return "NG,读取数据超时";
  550. }
  551. catch (Exception ex)
  552. {
  553. Close();
  554. return $"NG,读取数据时发生错误: {ex.Message}";
  555. }
  556. }
  557. private void Close()
  558. {
  559. _telnetClient?.Close();
  560. _telnetStream?.Close();
  561. }
  562. }
  563. private void ShowMsg(int type, string msg)
  564. {
  565. msg = msg.Replace("\r", "").Replace("\n", "");
  566. string msgTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  567. string showMsg = $"{msgTime}: {msg}\n";
  568. if (type == 0)
  569. {
  570. OperatResult.AppendText(showMsg, Color.Red);
  571. }
  572. else if (type == 1)
  573. {
  574. OperatResult.AppendText(showMsg, Color.Green);
  575. }
  576. }
  577. private void Radio1_Click(object sender, EventArgs e)
  578. {
  579. if (Radio1.Checked)
  580. {
  581. testType = "2.4G";
  582. Radio2.Checked = false;
  583. }
  584. }
  585. private void Radio2_Click(object sender, EventArgs e)
  586. {
  587. if (Radio2.Checked)
  588. {
  589. testType = "5G";
  590. Radio1.Checked = false;
  591. }
  592. }
  593. private void Locat1_Click(object sender, EventArgs e)
  594. {
  595. if (Locat1.Checked)
  596. {
  597. Locat2.Checked = false;
  598. Locat3.Checked = false;
  599. }
  600. }
  601. private void Locat2_Click(object sender, EventArgs e)
  602. {
  603. if (Locat2.Checked)
  604. {
  605. Locat1.Checked = false;
  606. Locat3.Checked = false;
  607. }
  608. }
  609. private void Locat3_Click(object sender, EventArgs e)
  610. {
  611. if (Locat3.Checked)
  612. {
  613. Locat1.Checked = false;
  614. Locat2.Checked = false;
  615. }
  616. }
  617. private void Account_KeyDown(object sender, KeyEventArgs e)
  618. {
  619. if (e.KeyCode != Keys.Enter) return;
  620. Password.Focus();
  621. Password.SelectAll();
  622. }
  623. private void Password_KeyDown(object sender, KeyEventArgs e)
  624. {
  625. if (e.KeyCode != Keys.Enter) return;
  626. TestTime.Focus();
  627. TestTime.SelectAll();
  628. }
  629. private void ExePath_Click(object sender, EventArgs e)
  630. {
  631. using (OpenFileDialog openFileDialog = new OpenFileDialog())
  632. {
  633. openFileDialog.Title = "选择iperf3启动文件";
  634. openFileDialog.Filter = "可执行文件 (*.exe)|*.exe|所有文件 (*.*)|*.*";
  635. openFileDialog.FilterIndex = 1;
  636. openFileDialog.RestoreDirectory = true;
  637. // 设置初始目录(可选)
  638. if (!string.IsNullOrEmpty(ExePath.Text) && System.IO.File.Exists(ExePath.Text))
  639. {
  640. openFileDialog.InitialDirectory = System.IO.Path.GetDirectoryName(ExePath.Text);
  641. }
  642. else
  643. {
  644. openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
  645. }
  646. if (openFileDialog.ShowDialog() == DialogResult.OK)
  647. {
  648. ExePath.Text = openFileDialog.FileName;
  649. ShowMsg(1, $"已成功选择iperf3启动文件");
  650. }
  651. }
  652. }
  653. private void Clear_Click(object sender, EventArgs e)
  654. {
  655. OperatResult.Clear();
  656. }
  657. private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  658. {
  659. if (comboBox1.SelectedIndex == -1) return;
  660. if (string.IsNullOrEmpty(IPList.Text))
  661. {
  662. ShowMsg(0, $"请先选择iperf服务地址");
  663. comboBox1.SelectedIndex = -1;
  664. return;
  665. }
  666. if (string.IsNullOrEmpty(ProductList.Text))
  667. {
  668. ShowMsg(0, $"请先选择产品IP地址");
  669. comboBox1.SelectedIndex = -1;
  670. return;
  671. }
  672. string iperfServerPort = IPList.Text.Trim().Split(':')[1];
  673. string cameraIp = ProductList.Text.Trim().Split(':')[0];
  674. if (comboBox1.SelectedIndex == 0)
  675. {
  676. ShowMsg(1, $"指令一:");
  677. ShowMsg(1, $" killall iperf3");
  678. ShowMsg(1, $" iperf3 -s -p {iperfServerPort} -i 1&");
  679. ShowMsg(1, $" -c {cameraIp} -p {iperfServerPort} -t 10 -w 1M -i 1 -P 8 -R");
  680. ShowMsg(1, $" -c {cameraIp} -p {iperfServerPort} -t 10 -w 1M -i 1 -P 8");
  681. }
  682. else if (comboBox1.SelectedIndex == 1)
  683. {
  684. ShowMsg(1, $"指令二:");
  685. ShowMsg(1, $" killall iperf3_mstar");
  686. ShowMsg(1, $" cp /mnt/tf/usb1_1/iperf3_mstar /var/tmp/ -f");
  687. ShowMsg(1, $" chmod a+x /var/tmp/iperf3_mstar");
  688. ShowMsg(1, $" /var/tmp/iperf3_mstar -s -p {iperfServerPort} -i 1 &");
  689. ShowMsg(1, $" /var/tmp/iperf3 -c {cameraIp} -p {iperfServerPort} -t 6 -w 1M -i 1 -l 1518 -P 8 -R");
  690. ShowMsg(1, $" /var/tmp/iperf3 -c {cameraIp} -p {iperfServerPort} -t 6 -w 1M -i 1 -l 1518 -P 8");
  691. }
  692. }
  693. }
  694. }