Make_ParseLog.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. using BarTender;
  2. using DevExpress.Xpo.Logger;
  3. using DevExpress.XtraPrinting;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Security;
  12. using System.Security.AccessControl;
  13. using System.Security.Principal;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using System.Web.Services.Description;
  17. using System.Web.UI.WebControls;
  18. using System.Windows.Forms;
  19. using System.Xml;
  20. using UAS_MES_NEW.CustomControl.ButtonUtil;
  21. using UAS_MES_NEW.CustomControl.TextBoxWithIcon;
  22. using UAS_MES_NEW.DataOperate;
  23. using UAS_MES_NEW.Entity;
  24. using UAS_MES_NEW.PublicMethod;
  25. using static DevExpress.Xpo.DB.DataStoreLongrunnersWatch;
  26. namespace UAS_MES_NEW.Make
  27. {
  28. public partial class Make_ParseLog : Form
  29. {
  30. public Make_ParseLog()
  31. {
  32. InitializeComponent();
  33. }
  34. LogStringBuilder sql = new LogStringBuilder();
  35. DataTable Dbfind;
  36. DataTable dt;
  37. DataHelper dh;
  38. string currFileType = "";
  39. string SN, omakeCode, oMsid, oErrorMessage = "";
  40. string equiType = "";
  41. private void Make_ParseLog_Load(object sender, EventArgs e)
  42. {
  43. dh = SystemInf.dh;
  44. //设置锁定工单
  45. LockMakeCode.GetMakeCodeCtl(ma_code);
  46. ma_code.SetLockCheckBox(LockMakeCode);
  47. //工单号放大镜配置
  48. ma_code.TableName = "make left join product on ma_prodcode=pr_code";
  49. ma_code.SelectField = "ma_code # 工单号,pr_code # 产品编号,pr_detail # 产品名称";
  50. ma_code.FormName = Name;
  51. ma_code.SetValueField = new string[] { "ma_code", "pr_code", "pr_detail" };
  52. ma_code.Condition = "ma_statuscode='STARTED'";
  53. ma_code.DbChange += Ma_code_DbChange;
  54. }
  55. private void Ma_code_DbChange(object sender, EventArgs e)
  56. {
  57. Dbfind = ma_code.ReturnData;
  58. BaseUtil.SetFormValue(this.Controls, Dbfind);
  59. //获取工单的其他信息
  60. sql.Clear();
  61. sql.Append("select ma_code,nvl(mcd_okqty,0) mcd_okqty,ma_prodcode as pr_code ,pr_detail,");
  62. sql.Append("pr_spec,ma_qty - nvl(mcd_inqty, 0) mcd_remainqty from make left join makecraftdetail on ");
  63. sql.Append("mcd_maid=ma_id left join product on pr_code=ma_prodcode where ma_code='" + ma_code.Text + "' and mcd_stepcode='" + User.CurrentStepCode + "'");
  64. dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
  65. if (dt.Rows.Count > 0)
  66. {
  67. BaseUtil.SetFormValue(this.Controls, dt);
  68. }
  69. }
  70. private void Device_SelectedIndexChanged(object sender, EventArgs e)
  71. {
  72. if (Device.SelectedIndex == 3)
  73. {
  74. ma_code.Enabled = false;
  75. txtPath.Focus();
  76. txtPath.SelectAll();
  77. return;
  78. }
  79. if (string.IsNullOrEmpty(ma_code.Text))
  80. {
  81. Device.SelectedIndex = -1;
  82. MessageBox.Show(this.ParentForm, "请选择工单", "提示");
  83. return;
  84. }
  85. txtPath.Focus();
  86. txtPath.SelectAll();
  87. }
  88. private void allParse_Click(object sender, EventArgs e)
  89. {
  90. if (string.IsNullOrEmpty(ma_code.Text))
  91. {
  92. MessageBox.Show(this.ParentForm, "请选择工单", "提示");
  93. return;
  94. }
  95. if (Device.SelectedIndex == -1)
  96. {
  97. Device.Focus();
  98. Device.SelectAll();
  99. MessageBox.Show(this.ParentForm, "请选择设备", "提示");
  100. return;
  101. }
  102. if (String.IsNullOrEmpty(txtPath.Text))
  103. {
  104. txtPath.Focus();
  105. txtPath.SelectAll();
  106. return;
  107. }
  108. string[] txtFiles = Directory.GetFiles(txtPath.Text, $"*.{currFileType}");
  109. if (txtFiles.Length == 0)
  110. {
  111. LogMessage($"当前没有{currFileType.ToUpper()}文件");
  112. return;
  113. }
  114. RefreshFileList();
  115. }
  116. private void Choose_Click(object sender, EventArgs e)
  117. {
  118. if (string.IsNullOrEmpty(ma_code.Text))
  119. {
  120. MessageBox.Show(this.ParentForm, "请选择工单", "提示");
  121. return;
  122. }
  123. if (Device.SelectedIndex == -1)
  124. {
  125. Device.Focus();
  126. Device.SelectAll();
  127. MessageBox.Show(this.ParentForm, "请选择设备", "提示");
  128. return;
  129. }
  130. if (!String.IsNullOrEmpty(txtPath.Text))
  131. {
  132. txtPath.Focus();
  133. txtPath.SelectAll();
  134. }
  135. try
  136. {
  137. FolderBrowserDialog dialog = new FolderBrowserDialog();
  138. switch (Device.SelectedIndex)
  139. {
  140. case 0:
  141. dialog.Description = "请选择读取SPI设备文件夹";
  142. currFileType = "txt";
  143. equiType = "SPI";
  144. break;
  145. case 1:
  146. dialog.Description = "请选择读取AOI设备文本文件";
  147. currFileType = "txt";
  148. equiType = "AOI";
  149. break;
  150. case 2:
  151. dialog.Description = "请选择读取印刷机设备的文本文件夹";
  152. equiType = "印刷机";
  153. break;
  154. case 3:
  155. dialog.Description = "请选择读取贴片机的文本文件夹";
  156. currFileType = "xml";
  157. equiType = "贴片机";
  158. break;
  159. }
  160. if (dialog.ShowDialog() == DialogResult.OK)
  161. {
  162. txtPath.Text = dialog.SelectedPath;
  163. txtPath.Enabled = false;
  164. Device.Enabled = false;
  165. var watcher = new FileSystemWatcher(txtPath.Text);
  166. if (equiType == "印刷机")
  167. {
  168. var xmlFiles = Directory.GetFiles(txtPath.Text, "*.xml");
  169. var csvFiles = Directory.GetFiles(txtPath.Text, "*.csv");
  170. currFileType = xmlFiles.Length > 0 ? "xml" : "csv";
  171. watcher.Filter = $"*.{currFileType}";
  172. }
  173. else
  174. {
  175. watcher.Filter = $"*.{currFileType}";
  176. }
  177. watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;
  178. watcher.Created += OnFileCreated;
  179. /*watcher.Changed += OnFileChanged;
  180. watcher.Deleted += OnFileChanged;
  181. watcher.Renamed += OnFileChanged;*/
  182. watcher.EnableRaisingEvents = true;
  183. }
  184. #region
  185. /*OpenFileDialog dialog = new OpenFileDialog();
  186. dialog.Multiselect = true;//该值确定是否可以选择多个文件
  187. dialog.Filter = "文本文件 (*.txt)|*.txt|All files (*.*)|*.*";
  188. switch (Device.SelectedIndex)
  189. {
  190. case 0:
  191. dialog.Title = "请选择读取SPI设备文件夹";
  192. dialog.Filter = "All files (*.*)|*.*";
  193. break;
  194. case 1:
  195. dialog.Title = "请选择读取AOI设备文本文件";
  196. dialog.Filter = "All files (*.*)|*.*";
  197. break;
  198. case 2:
  199. dialog.Title = "请选择读取印刷机设备的文本文件夹";
  200. dialog.Filter = "文本文件 (*.xml)|*.xml|All files (*.*)|*.*";
  201. break;
  202. }
  203. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  204. {
  205. txtPath.Text = dialog.FileName;
  206. dialog.Dispose();
  207. ListViewItem item = new ListViewItem();
  208. int count = logList.Items.Count;
  209. item.Text = (++count).ToString();
  210. item.SubItems.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  211. item.SubItems.Add(txtPath.Text);
  212. logList.Items.Add(item);
  213. }
  214. if (Device.SelectedIndex == 0) // SPI
  215. {
  216. ParseLogInsert(txtPath.Text);
  217. Console.WriteLine();
  218. }
  219. else if (Device.SelectedIndex == 1) // AOI
  220. {
  221. }
  222. else if (Device.SelectedIndex == 2) // 印刷机
  223. {
  224. PrinterData xmlData = ReadWithXmlReader(txtPath.Text);
  225. Console.WriteLine();
  226. }*/
  227. #endregion
  228. }
  229. catch (Exception ex)
  230. {
  231. MessageBox.Show(this.ParentForm, ex.Message, "警告");
  232. }
  233. }
  234. private void OnFileCreated(object sender, FileSystemEventArgs e)
  235. {
  236. RefreshFileList();
  237. }
  238. private void OnFileChanged(object sender, FileSystemEventArgs e)
  239. {
  240. if (e.ChangeType == WatcherChangeTypes.Changed)
  241. {
  242. RefreshFileList();
  243. }
  244. }
  245. private void RefreshFileList()
  246. {
  247. if (lstFiles.InvokeRequired)
  248. {
  249. //lstFiles.Invoke(new Action(RefreshFileList));
  250. lstFiles.BeginInvoke(new Action(RefreshFileList));
  251. return;
  252. }
  253. try
  254. {
  255. if (!Directory.Exists(txtPath.Text))
  256. {
  257. LogMessage($"目录不存在: {txtPath.Text}");
  258. return;
  259. }
  260. string[] txtFiles = Directory.GetFiles(txtPath.Text, $"*.{currFileType}");
  261. if (txtFiles.Length == 0)
  262. {
  263. return;
  264. }
  265. else
  266. {
  267. foreach (string file in txtFiles)
  268. {
  269. //LogMessage(file);
  270. if (Device.SelectedIndex == 0) // SPI 已测试对接
  271. {
  272. ParseLogInsert(file);
  273. }
  274. else if (Device.SelectedIndex == 1) // AOI 已测试对接
  275. {
  276. ParseLogInsert(file);
  277. }
  278. else if (Device.SelectedIndex == 2) // 印刷机 待测试对接
  279. {
  280. string fileType = Path.GetExtension(file).ToLower();
  281. PrinterData xmlData;
  282. List<MesData> csvData;
  283. if (fileType == ".csv"){
  284. csvData = ParseCsvFile(file);
  285. }
  286. else if (fileType == ".xml"){
  287. xmlData = ReadWithXmlReader(file);
  288. }
  289. string xmlContent = File.ReadAllText(file);
  290. if (ConsoleLog(xmlContent, file))
  291. {
  292. File.Delete(file);
  293. }
  294. }
  295. else if (Device.SelectedIndex == 3) // 贴片机 导站位表已测试对接
  296. {
  297. if (!File.Exists(file))
  298. {
  299. LogMessage($"文件不存在: {file}");
  300. }
  301. string xmlContent = File.ReadAllText(file);
  302. var xmlList = ParseXml(xmlContent);
  303. if (ConsoleLog(xmlContent, file))
  304. {
  305. File.Delete(file);
  306. }
  307. List<string> param = new List<string>() { };
  308. string PS_CODE = "";
  309. LogicHandler.GetSerialNumByCaller("Make!ParseLog", out PS_CODE);
  310. List<string> MachineName = new List<string>();
  311. List<string> Part = new List<string>();
  312. List<string> Station = new List<string>();
  313. List<string> ReelNo = new List<string>();
  314. List<string> letNo = new List<string>();
  315. string Part_ = "";
  316. int letNoNum = 0;
  317. foreach (var item in xmlList)
  318. {
  319. MachineName.Add(item.MachineName);
  320. Part_ = item.Part.Split('.')[0];
  321. Station.Add(item.Station);
  322. ReelNo.Add(item.ReelNo);
  323. letNo.Add((++letNoNum).ToString());
  324. }
  325. string ps_id = "";
  326. string lineId = dh.getFieldDataByCondition("smtlocationinto", "SLI_LANE", "SLI_MCNO='" + MachineName[0] + "'").ToString();
  327. if (!dh.CheckExist("productsmt", "ps_prodcode='" + Part_ + "'"))
  328. {
  329. ps_id = dh.GetSEQ("productsmt_seq");
  330. dh.ExecuteSql("insert into productsmt(ps_id,ps_prodcode,ps_status,ps_statuscode,PS_LINECODE,PS_CODE)" +
  331. "values('" + ps_id + "','" + Part_ + "','在录入','ENTERING','"+ lineId + "','"+ PS_CODE + "')", "insert");
  332. }
  333. else
  334. {
  335. ps_id = dh.getFieldDataByCondition("productsmt", "ps_id", "ps_prodcode='"+Part_+"'").ToString();
  336. }
  337. if (dh.getRowCount("productsmtlocation", "PSL_PSID=" + ps_id) > 0)
  338. {
  339. dh.ExecuteSql("delete from productsmtlocation where PSL_PSID" + ps_id, "delete");
  340. }
  341. sql.Clear();
  342. sql.Append("insert into productsmtlocation(PSL_ID, PSL_PSID, PSL_DETNO, PSL_PRODCODE, PSL_REPCODE, PSL_LOCATION, PSL_BASEQTY, PSL_TABLE)" +
  343. "select productsmtlocation_seq.nextval," + ps_id + ",:letArr, :ReelNo,:ReelNo,:Station,BD_BASEQTY,'' from bom@ERP " +
  344. "left join bomdetail@ERP on bo_id=bd_bomid where bo_mothercode='" + Part_ + "' and BD_SONCODE=:ReelNo");
  345. dh.BatchInsert(sql.GetString(), new string[] {"letArr", "ReelNo", "ReelNo", "Station", "ReelNo" },
  346. letNo.ToArray(),ReelNo.ToArray(), ReelNo.ToArray(), Station.ToArray(), ReelNo.ToArray());
  347. Console.WriteLine();
  348. }
  349. }
  350. }
  351. }
  352. catch (Exception ex)
  353. {
  354. LogMessage($"刷新文件列表时出错: {ex.Message}");
  355. }
  356. }
  357. private void ParseLogInsert(string PathName)
  358. {
  359. try
  360. {
  361. StreamReader SR = File.OpenText(PathName);
  362. string restOfStream = SR.ReadToEnd();
  363. SR.Close();
  364. SR.Dispose();
  365. List<Log> logArr = new List<Log>() { };
  366. string[] lines = restOfStream.Split(new string[] { "\r\n" }, StringSplitOptions.None);
  367. int fileNum = string.IsNullOrEmpty(lines[lines.Length - 1]) ? lines.Length - 1 : lines.Length;
  368. LogMessage($"文件夹{PathName},共{fileNum}条记录");
  369. foreach (var item in lines)
  370. {
  371. if (string.IsNullOrEmpty(item)) continue;
  372. string[] currItem = item.Split(',');
  373. string res = "";
  374. /*List<NgData> data = new List<NgData>();*/
  375. if(equiType == "SPI")
  376. {
  377. if (currItem[1] != "PASS")
  378. {
  379. res = "NG";
  380. #region
  381. /*string[] NgArr = currItem[1].Split(';');
  382. foreach (var ngItem in NgArr)
  383. {
  384. string[] ngItemArr = ngItem.Split('*');
  385. List<string> ngItemLocal = ngItemArr[1].Split('&').ToList();
  386. NgData ngData = new NgData {
  387. Code = ngItemArr[0],
  388. Local = ngItemLocal
  389. };
  390. data.Add(ngData);
  391. }*/
  392. #endregion
  393. }
  394. else
  395. {
  396. res = "PASS";
  397. }
  398. }else if (equiType == "AOI")
  399. {
  400. if (currItem[3] != "PASS")
  401. {
  402. res = "NG";
  403. }
  404. else
  405. {
  406. res = "PASS";
  407. }
  408. }
  409. Log itemLog = new Log()
  410. {
  411. SN = currItem[0],
  412. Result = res,
  413. Details = equiType == "SPI" ? currItem[1] : currItem[3]
  414. };
  415. logArr.Add(itemLog);
  416. LogMessage($"已解析: {currItem[0]}");
  417. }
  418. InsertDb(logArr, PathName);
  419. File.WriteAllText(PathName, string.Empty);
  420. if (ConsoleLog(restOfStream, PathName))
  421. {
  422. File.Delete(PathName);
  423. }
  424. }
  425. catch (Exception ex)
  426. {
  427. MessageBox.Show(this.ParentForm, ex.Message, "警告");
  428. }
  429. }
  430. private void InsertDb(List<Log> logs, string PathName)
  431. {
  432. try
  433. {
  434. int type = Device.SelectedIndex;
  435. StringBuilder sql = new StringBuilder();
  436. List<string> param = new List<string>() { };
  437. foreach (var item in logs)
  438. {
  439. param.Add(item.SN);
  440. param.Add(ma_code.Text);
  441. if (item.Result == "PASS")
  442. {
  443. param.Add(item.Result);
  444. }
  445. else
  446. {
  447. param.Add(item.Result);
  448. }
  449. param.Add(equiType);
  450. param.Add(item.Details);
  451. param.Add(User.UserCode);
  452. param.Add(User.UserSourceCode);
  453. param.Add(User.UserLineCode);
  454. string res = "";
  455. param.Add(res);
  456. string[] paramList = param.ToArray();
  457. dh.CallProcedure("cs_insert_testrejects", ref paramList);
  458. param.Clear();
  459. if (LogicHandler.CheckStepSNAndMacode(ma_code.Text, User.UserSourceCode, item.SN, User.UserCode, out omakeCode, out oMsid, out oErrorMessage))
  460. {
  461. if (LogicHandler.SetStepResult(omakeCode, User.UserSourceCode, item.SN, "日志解析", "OK", User.UserCode, out oErrorMessage))
  462. {
  463. if (oErrorMessage.Contains("AFTERSUCCESS")) LogMessage(">>" + oErrorMessage + "\n");
  464. sql.Clear();
  465. sql.Append("select ma_code,nvl(mcd_okqty,0) mcd_okqty,ma_prodcode,pr_detail,");
  466. sql.Append("ma_qty - nvl(mcd_okqty, 0) remain_qty from make left join makecraftdetail on ");
  467. sql.Append("mcd_maid=ma_id left join product on pr_code=ma_prodcode where ma_code='" + omakeCode + "' and mcd_stepcode='" + User.CurrentStepCode + "'");
  468. dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  469. BaseUtil.SetFormValue(Controls, dt);
  470. //记录日志
  471. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ma_code.Text, User.UserLineCode, User.UserSourceCode, "日志解析", "日志解析成功", item.SN, "");
  472. LogMessage("<<解析成功\n");
  473. }
  474. else
  475. {
  476. LogMessage(">>" + oErrorMessage + "\n");
  477. }
  478. }
  479. else
  480. {
  481. LogMessage(">>" + oErrorMessage + "\n");
  482. }
  483. }
  484. }
  485. catch (Exception ex)
  486. {
  487. MessageBox.Show(this.ParentForm, ex.Message, "警告");
  488. }
  489. }
  490. private bool ConsoleLog(string Content, string PathName)
  491. {
  492. try
  493. {
  494. string sourceDir = Path.GetDirectoryName(PathName);
  495. string newFolderName = "Logs";
  496. string newFolderPath = Path.Combine(sourceDir, newFolderName);
  497. if (!Directory.Exists(newFolderPath))
  498. {
  499. Directory.CreateDirectory(newFolderPath);
  500. }
  501. string newFileName = "Log_" + Path.GetFileName(PathName);
  502. string newFilePath = Path.Combine(newFolderPath, newFileName);
  503. File.AppendAllText(newFilePath, Content + Environment.NewLine);
  504. return true;
  505. }
  506. catch (Exception ex)
  507. {
  508. MessageBox.Show(this.ParentForm, ex.Message, "警告");
  509. return false;
  510. }
  511. }
  512. private void LogMessage(string message)
  513. {
  514. if (lstFiles.InvokeRequired)
  515. {
  516. lstFiles.Invoke(new Action<string>(LogMessage), message);
  517. return;
  518. }
  519. lstFiles.Items.Add($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {message}");
  520. lstFiles.TopIndex = lstFiles.Items.Count - 1;
  521. }
  522. private PrinterData ReadWithXmlReader(string filePath)
  523. {
  524. PrinterData data = new PrinterData();
  525. using (XmlReader reader = XmlReader.Create(filePath))
  526. {
  527. while (reader.Read())
  528. {
  529. if (reader.NodeType == XmlNodeType.Element)
  530. {
  531. switch (reader.Name)
  532. {
  533. case "SN":
  534. data.SN = reader.ReadElementContentAsString();
  535. break;
  536. case "Barcode":
  537. data.Barcode = reader.ReadElementContentAsString();
  538. break;
  539. case "Direction":
  540. data.Direction = reader.ReadElementContentAsString();
  541. break;
  542. case "PrintTime":
  543. data.PrintTime = reader.ReadElementContentAsString();
  544. break;
  545. case "PANEL":
  546. ReadPanelData(reader, data.Panel);
  547. break;
  548. case "PROCESS":
  549. ReadProcessData(reader, data.Process);
  550. break;
  551. }
  552. }
  553. }
  554. }
  555. return data;
  556. }
  557. private void ReadPanelData(XmlReader reader, Panel panel)
  558. {
  559. while (reader.Read())
  560. {
  561. if (reader.NodeType == XmlNodeType.Element)
  562. {
  563. switch (reader.Name)
  564. {
  565. case "PanelID":
  566. panel.PanelID = reader.ReadElementContentAsString();
  567. break;
  568. case "PanelName":
  569. panel.PanelName = reader.ReadElementContentAsString();
  570. break;
  571. case "PanelFirm":
  572. panel.PanelFirm = reader.ReadElementContentAsString();
  573. break;
  574. case "PanelLength":
  575. panel.PanelLength = reader.ReadElementContentAsDouble();
  576. break;
  577. case "PanelWide":
  578. panel.PanelWide = reader.ReadElementContentAsDouble();
  579. break;
  580. case "PanelThickness":
  581. panel.PanelThickness = reader.ReadElementContentAsDouble();
  582. break;
  583. }
  584. }
  585. else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "PANEL")
  586. {
  587. break;
  588. }
  589. }
  590. }
  591. private void ReadProcessData(XmlReader reader, Process process)
  592. {
  593. while (reader.Read())
  594. {
  595. if (reader.NodeType == XmlNodeType.Element)
  596. {
  597. switch (reader.Name)
  598. {
  599. case "ProcessName":
  600. process.ProcessName = reader.ReadElementContentAsString();
  601. break;
  602. case "LineNumber":
  603. process.LineNumber = reader.ReadElementContentAsString();
  604. break;
  605. case "StencilNum":
  606. process.StencilNum = reader.ReadElementContentAsString();
  607. break;
  608. }
  609. }
  610. else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "PROCESS")
  611. {
  612. break;
  613. }
  614. }
  615. }
  616. public List<MachineList> ParseXml(string xmlContent)
  617. {
  618. List<MachineList> result = new List<MachineList>();
  619. try
  620. {
  621. XmlDocument xmlDoc = new XmlDocument();
  622. xmlDoc.LoadXml(xmlContent);
  623. XmlNode machineNode = xmlDoc.SelectSingleNode("//TotalProductionInfo");
  624. string machineName = machineNode?.Attributes["MachineName"]?.Value ?? "Unknown";
  625. string part = machineNode?.Attributes["ProductionProgramName"]?.Value ?? "Unknown";
  626. XmlNodeList feederNodes = xmlDoc.SelectNodes("//FeederProductionManageInfo");
  627. foreach (XmlNode feederNode in feederNodes)
  628. {
  629. MachineList machineList = new MachineList();
  630. string feederBankPosition = feederNode.Attributes["FeederBankPosition"]?.Value ?? "Unknown";
  631. string holeNo = feederNode.Attributes["HoleNo"]?.Value ?? "Unknown";
  632. string componentName = feederNode.Attributes["ComponentName"]?.Value ?? "Unknown";
  633. machineList.MachineName = machineName;
  634. machineList.Part = part;
  635. machineList.ReelNo = componentName;
  636. machineList.Station = $"{machineName}-{feederBankPosition[0]}-{holeNo}";
  637. result.Add(machineList);
  638. }
  639. }
  640. catch (Exception ex)
  641. {
  642. LogMessage($"Error parsing XML: {ex.Message}");
  643. }
  644. return result;
  645. }
  646. public List<MesData> ParseCsvFile(string filePath)
  647. {
  648. var mesDataList = new List<MesData>();
  649. try
  650. {
  651. string fileName = Path.GetFileName(filePath);
  652. var lines = File.ReadAllLines(filePath);
  653. if (lines.Length == 0)
  654. return mesDataList;
  655. for (int i = 0; i < lines.Length; i++)
  656. {
  657. var values = ParseCsvLine(lines[i]);
  658. var mesData = new MesData
  659. {
  660. FileName = fileName,
  661. ModelName = GetValue(values, 0),
  662. LineNumber = GetValue(values, 1),
  663. BoardStatus = GetValue(values, 2),
  664. BoardBarcode = GetValue(values, 3),
  665. ArrayID = GetValue(values, 4),
  666. ArrayStatus = GetValue(values, 5),
  667. ArrayBarcode = GetValue(values, 6),
  668. DateTime = GetValue(values, 7),
  669. PadID = GetValue(values, 8),
  670. ComponentID = GetValue(values, 9),
  671. Type = GetValue(values, 10),
  672. AreaPercent = GetValue(values, 11),
  673. Height = GetValue(values, 12),
  674. VolumePercent = GetValue(values, 13),
  675. XOffset = GetValue(values, 14),
  676. YOffset = GetValue(values, 15),
  677. PadSizeX = GetValue(values, 16),
  678. PadSizeY = GetValue(values, 17),
  679. Area = GetValue(values, 18),
  680. HeightPercent = GetValue(values, 19),
  681. Volume = GetValue(values, 20),
  682. Result = GetValue(values, 21),
  683. Errcode = GetValue(values, 22),
  684. PinNum = GetValue(values, 23),
  685. Barcode = GetValue(values, 24),
  686. Date = GetValue(values, 25),
  687. Time = GetValue(values, 26),
  688. ArrayID2 = GetValue(values, 27)
  689. };
  690. mesDataList.Add(mesData);
  691. }
  692. }
  693. catch (Exception ex)
  694. {
  695. LogMessage($"解析csv文件报错,{ex.Message}");
  696. }
  697. return mesDataList;
  698. }
  699. private string[] ParseCsvLine(string line)
  700. {
  701. List<string> values = new List<string>();
  702. bool inQuotes = false;
  703. int startIndex = 0;
  704. for (int i = 0; i < line.Length; i++)
  705. {
  706. if (line[i] == '"')
  707. {
  708. inQuotes = !inQuotes;
  709. }
  710. else if (line[i] == ',' && !inQuotes)
  711. {
  712. string value = line.Substring(startIndex, i - startIndex).Trim('"');
  713. values.Add(value);
  714. startIndex = i + 1;
  715. }
  716. }
  717. string lastValue = line.Substring(startIndex).Trim('"');
  718. values.Add(lastValue);
  719. return values.ToArray();
  720. }
  721. private string GetValue(string[] values, int index)
  722. {
  723. return index < values.Length ? values[index] : string.Empty;
  724. }
  725. private class Log
  726. {
  727. public string SN { set; get; }
  728. public string Result { set; get; }
  729. /*public List<NgData> Details { set; get; }*/
  730. public string Details { set; get; }
  731. }
  732. public class MachineList
  733. {
  734. public string MachineName { set; get; }
  735. public string Part { set; get; }
  736. public string Station { set; get; }
  737. public string ReelNo { set; get; }
  738. }
  739. private class NgData
  740. {
  741. public string Code { set; get; }
  742. public List<string> Local { set; get; }
  743. }
  744. private class PrinterData
  745. {
  746. public string SN { get; set; }
  747. public string Barcode { get; set; }
  748. public string Direction { get; set; }
  749. public string PrintTime { get; set; }
  750. public Panel Panel { get; set; } = new Panel();
  751. public Process Process { get; set; } = new Process();
  752. }
  753. private class Panel
  754. {
  755. public string PanelID { get; set; }
  756. public string PanelName { get; set; }
  757. public string PanelFirm { get; set; }
  758. public double PanelLength { get; set; }
  759. public double PanelWide { get; set; }
  760. public double PanelThickness { get; set; }
  761. }
  762. private class Process
  763. {
  764. public string ProcessName { get; set; }
  765. public string LineNumber { get; set; }
  766. public string StencilNum { get; set; }
  767. }
  768. public class MesData
  769. {
  770. public string FileName { get; set; }
  771. public string ModelName { get; set; }
  772. public string LineNumber { get; set; }
  773. public string BoardStatus { get; set; }
  774. public string BoardBarcode { get; set; }
  775. public string ArrayID { get; set; }
  776. public string ArrayStatus { get; set; }
  777. public string ArrayBarcode { get; set; }
  778. public string DateTime { get; set; }
  779. public string PadID { get; set; }
  780. public string ComponentID { get; set; }
  781. public string Type { get; set; }
  782. public string AreaPercent { get; set; }
  783. public string Height { get; set; }
  784. public string VolumePercent { get; set; }
  785. public string XOffset { get; set; }
  786. public string YOffset { get; set; }
  787. public string PadSizeX { get; set; }
  788. public string PadSizeY { get; set; }
  789. public string Area { get; set; }
  790. public string HeightPercent { get; set; }
  791. public string Volume { get; set; }
  792. public string Result { get; set; }
  793. public string Errcode { get; set; }
  794. public string PinNum { get; set; }
  795. public string Barcode { get; set; }
  796. public string Date { get; set; }
  797. public string Time { get; set; }
  798. public string ArrayID2 { get; set; }
  799. }
  800. private void claerBtn_Click(object sender, EventArgs e)
  801. {
  802. lstFiles.Items.Clear();
  803. }
  804. }
  805. }