Make_ParseLog.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. using DevExpress.XtraPrinting;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Web.Services.Description;
  12. using System.Windows.Forms;
  13. using System.Xml;
  14. using UAS_MES_NEW.DataOperate;
  15. using UAS_MES_NEW.Entity;
  16. using UAS_MES_NEW.PublicMethod;
  17. using static DevExpress.Xpo.DB.DataStoreLongrunnersWatch;
  18. namespace UAS_MES_NEW.Make
  19. {
  20. public partial class Make_ParseLog : Form
  21. {
  22. public Make_ParseLog()
  23. {
  24. InitializeComponent();
  25. }
  26. DataHelper dh;
  27. private void Make_ParseLog_Load(object sender, EventArgs e)
  28. {
  29. dh = SystemInf.dh;
  30. }
  31. private void Device_SelectedIndexChanged(object sender, EventArgs e)
  32. {
  33. txtPath.Focus();
  34. txtPath.SelectAll();
  35. }
  36. private void Choose_Click(object sender, EventArgs e)
  37. {
  38. if (Device.SelectedIndex == -1)
  39. {
  40. Device.Focus();
  41. Device.SelectAll();
  42. MessageBox.Show(this.ParentForm,"请选择设备", "提示");
  43. return;
  44. }
  45. if (!String.IsNullOrEmpty(txtPath.Text))
  46. {
  47. txtPath.Focus();
  48. txtPath.SelectAll();
  49. }
  50. try {
  51. OpenFileDialog dialog = new OpenFileDialog();
  52. /*dialog.Multiselect = true;//该值确定是否可以选择多个文件*/
  53. switch (Device.SelectedIndex)
  54. {
  55. case 0:
  56. dialog.Title = "请选择读取SPI设备文本文件";
  57. dialog.Filter = "文本文件 (*.txt)|*.txt|All files (*.*)|*.*";
  58. break;
  59. case 1:
  60. dialog.Title = "请选择读取AOI设备文本文件";
  61. dialog.Filter = "文本文件 (*.txt)|*.txt|All files (*.*)|*.*";
  62. break;
  63. case 2:
  64. dialog.Title = "请选择读取印刷机设备的文本文件";
  65. dialog.Filter = "文本文件 (*.xml)|*.xml|All files (*.*)|*.*";
  66. break;
  67. }
  68. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  69. {
  70. txtPath.Text = dialog.FileName;
  71. dialog.Dispose();
  72. ListViewItem item = new ListViewItem();
  73. int count = logList.Items.Count;
  74. item.Text = (++count).ToString();
  75. item.SubItems.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  76. item.SubItems.Add(txtPath.Text);
  77. logList.Items.Add(item);
  78. }
  79. if (Device.SelectedIndex == 0) // SPI
  80. {
  81. ParseLogInsert(txtPath.Text);
  82. Console.WriteLine();
  83. }
  84. else if (Device.SelectedIndex == 1) // AOI
  85. {
  86. }
  87. else if (Device.SelectedIndex == 2) // 印刷机
  88. {
  89. PrinterData xmlData = ReadWithXmlReader(txtPath.Text);
  90. Console.WriteLine();
  91. }
  92. }
  93. catch (Exception ex)
  94. {
  95. MessageBox.Show(this.ParentForm, ex.Message, "警告");
  96. }
  97. }
  98. private void ParseLogInsert(string PathName)
  99. {
  100. try
  101. {
  102. StreamReader SR = File.OpenText(PathName);
  103. string restOfStream = SR.ReadToEnd();
  104. SR.Close();
  105. SR.Dispose();
  106. List<Log> logArr = new List<Log>() { };
  107. string[] lines = restOfStream.Split(new string[] { "\r\n" }, StringSplitOptions.None);
  108. foreach(var item in lines)
  109. {
  110. string[] currItem = item.Split(',');
  111. string res = "";
  112. /*List<NgData> data = new List<NgData>();*/
  113. if (currItem[1] != "PASS")
  114. {
  115. res = "NG";
  116. #region
  117. /*string[] NgArr = currItem[1].Split(';');
  118. foreach (var ngItem in NgArr)
  119. {
  120. string[] ngItemArr = ngItem.Split('*');
  121. List<string> ngItemLocal = ngItemArr[1].Split('&').ToList();
  122. NgData ngData = new NgData {
  123. Code = ngItemArr[0],
  124. Local = ngItemLocal
  125. };
  126. data.Add(ngData);
  127. }*/
  128. #endregion
  129. }
  130. else
  131. {
  132. res = "PASS";
  133. }
  134. Log itemLog = new Log()
  135. {
  136. SN = currItem[0],
  137. Result = res,
  138. Details = currItem[1]
  139. };
  140. logArr.Add(itemLog);
  141. }
  142. InsertDb(logArr);
  143. }
  144. catch (Exception ex)
  145. {
  146. MessageBox.Show(this.ParentForm, ex.Message, "警告");
  147. }
  148. }
  149. private void InsertDb(List<Log> logs)
  150. {
  151. try
  152. {
  153. int type = Device.SelectedIndex;
  154. StringBuilder sql = new StringBuilder();
  155. List<string> param = new List<string>() { };
  156. foreach (var item in logs)
  157. {
  158. ConsoleLog(item);
  159. param.Add(item.SN);
  160. if (item.Result == "PASS")
  161. {
  162. param.Add(item.Result);
  163. }
  164. else
  165. {
  166. param.Add(item.Result);
  167. }
  168. param.Add(item.Details);
  169. param.Add(User.UserCode);
  170. param.Add(User.UserSourceCode);
  171. param.Add(User.UserLineCode);
  172. string res = "";
  173. param.Add(res);
  174. string[] paramList = param.ToArray();
  175. dh.CallProcedure("cs_insert_testrejects", ref paramList);
  176. Console.WriteLine(res);
  177. param.Clear();
  178. }
  179. }
  180. catch (Exception ex)
  181. {
  182. MessageBox.Show(this.ParentForm, ex.Message, "警告");
  183. }
  184. }
  185. private void ConsoleLog(Log item)
  186. {
  187. try
  188. {
  189. string sourceDir = Path.GetDirectoryName(txtPath.Text);
  190. string newFolderName = "Logs";
  191. string newFolderPath = Path.Combine(sourceDir, newFolderName);
  192. if (!Directory.Exists(newFolderPath))
  193. {
  194. Directory.CreateDirectory(newFolderPath);
  195. }
  196. string newFileName = "Log_" + Path.GetFileName(txtPath.Text);
  197. string newFilePath = Path.Combine(newFolderPath, newFileName);
  198. string newContent = $"Time: {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} SN:{item.SN},Result:{item.Result},NgDetails:{item.Details}\r\n";
  199. File.AppendAllText(newFilePath, newContent);
  200. }
  201. catch (Exception ex)
  202. {
  203. MessageBox.Show(this.ParentForm, ex.Message, "警告");
  204. }
  205. }
  206. private PrinterData ReadWithXmlReader(string filePath)
  207. {
  208. PrinterData data = new PrinterData();
  209. using (XmlReader reader = XmlReader.Create(filePath))
  210. {
  211. while (reader.Read())
  212. {
  213. if (reader.NodeType == XmlNodeType.Element)
  214. {
  215. switch (reader.Name)
  216. {
  217. case "SN":
  218. data.SN = reader.ReadElementContentAsString();
  219. break;
  220. case "Barcode":
  221. data.Barcode = reader.ReadElementContentAsString();
  222. break;
  223. case "Direction":
  224. data.Direction = reader.ReadElementContentAsString();
  225. break;
  226. case "PrintTime":
  227. data.PrintTime = reader.ReadElementContentAsString();
  228. break;
  229. case "PANEL":
  230. ReadPanelData(reader, data.Panel);
  231. break;
  232. case "PROCESS":
  233. ReadProcessData(reader, data.Process);
  234. break;
  235. }
  236. }
  237. }
  238. }
  239. return data;
  240. }
  241. private void ReadPanelData(XmlReader reader, Panel panel)
  242. {
  243. while (reader.Read())
  244. {
  245. if (reader.NodeType == XmlNodeType.Element)
  246. {
  247. switch (reader.Name)
  248. {
  249. case "PanelID":
  250. panel.PanelID = reader.ReadElementContentAsString();
  251. break;
  252. case "PanelName":
  253. panel.PanelName = reader.ReadElementContentAsString();
  254. break;
  255. case "PanelFirm":
  256. panel.PanelFirm = reader.ReadElementContentAsString();
  257. break;
  258. case "PanelLength":
  259. panel.PanelLength = reader.ReadElementContentAsDouble();
  260. break;
  261. case "PanelWide":
  262. panel.PanelWide = reader.ReadElementContentAsDouble();
  263. break;
  264. case "PanelThickness":
  265. panel.PanelThickness = reader.ReadElementContentAsDouble();
  266. break;
  267. }
  268. }
  269. else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "PANEL")
  270. {
  271. break;
  272. }
  273. }
  274. }
  275. private void ReadProcessData(XmlReader reader, Process process)
  276. {
  277. while (reader.Read())
  278. {
  279. if (reader.NodeType == XmlNodeType.Element)
  280. {
  281. switch (reader.Name)
  282. {
  283. case "ProcessName":
  284. process.ProcessName = reader.ReadElementContentAsString();
  285. break;
  286. case "LineNumber":
  287. process.LineNumber = reader.ReadElementContentAsString();
  288. break;
  289. case "StencilNum":
  290. process.StencilNum = reader.ReadElementContentAsString();
  291. break;
  292. }
  293. }
  294. else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "PROCESS")
  295. {
  296. break;
  297. }
  298. }
  299. }
  300. private class Log
  301. {
  302. public string SN { set; get; }
  303. public string Result { set; get; }
  304. /*public List<NgData> Details { set; get; }*/
  305. public string Details { set; get; }
  306. }
  307. private class NgData
  308. {
  309. public string Code { set; get; }
  310. public List<string> Local{ set; get; }
  311. }
  312. private class PrinterData
  313. {
  314. public string SN { get; set; }
  315. public string Barcode { get; set; }
  316. public string Direction { get; set; }
  317. public string PrintTime { get; set; }
  318. public Panel Panel { get; set; } = new Panel();
  319. public Process Process { get; set; } = new Process();
  320. }
  321. private class Panel
  322. {
  323. public string PanelID { get; set; }
  324. public string PanelName { get; set; }
  325. public string PanelFirm { get; set; }
  326. public double PanelLength { get; set; }
  327. public double PanelWide { get; set; }
  328. public double PanelThickness { get; set; }
  329. }
  330. private class Process
  331. {
  332. public string ProcessName { get; set; }
  333. public string LineNumber { get; set; }
  334. public string StencilNum { get; set; }
  335. }
  336. }
  337. }