Make_ThruputLog.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. using NPOI.SS.Formula.Eval;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Text.RegularExpressions;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. using UAS_MES_NEW.DataOperate;
  14. using UAS_MES_NEW.Entity;
  15. using UAS_MES_NEW.PublicMethod;
  16. namespace UAS_MES_NEW.Make
  17. {
  18. public partial class Make_ThruputLog : Form
  19. {
  20. public Make_ThruputLog()
  21. {
  22. InitializeComponent();
  23. }
  24. StringBuilder SQL = new StringBuilder();
  25. DataTable dt;
  26. DataHelper dh;
  27. string sVal1, sVal2, rVal1, rVal2;
  28. private void Make_ThruputLog_Load(object sender, EventArgs e)
  29. {
  30. dh = SystemInf.dh;
  31. }
  32. private void Parse_Click(object sender, EventArgs e)
  33. {
  34. if (string.IsNullOrEmpty(ExePath.Text))
  35. {
  36. ShowMsg(0, $"请选择测试使用的iperf3.exe");
  37. return;
  38. }
  39. string passPath, failPath;
  40. string parentPath = Path.GetDirectoryName(ExePath.Text.Trim());
  41. string logsPath = Path.Combine(parentPath, "log");
  42. passPath = Path.Combine(logsPath, "pass");
  43. failPath = Path.Combine(logsPath, "fail");
  44. string[] passFiles = Directory.GetFiles(passPath, $"*up.log");
  45. foreach (string currLog in passFiles)
  46. {
  47. string upRes = ParseIperfBitrate(currLog);
  48. string sn = Path.GetFileName(currLog).Split('_')[0];
  49. UpdateSN("L", sn);
  50. SQL.Clear();
  51. 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");
  52. dt = (DataTable)dh.ExecuteSql(SQL.ToString(), "select");
  53. if (dt.Rows.Count == 0)
  54. {
  55. ShowMsg(0, $"序列号无归属工单");
  56. continue;
  57. }
  58. string wo = dt.Rows[0]["ma_code"].ToString();
  59. string logPath = Path.GetDirectoryName(currLog);
  60. string downLog = Path.Combine(logPath, $"{sn}_down.log");
  61. string downRes = ParseIperfBitrate(downLog);
  62. string tDetail = $"upRate/{upRes};downRate/{downRes}";
  63. CheckPassStation(wo, sn, "PASS", tDetail);
  64. using (StreamReader SR = new StreamReader(currLog, Encoding.GetEncoding("GBK")))
  65. {
  66. string Content = SR.ReadToEnd();
  67. SR.Close();
  68. SR.Dispose();
  69. if (ConsoleLog(Content, currLog, $"{sn}_up.log"))
  70. {
  71. File.WriteAllText(currLog, string.Empty);
  72. File.Delete(currLog);
  73. }
  74. }
  75. using (StreamReader SR = new StreamReader(downLog, Encoding.GetEncoding("GBK")))
  76. {
  77. string Content = SR.ReadToEnd();
  78. SR.Close();
  79. SR.Dispose();
  80. if (ConsoleLog(Content, currLog, $"{sn}_down.log"))
  81. {
  82. File.WriteAllText(currLog, string.Empty);
  83. File.Delete(currLog);
  84. }
  85. }
  86. }
  87. string[] failFiles = Directory.GetFiles(failPath, $"*up.log");
  88. foreach (string currLog in failFiles)
  89. {
  90. string upRes = ParseIperfBitrate(currLog);
  91. string sn = Path.GetFileName(currLog).Split('_')[0];
  92. UpdateSN("L", sn);
  93. SQL.Clear();
  94. 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");
  95. dt = (DataTable)dh.ExecuteSql(SQL.ToString(), "select");
  96. if (dt.Rows.Count == 0)
  97. {
  98. ShowMsg(0, $"序列号无归属工单");
  99. continue;
  100. }
  101. string wo = dt.Rows[0]["ma_code"].ToString();
  102. string logPath = Path.GetDirectoryName(currLog);
  103. string downLog = Path.Combine(logPath, $"{sn}_down.log");
  104. string downRes = ParseIperfBitrate(downLog);
  105. string tDetail = $"upRate/{upRes};downRate/{downRes}";
  106. CheckPassStation(wo, sn, "PASS", tDetail);
  107. using (StreamReader SR = new StreamReader(currLog, Encoding.GetEncoding("GBK")))
  108. {
  109. string Content = SR.ReadToEnd();
  110. SR.Close();
  111. SR.Dispose();
  112. if (ConsoleLog(Content, currLog, $"{sn}_up.log"))
  113. {
  114. File.WriteAllText(currLog, string.Empty);
  115. File.Delete(currLog);
  116. }
  117. }
  118. using (StreamReader SR = new StreamReader(downLog, Encoding.GetEncoding("GBK")))
  119. {
  120. string Content = SR.ReadToEnd();
  121. SR.Close();
  122. SR.Dispose();
  123. if (ConsoleLog(Content, currLog, $"{sn}_down.log"))
  124. {
  125. File.WriteAllText(currLog, string.Empty);
  126. File.Delete(currLog);
  127. }
  128. }
  129. }
  130. }
  131. private void CheckPassStation(string wo,string sn, string testRes, string testDetail)
  132. {
  133. List<string> param = new List<string>() { };
  134. string outMsg = "";
  135. param.Add(wo);
  136. param.Add(sn);
  137. param.Add(User.UserSourceCode);
  138. param.Add(testRes);
  139. param.Add("");
  140. param.Add("");
  141. param.Add("ThruputLog");
  142. param.Add(testDetail);
  143. param.Add(outMsg);
  144. string[] paramList = param.ToArray();
  145. dh.CallProcedure("cs_insert_testrejects", ref paramList);
  146. if (paramList[8].Substring(0, 2) == "OK")
  147. {
  148. ShowMsg(1, $"序列号{sn}采集成功:测试结果为{testRes}");
  149. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, workOrder.Text, User.UserLineCode, User.UserSourceCode, "无线吞吐量", "无线吞吐量过站成功", sn, "");
  150. }
  151. dt = (DataTable)dh.ExecuteSql($"SELECT mp_id FROM makeprocess WHERE mp_sncode = '{sn}' AND instr(mp_stepname, '吞吐量') > 0", "select");
  152. if (dt.Rows.Count > 0)
  153. {
  154. ShowMsg(1, $"序列号:{sn}已经过站记录");
  155. return;
  156. }
  157. string oWO, oWOId, oErrMsg = "";
  158. if (LogicHandler.CheckStepSNAndMacode(workOrder.Text, User.UserSourceCode, sn, User.UserCode, out oWO, out oWOId, out oErrMsg))
  159. {
  160. if (LogicHandler.SetStepResult(oWO, User.UserSourceCode, sn, "无线吞吐量", "OK", User.UserCode, out oErrMsg))
  161. {
  162. ShowMsg(0, $"序列号{sn},过站记录成功");
  163. }
  164. else
  165. {
  166. ShowMsg(0, $"序列号{sn},处理过站NG:{oErrMsg}");
  167. }
  168. }
  169. else
  170. {
  171. ShowMsg(0, $"序列号{sn},过站核对NG:{oErrMsg}");
  172. }
  173. }
  174. public string ParseIperfBitrate(string logPath)
  175. {
  176. if (!File.Exists(logPath)) return "0.0";
  177. var lines = File.ReadAllLines(logPath);
  178. List<string> logItem = new List<string> { };
  179. foreach (var line in lines)
  180. {
  181. //if (line.Trim().StartsWith("[SUM]"))
  182. //{
  183. // logItem.Add(line);
  184. //}
  185. if(line.Contains("sender") || line.Contains("receiver"))
  186. {
  187. logItem.Add(line);
  188. }
  189. }
  190. if (logItem.Count > 0 && logItem[logItem.Count - 2].Contains("sender"))
  191. {
  192. var match = Regex.Match(logItem[logItem.Count - 2], @"(\d+\.\d+\s+MBytes).*?(\d+\.\d+\s+Mbits/sec)");
  193. if (match.Success)
  194. {
  195. sVal1 = match.Groups[1].Value;
  196. sVal2 = match.Groups[2].Value;
  197. }
  198. }
  199. if (logItem.Count > 0 && logItem[logItem.Count - 1].Contains("receiver"))
  200. {
  201. var match = Regex.Match(logItem[logItem.Count - 1], @"(\d+\.\d+\s+MBytes).*?(\d+\.\d+\s+Mbits/sec)");
  202. if (match.Success)
  203. {
  204. rVal1 = match.Groups[1].Value;
  205. rVal2 = match.Groups[2].Value;
  206. }
  207. }
  208. //double num1 = (double.Parse(sVal1.Split(' ')[0]) + double.Parse(rVal1.Split(' ')[0])) / 2;
  209. //double num2 = (double.Parse(sVal2.Split(' ')[0]) + double.Parse(rVal2.Split(' ')[0])) / 2;
  210. return rVal2;
  211. }
  212. private void Clear_Click(object sender, EventArgs e)
  213. {
  214. OperatResult.Clear();
  215. UpdateSN("C", "");
  216. }
  217. private void ShowMsg(int type, string msg)
  218. {
  219. msg = msg.Replace("\r", "").Replace("\n", "");
  220. string msgTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  221. string showMsg = $"{msgTime}: {msg}\n";
  222. if (type == 0)
  223. {
  224. OperatResult.AppendText(showMsg, Color.Red);
  225. }
  226. else if (type == 1)
  227. {
  228. OperatResult.AppendText(showMsg, Color.Green);
  229. }
  230. }
  231. private void ExePath_Click(object sender, EventArgs e)
  232. {
  233. using (OpenFileDialog openFileDialog = new OpenFileDialog())
  234. {
  235. openFileDialog.Title = "选择iperf3启动文件";
  236. openFileDialog.Filter = "可执行文件 (*.exe)|*.exe|所有文件 (*.*)|*.*";
  237. openFileDialog.FilterIndex = 1;
  238. openFileDialog.RestoreDirectory = true;
  239. // 设置初始目录(可选)
  240. if (!string.IsNullOrEmpty(ExePath.Text) && System.IO.File.Exists(ExePath.Text))
  241. {
  242. openFileDialog.InitialDirectory = System.IO.Path.GetDirectoryName(ExePath.Text);
  243. }
  244. else
  245. {
  246. openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
  247. }
  248. if (openFileDialog.ShowDialog() == DialogResult.OK)
  249. {
  250. ExePath.Text = openFileDialog.FileName;
  251. ShowMsg(1, $"已成功选择iperf3启动文件");
  252. }
  253. }
  254. }
  255. private void UpdateSN(string type, string sn)
  256. {
  257. if (type == "C")
  258. {
  259. serialNumber.Text = "";
  260. workOrder.Text = "";
  261. productCode.Text = "";
  262. productName.Text = "";
  263. }
  264. else if (type == "L")
  265. {
  266. SQL.Clear();
  267. SQL.Append($@"SELECT ms_sncode,ma_code,pr_code,pr_spec FROM makeserial,make,product
  268. WHERE ms_sncode = '{sn}' AND ms_makecode = ma_code AND ms_prodcode = pr_code");
  269. dt = (DataTable)dh.ExecuteSql(SQL.ToString(), "select");
  270. if (dt.Rows.Count > 0)
  271. {
  272. serialNumber.Text = dt.Rows[0]["ms_sncode"].ToString();
  273. workOrder.Text = dt.Rows[0]["ma_code"].ToString();
  274. productCode.Text = dt.Rows[0]["pr_code"].ToString();
  275. productName.Text = dt.Rows[0]["pr_spec"].ToString();
  276. }
  277. else
  278. {
  279. UpdateSN("C", sn);
  280. }
  281. }
  282. }
  283. private bool ConsoleLog(string Content, string PathName, string SN)
  284. {
  285. try
  286. {
  287. string sourcePaht = Path.GetDirectoryName(PathName);
  288. string changeName = Path.Combine(sourcePaht, SN);
  289. string newFolderName = "Logs";
  290. string newFolderPath = Path.Combine(sourcePaht, newFolderName);
  291. if (!Directory.Exists(newFolderPath))
  292. {
  293. Directory.CreateDirectory(newFolderPath);
  294. }
  295. string newFileName = "Log_" + Path.GetFileName(changeName);
  296. string newFilePath = Path.Combine(newFolderPath, newFileName);
  297. File.AppendAllText(newFilePath, Content + Environment.NewLine);
  298. return true;
  299. }
  300. catch (Exception ex)
  301. {
  302. MessageBox.Show(this.ParentForm, ex.Message, "警告");
  303. return false;
  304. }
  305. }
  306. }
  307. }