using NPOI.SS.Formula.Eval; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; using UAS_MES_NEW.DataOperate; using UAS_MES_NEW.Entity; using UAS_MES_NEW.PublicMethod; namespace UAS_MES_NEW.Make { public partial class Make_ThruputLog : Form { public Make_ThruputLog() { InitializeComponent(); } StringBuilder SQL = new StringBuilder(); DataTable dt; DataHelper dh; string sVal1, sVal2, rVal1, rVal2; private void Make_ThruputLog_Load(object sender, EventArgs e) { dh = SystemInf.dh; } private void Parse_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(ExePath.Text)) { ShowMsg(0, $"请选择测试使用的iperf3.exe"); return; } string passPath, failPath; string parentPath = Path.GetDirectoryName(ExePath.Text.Trim()); string logsPath = Path.Combine(parentPath, "log"); passPath = Path.Combine(logsPath, "pass"); failPath = Path.Combine(logsPath, "fail"); string upRes, downRes; List SNArr = new List { }; string[] passFiles = Directory.GetFiles(passPath, $"*.log"); foreach (string currLog in passFiles) { if (Path.GetFileName(currLog).Contains("up")) { upRes = ParseIperfBitrate(currLog); string sn = Path.GetFileName(currLog).Split('_')[0]; UpdateSN("L", sn); SNArr.Add(sn); if (SNArr.IndexOf(sn) > 0) continue; SQL.Clear(); SQL.Append($@"SELECT ms_sncode,ma_code,pr_code,pr_spec FROM makeserial,make,product WHERE ms_sncode = '{sn.Replace(";", ";")}' AND ms_makecode = ma_code AND ms_prodcode = pr_code"); dt = (DataTable)dh.ExecuteSql(SQL.ToString(), "select"); if (dt.Rows.Count == 0) { ShowMsg(0, $"序列号:{sn.Replace(";", ";")},无归属工单"); continue; } string wo = dt.Rows[0]["ma_code"].ToString(); string logPath = Path.GetDirectoryName(currLog); string downLog = Path.Combine(logPath, $"{Path.GetFileName(currLog).Replace("up", "down")}"); downRes = ParseIperfBitrate(downLog); string tDetail = $"upRate/{upRes};downRate/{downRes}"; CheckPassStation(wo, sn, "PASS", tDetail); using (StreamReader SR = new StreamReader(currLog, Encoding.GetEncoding("GBK"))) { string Content = SR.ReadToEnd(); SR.Close(); SR.Dispose(); if (ConsoleLog(Content, currLog, $"{Path.GetFileName(currLog)}")) { File.WriteAllText(currLog, string.Empty); File.Delete(currLog); } } using (StreamReader SR = new StreamReader(downLog, Encoding.GetEncoding("GBK"))) { string Content = SR.ReadToEnd(); SR.Close(); SR.Dispose(); if (ConsoleLog(Content, currLog, $"{Path.GetFileName(currLog).Replace("up", "down")}")) { File.WriteAllText(currLog, string.Empty); File.Delete(currLog); } } } } string[] failFiles = Directory.GetFiles(failPath, $"*.log"); foreach (string currLog in failFiles) { if (Path.GetFileName(currLog).Contains("up")) { upRes = ParseIperfBitrate(currLog); string sn = Path.GetFileName(currLog).Split('_')[0]; UpdateSN("L", sn); SNArr.Add(sn); if (SNArr.IndexOf(sn) > 0) continue; SQL.Clear(); SQL.Append($@"SELECT ms_sncode,ma_code,pr_code,pr_spec FROM makeserial,make,product WHERE ms_sncode = '{sn.Replace(";", ";")}' AND ms_makecode = ma_code AND ms_prodcode = pr_code"); dt = (DataTable)dh.ExecuteSql(SQL.ToString(), "select"); if (dt.Rows.Count == 0) { ShowMsg(0, $"序列号:{sn.Replace(";", ";")},无归属工单"); continue; } string wo = dt.Rows[0]["ma_code"].ToString(); string logPath = Path.GetDirectoryName(currLog); string downLog = Path.Combine(logPath, $"{Path.GetFileName(currLog).Replace("up", "down")}"); downRes = ParseIperfBitrate(downLog); string tDetail = $"upRate/{upRes};downRate/{downRes}"; CheckPassStation(wo, sn, "PASS", tDetail); using (StreamReader SR = new StreamReader(currLog, Encoding.GetEncoding("GBK"))) { string Content = SR.ReadToEnd(); SR.Close(); SR.Dispose(); if (ConsoleLog(Content, currLog, $"{Path.GetFileName(currLog)}")) { File.WriteAllText(currLog, string.Empty); File.Delete(currLog); } } using (StreamReader SR = new StreamReader(downLog, Encoding.GetEncoding("GBK"))) { string Content = SR.ReadToEnd(); SR.Close(); SR.Dispose(); if (ConsoleLog(Content, currLog, $"{Path.GetFileName(currLog).Replace("up", "down")}")) { File.WriteAllText(currLog, string.Empty); File.Delete(currLog); } } } } } private void CheckPassStation(string wo,string sn, string testRes, string testDetail) { sn = sn.Replace(";", ";"); List param = new List() { }; string outMsg = ""; param.Add(wo); param.Add(sn); param.Add(User.UserSourceCode); param.Add(testRes); param.Add(""); param.Add(""); param.Add("ThruputLog"); param.Add(testDetail); param.Add(outMsg); string[] paramList = param.ToArray(); dh.CallProcedure("cs_insert_testrejects", ref paramList); if (paramList[8].Substring(0, 2) == "OK") { ShowMsg(1, $"序列号{sn}采集成功:测试结果为{testRes},{testDetail}"); LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, workOrder.Text, User.UserLineCode, User.UserSourceCode, "无线吞吐量", "无线吞吐量过站成功", sn, ""); } dt = (DataTable)dh.ExecuteSql($"SELECT mp_id FROM makeprocess WHERE mp_sncode = '{sn}' AND instr(mp_stepname, '吞吐量') > 0", "select"); if (dt.Rows.Count > 0) { ShowMsg(1, $"序列号:{sn}已经过站记录"); return; } string oWO, oWOId, oErrMsg = ""; if (LogicHandler.CheckStepSNAndMacode(workOrder.Text, User.UserSourceCode, sn, User.UserCode, out oWO, out oWOId, out oErrMsg)) { if (LogicHandler.SetStepResult(oWO, User.UserSourceCode, sn, "无线吞吐量", "OK", User.UserCode, out oErrMsg)) { ShowMsg(1, $"序列号{sn},过站记录成功"); } else { ShowMsg(0, $"序列号{sn},处理过站NG:{oErrMsg}"); } } else { ShowMsg(0, $"序列号{sn},过站核对NG:{oErrMsg}"); } } public string ParseIperfBitrate(string logPath) { if (!File.Exists(logPath)) return "0.0"; var lines = File.ReadAllLines(logPath); List logItem = new List { }; foreach (var line in lines) { //if (line.Trim().StartsWith("[SUM]")) //{ // logItem.Add(line); //} if(line.Contains("sender") || line.Contains("receiver")) { logItem.Add(line); } } if (logItem.Count > 0 && logItem[logItem.Count - 2].Contains("sender")) { var match = Regex.Match(logItem[logItem.Count - 2], @"(\d+\.\d+\s+MBytes).*?(\d+\.\d+\s+Mbits/sec)"); if (match.Success) { sVal1 = match.Groups[1].Value; sVal2 = match.Groups[2].Value; } } if (logItem.Count > 0 && logItem[logItem.Count - 1].Contains("receiver")) { var match = Regex.Match(logItem[logItem.Count - 1], @"(\d+\.\d+\s+MBytes).*?(\d+\.\d+\s+Mbits/sec)"); if (match.Success) { rVal1 = match.Groups[1].Value; rVal2 = match.Groups[2].Value; } } //double num1 = (double.Parse(sVal1.Split(' ')[0]) + double.Parse(rVal1.Split(' ')[0])) / 2; //double num2 = (double.Parse(sVal2.Split(' ')[0]) + double.Parse(rVal2.Split(' ')[0])) / 2; return rVal2; } private void Clear_Click(object sender, EventArgs e) { OperatResult.Clear(); UpdateSN("C", ""); } 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 ExePath_Click(object sender, EventArgs e) { using (OpenFileDialog openFileDialog = new OpenFileDialog()) { openFileDialog.Title = "选择iperf3启动文件"; openFileDialog.Filter = "可执行文件 (*.exe)|*.exe|所有文件 (*.*)|*.*"; openFileDialog.FilterIndex = 1; openFileDialog.RestoreDirectory = true; // 设置初始目录(可选) if (!string.IsNullOrEmpty(ExePath.Text) && System.IO.File.Exists(ExePath.Text)) { openFileDialog.InitialDirectory = System.IO.Path.GetDirectoryName(ExePath.Text); } else { openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles); } if (openFileDialog.ShowDialog() == DialogResult.OK) { ExePath.Text = openFileDialog.FileName; ShowMsg(1, $"已成功选择iperf3启动文件"); } } } 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 bool ConsoleLog(string Content, string PathName, string SN) { try { string sourcePaht = Path.GetDirectoryName(PathName); string changeName = Path.Combine(sourcePaht, SN); string newFolderName = "Logs"; string newFolderPath = Path.Combine(sourcePaht, newFolderName); if (!Directory.Exists(newFolderPath)) { Directory.CreateDirectory(newFolderPath); } string newFileName = "Log_" + Path.GetFileName(changeName); string newFilePath = Path.Combine(newFolderPath, newFileName); File.AppendAllText(newFilePath, Content + Environment.NewLine); return true; } catch (Exception ex) { MessageBox.Show(this.ParentForm, ex.Message, "警告"); return false; } } } }