Make_ParseLog.cs 34 KB

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