using BarTender; using DevExpress.Utils.OAuth.Provider; using DevExpress.Xpo.Logger; using DevExpress.XtraEditors.Controls; using DevExpress.XtraExport; using DevExpress.XtraPrinting; using NPOI.SS.Formula.Functions; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Net; 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; using static System.Windows.Forms.VisualStyles.VisualStyleElement.Tab; namespace UAS_MES_NEW.Make { public partial class Make_ParseLog : Form { public Make_ParseLog() { InitializeComponent(); } FileSystemWatcher watcher; LogStringBuilder sql = new LogStringBuilder(); DataTable Dbfind; DataTable dt; DataHelper dh; string currFileType = "",equiType = ""; string SN, omakeCode, oMsid, oErrorMessage = ""; List fileList = new List(); Timer formsTimer; string Board, Module; private void Make_ParseLog_Load(object sender, EventArgs e) { dh = SystemInf.dh; fileList.Add("C:\\Users\\MI\\Desktop"); formsTimer = new Timer(); formsTimer.Interval = 10000; formsTimer.Tick += timer1_Tick; //formsTimer.Start(); //formsTimer.Stop(); watcher = new FileSystemWatcher(); watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite; watcher.Created += OnFileCreated; watcher.Changed += OnFileChanged; /*watcher.Deleted += OnFileChanged; watcher.Renamed += OnFileChanged;*/ //设置锁定工单 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 (Device.SelectedIndex == -1) return; switch (Device.SelectedIndex) { case 0: currFileType = "ini"; equiType = "贴片设备"; break; } /*if (string.IsNullOrEmpty(ma_code.Text)) { Device.SelectedIndex = -1; txtPath.Text = ""; MessageBox.Show(this.ParentForm, "请选择工单", "提示"); return; }*/ txtPath.Text = fileList[Device.SelectedIndex]; txtPath.Focus(); txtPath.SelectAll(); //ma_code.Enabled = true; txtPath.Enabled = false; } private void Choose_Click(object sender, EventArgs e) { using (var dialog = new FolderBrowserDialog()) { switch (Device.SelectedIndex) { case 0: dialog.Description = "请选择读取设备文件夹"; currFileType = "ini"; equiType = "贴片设备"; break; } if (dialog.ShowDialog() == DialogResult.OK) { txtPath.Text = dialog.SelectedPath; txtPath.Enabled = false; Device.Enabled = false; watcher.EnableRaisingEvents = false; } } } private void claerBtn_Click(object sender, EventArgs e) { lstFiles.Items.Clear(); } private void allParse_Click(object sender, EventArgs e) { /*if (Device.SelectedIndex == 0) { 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; } string[] txtFiles = Directory.GetFiles(txtPath.Text, $"*.{currFileType}"); if (txtFiles.Length == 0) { LogMessage($"当前{txtPath.Text},没有{equiType}设备输出的{currFileType.ToLower()}文件"); return; } RefreshFileList(); } private void onWatch_Click(object sender, EventArgs e) { /*if (Device.SelectedIndex == 0) { 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; } try { watcher.Path = txtPath.Text; watcher.Filter = $"*.{currFileType}"; if (onWatch.Text == "开启解析") { watcher.EnableRaisingEvents = true; onWatch.Text = "关闭解析"; } else { watcher.EnableRaisingEvents = false; onWatch.Text = "开启解析"; } } 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() { if (lstFiles.InvokeRequired) { lstFiles.Invoke(new Action(RefreshFileList)); lstFiles.BeginInvoke(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) { return; } else { foreach (string file in txtFiles) { if (!File.Exists(file)) { LogMessage($"文件不存在: {file}"); } string outFileMsg = CheckFileAccess(file); if (outFileMsg != "OK") { LogMessage(outFileMsg); continue; } if(File.ReadAllText(file).Length == 0) { continue; } if (Device.SelectedIndex == 0) { ParseLogInsert(file); } } } } catch (Exception ex) { LogMessage($"Error: 解析文件列表失败: {ex.Message}"); } } private void ParseLogInsert(string PathName) { try { StreamReader SR = File.OpenText(PathName); string restOfStream = SR.ReadToEnd(); SR.Close(); SR.Dispose(); string[] lines = restOfStream.Split(new string[] { "\r\n" }, StringSplitOptions.None); for (int i = 0; i < lines.Length; i++) { if (string.IsNullOrEmpty(lines[i])) continue; if (equiType == "贴片设备") { if (lines[i].Trim() == "[General.Count]") { if(string.IsNullOrEmpty(lines[i + 1])) { LogMessage($"文件: {PathName},更新[General.Count]下无Board数据NG"); } if(lines[i + 1].Split('=')[0].ToUpper() != "BOARD") { LogMessage($"文件: {PathName},更新[General.Count]下无Board数据NG"); } Board = lines[i + 1].Split('=')[1]; break; } } } if (string.IsNullOrEmpty(Board)) { LogMessage($"文件: {PathName},更新[General.Count]下Board数据NG: 未找到"); return; } else { LogMessage($"文件: {PathName},[General.Count]下Board已更新: {Board}"); } if (ConsoleLog(restOfStream, PathName)) { File.WriteAllText(PathName, string.Empty); File.Delete(PathName); } } catch (Exception ex) { LogMessage($"Error: 文件 {PathName}, {ex.Message}"); } } private void timer1_Tick(object sender, EventArgs e) { try { } catch (Exception ex) { LogMessage($"定时器异常: {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) { bool isNg; if (message.ToUpper().Contains("NG")) { isNg = true; } else if (message.ToUpper().Contains("ERROR")) { isNg = true; } else { isNg = false; } if (isNg) { if (lstOk.InvokeRequired) { lstOk.Invoke(new Action(LogMessage), message); return; } lstOk.Items.Add($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {message}"); lstOk.TopIndex = lstFiles.Items.Count - 1; } else { if (lstFiles.InvokeRequired) { lstFiles.Invoke(new Action(LogMessage), message); return; } lstFiles.Items.Add($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {message}"); lstFiles.TopIndex = lstFiles.Items.Count - 1; lstFiles.DrawMode = DrawMode.OwnerDrawFixed; } } private void lstFiles_DrawItem_1(object sender, DrawItemEventArgs e) { e.DrawBackground(); Brush mybsh = Brushes.Black; if (lstFiles.Items[e.Index].ToString().Contains("NG")) { mybsh = Brushes.Red; } else if (lstFiles.Items[e.Index].ToString().Contains("ERROR")) { mybsh = Brushes.Red; } e.DrawFocusRectangle(); e.Graphics.DrawString(lstFiles.Items[e.Index].ToString(), e.Font, mybsh, e.Bounds, StringFormat.GenericDefault); } public string CheckFileAccess(string filePath) { try { using (FileStream fileStream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.None)) { return "OK"; } } catch (IOException ex) { return "Error文件被占用: " + ex.Message; } catch (UnauthorizedAccessException ex) { return "Error权限不足,无法访问文件: " + ex.Message; } catch (Exception ex) { return ex.Message; } } private class Log { public string SN { set; get; } public string Result { set; get; } } } }