using JinYuan.ControlCenters; using JinYuan.Helper; using JinYuan.MES.Models; using JinYuan.Models; using JinYuan.VirtualDataLibrary; using Language; using LargeSquareOne.ViewModel; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Threading.Tasks; using System.Windows.Forms; namespace LargeSquareOne { public partial class FrmDataMonitor : MultiLanguageForm { private System.Threading.Timer m_RefreshTimer; /// /// 组盘数据显示 /// private BindingList listTA = new BindingList(); /// /// 组盘抽检数据显示 /// private BindingList listTC = new BindingList(); /// /// 叠盘数据绑定委托 /// /// private BindingList listTB = new BindingList(); /// /// 底托卡板绑定数据显示 /// private BindingList listTD = new BindingList(); /// /// 底托卡板绑定数据显示 /// private BindingList listTE = new BindingList(); public FrmDataMonitor() { InitializeComponent(); this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.DoubleBuffer, true); this.SetStyle(ControlStyles.ResizeRedraw, true); this.SetStyle(ControlStyles.Selectable, true); this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); this.SetStyle(ControlStyles.UserPaint, true); this.dgvFeedingData.AutoGenerateColumns = false; this.dgvFeedingData.DataSource = listTA; this.dgvFeedingData.DataError += delegate (object sender, DataGridViewDataErrorEventArgs e) { }; this.dgvFeedingData.DoubleBufferedDataGirdView(true); this.dgvBlankingData.AutoGenerateColumns = false; this.dgvBlankingData.DataSource = listTB; this.dgvBlankingData.DataError += delegate (object sender, DataGridViewDataErrorEventArgs e) { }; this.dgvBlankingData.DoubleBufferedDataGirdView(true); this.dgvPrintContainerData.AutoGenerateColumns = false; this.dgvPrintContainerData.DataSource = listTD; this.dgvPrintContainerData.DataError += delegate (object sender, DataGridViewDataErrorEventArgs e) { }; this.dgvPrintContainerData.DoubleBufferedDataGirdView(true); this.dgvPrintPalletData.AutoGenerateColumns = false; this.dgvPrintPalletData.DataSource = listTE; this.dgvPrintPalletData.DataError += delegate (object sender, DataGridViewDataErrorEventArgs e) { }; this.dgvPrintPalletData.DoubleBufferedDataGirdView(true); this.lVConfigRange.DoubleBufferedListView(true); this.FormClosing += (sender, e) => { }; } private void FrmDataMonitor_Load(object sender, EventArgs e) { dgvFeedingData.ClearSelection(); dgvBlankingData.ClearSelection(); dgvPrintContainerData.ClearSelection(); dgvPrintPalletData.ClearSelection(); //表头动态渲染 this.InitFrmDataMonitor(); } private string CurrentTime { get { return DateTime.Now.ToString("HH:mm:ss"); } } /// /// 刷新参数 /// public void GetLocalParaData() { try { if (CommonMethods.paramlists != null) { System.Windows.Forms.ListViewItem listItem = null; lVConfigRange.Items.Clear(); int j = 0; foreach (var ProductPara in CommonMethods.paramlists) //遍历list集合 { string strRemark = ProductPara.Remark; string strName = ProductPara.ParaName; listItem = new System.Windows.Forms.ListViewItem(CommonMethods.paramlists[j].Remark); listItem.SubItems.Add("0"); listItem.SubItems.Add("0"); listItem.SubItems.Add("0"); lVConfigRange.Items.Add(listItem); lVConfigRange.Items[j].SubItems[1].Text = ProductPara.ParaValueMin; lVConfigRange.Items[j].SubItems[2].Text = ProductPara.ParaValueMax; lVConfigRange.Items[j].SubItems[3].Text = ProductPara.Uint; switch (strName) { //case "OcvIr": //OCV内阻 // CommonMethods.parRange.OcvIrMax = Convert.ToDecimal(ProductPara.ParaValueMax == "" ? "0" : ProductPara.ParaValueMax); // CommonMethods.parRange.OcvIrMin = Convert.ToDecimal(ProductPara.ParaValueMin == "" ? "0" : ProductPara.ParaValueMin); // break; //case "OcvVoltage": //OCV电压 // CommonMethods.parRange.OcvVoltageMax = Convert.ToDecimal(ProductPara.ParaValueMax == "" ? "0" : ProductPara.ParaValueMax); // CommonMethods.parRange.OcvVoltageMin = Convert.ToDecimal(ProductPara.ParaValueMin == "" ? "0" : ProductPara.ParaValueMin); // break; //case "OcvTime": //OCV静置时间 // CommonMethods.parRange.OcvTimeMax = Convert.ToDecimal(ProductPara.ParaValueMax == "" ? "0" : ProductPara.ParaValueMax); // CommonMethods.parRange.OcvTimeMin = Convert.ToDecimal(ProductPara.ParaValueMin == "" ? "0" : ProductPara.ParaValueMin); // break; //case "CoefficientA"://常数A // CommonMethods.parRange.CoefficientA = Convert.ToDecimal(ProductPara.ParaValueMin); // break; //case "ExponentB"://常数B // CommonMethods.parRange.ExponentB = Convert.ToDecimal(ProductPara.ParaValueMin); // break; default: break; } j++; } } } catch (Exception ex) { LogHelper.Instance.WriteEX("获取参数失败".Translated(), ex); } } /// /// 加载MES参数 /// public async Task<(bool Success, string msg)> GetMesParaDataAsync() { bool Success = false; // 操作结果 string Message = ""; // 消息 try { bool isChangeParam = false; var nowDate = DateTime.Now; string filePath = $@"{CommonMethods.strMesLogspath}\参数设定值请求\变更记录\{nowDate.Year}\{nowDate.Month}"; string fileName = $@"{nowDate.Day}.csv"; string fileTitle = "时间,机台,MES账户,更改参数名称,更改前参数值,更改后参数值"; string fileContent = string.Empty; DataTable dt = new DataTable(); dt.Columns.Add("MES账户"); dt.Columns.Add("更改参数名称"); dt.Columns.Add("更改前参数值"); dt.Columns.Add("更改后参数值"); var (resultData, Mesage) = await CommonMethods.hbgMes.GetParamSetRequestAsync( CommonMethods.mesConfig.DeviceParamChange, CommonMethods.mesConfig.equipNum, CommonMethods.mesConfig.siteCode, CommonMethods.mesConfig.lineCode, CommonMethods.mesConfig.MaterialCode, CommonMethods.mesConfig.mesUserName); //var resultData = JsonConvert.DeserializeObject(Data); if (!resultData.success) { // 获取参数未成功 Message = "获取MES参数不成功!"; return (Success, Message); } List NewParamlists = CommonMethods.paramlists; for (int i = 0; i < resultData.total; i++) { List rows = resultData.rows; for (int j = 0; j < rows.Count; j++) { var tagList = rows[j].tagList; for (int k = 0; k < NewParamlists.Count; k++) { for (int n = 0; n < rows[j].tagList.Count; n++) { if (NewParamlists[k].ParaName == tagList[n].tagCode) { string strMin = "0"; if (String.IsNullOrEmpty(tagList[n].minValue.Trim()) || tagList[n].minValue.Trim() == "null") strMin = "0"; else strMin = tagList[n].minValue.Trim(); if (NewParamlists[k].ParaValueMin != strMin) { fileContent += $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss")},{CommonMethods.mesConfig.equipNum}," + $"{CommonMethods.mesConfig.mesUserName},{tagList[n].tagDescription}最小值,{NewParamlists[k].ParaValueMin},{strMin}\n"; dt.Rows.Add(CommonMethods.mesConfig.mesUserName, $"{tagList[n].tagDescription}最小值", NewParamlists[k].ParaValueMin, strMin); NewParamlists[k].ParaValueMin = strMin; isChangeParam = true; } string strMax = "0"; if (String.IsNullOrEmpty(tagList[n].maxValue.Trim()) || tagList[n].maxValue.Trim() == "null") strMax = "0"; else strMax = tagList[n].maxValue.Trim(); if (NewParamlists[k].ParaValueMax != tagList[n].maxValue) { fileContent += $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss")},{CommonMethods.mesConfig.equipNum}," + $"{CommonMethods.mesConfig.mesUserName},{tagList[n].tagDescription}最大值,{NewParamlists[k].ParaValueMax},{strMax}\n"; dt.Rows.Add(CommonMethods.mesConfig.mesUserName, $"{tagList[n].tagDescription}最大值", NewParamlists[k].ParaValueMax, strMax); NewParamlists[k].ParaValueMax = strMax; isChangeParam = true; } } } } if (isChangeParam) { CustomMessageBox customMessageBox = new CustomMessageBox(dt, $"当前MES下发参数与设备本地参数不一致,请确认是否使用MES下发参数"); if (customMessageBox.ShowDialog() == DialogResult.OK) { // 写csv文件 fileContent = fileContent.Substring(0, fileContent.Length - 1); CSVHelper.WriterCSV(filePath, fileName, fileTitle, fileContent); CommonMethods.paramlists = NewParamlists; int res = CommonMethods.db.Delete(it => it.ModelName == CommonMethods.paramlists[0].ModelName); if (res > 0) { CommonMethods.db.AddReturnBool(CommonMethods.paramlists); } Message = "变更成功并保存"; Success = true; } else { Message = "取消变更,采用本地参数配置"; } LogHelper.Instance.WriteLog(Message); } } } Success = true; // 操作成功 Message = "参数获取成功"; } catch (Exception ex) { Message = "获取参数失败:" + ex.Message; LogHelper.Instance.WriteEX("获取参数失败", ex); } return (Success, Message); } #region 显示包装进站数据 /// /// 显示组盘数据 /// /// /// private readonly ConcurrentQueue> _dataQueue = new ConcurrentQueue>(); private volatile bool _isUpdating = false; private const int MAX_ROWS = 500; public void ShowFeedingData(List list) { if (list == null || list.Count == 0) return; try { _dataQueue.Enqueue(list); if (!_isUpdating) { ProcessDataQueue(); } } catch (Exception ex) { LogHelper.Instance.WriteEX(ex); } } private void ProcessDataQueue() { if (_isUpdating) return; try { _isUpdating = true; while (_dataQueue.TryDequeue(out var data)) { UpdateGrid(data); } } finally { _isUpdating = false; } } private void UpdateGrid(List data) { if (!dgvFeedingData.IsHandleCreated) return; try { dgvFeedingData.BeginInvoke(new Action(() => { try { dgvFeedingData.SuspendLayout(); // 检查是否需要清理数据 if (listTA.Count > MAX_ROWS) { listTA.Clear(); GC.Collect(0, GCCollectionMode.Optimized); } // 添加新数据 foreach (var item in data) { listTA.Add(item); } dgvFeedingData.DataSource = null; dgvFeedingData.DataSource = listTA; // 更新显示 int lastIndex = dgvFeedingData.Rows.Count - 1; if (lastIndex >= 0) { // 滚动到最新行 dgvFeedingData.FirstDisplayedScrollingRowIndex = lastIndex; dgvFeedingData.CurrentCell = dgvFeedingData.Rows[lastIndex].Cells[0]; } } finally { dgvFeedingData.ResumeLayout(); } })); } catch (Exception ex) { LogHelper.Instance.WriteEX(ex); } } #endregion #region 显示包装出站数据 /// /// 显示包装出站 /// /// public void ShowBlankingData(List list) { try { if (this.dgvBlankingData.InvokeRequired) { this.dgvBlankingData.Invoke(new Action>(ShowBlankingData), list); } else { if (list != null && list.Count > 0) { foreach (BGearEntity m in list) { listTB.Add(m); } if (listTB.Count >= 500) { listTB.Clear(); GC.Collect(); } } // 设置最新行为活动行 (当前行) int index = this.dgvBlankingData.Rows.Count - 1; this.dgvBlankingData.CurrentCell = this.dgvBlankingData.Rows[index].Cells[0]; } } catch (Exception ex) { //ShowLog("显示上料出现" + ex.Message, 1); } } #endregion #region 显示栈板打印数据 /// /// 显示栈板打印数据 /// /// public void ShowPrintPalletData(List list) { try { if (this.dgvPrintPalletData.InvokeRequired) { this.dgvPrintPalletData.Invoke(new Action>(ShowPrintPalletData), list); } else { if (list != null && list.Count > 0) { foreach (EGearEntity m in list) { listTE.Add(m); } if (listTE.Count >= 500) { listTE.Clear(); GC.Collect(); } } // 设置最新行为活动行 (当前行) int index = this.dgvPrintPalletData.Rows.Count - 1; this.dgvPrintPalletData.CurrentCell = this.dgvPrintPalletData.Rows[index].Cells[0]; } } catch (Exception ex) { //ShowLog("显示上料出现" + ex.Message, 1); } } #endregion #region 显示箱唛打印数据 /// /// 显示箱唛打印数据 /// /// public void ShowPrintContainerData(List list) { try { if (this.dgvPrintContainerData.InvokeRequired) { this.dgvPrintContainerData.Invoke(new Action>(ShowPrintContainerData), list); } else { if (list != null && list.Count > 0) { foreach (DGearEntity m in list) { listTD.Add(m); } if (listTD.Count >= 500) { listTD.Clear(); GC.Collect(); } } // 设置最新行为活动行 (当前行) int index = this.dgvPrintContainerData.Rows.Count - 1; this.dgvPrintContainerData.CurrentCell = this.dgvPrintContainerData.Rows[index].Cells[0]; //for (int i = 0; i < list.Count; i++) //{ // this.dgvBlankingData.Rows[index - i].Cells[0].Value = index + 1 - i; //} } } catch (Exception ex) { //ShowLog("显示上料出现" + ex.Message, 1); } } #endregion #region 包装进站 private void dgvFeedingData_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { //显示序列号 DataGridViewHelper.DgvRowPostPaint(this.dgvFeedingData, e); } private void dgvFeedingData_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) { //指定列字体颜色 DataGridViewHelper.DgvRowPrePaint(this.dgvFeedingData, "InResult", sender, e); } #endregion #region 包装出站工位 private void dgvBlankingData_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { //显示序列号 DataGridViewHelper.DgvRowPostPaint(this.dgvBlankingData, e); } private void dgvBlankingData_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) { //指定列字体颜色 DataGridViewHelper.DgvRowPrePaint(this.dgvBlankingData, "OutResult", sender, e); } #endregion #region 箱唛打印工位 private void dgvPrintContainerData_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { //显示序列号 DataGridViewHelper.DgvRowPostPaint(this.dgvPrintContainerData, e); } private void dgvPrintContainerData_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) { //指定列字体颜色 DataGridViewHelper.DgvRowPrePaint(this.dgvPrintContainerData, "Result", sender, e); } #endregion #region 栈板打印工位 private void dgvPrintPalletData_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { //显示序列号 DataGridViewHelper.DgvRowPostPaint(this.dgvPrintPalletData, e); } private void dgvPrintPalletData_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) { //指定列字体颜色 DataGridViewHelper.DgvRowPrePaint(this.dgvPrintPalletData, "Result", sender, e); } #endregion #region 减少闪烁 protected override CreateParams CreateParams { get { CreateParams parss = base.CreateParams; parss.ExStyle |= 0x02000000; return parss; } } #endregion #region StringFormat sf = new StringFormat { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Center };//字体水平位置居左,垂直居中 private void lVConfigRange_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e) { Brush CustomizeBrushColor = new SolidBrush(Color.FromArgb(21, 77, 147));//自定义RGB e.Graphics.FillRectangle(CustomizeBrushColor, e.Bounds); //设置背景颜色 Font font = new Font("微软雅黑", 12, FontStyle.Bold); //设置字体大小 e.Graphics.DrawString(e.Header.Text, font, Brushes.White, e.Bounds, sf); //设置字体颜色 } private void lVConfigRange_DrawItem(object sender, DrawListViewItemEventArgs e) { e.DrawDefault = true; //采用系统默认方式绘制项 } private void lVConfigRange_DrawSubItem(object sender, DrawListViewSubItemEventArgs e) { e.DrawDefault = true; //采用系统默认方式绘制项 } #endregion #region ListView日志记录 //public void AddDataMonitorLog(int level, string log) //{ // if (level > 2) // { // level = 2; // } // if (level < 0) // { // level = 0; // } // if (this.lst_Info.InvokeRequired) // { // this.lst_Info.Invoke(new Action(AddDataMonitorLog), level, log); // } // else // { // if (this.lst_Info.Items.Count > 1000) // { // this.lst_Info.Items.Clear(); // } // System.Windows.Forms.ListViewItem item = new System.Windows.Forms.ListViewItem(" " + CurrentTime, level); // item.SubItems.Add(log); // this.lst_Info.Items.Add(item); // //让最新的日志显示在最下面 // this.lst_Info.Items[this.lst_Info.Items.Count - 1].EnsureVisible(); // } //} #endregion #region RichTextBox日志记录 public void AddDataMonitorLog(int type, string info) { if (this.rtb_Info.InvokeRequired) { this.rtb_Info.Invoke(new Action(AddDataMonitorLog), type, info); } else { if (rtb_Info.Lines.Length > 200) //每当显示的通知数超过200行时,清除前面的150行 { string[] strlines = new string[rtb_Info.Lines.Length - 150]; Array.ConstrainedCopy(rtb_Info.Lines, 50, strlines, 0, strlines.Length); rtb_Info.Lines = strlines; GC.Collect(); } rtb_Info.SelectionStart = rtb_Info.Text.Length; rtb_Info.SelectionLength = info.Length; switch (type) { case 0: rtb_Info.SelectionColor = Color.White; break; case 1: rtb_Info.SelectionColor = Color.DarkOrange; break; case 2: rtb_Info.SelectionColor = Color.Red; break; } rtb_Info.AppendText(string.Format("时间:{0}-{1}\r\n", DateTime.Now.ToString("HH:mm:ss.fff"), info)); rtb_Info.SelectionStart = rtb_Info.Text.Length; rtb_Info.SelectionLength = info.Length; rtb_Info.Focus(); } } #endregion #region ListBox日志记录 //public void AddDataMonitorLog(int level, string log) //{ // Color color = Color.White; // if (level > 2) // { // color= Color.Red; // level = 2; // } // if (level < 0) // { // color = Color.White; // level = 0; // } // if (this.lis_Info.InvokeRequired) // { // this.lis_Info.Invoke(new Action(AddDataMonitorLog), level, log); // } // else // { // this.lis_Info.AddLog(log, color); // this.lis_Info.SelectedIndex = this.lis_Info.Items.Count - 1; // this.lis_Info.SelectedIndex = -1; // //System.Windows.Forms.ListViewItem item = new System.Windows.Forms.ListViewItem(" " + CurrentTime, level); // //item.SubItems.Add(log); // //this.rtb_Info.Items.Add(item); // ////让最新的日志显示在最下面 // //this.rtb_Info.Items[this.lst_Info.Items.Count - 1].EnsureVisible(); // } //} #endregion //private async void btn_printpallet_Click(object sender, EventArgs e) //{ // string palletcode = this.txt_palletcode.Text.Trim(); // // 输入验证 // if (string.IsNullOrWhiteSpace(palletcode)) // { // MessageBox.Show("请输入有效的栈板码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); // this.txt_palletcode.Focus(); // return; // } // try // { // // 禁用按钮防止重复点击 // btn_printpallet.Enabled = false; // // 调用手动打印方法 // short result = await ControlCenter.Instance.ManualPrintPalletAsync(palletcode); // // 显示结果 // switch (result) // { // case 1: // MessageBox.Show($"栈板码 {palletcode} 打印成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information); // // 清空输入框 // this.txt_palletcode.Clear(); // break; // case 2: // MessageBox.Show($"栈板码 {palletcode} 打印失败!", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error); // break; // case 3: // MessageBox.Show($"栈板码 {palletcode} 未查询到打印参数!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); // break; // } // } // catch (Exception ex) // { // MessageBox.Show($"打印失败:{ex.Message}", "异常", MessageBoxButtons.OK, MessageBoxIcon.Error); // LogHelper.Instance.WriteError($"手动打印栈板异常:{ex}", "printLog"); // } // finally // { // // 恢复按钮 // btn_printpallet.Enabled = true; // this.txt_palletcode.Focus(); // } //} //private async void btn_printContainer_Click(object sender, EventArgs e) //{ // string containercode = this.txt_containercode.Text.Trim(); // // 输入验证 // if (string.IsNullOrWhiteSpace(containercode)) // { // MessageBox.Show("请输入有效的箱唛码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); // this.txt_containercode.Focus(); // return; // } // try // { // // 禁用按钮防止重复点击 // btn_printContainer.Enabled = false; // // 调用手动打印方法 // short result = await ControlCenter.Instance.ManualPrintContainerAsync(containercode); // // 显示结果 // switch (result) // { // case 1: // MessageBox.Show($"箱唛码 {containercode} 打印成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information); // // 清空输入框 // this.txt_containercode.Clear(); // break; // case 2: // MessageBox.Show($"箱唛码 {containercode} 打印失败!", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error); // break; // case 3: // MessageBox.Show($"箱唛码 {containercode} 未查询到打印参数!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); // break; // } // } // catch (Exception ex) // { // MessageBox.Show($"打印失败:{ex.Message}", "异常", MessageBoxButtons.OK, MessageBoxIcon.Error); // LogHelper.Instance.WriteError($"手动打印箱唛异常:{ex}", "printLog"); // } // finally // { // // 恢复按钮 // btn_printContainer.Enabled = true; // this.txt_containercode.Focus(); // } //} } }