Make_ParseLog.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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 (string.IsNullOrEmpty(ma_code.Text))
  73. {
  74. Device.SelectedIndex = -1;
  75. MessageBox.Show(this.ParentForm, "请选择工单", "提示");
  76. return;
  77. }
  78. txtPath.Focus();
  79. txtPath.SelectAll();
  80. }
  81. private void allParse_Click(object sender, EventArgs e)
  82. {
  83. if (string.IsNullOrEmpty(ma_code.Text))
  84. {
  85. MessageBox.Show(this.ParentForm, "请选择工单", "提示");
  86. return;
  87. }
  88. if (Device.SelectedIndex == -1)
  89. {
  90. Device.Focus();
  91. Device.SelectAll();
  92. MessageBox.Show(this.ParentForm, "请选择设备", "提示");
  93. return;
  94. }
  95. if (String.IsNullOrEmpty(txtPath.Text))
  96. {
  97. txtPath.Focus();
  98. txtPath.SelectAll();
  99. return;
  100. }
  101. RefreshFileList();
  102. }
  103. private void Choose_Click(object sender, EventArgs e)
  104. {
  105. if (string.IsNullOrEmpty(ma_code.Text))
  106. {
  107. MessageBox.Show(this.ParentForm, "请选择工单", "提示");
  108. return;
  109. }
  110. if (Device.SelectedIndex == -1)
  111. {
  112. Device.Focus();
  113. Device.SelectAll();
  114. MessageBox.Show(this.ParentForm,"请选择设备", "提示");
  115. return;
  116. }
  117. if (!String.IsNullOrEmpty(txtPath.Text))
  118. {
  119. txtPath.Focus();
  120. txtPath.SelectAll();
  121. }
  122. try {
  123. FolderBrowserDialog dialog = new FolderBrowserDialog();
  124. switch (Device.SelectedIndex)
  125. {
  126. case 0:
  127. dialog.Description = "请选择读取SPI设备文件夹";
  128. currFileType = "txt";
  129. equiType = "1";
  130. break;
  131. case 1:
  132. dialog.Description = "请选择读取AOI设备文本文件";
  133. currFileType = "txt";
  134. equiType = "2";
  135. break;
  136. case 2:
  137. dialog.Description = "请选择读取印刷机设备的文本文件夹";
  138. currFileType = "xml";
  139. equiType = "3";
  140. break;
  141. case 3:
  142. dialog.Description = "请选择读取贴片机的文本文件夹";
  143. currFileType = "xml";
  144. equiType = "4";
  145. break;
  146. }
  147. if (dialog.ShowDialog() == DialogResult.OK)
  148. {
  149. txtPath.Text = dialog.SelectedPath;
  150. txtPath.Enabled = false;
  151. Device.Enabled = false;
  152. var watcher = new FileSystemWatcher(txtPath.Text);
  153. watcher.Filter = $"*.{currFileType}";
  154. watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;
  155. watcher.Created += OnFileCreated;
  156. /*watcher.Changed += OnFileChanged;
  157. watcher.Deleted += OnFileChanged;
  158. watcher.Renamed += OnFileChanged;*/
  159. watcher.EnableRaisingEvents = true;
  160. }
  161. #region
  162. /*OpenFileDialog dialog = new OpenFileDialog();
  163. dialog.Multiselect = true;//该值确定是否可以选择多个文件
  164. dialog.Filter = "文本文件 (*.txt)|*.txt|All files (*.*)|*.*";
  165. switch (Device.SelectedIndex)
  166. {
  167. case 0:
  168. dialog.Title = "请选择读取SPI设备文件夹";
  169. dialog.Filter = "All files (*.*)|*.*";
  170. break;
  171. case 1:
  172. dialog.Title = "请选择读取AOI设备文本文件";
  173. dialog.Filter = "All files (*.*)|*.*";
  174. break;
  175. case 2:
  176. dialog.Title = "请选择读取印刷机设备的文本文件夹";
  177. dialog.Filter = "文本文件 (*.xml)|*.xml|All files (*.*)|*.*";
  178. break;
  179. }
  180. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  181. {
  182. txtPath.Text = dialog.FileName;
  183. dialog.Dispose();
  184. ListViewItem item = new ListViewItem();
  185. int count = logList.Items.Count;
  186. item.Text = (++count).ToString();
  187. item.SubItems.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  188. item.SubItems.Add(txtPath.Text);
  189. logList.Items.Add(item);
  190. }
  191. if (Device.SelectedIndex == 0) // SPI
  192. {
  193. ParseLogInsert(txtPath.Text);
  194. Console.WriteLine();
  195. }
  196. else if (Device.SelectedIndex == 1) // AOI
  197. {
  198. }
  199. else if (Device.SelectedIndex == 2) // 印刷机
  200. {
  201. PrinterData xmlData = ReadWithXmlReader(txtPath.Text);
  202. Console.WriteLine();
  203. }*/
  204. #endregion
  205. }
  206. catch (Exception ex)
  207. {
  208. MessageBox.Show(this.ParentForm, ex.Message, "警告");
  209. }
  210. }
  211. private void OnFileCreated(object sender, FileSystemEventArgs e)
  212. {
  213. RefreshFileList();
  214. }
  215. private void OnFileChanged(object sender, FileSystemEventArgs e)
  216. {
  217. if (e.ChangeType == WatcherChangeTypes.Changed)
  218. {
  219. RefreshFileList();
  220. }
  221. }
  222. private void RefreshFileList()
  223. {
  224. // 在UI线程上执行更新
  225. if (lstFiles.InvokeRequired)
  226. {
  227. lstFiles.Invoke(new Action(RefreshFileList));
  228. return;
  229. }
  230. try
  231. {
  232. if (!Directory.Exists(txtPath.Text))
  233. {
  234. LogMessage($"目录不存在: {txtPath.Text}");
  235. return;
  236. }
  237. string[] txtFiles = Directory.GetFiles(txtPath.Text, $"*.{currFileType}");
  238. if (txtFiles.Length == 0)
  239. {
  240. LogMessage($"当前没有{currFileType.ToUpper()}文件");
  241. return;
  242. }
  243. else
  244. {
  245. foreach (string file in txtFiles)
  246. {
  247. LogMessage(file);
  248. if (Device.SelectedIndex == 0) // SPI
  249. {
  250. ParseLogInsert(file);
  251. Console.WriteLine();
  252. }
  253. else if (Device.SelectedIndex == 1) // AOI
  254. {
  255. }
  256. else if (Device.SelectedIndex == 2) // 印刷机
  257. {
  258. PrinterData xmlData = ReadWithXmlReader(file);
  259. Console.WriteLine();
  260. }
  261. else if (Device.SelectedIndex == 3) // 贴片机
  262. {
  263. if (!File.Exists(file))
  264. {
  265. LogMessage($"文件不存在: {file}");
  266. }
  267. string xmlContent = File.ReadAllText(file);
  268. var xmlList = ParseXml(xmlContent);
  269. if(ConsoleLog(xmlContent, file))
  270. {
  271. File.Delete(file);
  272. }
  273. List<string> param = new List<string>() { };
  274. string PS_CODE = "";
  275. LogicHandler.GetSerialNumByCaller("Make!ParseLog", out PS_CODE);
  276. foreach (var item in xmlList)
  277. {
  278. param.Clear();
  279. param.Add(item.MachineName);
  280. param.Add(item.Part);
  281. param.Add(item.Station);
  282. param.Add(item.ReelNo);
  283. param.Add(User.UserCode);
  284. param.Add(PS_CODE);
  285. string res = "";
  286. param.Add(res);
  287. string[] paramList = param.ToArray();
  288. dh.CallProcedure("CS_INSERT_STATION", ref paramList);
  289. res = paramList[6];
  290. if (!string.IsNullOrEmpty(res))
  291. {
  292. LogMessage(res);
  293. }
  294. }
  295. Console.WriteLine();
  296. }
  297. }
  298. LogMessage($"共找到{txtFiles.Length}个{currFileType.ToUpper()}文件");
  299. }
  300. }
  301. catch (Exception ex)
  302. {
  303. LogMessage($"刷新文件列表时出错: {ex.Message}");
  304. }
  305. }
  306. private void ParseLogInsert(string PathName)
  307. {
  308. try
  309. {
  310. StreamReader SR = File.OpenText(PathName);
  311. string restOfStream = SR.ReadToEnd();
  312. SR.Close();
  313. SR.Dispose();
  314. List<Log> logArr = new List<Log>() { };
  315. string[] lines = restOfStream.Split(new string[] { "\r\n" }, StringSplitOptions.None);
  316. LogMessage($"文件夹{PathName},共{lines.Length}条记录");
  317. foreach (var item in lines)
  318. {
  319. if (string.IsNullOrEmpty(item)) continue;
  320. string[] currItem = item.Split(',');
  321. string res = "";
  322. /*List<NgData> data = new List<NgData>();*/
  323. if (currItem[1] != "PASS")
  324. {
  325. res = "NG";
  326. #region
  327. /*string[] NgArr = currItem[1].Split(';');
  328. foreach (var ngItem in NgArr)
  329. {
  330. string[] ngItemArr = ngItem.Split('*');
  331. List<string> ngItemLocal = ngItemArr[1].Split('&').ToList();
  332. NgData ngData = new NgData {
  333. Code = ngItemArr[0],
  334. Local = ngItemLocal
  335. };
  336. data.Add(ngData);
  337. }*/
  338. #endregion
  339. }
  340. else
  341. {
  342. res = "PASS";
  343. }
  344. Log itemLog = new Log()
  345. {
  346. SN = currItem[0],
  347. Result = res,
  348. Details = currItem[1]
  349. };
  350. logArr.Add(itemLog);
  351. LogMessage($"已解析: {currItem[0]}");
  352. }
  353. InsertDb(logArr, PathName);
  354. File.WriteAllText(PathName, string.Empty);
  355. if (ConsoleLog(restOfStream, PathName))
  356. {
  357. File.Delete(PathName);
  358. }
  359. }
  360. catch (Exception ex)
  361. {
  362. MessageBox.Show(this.ParentForm, ex.Message, "警告");
  363. }
  364. }
  365. private void InsertDb(List<Log> logs, string PathName)
  366. {
  367. try
  368. {
  369. int type = Device.SelectedIndex;
  370. StringBuilder sql = new StringBuilder();
  371. List<string> param = new List<string>() { };
  372. foreach (var item in logs)
  373. {
  374. param.Add(item.SN);
  375. param.Add(ma_code.Text);
  376. if (item.Result == "PASS")
  377. {
  378. param.Add(item.Result);
  379. }
  380. else
  381. {
  382. param.Add(item.Result);
  383. }
  384. param.Add(equiType);
  385. param.Add(item.Details);
  386. param.Add(User.UserCode);
  387. param.Add(User.UserSourceCode);
  388. param.Add(User.UserLineCode);
  389. string res = "";
  390. param.Add(res);
  391. string[] paramList = param.ToArray();
  392. dh.CallProcedure("cs_insert_testrejects", ref paramList);
  393. param.Clear();
  394. if (LogicHandler.CheckStepSNAndMacode(ma_code.Text, User.UserSourceCode, item.SN, User.UserCode, out omakeCode, out oMsid, out oErrorMessage))
  395. {
  396. if (LogicHandler.SetStepResult(omakeCode, User.UserSourceCode, item.SN, "日志解析", "OK", User.UserCode, out oErrorMessage))
  397. {
  398. if (oErrorMessage.Contains("AFTERSUCCESS")) LogMessage(">>" + oErrorMessage + "\n");
  399. sql.Clear();
  400. sql.Append("select ma_code,nvl(mcd_okqty,0) mcd_okqty,ma_prodcode,pr_detail,");
  401. sql.Append("ma_qty - nvl(mcd_okqty, 0) remain_qty from make left join makecraftdetail on ");
  402. sql.Append("mcd_maid=ma_id left join product on pr_code=ma_prodcode where ma_code='" + omakeCode + "' and mcd_stepcode='" + User.CurrentStepCode + "'");
  403. dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
  404. BaseUtil.SetFormValue(Controls, dt);
  405. //记录日志
  406. LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ma_code.Text, User.UserLineCode, User.UserSourceCode, "日志解析", "日志解析成功", item.SN, "");
  407. LogMessage("<<解析成功\n");
  408. }
  409. else
  410. {
  411. LogMessage(">>" + oErrorMessage + "\n");
  412. }
  413. }
  414. else
  415. {
  416. LogMessage(">>" + oErrorMessage + "\n");
  417. }
  418. }
  419. }
  420. catch (Exception ex)
  421. {
  422. MessageBox.Show(this.ParentForm, ex.Message, "警告");
  423. }
  424. }
  425. private bool ConsoleLog(string Content, string PathName)
  426. {
  427. try
  428. {
  429. string sourceDir = Path.GetDirectoryName(PathName);
  430. string newFolderName = "Logs";
  431. string newFolderPath = Path.Combine(sourceDir, newFolderName);
  432. if (!Directory.Exists(newFolderPath))
  433. {
  434. Directory.CreateDirectory(newFolderPath);
  435. }
  436. string newFileName = "Log_" + Path.GetFileName(PathName);
  437. string newFilePath = Path.Combine(newFolderPath, newFileName);
  438. File.AppendAllText(newFilePath, Content + Environment.NewLine);
  439. return true;
  440. }
  441. catch (Exception ex)
  442. {
  443. MessageBox.Show(this.ParentForm, ex.Message, "警告");
  444. return false;
  445. }
  446. }
  447. private void LogMessage(string message)
  448. {
  449. if (lstFiles.InvokeRequired)
  450. {
  451. lstFiles.Invoke(new Action<string>(LogMessage), message);
  452. return;
  453. }
  454. lstFiles.Items.Add($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {message}");
  455. lstFiles.TopIndex = lstFiles.Items.Count - 1;
  456. }
  457. private PrinterData ReadWithXmlReader(string filePath)
  458. {
  459. PrinterData data = new PrinterData();
  460. using (XmlReader reader = XmlReader.Create(filePath))
  461. {
  462. while (reader.Read())
  463. {
  464. if (reader.NodeType == XmlNodeType.Element)
  465. {
  466. switch (reader.Name)
  467. {
  468. case "SN":
  469. data.SN = reader.ReadElementContentAsString();
  470. break;
  471. case "Barcode":
  472. data.Barcode = reader.ReadElementContentAsString();
  473. break;
  474. case "Direction":
  475. data.Direction = reader.ReadElementContentAsString();
  476. break;
  477. case "PrintTime":
  478. data.PrintTime = reader.ReadElementContentAsString();
  479. break;
  480. case "PANEL":
  481. ReadPanelData(reader, data.Panel);
  482. break;
  483. case "PROCESS":
  484. ReadProcessData(reader, data.Process);
  485. break;
  486. }
  487. }
  488. }
  489. }
  490. return data;
  491. }
  492. private void ReadPanelData(XmlReader reader, Panel panel)
  493. {
  494. while (reader.Read())
  495. {
  496. if (reader.NodeType == XmlNodeType.Element)
  497. {
  498. switch (reader.Name)
  499. {
  500. case "PanelID":
  501. panel.PanelID = reader.ReadElementContentAsString();
  502. break;
  503. case "PanelName":
  504. panel.PanelName = reader.ReadElementContentAsString();
  505. break;
  506. case "PanelFirm":
  507. panel.PanelFirm = reader.ReadElementContentAsString();
  508. break;
  509. case "PanelLength":
  510. panel.PanelLength = reader.ReadElementContentAsDouble();
  511. break;
  512. case "PanelWide":
  513. panel.PanelWide = reader.ReadElementContentAsDouble();
  514. break;
  515. case "PanelThickness":
  516. panel.PanelThickness = reader.ReadElementContentAsDouble();
  517. break;
  518. }
  519. }
  520. else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "PANEL")
  521. {
  522. break;
  523. }
  524. }
  525. }
  526. private void ReadProcessData(XmlReader reader, Process process)
  527. {
  528. while (reader.Read())
  529. {
  530. if (reader.NodeType == XmlNodeType.Element)
  531. {
  532. switch (reader.Name)
  533. {
  534. case "ProcessName":
  535. process.ProcessName = reader.ReadElementContentAsString();
  536. break;
  537. case "LineNumber":
  538. process.LineNumber = reader.ReadElementContentAsString();
  539. break;
  540. case "StencilNum":
  541. process.StencilNum = reader.ReadElementContentAsString();
  542. break;
  543. }
  544. }
  545. else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "PROCESS")
  546. {
  547. break;
  548. }
  549. }
  550. }
  551. public List<MachineList> ParseXml(string xmlContent)
  552. {
  553. List<MachineList> result = new List<MachineList>();
  554. try
  555. {
  556. XmlDocument xmlDoc = new XmlDocument();
  557. xmlDoc.LoadXml(xmlContent);
  558. XmlNode machineNode = xmlDoc.SelectSingleNode("//TotalProductionInfo");
  559. string machineName = machineNode?.Attributes["MachineName"]?.Value ?? "Unknown";
  560. string part = machineNode?.Attributes["ProductionProgramName"]?.Value ?? "Unknown";
  561. XmlNodeList feederNodes = xmlDoc.SelectNodes("//FeederProductionManageInfo");
  562. foreach (XmlNode feederNode in feederNodes)
  563. {
  564. MachineList machineList = new MachineList();
  565. string feederBankPosition = feederNode.Attributes["FeederBankPosition"]?.Value ?? "Unknown";
  566. string holeNo = feederNode.Attributes["HoleNo"]?.Value ?? "Unknown";
  567. string componentName = feederNode.Attributes["ComponentName"]?.Value ?? "Unknown";
  568. machineList.MachineName = machineName;
  569. machineList.Part = part;
  570. machineList.ReelNo = componentName;
  571. machineList.Station = $"{machineName}-{feederBankPosition[0]}-{holeNo}";
  572. result.Add(machineList);
  573. }
  574. }
  575. catch (Exception ex)
  576. {
  577. LogMessage($"Error parsing XML: {ex.Message}");
  578. }
  579. return result;
  580. }
  581. private class Log
  582. {
  583. public string SN { set; get; }
  584. public string Result { set; get; }
  585. /*public List<NgData> Details { set; get; }*/
  586. public string Details { set; get; }
  587. }
  588. public class MachineList
  589. {
  590. public string MachineName { set; get; }
  591. public string Part { set; get; }
  592. public string Station { set; get; }
  593. public string ReelNo { set; get; }
  594. }
  595. private class NgData
  596. {
  597. public string Code { set; get; }
  598. public List<string> Local{ set; get; }
  599. }
  600. private class PrinterData
  601. {
  602. public string SN { get; set; }
  603. public string Barcode { get; set; }
  604. public string Direction { get; set; }
  605. public string PrintTime { get; set; }
  606. public Panel Panel { get; set; } = new Panel();
  607. public Process Process { get; set; } = new Process();
  608. }
  609. private class Panel
  610. {
  611. public string PanelID { get; set; }
  612. public string PanelName { get; set; }
  613. public string PanelFirm { get; set; }
  614. public double PanelLength { get; set; }
  615. public double PanelWide { get; set; }
  616. public double PanelThickness { get; set; }
  617. }
  618. private class Process
  619. {
  620. public string ProcessName { get; set; }
  621. public string LineNumber { get; set; }
  622. public string StencilNum { get; set; }
  623. }
  624. private void claerBtn_Click(object sender, EventArgs e)
  625. {
  626. lstFiles.Items.Clear();
  627. }
  628. }
  629. }