using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Net.Sockets; using System.Text; using System.Text.RegularExpressions; using System.Threading; using System.Web.Services.Description; using System.Web.UI.WebControls.WebParts; using System.Windows.Forms; using UAS_MES_NEW.DataOperate; using UAS_MES_NEW.Entity; using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel; namespace UAS_MES_NEW.Make { public partial class Make_WirelessThroughput : Form { public Make_WirelessThroughput() { InitializeComponent(); } StringBuilder SQL = new StringBuilder(); DataTable dt; DataHelper dh; private void Make_WirelessThroughput_Load(object sender, EventArgs e) { dh = SystemInf.dh; } private void Start_Click(object sender, EventArgs e) { if (IsCheckSet()) return; ShowMsg(1, $"开始测试"); var tester = new CameraIperfTester( cameraIp: ProductList.Text.Trim(), username: Account.Text.Trim(), password: ProductList.Text.Trim(), iperfServerIp: IPList.Text.Trim() ); var loginResult = tester.TelnetLogin(); if (!loginResult.Success) { ShowMsg(0, $"Telnet登录失败,{loginResult.Message}"); return; } ShowMsg(1, $"Telnet登录成功"); var testResult = tester.StartIperfTest( iperfToolPath: @"C:\iperf\iperf3.exe", logDirectory: @"C:\iperf_logs" ); } private void SN_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode != Keys.Enter) return; UpdateSN("L", SN.Text.Trim()); } private bool IsCheckSet() { if (string.IsNullOrEmpty(IPList.Text)) { ShowMsg(0, "请选择本地iperf 服务IP地址"); return true; } if (string.IsNullOrEmpty(ProductList.Text)) { ShowMsg(0, "请选择产品固定IP地址"); return true; } if (string.IsNullOrEmpty(Account.Text)) { ShowMsg(0, "请输入Telnet登录账号"); return true; } if (string.IsNullOrEmpty(Password.Text)) { ShowMsg(0, "请输入Telnet登录密码"); return true; } if (string.IsNullOrEmpty(TestTime.Text)) { ShowMsg(0, "请输入测试时长"); return true; } //if (Locat1.Checked == false || Locat2.Checked == false || Locat3.Checked == false) //{ // ShowMsg(0, "请选择固件位置"); // return true; //} //if (Radio1.Checked == false || Radio1.Checked == false ) //{ // ShowMsg(0, "请选择测试类型"); // return true; //} return false; } private void UpdateSN(string type, string sn) { if (type == "C") { serialNumber.Text = ""; workOrder.Text = ""; productCode.Text = ""; productName.Text = ""; } else if (type == "L") { SQL.Clear(); SQL.Append($@"SELECT ms_sncode,ma_code,pr_code,pr_spec FROM makeserial,make,product WHERE ms_sncode = '{sn}' AND ms_makecode = ma_code AND ms_prodcode = pr_code"); dt = (DataTable)dh.ExecuteSql(SQL.ToString(), "select"); if (dt.Rows.Count > 0) { serialNumber.Text = dt.Rows[0]["ms_sncode"].ToString(); workOrder.Text = dt.Rows[0]["ma_code"].ToString(); productCode.Text = dt.Rows[0]["pr_code"].ToString(); productName.Text = dt.Rows[0]["pr_spec"].ToString(); } else { UpdateSN("C", sn); } } } private void ShowMsg(int type, string msg) { msg = msg.Replace("\r", "").Replace("\n", ""); string msgTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); string showMsg = $"{msgTime}: {msg}\n"; if (type == 0) { OperatResult.AppendText(showMsg, Color.Red); } else if (type == 1) { OperatResult.AppendText(showMsg, Color.Green); } } private void Radio1_Click(object sender, EventArgs e) { if (Radio1.Checked) { Radio2.Checked = false; } } private void Radio2_Click(object sender, EventArgs e) { if (Radio2.Checked) { Radio1.Checked = false; } } private void Locat1_Click(object sender, EventArgs e) { if (Locat1.Checked) { Locat2.Checked = false; Locat3.Checked = false; } } private void Locat2_Click(object sender, EventArgs e) { if (Locat2.Checked) { Locat1.Checked = false; Locat3.Checked = false; } } private void Locat3_Click(object sender, EventArgs e) { if (Locat3.Checked) { Locat1.Checked = false; Locat2.Checked = false; } } private void Account_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode != Keys.Enter) return; Password.Focus(); Password.SelectAll(); } private void Password_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode != Keys.Enter) return; TestTime.Focus(); TestTime.SelectAll(); } public class CameraIperfTester { string cameraIp; string username; string password; string iperfServerIp; int telnetPort; public CameraIperfTester(string cameraIp, string username, string password, string iperfServerIp, int telnetPort = 23) { this.cameraIp = cameraIp; this.username = username; this.password = password; this.iperfServerIp = iperfServerIp; this.telnetPort = telnetPort; } public TelnetLoginResult TelnetLogin() { try { using (Process telnetProcess = new Process()) { telnetProcess.StartInfo.FileName = "cmd.exe"; telnetProcess.StartInfo.Arguments = $"/c telnet {cameraIp} {telnetPort}"; telnetProcess.StartInfo.UseShellExecute = false; telnetProcess.StartInfo.RedirectStandardInput = true; telnetProcess.StartInfo.RedirectStandardOutput = true; telnetProcess.StartInfo.RedirectStandardError = true; telnetProcess.StartInfo.CreateNoWindow = true; StringBuilder output = new StringBuilder(); AutoResetEvent outputWaitHandle = new AutoResetEvent(false); telnetProcess.OutputDataReceived += (sender, e) => { if (e.Data == null) { outputWaitHandle.Set(); } else { output.AppendLine(e.Data); } }; telnetProcess.Start(); telnetProcess.BeginOutputReadLine(); // 发送登录凭证 StreamWriter stdin = telnetProcess.StandardInput; stdin.WriteLine(username); Thread.Sleep(1000); stdin.WriteLine(password); Thread.Sleep(1000); // 检查登录是否成功的关键词 string result = output.ToString(); if (result.Contains("Login OK") || result.Contains("登录成功") || result.Contains("#") || result.Contains("$")) { stdin.WriteLine("exit"); telnetProcess.WaitForExit(3000); return new TelnetLoginResult { Success = true, Message = result }; } else { stdin.WriteLine("exit"); telnetProcess.WaitForExit(3000); return new TelnetLoginResult { Success = false, Message = result }; } } } catch (Exception ex) { return new TelnetLoginResult { Success = false, Message = $"Telnet登录异常: {ex.Message}", RawOutput = ex.ToString() }; } } public IperfTestResult StartIperfTest(string iperfToolPath, string logDirectory) { try { // 生成基于时间的日志文件名 string timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss"); string upLogPath = Path.Combine(logDirectory, $"iperf_up_{timestamp}.log"); string downLogPath = Path.Combine(logDirectory, $"iperf_down_{timestamp}.log"); // 通过telnet执行iperf服务器端命令 var telnetResult = ExecuteTelnetCommands(); if (!telnetResult.Success) { return new IperfTestResult { Success = false, Message = "在摄像机端启动iperf服务器失败", UpLogPath = "", DownLogPath = "" }; } // 测试上行流量 ExecuteIperfTest(iperfToolPath, $"-c {iperfServerIp} -p 5201 -t 10 -w 1M -i 1 -P 8 -R", upLogPath); // 等待2S Thread.Sleep(2000); // 测试下行流量 ExecuteIperfTest(iperfToolPath, $"-c {iperfServerIp} -p 5201 -t 10 -w 1M -i 1 -P 8", downLogPath); return new IperfTestResult { Success = true, Message = "iperf测试完成", UpLogPath = upLogPath, DownLogPath = downLogPath }; } catch (Exception ex) { return new IperfTestResult { Success = false, Message = $"iperf测试异常: {ex.Message}", UpLogPath = "", DownLogPath = "" }; } } private void ExecuteIperfTest(string iperfToolPath, string arguments, string logPath) { using (Process iperfProcess = new Process()) { iperfProcess.StartInfo.FileName = iperfToolPath; iperfProcess.StartInfo.Arguments = arguments; iperfProcess.StartInfo.UseShellExecute = false; iperfProcess.StartInfo.RedirectStandardOutput = true; iperfProcess.StartInfo.CreateNoWindow = true; iperfProcess.Start(); string output = iperfProcess.StandardOutput.ReadToEnd(); iperfProcess.WaitForExit(); // 将输出写入日志文件 File.WriteAllText(logPath, output); } } private TelnetLoginResult ExecuteTelnetCommands() { try { using (Process telnetProcess = new Process()) { telnetProcess.StartInfo.FileName = "telnet"; telnetProcess.StartInfo.Arguments = $"{cameraIp} {telnetPort}"; telnetProcess.StartInfo.UseShellExecute = false; telnetProcess.StartInfo.RedirectStandardInput = true; telnetProcess.StartInfo.RedirectStandardOutput = true; telnetProcess.StartInfo.RedirectStandardError = true; telnetProcess.StartInfo.CreateNoWindow = true; StringBuilder output = new StringBuilder(); AutoResetEvent outputWaitHandle = new AutoResetEvent(false); telnetProcess.OutputDataReceived += (sender, e) => { if (e.Data == null) { outputWaitHandle.Set(); } else { output.AppendLine(e.Data); } }; telnetProcess.Start(); telnetProcess.BeginOutputReadLine(); StreamWriter stdin = telnetProcess.StandardInput; // 发送登录凭证 stdin.WriteLine(username); Thread.Sleep(1000); stdin.WriteLine(password); Thread.Sleep(2000); // 执行iperf相关命令 stdin.WriteLine("killall iperf3_mstar"); Thread.Sleep(1000); stdin.WriteLine("cp /mnt/tf/usb1_1/iperf3_mstar /var/tmp/ -f"); Thread.Sleep(1000); stdin.WriteLine("chmod a+x /var/tmp/iperf3_mstar"); Thread.Sleep(1000); stdin.WriteLine("/var/tmp/iperf3_mstar -s -p 5201 -i 1 &"); Thread.Sleep(2000); // 发送退出命令 stdin.WriteLine("exit"); telnetProcess.WaitForExit(5000); return new TelnetLoginResult { Success = true, Message = "Telnet命令执行完成", RawOutput = output.ToString() }; } } catch (Exception ex) { return new TelnetLoginResult { Success = false, Message = $"Telnet命令执行异常: {ex.Message}", RawOutput = ex.ToString() }; } } public class TelnetLoginResult { public bool Success { get; set; } public string Message { get; set; } public string RawOutput { get; set; } } public class IperfTestResult { public bool Success { get; set; } public string Message { get; set; } public string UpLogPath { get; set; } public string DownLogPath { get; set; } } } } }