Make_ThruputLog.cs 14 KB

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