Make_WirelessThroughput.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
  19. namespace UAS_MES_NEW.Make
  20. {
  21. public partial class Make_WirelessThroughput : Form
  22. {
  23. public Make_WirelessThroughput()
  24. {
  25. InitializeComponent();
  26. }
  27. StringBuilder SQL = new StringBuilder();
  28. DataTable dt;
  29. DataHelper dh;
  30. private void Make_WirelessThroughput_Load(object sender, EventArgs e)
  31. {
  32. dh = SystemInf.dh;
  33. }
  34. private void Start_Click(object sender, EventArgs e)
  35. {
  36. if (IsCheckSet()) return;
  37. ShowMsg(1, $"开始测试");
  38. var tester = new CameraIperfTester(
  39. cameraIp: ProductList.Text.Trim(),
  40. username: Account.Text.Trim(),
  41. password: ProductList.Text.Trim(),
  42. iperfServerIp: IPList.Text.Trim()
  43. );
  44. var loginResult = tester.TelnetLogin();
  45. if (!loginResult.Success)
  46. {
  47. ShowMsg(0, $"Telnet登录失败,{loginResult.Message}");
  48. return;
  49. }
  50. ShowMsg(1, $"Telnet登录成功");
  51. var testResult = tester.StartIperfTest(
  52. iperfToolPath: @"C:\iperf\iperf3.exe",
  53. logDirectory: @"C:\iperf_logs"
  54. );
  55. }
  56. private void SN_KeyDown(object sender, KeyEventArgs e)
  57. {
  58. if (e.KeyCode != Keys.Enter) return;
  59. UpdateSN("L", SN.Text.Trim());
  60. }
  61. private bool IsCheckSet()
  62. {
  63. if (string.IsNullOrEmpty(IPList.Text))
  64. {
  65. ShowMsg(0, "请选择本地iperf 服务IP地址");
  66. return true;
  67. }
  68. if (string.IsNullOrEmpty(ProductList.Text))
  69. {
  70. ShowMsg(0, "请选择产品固定IP地址");
  71. return true;
  72. }
  73. if (string.IsNullOrEmpty(Account.Text))
  74. {
  75. ShowMsg(0, "请输入Telnet登录账号");
  76. return true;
  77. }
  78. if (string.IsNullOrEmpty(Password.Text))
  79. {
  80. ShowMsg(0, "请输入Telnet登录密码");
  81. return true;
  82. }
  83. if (string.IsNullOrEmpty(TestTime.Text))
  84. {
  85. ShowMsg(0, "请输入测试时长");
  86. return true;
  87. }
  88. //if (Locat1.Checked == false || Locat2.Checked == false || Locat3.Checked == false)
  89. //{
  90. // ShowMsg(0, "请选择固件位置");
  91. // return true;
  92. //}
  93. //if (Radio1.Checked == false || Radio1.Checked == false )
  94. //{
  95. // ShowMsg(0, "请选择测试类型");
  96. // return true;
  97. //}
  98. return false;
  99. }
  100. private void UpdateSN(string type, string sn)
  101. {
  102. if (type == "C")
  103. {
  104. serialNumber.Text = "";
  105. workOrder.Text = "";
  106. productCode.Text = "";
  107. productName.Text = "";
  108. }
  109. else if (type == "L")
  110. {
  111. SQL.Clear();
  112. SQL.Append($@"SELECT ms_sncode,ma_code,pr_code,pr_spec FROM makeserial,make,product
  113. WHERE ms_sncode = '{sn}' AND ms_makecode = ma_code AND ms_prodcode = pr_code");
  114. dt = (DataTable)dh.ExecuteSql(SQL.ToString(), "select");
  115. if (dt.Rows.Count > 0)
  116. {
  117. serialNumber.Text = dt.Rows[0]["ms_sncode"].ToString();
  118. workOrder.Text = dt.Rows[0]["ma_code"].ToString();
  119. productCode.Text = dt.Rows[0]["pr_code"].ToString();
  120. productName.Text = dt.Rows[0]["pr_spec"].ToString();
  121. }
  122. else
  123. {
  124. UpdateSN("C", sn);
  125. }
  126. }
  127. }
  128. private void ShowMsg(int type, string msg)
  129. {
  130. msg = msg.Replace("\r", "").Replace("\n", "");
  131. string msgTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  132. string showMsg = $"{msgTime}: {msg}\n";
  133. if (type == 0)
  134. {
  135. OperatResult.AppendText(showMsg, Color.Red);
  136. }
  137. else if (type == 1)
  138. {
  139. OperatResult.AppendText(showMsg, Color.Green);
  140. }
  141. }
  142. private void Radio1_Click(object sender, EventArgs e)
  143. {
  144. if (Radio1.Checked)
  145. {
  146. Radio2.Checked = false;
  147. }
  148. }
  149. private void Radio2_Click(object sender, EventArgs e)
  150. {
  151. if (Radio2.Checked)
  152. {
  153. Radio1.Checked = false;
  154. }
  155. }
  156. private void Locat1_Click(object sender, EventArgs e)
  157. {
  158. if (Locat1.Checked)
  159. {
  160. Locat2.Checked = false;
  161. Locat3.Checked = false;
  162. }
  163. }
  164. private void Locat2_Click(object sender, EventArgs e)
  165. {
  166. if (Locat2.Checked)
  167. {
  168. Locat1.Checked = false;
  169. Locat3.Checked = false;
  170. }
  171. }
  172. private void Locat3_Click(object sender, EventArgs e)
  173. {
  174. if (Locat3.Checked)
  175. {
  176. Locat1.Checked = false;
  177. Locat2.Checked = false;
  178. }
  179. }
  180. private void Account_KeyDown(object sender, KeyEventArgs e)
  181. {
  182. if (e.KeyCode != Keys.Enter) return;
  183. Password.Focus();
  184. Password.SelectAll();
  185. }
  186. private void Password_KeyDown(object sender, KeyEventArgs e)
  187. {
  188. if (e.KeyCode != Keys.Enter) return;
  189. TestTime.Focus();
  190. TestTime.SelectAll();
  191. }
  192. public class CameraIperfTester
  193. {
  194. string cameraIp;
  195. string username;
  196. string password;
  197. string iperfServerIp;
  198. int telnetPort;
  199. public CameraIperfTester(string cameraIp, string username, string password, string iperfServerIp, int telnetPort = 23)
  200. {
  201. this.cameraIp = cameraIp;
  202. this.username = username;
  203. this.password = password;
  204. this.iperfServerIp = iperfServerIp;
  205. this.telnetPort = telnetPort;
  206. }
  207. public TelnetLoginResult TelnetLogin()
  208. {
  209. try
  210. {
  211. using (Process telnetProcess = new Process())
  212. {
  213. telnetProcess.StartInfo.FileName = "cmd.exe";
  214. telnetProcess.StartInfo.Arguments = $"/c telnet {cameraIp} {telnetPort}";
  215. telnetProcess.StartInfo.UseShellExecute = false;
  216. telnetProcess.StartInfo.RedirectStandardInput = true;
  217. telnetProcess.StartInfo.RedirectStandardOutput = true;
  218. telnetProcess.StartInfo.RedirectStandardError = true;
  219. telnetProcess.StartInfo.CreateNoWindow = true;
  220. StringBuilder output = new StringBuilder();
  221. AutoResetEvent outputWaitHandle = new AutoResetEvent(false);
  222. telnetProcess.OutputDataReceived += (sender, e) =>
  223. {
  224. if (e.Data == null)
  225. {
  226. outputWaitHandle.Set();
  227. }
  228. else
  229. {
  230. output.AppendLine(e.Data);
  231. }
  232. };
  233. telnetProcess.Start();
  234. telnetProcess.BeginOutputReadLine();
  235. // 发送登录凭证
  236. StreamWriter stdin = telnetProcess.StandardInput;
  237. stdin.WriteLine(username);
  238. Thread.Sleep(1000);
  239. stdin.WriteLine(password);
  240. Thread.Sleep(1000);
  241. // 检查登录是否成功的关键词
  242. string result = output.ToString();
  243. if (result.Contains("Login OK") || result.Contains("登录成功") || result.Contains("#") || result.Contains("$"))
  244. {
  245. stdin.WriteLine("exit");
  246. telnetProcess.WaitForExit(3000);
  247. return new TelnetLoginResult
  248. {
  249. Success = true,
  250. Message = result
  251. };
  252. }
  253. else
  254. {
  255. stdin.WriteLine("exit");
  256. telnetProcess.WaitForExit(3000);
  257. return new TelnetLoginResult
  258. {
  259. Success = false,
  260. Message = result
  261. };
  262. }
  263. }
  264. }
  265. catch (Exception ex)
  266. {
  267. return new TelnetLoginResult
  268. {
  269. Success = false,
  270. Message = $"Telnet登录异常: {ex.Message}",
  271. RawOutput = ex.ToString()
  272. };
  273. }
  274. }
  275. public IperfTestResult StartIperfTest(string iperfToolPath, string logDirectory)
  276. {
  277. try
  278. {
  279. // 生成基于时间的日志文件名
  280. string timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
  281. string upLogPath = Path.Combine(logDirectory, $"iperf_up_{timestamp}.log");
  282. string downLogPath = Path.Combine(logDirectory, $"iperf_down_{timestamp}.log");
  283. // 通过telnet执行iperf服务器端命令
  284. var telnetResult = ExecuteTelnetCommands();
  285. if (!telnetResult.Success)
  286. {
  287. return new IperfTestResult
  288. {
  289. Success = false,
  290. Message = "在摄像机端启动iperf服务器失败",
  291. UpLogPath = "",
  292. DownLogPath = ""
  293. };
  294. }
  295. // 测试上行流量
  296. ExecuteIperfTest(iperfToolPath, $"-c {iperfServerIp} -p 5201 -t 10 -w 1M -i 1 -P 8 -R", upLogPath);
  297. // 等待2S
  298. Thread.Sleep(2000);
  299. // 测试下行流量
  300. ExecuteIperfTest(iperfToolPath, $"-c {iperfServerIp} -p 5201 -t 10 -w 1M -i 1 -P 8", downLogPath);
  301. return new IperfTestResult
  302. {
  303. Success = true,
  304. Message = "iperf测试完成",
  305. UpLogPath = upLogPath,
  306. DownLogPath = downLogPath
  307. };
  308. }
  309. catch (Exception ex)
  310. {
  311. return new IperfTestResult
  312. {
  313. Success = false,
  314. Message = $"iperf测试异常: {ex.Message}",
  315. UpLogPath = "",
  316. DownLogPath = ""
  317. };
  318. }
  319. }
  320. private void ExecuteIperfTest(string iperfToolPath, string arguments, string logPath)
  321. {
  322. using (Process iperfProcess = new Process())
  323. {
  324. iperfProcess.StartInfo.FileName = iperfToolPath;
  325. iperfProcess.StartInfo.Arguments = arguments;
  326. iperfProcess.StartInfo.UseShellExecute = false;
  327. iperfProcess.StartInfo.RedirectStandardOutput = true;
  328. iperfProcess.StartInfo.CreateNoWindow = true;
  329. iperfProcess.Start();
  330. string output = iperfProcess.StandardOutput.ReadToEnd();
  331. iperfProcess.WaitForExit();
  332. // 将输出写入日志文件
  333. File.WriteAllText(logPath, output);
  334. }
  335. }
  336. private TelnetLoginResult ExecuteTelnetCommands()
  337. {
  338. try
  339. {
  340. using (Process telnetProcess = new Process())
  341. {
  342. telnetProcess.StartInfo.FileName = "telnet";
  343. telnetProcess.StartInfo.Arguments = $"{cameraIp} {telnetPort}";
  344. telnetProcess.StartInfo.UseShellExecute = false;
  345. telnetProcess.StartInfo.RedirectStandardInput = true;
  346. telnetProcess.StartInfo.RedirectStandardOutput = true;
  347. telnetProcess.StartInfo.RedirectStandardError = true;
  348. telnetProcess.StartInfo.CreateNoWindow = true;
  349. StringBuilder output = new StringBuilder();
  350. AutoResetEvent outputWaitHandle = new AutoResetEvent(false);
  351. telnetProcess.OutputDataReceived += (sender, e) =>
  352. {
  353. if (e.Data == null)
  354. {
  355. outputWaitHandle.Set();
  356. }
  357. else
  358. {
  359. output.AppendLine(e.Data);
  360. }
  361. };
  362. telnetProcess.Start();
  363. telnetProcess.BeginOutputReadLine();
  364. StreamWriter stdin = telnetProcess.StandardInput;
  365. // 发送登录凭证
  366. stdin.WriteLine(username);
  367. Thread.Sleep(1000);
  368. stdin.WriteLine(password);
  369. Thread.Sleep(2000);
  370. // 执行iperf相关命令
  371. stdin.WriteLine("killall iperf3_mstar");
  372. Thread.Sleep(1000);
  373. stdin.WriteLine("cp /mnt/tf/usb1_1/iperf3_mstar /var/tmp/ -f");
  374. Thread.Sleep(1000);
  375. stdin.WriteLine("chmod a+x /var/tmp/iperf3_mstar");
  376. Thread.Sleep(1000);
  377. stdin.WriteLine("/var/tmp/iperf3_mstar -s -p 5201 -i 1 &");
  378. Thread.Sleep(2000);
  379. // 发送退出命令
  380. stdin.WriteLine("exit");
  381. telnetProcess.WaitForExit(5000);
  382. return new TelnetLoginResult
  383. {
  384. Success = true,
  385. Message = "Telnet命令执行完成",
  386. RawOutput = output.ToString()
  387. };
  388. }
  389. }
  390. catch (Exception ex)
  391. {
  392. return new TelnetLoginResult
  393. {
  394. Success = false,
  395. Message = $"Telnet命令执行异常: {ex.Message}",
  396. RawOutput = ex.ToString()
  397. };
  398. }
  399. }
  400. public class TelnetLoginResult
  401. {
  402. public bool Success { get; set; }
  403. public string Message { get; set; }
  404. public string RawOutput { get; set; }
  405. }
  406. public class IperfTestResult
  407. {
  408. public bool Success { get; set; }
  409. public string Message { get; set; }
  410. public string UpLogPath { get; set; }
  411. public string DownLogPath { get; set; }
  412. }
  413. }
  414. }
  415. }