| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- 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;
- 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(),
- cameraPort: 23,
- username: Account.Text.Trim(),
- password: ProductList.Text.Trim(),
- iperfServerIp: IPList.Text.Trim(),
- iperfServerPort : 5201
- );
- string Msg = tester.TelnetConnect();
- if (Msg.Substring(0,2) != "OK")
- {
- ShowMsg(0, $"Telnet登录失败,{Msg}");
- return;
- }
- ShowMsg(1, $"Telnet登录成功");
- Msg = tester.StartIperfTest(@"D:\MesCoder\Company\慧眼\吞吐量测试\iperf3.exe", @"D:\MesCoder\Company\慧眼\吞吐量测试\iperf_logs");
- if (Msg.Substring(0, 2) != "OK")
- {
- ShowMsg(0, $"iperf3启动失败,{Msg}");
- return;
- }
- }
- 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);
- }
- }
- }
- public class CameraIperfTester
- {
- string cameraIp;
- int cameraPort;
- string username;
- string password;
- string iperfServerIp;
- int iperfServerPort;
- public CameraIperfTester(string cameraIp, int cameraPort, string username, string password, string iperfServerIp, int iperfServerPort)
- {
- this.cameraIp = cameraIp;
- this.cameraPort = cameraPort;
- this.username = username;
- this.password = password;
- this.iperfServerIp = iperfServerIp;
- this.iperfServerPort = iperfServerPort;
- }
- public string StartIperfTest(string iperfPath,string logDirectory)
- {
- return "OK,测试";
- }
- public string TelnetConnect()
- {
- try
- {
- using (TcpClient client = new TcpClient())
- {
- IAsyncResult connectResult = client.BeginConnect(cameraIp, cameraPort, null, null);
- bool connected = connectResult.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(5000)); // 5秒连接超时
- if (!connected)
- {
- client.Close();
- return "NG,连接超时";
- }
- try
- {
- client.EndConnect(connectResult);
- }
- catch (SocketException ex)
- {
- return $"NG,连接失败: {ex.Message}";
- }
- using (NetworkStream stream = client.GetStream())
- {
- // 3S读取超时
- stream.ReadTimeout = 3000;
- StringBuilder fullResponse = new StringBuilder();
- string welcomeResponse = ReadStream(stream);
- fullResponse.Append(welcomeResponse);
- WriteStream(stream, username);
- string usernameResponse = ReadStream(stream);
- fullResponse.Append(usernameResponse);
- WriteStream(stream, password);
- string loginResponse = ReadStream(stream);
- fullResponse.Append(loginResponse);
- return $"OK,{fullResponse.ToString()}";
- }
- }
- }
- catch (Exception ex)
- {
- return $"NG,{ex.Message}";
- }
- }
- private void WriteStream(NetworkStream stream, string text)
- {
- byte[] data = Encoding.ASCII.GetBytes(text + "\r\n");
- stream.Write(data, 0, data.Length);
- stream.Flush();
- }
- private string ReadStream(NetworkStream stream)
- {
- StringBuilder sb = new StringBuilder();
- byte[] buffer = new byte[1024];
- int totalBytesRead = 0;
- int currentBytesRead = 0;
- DateTime lastReadTime = DateTime.UtcNow;
- TimeSpan idleTimeout = TimeSpan.FromMilliseconds(800); // 空闲超时
- try
- {
- do
- {
- currentBytesRead = stream.Read(buffer, 0, buffer.Length);
- if (currentBytesRead > 0)
- {
- sb.Append(Encoding.ASCII.GetString(buffer, 0, currentBytesRead));
- totalBytesRead += currentBytesRead;
- lastReadTime = DateTime.UtcNow;
- }
- } while ((DateTime.UtcNow - lastReadTime) < idleTimeout);
- }
- catch (IOException ioEx)
- {
- }
- catch (ObjectDisposedException)
- {
- throw;
- }
- catch (SocketException sockEx)
- {
- throw new IOException($"NG,网络错误: {sockEx.Message}", sockEx);
- }
- return sb.ToString();
- }
- }
- 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();
- }
- }
- }
|