Make_ParseLog.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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 = 0;
  348. foreach (var item in lines)
  349. {
  350. if (string.IsNullOrEmpty(item)) continue;
  351. fileNum++;
  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. LogMessage($"文件夹{PathName},共{fileNum}条记录");
  399. InsertDb(logArr, PathName);
  400. File.WriteAllText(PathName, string.Empty);
  401. if (ConsoleLog(restOfStream, PathName))
  402. {
  403. File.Delete(PathName);
  404. }
  405. }
  406. catch (Exception ex)
  407. {
  408. MessageBox.Show(this.ParentForm, ex.Message, "警告");
  409. }
  410. }
  411. private void InsertDb(List<Log> logs, string PathName)
  412. {
  413. try
  414. {
  415. int type = Device.SelectedIndex;
  416. StringBuilder sql = new StringBuilder();
  417. List<string> param = new List<string>() { };
  418. foreach (var item in logs)
  419. {
  420. param.Add(item.SN);
  421. param.Add(ma_code.Text);
  422. if (item.Result == "PASS")
  423. {
  424. param.Add(item.Result);
  425. }
  426. else
  427. {
  428. param.Add(item.Result);
  429. }
  430. param.Add(equiType);
  431. param.Add(item.Details);
  432. param.Add(User.UserCode);
  433. param.Add(User.UserSourceCode);
  434. param.Add(User.UserLineCode);
  435. string res = "";
  436. param.Add(res);
  437. string[] paramList = param.ToArray();
  438. dh.CallProcedure("cs_insert_testrejects", ref paramList);
  439. param.Clear();
  440. if (LogicHandler.CheckStepSNAndMacode(ma_code.Text, User.UserSourceCode, item.SN, User.UserCode, out omakeCode, out oMsid, out oErrorMessage))
  441. {
  442. if (LogicHandler.SetStepResult(omakeCode, User.UserSourceCode, item.SN, "日志解析", "OK", User.UserCode, out oErrorMessage))
  443. {
  444. if (oErrorMessage.Contains("AFTERSUCCESS")) LogMessage(">>" + oErrorMessage + "\n");
  445. sql.Clear();
  446. sql.Append("select ma_code,nvl(mcd_okqty,0) mcd_okqty,ma_prodcode,pr_detail,");
  447. sql.Append("ma_qty - nvl(mcd_okqty, 0) remain_qty from make left join makecraftdetail on ");
  448. sql.Append("mcd_maid=ma_id left join product on pr_code=ma_prodcode where ma_code='" + omakeCode + "' and mcd_stepcode='" + User.CurrentStepCode + "'");
  449. dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  450. BaseUtil.SetFormValue(Controls, dt);
  451. //记录日志
  452. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ma_code.Text, User.UserLineCode, User.UserSourceCode, "日志解析", "日志解析成功", item.SN, "");
  453. LogMessage("<<解析成功\n");
  454. }
  455. else
  456. {
  457. LogMessage(">>" + oErrorMessage + "\n");
  458. }
  459. }
  460. else
  461. {
  462. LogMessage(">>" + oErrorMessage + "\n");
  463. }
  464. }
  465. }
  466. catch (Exception ex)
  467. {
  468. MessageBox.Show(this.ParentForm, ex.Message, "警告");
  469. }
  470. }
  471. private bool ConsoleLog(string Content, string PathName)
  472. {
  473. try
  474. {
  475. string sourceDir = Path.GetDirectoryName(PathName);
  476. string newFolderName = "Logs";
  477. string newFolderPath = Path.Combine(sourceDir, newFolderName);
  478. if (!Directory.Exists(newFolderPath))
  479. {
  480. Directory.CreateDirectory(newFolderPath);
  481. }
  482. string newFileName = "Log_" + Path.GetFileName(PathName);
  483. string newFilePath = Path.Combine(newFolderPath, newFileName);
  484. File.AppendAllText(newFilePath, Content + Environment.NewLine);
  485. return true;
  486. }
  487. catch (Exception ex)
  488. {
  489. MessageBox.Show(this.ParentForm, ex.Message, "警告");
  490. return false;
  491. }
  492. }
  493. private void LogMessage(string message)
  494. {
  495. if (lstFiles.InvokeRequired)
  496. {
  497. lstFiles.Invoke(new Action<string>(LogMessage), message);
  498. return;
  499. }
  500. lstFiles.Items.Add($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {message}");
  501. lstFiles.TopIndex = lstFiles.Items.Count - 1;
  502. }
  503. private PrinterData ReadWithXmlReader(string filePath)
  504. {
  505. PrinterData data = new PrinterData();
  506. using (XmlReader reader = XmlReader.Create(filePath))
  507. {
  508. while (reader.Read())
  509. {
  510. if (reader.NodeType == XmlNodeType.Element)
  511. {
  512. switch (reader.Name)
  513. {
  514. case "SN":
  515. data.SN = reader.ReadElementContentAsString();
  516. break;
  517. case "Barcode":
  518. data.Barcode = reader.ReadElementContentAsString();
  519. break;
  520. case "Direction":
  521. data.Direction = reader.ReadElementContentAsString();
  522. break;
  523. case "PrintTime":
  524. data.PrintTime = reader.ReadElementContentAsString();
  525. break;
  526. case "PANEL":
  527. ReadPanelData(reader, data.Panel);
  528. break;
  529. case "PROCESS":
  530. ReadProcessData(reader, data.Process);
  531. break;
  532. }
  533. }
  534. }
  535. }
  536. return data;
  537. }
  538. private void ReadPanelData(XmlReader reader, Panel panel)
  539. {
  540. while (reader.Read())
  541. {
  542. if (reader.NodeType == XmlNodeType.Element)
  543. {
  544. switch (reader.Name)
  545. {
  546. case "PanelID":
  547. panel.PanelID = reader.ReadElementContentAsString();
  548. break;
  549. case "PanelName":
  550. panel.PanelName = reader.ReadElementContentAsString();
  551. break;
  552. case "PanelFirm":
  553. panel.PanelFirm = reader.ReadElementContentAsString();
  554. break;
  555. case "PanelLength":
  556. panel.PanelLength = reader.ReadElementContentAsDouble();
  557. break;
  558. case "PanelWide":
  559. panel.PanelWide = reader.ReadElementContentAsDouble();
  560. break;
  561. case "PanelThickness":
  562. panel.PanelThickness = reader.ReadElementContentAsDouble();
  563. break;
  564. }
  565. }
  566. else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "PANEL")
  567. {
  568. break;
  569. }
  570. }
  571. }
  572. private void ReadProcessData(XmlReader reader, Process process)
  573. {
  574. while (reader.Read())
  575. {
  576. if (reader.NodeType == XmlNodeType.Element)
  577. {
  578. switch (reader.Name)
  579. {
  580. case "ProcessName":
  581. process.ProcessName = reader.ReadElementContentAsString();
  582. break;
  583. case "LineNumber":
  584. process.LineNumber = reader.ReadElementContentAsString();
  585. break;
  586. case "StencilNum":
  587. process.StencilNum = reader.ReadElementContentAsString();
  588. break;
  589. }
  590. }
  591. else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "PROCESS")
  592. {
  593. break;
  594. }
  595. }
  596. }
  597. public List<MachineList> ParseXml(string xmlContent)
  598. {
  599. List<MachineList> result = new List<MachineList>();
  600. try
  601. {
  602. XmlDocument xmlDoc = new XmlDocument();
  603. xmlDoc.LoadXml(xmlContent);
  604. XmlNode machineNode = xmlDoc.SelectSingleNode("//TotalProductionInfo");
  605. string machineName = machineNode?.Attributes["MachineName"]?.Value ?? "Unknown";
  606. string part = machineNode?.Attributes["ProductionProgramName"]?.Value ?? "Unknown";
  607. XmlNodeList feederNodes = xmlDoc.SelectNodes("//FeederProductionManageInfo");
  608. foreach (XmlNode feederNode in feederNodes)
  609. {
  610. MachineList machineList = new MachineList();
  611. string feederBankPosition = feederNode.Attributes["FeederBankPosition"]?.Value ?? "Unknown";
  612. string holeNo = feederNode.Attributes["HoleNo"]?.Value ?? "Unknown";
  613. string componentName = feederNode.Attributes["ComponentName"]?.Value ?? "Unknown";
  614. machineList.MachineName = machineName;
  615. machineList.Part = part;
  616. machineList.ReelNo = componentName;
  617. machineList.Station = $"{machineName}-{feederBankPosition[0]}-{holeNo}";
  618. result.Add(machineList);
  619. }
  620. }
  621. catch (Exception ex)
  622. {
  623. LogMessage($"Error parsing XML: {ex.Message}");
  624. }
  625. return result;
  626. }
  627. public List<MesData> ParseCsvFile(string filePath)
  628. {
  629. var mesDataList = new List<MesData>();
  630. try
  631. {
  632. string fileName = Path.GetFileName(filePath);
  633. var lines = File.ReadAllLines(filePath);
  634. if (lines.Length == 0)
  635. return mesDataList;
  636. for (int i = 0; i < lines.Length; i++)
  637. {
  638. var values = ParseCsvLine(lines[i]);
  639. var mesData = new MesData
  640. {
  641. FileName = fileName,
  642. ModelName = GetValue(values, 0),
  643. LineNumber = GetValue(values, 1),
  644. BoardStatus = GetValue(values, 2),
  645. BoardBarcode = GetValue(values, 3),
  646. ArrayID = GetValue(values, 4),
  647. ArrayStatus = GetValue(values, 5),
  648. ArrayBarcode = GetValue(values, 6),
  649. DateTime = GetValue(values, 7),
  650. PadID = GetValue(values, 8),
  651. ComponentID = GetValue(values, 9),
  652. Type = GetValue(values, 10),
  653. AreaPercent = GetValue(values, 11),
  654. Height = GetValue(values, 12),
  655. VolumePercent = GetValue(values, 13),
  656. XOffset = GetValue(values, 14),
  657. YOffset = GetValue(values, 15),
  658. PadSizeX = GetValue(values, 16),
  659. PadSizeY = GetValue(values, 17),
  660. Area = GetValue(values, 18),
  661. HeightPercent = GetValue(values, 19),
  662. Volume = GetValue(values, 20),
  663. Result = GetValue(values, 21),
  664. Errcode = GetValue(values, 22),
  665. PinNum = GetValue(values, 23),
  666. Barcode = GetValue(values, 24),
  667. Date = GetValue(values, 25),
  668. Time = GetValue(values, 26),
  669. ArrayID2 = GetValue(values, 27)
  670. };
  671. mesDataList.Add(mesData);
  672. }
  673. }
  674. catch (Exception ex)
  675. {
  676. LogMessage($"解析csv文件报错,{ex.Message}");
  677. }
  678. return mesDataList;
  679. }
  680. private string[] ParseCsvLine(string line)
  681. {
  682. List<string> values = new List<string>();
  683. bool inQuotes = false;
  684. int startIndex = 0;
  685. for (int i = 0; i < line.Length; i++)
  686. {
  687. if (line[i] == '"')
  688. {
  689. inQuotes = !inQuotes;
  690. }
  691. else if (line[i] == ',' && !inQuotes)
  692. {
  693. string value = line.Substring(startIndex, i - startIndex).Trim('"');
  694. values.Add(value);
  695. startIndex = i + 1;
  696. }
  697. }
  698. string lastValue = line.Substring(startIndex).Trim('"');
  699. values.Add(lastValue);
  700. return values.ToArray();
  701. }
  702. private string GetValue(string[] values, int index)
  703. {
  704. return index < values.Length ? values[index] : string.Empty;
  705. }
  706. private class Log
  707. {
  708. public string SN { set; get; }
  709. public string Result { set; get; }
  710. /*public List<NgData> Details { set; get; }*/
  711. public string Details { set; get; }
  712. }
  713. public class MachineList
  714. {
  715. public string MachineName { set; get; }
  716. public string Part { set; get; }
  717. public string Station { set; get; }
  718. public string ReelNo { set; get; }
  719. }
  720. private class NgData
  721. {
  722. public string Code { set; get; }
  723. public List<string> Local { set; get; }
  724. }
  725. private class PrinterData
  726. {
  727. public string SN { get; set; }
  728. public string Barcode { get; set; }
  729. public string Direction { get; set; }
  730. public string PrintTime { get; set; }
  731. public Panel Panel { get; set; } = new Panel();
  732. public Process Process { get; set; } = new Process();
  733. }
  734. private class Panel
  735. {
  736. public string PanelID { get; set; }
  737. public string PanelName { get; set; }
  738. public string PanelFirm { get; set; }
  739. public double PanelLength { get; set; }
  740. public double PanelWide { get; set; }
  741. public double PanelThickness { get; set; }
  742. }
  743. private class Process
  744. {
  745. public string ProcessName { get; set; }
  746. public string LineNumber { get; set; }
  747. public string StencilNum { get; set; }
  748. }
  749. public class MesData
  750. {
  751. public string FileName { get; set; }
  752. public string ModelName { get; set; }
  753. public string LineNumber { get; set; }
  754. public string BoardStatus { get; set; }
  755. public string BoardBarcode { get; set; }
  756. public string ArrayID { get; set; }
  757. public string ArrayStatus { get; set; }
  758. public string ArrayBarcode { get; set; }
  759. public string DateTime { get; set; }
  760. public string PadID { get; set; }
  761. public string ComponentID { get; set; }
  762. public string Type { get; set; }
  763. public string AreaPercent { get; set; }
  764. public string Height { get; set; }
  765. public string VolumePercent { get; set; }
  766. public string XOffset { get; set; }
  767. public string YOffset { get; set; }
  768. public string PadSizeX { get; set; }
  769. public string PadSizeY { get; set; }
  770. public string Area { get; set; }
  771. public string HeightPercent { get; set; }
  772. public string Volume { get; set; }
  773. public string Result { get; set; }
  774. public string Errcode { get; set; }
  775. public string PinNum { get; set; }
  776. public string Barcode { get; set; }
  777. public string Date { get; set; }
  778. public string Time { get; set; }
  779. public string ArrayID2 { get; set; }
  780. }
  781. private void claerBtn_Click(object sender, EventArgs e)
  782. {
  783. lstFiles.Items.Clear();
  784. }
  785. }
  786. }