Make_ThruputLog.cs 16 KB

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