Make_ParseLog.cs 35 KB

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