| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688 |
- using BarTender;
- using DevExpress.Xpo.Logger;
- 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.Security;
- using System.Security.AccessControl;
- using System.Security.Principal;
- using System.Text;
- using System.Threading.Tasks;
- using System.Web.Services.Description;
- using System.Web.UI.WebControls;
- using System.Windows.Forms;
- using System.Xml;
- using UAS_MES_NEW.CustomControl.ButtonUtil;
- using UAS_MES_NEW.CustomControl.TextBoxWithIcon;
- 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();
- }
- LogStringBuilder sql = new LogStringBuilder();
- DataTable Dbfind;
- DataTable dt;
- DataHelper dh;
- string currFileType = "";
- string SN, omakeCode, oMsid, oErrorMessage = "";
- string equiType = "";
- private void Make_ParseLog_Load(object sender, EventArgs e)
- {
- dh = SystemInf.dh;
- //设置锁定工单
- LockMakeCode.GetMakeCodeCtl(ma_code);
- ma_code.SetLockCheckBox(LockMakeCode);
- //工单号放大镜配置
- ma_code.TableName = "make left join product on ma_prodcode=pr_code";
- ma_code.SelectField = "ma_code # 工单号,pr_code # 产品编号,pr_detail # 产品名称";
- ma_code.FormName = Name;
- ma_code.SetValueField = new string[] { "ma_code", "pr_code", "pr_detail" };
- ma_code.Condition = "ma_statuscode='STARTED'";
- ma_code.DbChange += Ma_code_DbChange;
- }
- private void Ma_code_DbChange(object sender, EventArgs e)
- {
- Dbfind = ma_code.ReturnData;
- BaseUtil.SetFormValue(this.Controls, Dbfind);
- //获取工单的其他信息
- sql.Clear();
- sql.Append("select ma_code,nvl(mcd_okqty,0) mcd_okqty,ma_prodcode as pr_code ,pr_detail,");
- sql.Append("pr_spec,ma_qty - nvl(mcd_inqty, 0) mcd_remainqty from make left join makecraftdetail on ");
- 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 + "'");
- dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
- if (dt.Rows.Count > 0)
- {
- BaseUtil.SetFormValue(this.Controls, dt);
- }
- }
- private void Device_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (string.IsNullOrEmpty(ma_code.Text))
- {
- Device.SelectedIndex = -1;
- MessageBox.Show(this.ParentForm, "请选择工单", "提示");
- return;
- }
- txtPath.Focus();
- txtPath.SelectAll();
- }
- private void allParse_Click(object sender, EventArgs e)
- {
- if (string.IsNullOrEmpty(ma_code.Text))
- {
- MessageBox.Show(this.ParentForm, "请选择工单", "提示");
- return;
- }
- if (Device.SelectedIndex == -1)
- {
- Device.Focus();
- Device.SelectAll();
- MessageBox.Show(this.ParentForm, "请选择设备", "提示");
- return;
- }
- if (String.IsNullOrEmpty(txtPath.Text))
- {
- txtPath.Focus();
- txtPath.SelectAll();
- return;
- }
- RefreshFileList();
- }
- private void Choose_Click(object sender, EventArgs e)
- {
- if (string.IsNullOrEmpty(ma_code.Text))
- {
- MessageBox.Show(this.ParentForm, "请选择工单", "提示");
- return;
- }
- if (Device.SelectedIndex == -1)
- {
- Device.Focus();
- Device.SelectAll();
- MessageBox.Show(this.ParentForm, "请选择设备", "提示");
- return;
- }
- if (!String.IsNullOrEmpty(txtPath.Text))
- {
- txtPath.Focus();
- txtPath.SelectAll();
- }
- try
- {
- FolderBrowserDialog dialog = new FolderBrowserDialog();
- switch (Device.SelectedIndex)
- {
- case 0:
- dialog.Description = "请选择读取SPI设备文件夹";
- currFileType = "txt";
- equiType = "1";
- break;
- case 1:
- dialog.Description = "请选择读取AOI设备文本文件";
- currFileType = "txt";
- equiType = "2";
- break;
- case 2:
- dialog.Description = "请选择读取印刷机设备的文本文件夹";
- currFileType = "xml";
- equiType = "3";
- break;
- case 3:
- dialog.Description = "请选择读取贴片机的文本文件夹";
- currFileType = "xml";
- equiType = "4";
- break;
- }
- if (dialog.ShowDialog() == DialogResult.OK)
- {
- txtPath.Text = dialog.SelectedPath;
- txtPath.Enabled = false;
- Device.Enabled = false;
- var watcher = new FileSystemWatcher(txtPath.Text);
- watcher.Filter = $"*.{currFileType}";
- watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;
- watcher.Created += OnFileCreated;
- /*watcher.Changed += OnFileChanged;
- watcher.Deleted += OnFileChanged;
- watcher.Renamed += OnFileChanged;*/
- watcher.EnableRaisingEvents = true;
- }
- #region
- /*OpenFileDialog dialog = new OpenFileDialog();
- dialog.Multiselect = true;//该值确定是否可以选择多个文件
- dialog.Filter = "文本文件 (*.txt)|*.txt|All files (*.*)|*.*";
- switch (Device.SelectedIndex)
- {
- case 0:
- dialog.Title = "请选择读取SPI设备文件夹";
- dialog.Filter = "All files (*.*)|*.*";
- break;
- case 1:
- dialog.Title = "请选择读取AOI设备文本文件";
- dialog.Filter = "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();
- }*/
- #endregion
- }
- catch (Exception ex)
- {
- MessageBox.Show(this.ParentForm, ex.Message, "警告");
- }
- }
- private void OnFileCreated(object sender, FileSystemEventArgs e)
- {
- RefreshFileList();
- }
- private void OnFileChanged(object sender, FileSystemEventArgs e)
- {
- if (e.ChangeType == WatcherChangeTypes.Changed)
- {
- RefreshFileList();
- }
- }
- private void RefreshFileList()
- {
- // 在UI线程上执行更新
- if (lstFiles.InvokeRequired)
- {
- lstFiles.Invoke(new Action(RefreshFileList));
- return;
- }
- try
- {
- if (!Directory.Exists(txtPath.Text))
- {
- LogMessage($"目录不存在: {txtPath.Text}");
- return;
- }
- string[] txtFiles = Directory.GetFiles(txtPath.Text, $"*.{currFileType}");
- if (txtFiles.Length == 0)
- {
- LogMessage($"当前没有{currFileType.ToUpper()}文件");
- return;
- }
- else
- {
- foreach (string file in txtFiles)
- {
- LogMessage(file);
- if (Device.SelectedIndex == 0) // SPI
- {
- ParseLogInsert(file);
- Console.WriteLine();
- }
- else if (Device.SelectedIndex == 1) // AOI
- {
- ParseLogInsert(file);
- Console.WriteLine();
- }
- else if (Device.SelectedIndex == 2) // 印刷机
- {
- PrinterData xmlData = ReadWithXmlReader(file);
- Console.WriteLine();
- }
- else if (Device.SelectedIndex == 3) // 贴片机
- {
- if (!File.Exists(file))
- {
- LogMessage($"文件不存在: {file}");
- }
- string xmlContent = File.ReadAllText(file);
- var xmlList = ParseXml(xmlContent);
- if (ConsoleLog(xmlContent, file))
- {
- File.Delete(file);
- }
- List<string> param = new List<string>() { };
- string PS_CODE = "";
- LogicHandler.GetSerialNumByCaller("Make!ParseLog", out PS_CODE);
- List<string> MachineName = new List<string>();
- List<string> Part = new List<string>();
- List<string> Station = new List<string>();
- List<string> ReelNo = new List<string>();
- List<string> letNo = new List<string>();
- string Part_ = "";
- int letNoNum = 0;
- foreach (var item in xmlList)
- {
- MachineName.Add(item.MachineName);
- Part_ = item.Part.Split('.')[0];
- Station.Add(item.Station);
- ReelNo.Add(item.ReelNo);
- letNo.Add((++letNoNum).ToString());
- }
- string ps_id = "";
- string lineId = dh.getFieldDataByCondition("smtlocationinto", "SLI_LANE", "SLI_MCNO='" + MachineName[0] + "'").ToString();
- if (!dh.CheckExist("productsmt", "ps_prodcode='" + Part_ + "'"))
- {
- ps_id = dh.GetSEQ("productsmt_seq");
- dh.ExecuteSql("insert into productsmt(ps_id,ps_prodcode,ps_status,ps_statuscode,PS_LINECODE,PS_CODE)" +
- "values('" + ps_id + "','" + Part_ + "','在录入','ENTERING','"+ lineId + "','"+ PS_CODE + "')", "insert");
- }
- else
- {
- ps_id = dh.getFieldDataByCondition("productsmt", "ps_id", "ps_prodcode='"+Part_+"'").ToString();
- }
- if (dh.getRowCount("productsmtlocation", "PSL_PSID=" + ps_id) > 0)
- {
- dh.ExecuteSql("delete from productsmtlocation where PSL_PSID" + ps_id, "delete");
- }
- sql.Clear();
- sql.Append("insert into productsmtlocation(PSL_ID, PSL_PSID, PSL_DETNO, PSL_PRODCODE, PSL_REPCODE, PSL_LOCATION, PSL_BASEQTY, PSL_TABLE)" +
- "select productsmtlocation_seq.nextval," + ps_id + ",:letArr, :ReelNo,:ReelNo,:Station,BD_BASEQTY,'' from bom@ERP " +
- "left join bomdetail@ERP on bo_id=bd_bomid where bo_mothercode='" + Part_ + "' and BD_SONCODE=:ReelNo");
- dh.BatchInsert(sql.GetString(), new string[] {"letArr", "ReelNo", "ReelNo", "Station", "ReelNo" },
- letNo.ToArray(),ReelNo.ToArray(), ReelNo.ToArray(), Station.ToArray(), ReelNo.ToArray());
- Console.WriteLine();
- }
- }
- LogMessage($"共找到{txtFiles.Length}个{currFileType.ToUpper()}文件");
- }
- }
- catch (Exception ex)
- {
- LogMessage($"刷新文件列表时出错: {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);
- LogMessage($"文件夹{PathName},共{lines.Length}条记录");
- foreach (var item in lines)
- {
- if (string.IsNullOrEmpty(item)) continue;
- 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 = equiType == "1" ? currItem[1] : currItem[3]
- };
- logArr.Add(itemLog);
- LogMessage($"已解析: {currItem[0]}");
- }
- InsertDb(logArr, PathName);
- File.WriteAllText(PathName, string.Empty);
- if (ConsoleLog(restOfStream, PathName))
- {
- File.Delete(PathName);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(this.ParentForm, ex.Message, "警告");
- }
- }
- private void InsertDb(List<Log> logs, string PathName)
- {
- try
- {
- int type = Device.SelectedIndex;
- StringBuilder sql = new StringBuilder();
- List<string> param = new List<string>() { };
- foreach (var item in logs)
- {
- param.Add(item.SN);
- param.Add(ma_code.Text);
- if (item.Result == "PASS")
- {
- param.Add(item.Result);
- }
- else
- {
- param.Add(item.Result);
- }
- param.Add(equiType);
- 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);
- param.Clear();
- if (LogicHandler.CheckStepSNAndMacode(ma_code.Text, User.UserSourceCode, item.SN, User.UserCode, out omakeCode, out oMsid, out oErrorMessage))
- {
- if (LogicHandler.SetStepResult(omakeCode, User.UserSourceCode, item.SN, "日志解析", "OK", User.UserCode, out oErrorMessage))
- {
- if (oErrorMessage.Contains("AFTERSUCCESS")) LogMessage(">>" + oErrorMessage + "\n");
- sql.Clear();
- sql.Append("select ma_code,nvl(mcd_okqty,0) mcd_okqty,ma_prodcode,pr_detail,");
- sql.Append("ma_qty - nvl(mcd_okqty, 0) remain_qty from make left join makecraftdetail on ");
- sql.Append("mcd_maid=ma_id left join product on pr_code=ma_prodcode where ma_code='" + omakeCode + "' and mcd_stepcode='" + User.CurrentStepCode + "'");
- dt = (DataTable)dh.ExecuteSql(sql.ToString(), "select");
- BaseUtil.SetFormValue(Controls, dt);
- //记录日志
- LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, ma_code.Text, User.UserLineCode, User.UserSourceCode, "日志解析", "日志解析成功", item.SN, "");
- LogMessage("<<解析成功\n");
- }
- else
- {
- LogMessage(">>" + oErrorMessage + "\n");
- }
- }
- else
- {
- LogMessage(">>" + oErrorMessage + "\n");
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(this.ParentForm, ex.Message, "警告");
- }
- }
- private bool ConsoleLog(string Content, string PathName)
- {
- try
- {
- string sourceDir = Path.GetDirectoryName(PathName);
- string newFolderName = "Logs";
- string newFolderPath = Path.Combine(sourceDir, newFolderName);
- if (!Directory.Exists(newFolderPath))
- {
- Directory.CreateDirectory(newFolderPath);
- }
- string newFileName = "Log_" + Path.GetFileName(PathName);
- string newFilePath = Path.Combine(newFolderPath, newFileName);
- File.AppendAllText(newFilePath, Content + Environment.NewLine);
- return true;
- }
- catch (Exception ex)
- {
- MessageBox.Show(this.ParentForm, ex.Message, "警告");
- return false;
- }
- }
- private void LogMessage(string message)
- {
- if (lstFiles.InvokeRequired)
- {
- lstFiles.Invoke(new Action<string>(LogMessage), message);
- return;
- }
- lstFiles.Items.Add($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {message}");
- lstFiles.TopIndex = lstFiles.Items.Count - 1;
- }
- 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;
- }
- }
- }
- public List<MachineList> ParseXml(string xmlContent)
- {
- List<MachineList> result = new List<MachineList>();
- try
- {
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.LoadXml(xmlContent);
- XmlNode machineNode = xmlDoc.SelectSingleNode("//TotalProductionInfo");
- string machineName = machineNode?.Attributes["MachineName"]?.Value ?? "Unknown";
- string part = machineNode?.Attributes["ProductionProgramName"]?.Value ?? "Unknown";
- XmlNodeList feederNodes = xmlDoc.SelectNodes("//FeederProductionManageInfo");
- foreach (XmlNode feederNode in feederNodes)
- {
- MachineList machineList = new MachineList();
- string feederBankPosition = feederNode.Attributes["FeederBankPosition"]?.Value ?? "Unknown";
- string holeNo = feederNode.Attributes["HoleNo"]?.Value ?? "Unknown";
- string componentName = feederNode.Attributes["ComponentName"]?.Value ?? "Unknown";
- machineList.MachineName = machineName;
- machineList.Part = part;
- machineList.ReelNo = componentName;
- machineList.Station = $"{machineName}-{feederBankPosition[0]}-{holeNo}";
- result.Add(machineList);
- }
- }
- catch (Exception ex)
- {
- LogMessage($"Error parsing XML: {ex.Message}");
- }
- return result;
- }
- private class Log
- {
- public string SN { set; get; }
- public string Result { set; get; }
- /*public List<NgData> Details { set; get; }*/
- public string Details { set; get; }
- }
- public class MachineList
- {
- public string MachineName { set; get; }
- public string Part { set; get; }
- public string Station { set; get; }
- public string ReelNo { 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; }
- }
- private void claerBtn_Click(object sender, EventArgs e)
- {
- lstFiles.Items.Clear();
- }
- }
- }
|