| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- using DevExpress.XtraPrinting;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Web.Services.Description;
- using System.Windows.Forms;
- using System.Xml;
- using UAS_MES_NEW.DataOperate;
- using UAS_MES_NEW.Entity;
- using UAS_MES_NEW.PublicMethod;
- using static DevExpress.Xpo.DB.DataStoreLongrunnersWatch;
- namespace UAS_MES_NEW.Make
- {
- public partial class Make_ParseLog : Form
- {
- public Make_ParseLog()
- {
- InitializeComponent();
- }
- DataHelper dh;
- private void Make_ParseLog_Load(object sender, EventArgs e)
- {
- dh = SystemInf.dh;
- }
- private void Device_SelectedIndexChanged(object sender, EventArgs e)
- {
- txtPath.Focus();
- txtPath.SelectAll();
- }
- private void Choose_Click(object sender, EventArgs e)
- {
- if (Device.SelectedIndex == -1)
- {
- Device.Focus();
- Device.SelectAll();
- MessageBox.Show(this.ParentForm,"请选择设备", "提示");
- return;
- }
- if (!String.IsNullOrEmpty(txtPath.Text))
- {
- txtPath.Focus();
- txtPath.SelectAll();
- }
- try {
- OpenFileDialog dialog = new OpenFileDialog();
- /*dialog.Multiselect = true;//该值确定是否可以选择多个文件*/
- switch (Device.SelectedIndex)
- {
- case 0:
- dialog.Title = "请选择读取SPI设备文本文件";
- dialog.Filter = "文本文件 (*.txt)|*.txt|All files (*.*)|*.*";
- break;
- case 1:
- dialog.Title = "请选择读取AOI设备文本文件";
- dialog.Filter = "文本文件 (*.txt)|*.txt|All files (*.*)|*.*";
- break;
- case 2:
- dialog.Title = "请选择读取印刷机设备的文本文件";
- dialog.Filter = "文本文件 (*.xml)|*.xml|All files (*.*)|*.*";
- break;
- }
-
- if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- txtPath.Text = dialog.FileName;
- dialog.Dispose();
- ListViewItem item = new ListViewItem();
- int count = logList.Items.Count;
- item.Text = (++count).ToString();
- item.SubItems.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
- item.SubItems.Add(txtPath.Text);
- logList.Items.Add(item);
- }
- if (Device.SelectedIndex == 0) // SPI
- {
- ParseLogInsert(txtPath.Text);
- Console.WriteLine();
- }
- else if (Device.SelectedIndex == 1) // AOI
- {
- }
- else if (Device.SelectedIndex == 2) // 印刷机
- {
- PrinterData xmlData = ReadWithXmlReader(txtPath.Text);
- Console.WriteLine();
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(this.ParentForm, ex.Message, "警告");
- }
- }
- private void ParseLogInsert(string PathName)
- {
- try
- {
- StreamReader SR = File.OpenText(PathName);
- string restOfStream = SR.ReadToEnd();
- SR.Close();
- SR.Dispose();
- List<Log> logArr = new List<Log>() { };
- string[] lines = restOfStream.Split(new string[] { "\r\n" }, StringSplitOptions.None);
- foreach(var item in lines)
- {
- string[] currItem = item.Split(',');
- string res = "";
- /*List<NgData> data = new List<NgData>();*/
- if (currItem[1] != "PASS")
- {
- res = "NG";
- #region
- /*string[] NgArr = currItem[1].Split(';');
- foreach (var ngItem in NgArr)
- {
- string[] ngItemArr = ngItem.Split('*');
- List<string> ngItemLocal = ngItemArr[1].Split('&').ToList();
- NgData ngData = new NgData {
- Code = ngItemArr[0],
- Local = ngItemLocal
- };
- data.Add(ngData);
- }*/
- #endregion
- }
- else
- {
- res = "PASS";
- }
- Log itemLog = new Log()
- {
- SN = currItem[0],
- Result = res,
- Details = currItem[1]
- };
- logArr.Add(itemLog);
- }
- InsertDb(logArr);
- }
- catch (Exception ex)
- {
- MessageBox.Show(this.ParentForm, ex.Message, "警告");
- }
- }
- private void InsertDb(List<Log> logs)
- {
- try
- {
- int type = Device.SelectedIndex;
- StringBuilder sql = new StringBuilder();
- List<string> param = new List<string>() { };
- foreach (var item in logs)
- {
- ConsoleLog(item);
- param.Add(item.SN);
- if (item.Result == "PASS")
- {
- param.Add(item.Result);
- }
- else
- {
- param.Add(item.Result);
- }
- param.Add(item.Details);
- param.Add(User.UserCode);
- param.Add(User.UserSourceCode);
- param.Add(User.UserLineCode);
- string res = "";
- param.Add(res);
- string[] paramList = param.ToArray();
- dh.CallProcedure("cs_insert_testrejects", ref paramList);
- Console.WriteLine(res);
- param.Clear();
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(this.ParentForm, ex.Message, "警告");
- }
- }
- private void ConsoleLog(Log item)
- {
- try
- {
- string sourceDir = Path.GetDirectoryName(txtPath.Text);
- string newFolderName = "Logs";
- string newFolderPath = Path.Combine(sourceDir, newFolderName);
- if (!Directory.Exists(newFolderPath))
- {
- Directory.CreateDirectory(newFolderPath);
- }
- string newFileName = "Log_" + Path.GetFileName(txtPath.Text);
- string newFilePath = Path.Combine(newFolderPath, newFileName);
- string newContent = $"Time: {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} SN:{item.SN},Result:{item.Result},NgDetails:{item.Details}\r\n";
- File.AppendAllText(newFilePath, newContent);
- }
- catch (Exception ex)
- {
- MessageBox.Show(this.ParentForm, ex.Message, "警告");
- }
- }
- private PrinterData ReadWithXmlReader(string filePath)
- {
- PrinterData data = new PrinterData();
- using (XmlReader reader = XmlReader.Create(filePath))
- {
- while (reader.Read())
- {
- if (reader.NodeType == XmlNodeType.Element)
- {
- switch (reader.Name)
- {
- case "SN":
- data.SN = reader.ReadElementContentAsString();
- break;
- case "Barcode":
- data.Barcode = reader.ReadElementContentAsString();
- break;
- case "Direction":
- data.Direction = reader.ReadElementContentAsString();
- break;
- case "PrintTime":
- data.PrintTime = reader.ReadElementContentAsString();
- break;
- case "PANEL":
- ReadPanelData(reader, data.Panel);
- break;
- case "PROCESS":
- ReadProcessData(reader, data.Process);
- break;
- }
- }
- }
- }
- return data;
- }
- private void ReadPanelData(XmlReader reader, Panel panel)
- {
- while (reader.Read())
- {
- if (reader.NodeType == XmlNodeType.Element)
- {
- switch (reader.Name)
- {
- case "PanelID":
- panel.PanelID = reader.ReadElementContentAsString();
- break;
- case "PanelName":
- panel.PanelName = reader.ReadElementContentAsString();
- break;
- case "PanelFirm":
- panel.PanelFirm = reader.ReadElementContentAsString();
- break;
- case "PanelLength":
- panel.PanelLength = reader.ReadElementContentAsDouble();
- break;
- case "PanelWide":
- panel.PanelWide = reader.ReadElementContentAsDouble();
- break;
- case "PanelThickness":
- panel.PanelThickness = reader.ReadElementContentAsDouble();
- break;
- }
- }
- else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "PANEL")
- {
- break;
- }
- }
- }
- private void ReadProcessData(XmlReader reader, Process process)
- {
- while (reader.Read())
- {
- if (reader.NodeType == XmlNodeType.Element)
- {
- switch (reader.Name)
- {
- case "ProcessName":
- process.ProcessName = reader.ReadElementContentAsString();
- break;
- case "LineNumber":
- process.LineNumber = reader.ReadElementContentAsString();
- break;
- case "StencilNum":
- process.StencilNum = reader.ReadElementContentAsString();
- break;
- }
- }
- else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "PROCESS")
- {
- break;
- }
- }
- }
- private class Log
- {
- public string SN { set; get; }
- public string Result { set; get; }
- /*public List<NgData> Details { set; get; }*/
- public string Details { set; get; }
- }
- private class NgData
- {
- public string Code { set; get; }
- public List<string> Local{ set; get; }
- }
- private class PrinterData
- {
- public string SN { get; set; }
- public string Barcode { get; set; }
- public string Direction { get; set; }
- public string PrintTime { get; set; }
- public Panel Panel { get; set; } = new Panel();
- public Process Process { get; set; } = new Process();
- }
- private class Panel
- {
- public string PanelID { get; set; }
- public string PanelName { get; set; }
- public string PanelFirm { get; set; }
- public double PanelLength { get; set; }
- public double PanelWide { get; set; }
- public double PanelThickness { get; set; }
- }
- private class Process
- {
- public string ProcessName { get; set; }
- public string LineNumber { get; set; }
- public string StencilNum { get; set; }
- }
- }
- }
|