领胜LDS 键盘AOI检测项目
patrick.xu
2021-03-06 8a3ab64a65da226636743be07c4bee63f50da25d
src/Bro.M071.Process/UI/M071_MainForm.cs
@@ -1,13 +1,16 @@
using Bro.Common.Base;
using Bro.Common.Helper;
using Bro.UI.Model.Winform;
using Bro.UI.Model.Winform.UI;
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;
using System.Threading.Tasks;
using System.Windows.Forms;
using static Bro.Common.Helper.EnumHelper;
@@ -21,6 +24,9 @@
        M071Config Config => Process?.IConfig as M071Config;
        M071Process Process_M071 => Process as M071Process;
        System.Threading.Timer _refreshUITimer = null;
        public M071_MainForm()
        {
            InitializeComponent();
@@ -33,6 +39,7 @@
            tscEditLocation.Visible = tsmiShowEditor.Checked = false;
            _refreshUITimer = new System.Threading.Timer(OnRefreshUI, null, -1, -1);
            this.Load += async (s, e) =>
               {
                   await Task.Delay(300);
@@ -40,6 +47,12 @@
                   cvImage.SetScreenSize();
                   cvImage.OnElementChangedHandle -= CvImage_OnElementChangedHandle;
                   cvImage.OnElementChangedHandle += CvImage_OnElementChangedHandle;
                   //timer1.Enabled = true;
                   btnReset.Text = $"复位(长按{Config.FullResetRequiredDuration}秒大复位)";
                   lblCT.Text = tsslError.Text = "";
               };
        }
@@ -53,38 +66,46 @@
            if (string.IsNullOrWhiteSpace(Config.BackgroundImagePath))
                return;
            this.Invoke(new Action(() =>
            try
            {
                try
                {
                    Bitmap image = (Bitmap)Image.FromFile(Config.BackgroundImagePath);
                    cvImage.LoadImage(image);
                    cvImage.SetScreenSize();
                }
                catch (Exception ex)
                {
                    LogAsync(new LogMsg(DateTime.Now, "载入背景图异常", ex.Message));
                    return;
                }
                this.Invoke(new Action(() =>
                   {
                       try
                       {
                           Bitmap image = (Bitmap)Image.FromFile(Config.BackgroundImagePath);
                           cvImage.LoadImage(image);
                           cvImage.SetScreenSize();
                       }
                       catch (Exception ex)
                       {
                           LogAsync(new LogMsg(DateTime.Now, "载入背景图异常", ex.Message));
                           return;
                       }
                cvImage.Elements.Clear();
                lvMeasures.Items.Clear();
                       cvImage.Elements.Clear();
                       lvMeasures.Items.Clear();
                txtBarcode.Enabled = !Config.IsBarcodeManualInputBlocked;
                       txtBarcode.Enabled = !Config.IsBarcodeManualInputBlocked;
                Config.MeasurementUnitCollection.ForEach(u =>
                {
                    if (!u.IsEnabled)
                        return;
                       Config.MeasurementUnitCollection.ForEach(u =>
                       {
                           if (!u.IsEnabled)
                               return;
                    var ele = new KeyIndicator(u.Id, u.DisplayLocation);
                    cvImage.Elements.Add(ele);
                           var ele = new KeyIndicator(u.Id, u.DisplayLocation);
                           cvImage.Elements.Add(ele);
                    ListViewItem item = new ListViewItem(u.GetDisplayText());
                    item.Tag = u.Id;
                    lvMeasures.Items.Add(item);
                });
            }));
                           ListViewItem item = new ListViewItem(u.GetDisplayText());
                           item.Tag = u.Id;
                           lvMeasures.Items.Add(item);
                       });
                       lblOperator.Text = string.IsNullOrWhiteSpace(Process_M071.OperatorCode) ? "NotLogin" : Process_M071.OperatorCode;
                   }));
            }
            catch (Exception ex)
            {
            }
            Process_M071.OnClearBarcode -= M071_MainForm_OnClearBarcode;
            Process_M071.OnClearBarcode += M071_MainForm_OnClearBarcode;
@@ -102,8 +123,25 @@
            Process_M071.OnFullResetDone -= Process_M071_FullResetDone;
            Process_M071.OnFullResetDone += Process_M071_FullResetDone;
            Process_M071.OnResetDone -= Process_M071_ResetDone;
            Process_M071.OnResetDone += Process_M071_ResetDone;
            Config.PropertyChanged -= Config_PropertyChanged;
            Config.PropertyChanged += Config_PropertyChanged;
            Process_M071.OnElementUpdated -= Process_M071_OnElementUpdated;
            Process_M071.OnElementUpdated += Process_M071_OnElementUpdated;
            Process_M071.OnCheckHintUpload = OnCheckHintUpload;
            Process_M071.OnOperatorLogin = OnOperatorLogin;
        }
        private void Process_M071_ResetDone()
        {
            lblCT.BeginInvoke(new Action(() =>
            {
                lblCT.Text = "";
            }));
        }
        private void Config_PropertyChanged(object sender, PropertyChangedEventArgs e)
@@ -126,6 +164,27 @@
        {
            tscEditLocation.Visible = tsmiShowEditor.Checked;
            tscEditLocation.BringToFront();
        }
        private void tsmiRefreshLabels_Click(object sender, EventArgs e)
        {
            cvImage.Elements.Clear();
            lvMeasures.Items.Clear();
            Config.MeasurementUnitCollection.ForEach(u =>
            {
                if (!u.IsEnabled)
                    return;
                var ele = new KeyIndicator(u.Id, u.DisplayLocation);
                cvImage.Elements.Add(ele);
                ListViewItem item = new ListViewItem(u.GetDisplayText());
                item.Tag = u.Id;
                lvMeasures.Items.Add(item);
            });
            this.Invalidate();
        }
        #endregion
@@ -223,6 +282,50 @@
        }
        #endregion
        #region 标签结果显示
        private void OnRefreshUI(object state)
        {
            cvImage.Invoke(new Action(() => Refresh()));
        }
        private async void Process_M071_OnElementUpdated(Common.Interface.IShapeElement obj)
        {
            //this.Invoke(new Action(() =>
            //{
            //    if (obj is KeyIndicator keyIndicator)
            //    {
            //        var ele = cvImage.Elements.FirstOrDefault(u => u.ID == keyIndicator.ID);
            //        //(ele as KeyIndicator).Text = keyIndicator.Text;
            //        //(ele as KeyIndicator).ResultState = keyIndicator.ResultState;
            //        if (ele != null)
            //        {
            //            ele = keyIndicator;
            //        }
            //        this.Invalidate();
            //    }
            //}));
            if (obj is KeyIndicator keyIndicator)
            {
                this.Invoke(new Action(() =>
                {
                    var ele = cvImage.Elements.FirstOrDefault(u => u.ID == keyIndicator.ID);
                    (ele as KeyIndicator).Text = keyIndicator.Text;
                    (ele as KeyIndicator).ResultState = keyIndicator.ResultState;
                    //this.Invalidate();
                    _refreshUITimer.Change(1000, -1);
                }));
            }
            await Task.Delay(100);
        }
        #endregion
        #region 上方状态区
        #region 条码
@@ -234,10 +337,18 @@
            {
                _barcode += keyStr.ToUpper();
            }
            else if (keyStr.Length == 2 && keyStr.StartsWith("D"))
            {
                _barcode += keyStr.Substring(1).ToUpper();
            }
            else if (keyStr.StartsWith("NumPad"))
            {
                _barcode += keyStr.Replace("NumPad", "");
            }
            if (e.KeyValue == 13)
            {
                txtBarcode.Text = _barcode;
                Process_M071.BarCode = txtBarcode.Text = _barcode;
                _barcode = "";
            }
@@ -249,7 +360,7 @@
        private void txtBarcode_TextChanged(object sender, EventArgs e)
        {
            Process_M071.BarCode = txtBarcode.Text.Trim();
            //Process_M071.BarCode = txtBarcode.Text.Trim();
        }
        private void M071_MainForm_OnClearBarcode()
@@ -261,46 +372,54 @@
        #region 机台状态
        private async void M071_MainForm_MachineStateChanged(MachineState state)
        {
            lblMachineState.BeginInvoke(new Action(() =>
            if (lblMachineState.IsHandleCreated)
            {
                switch (state)
                lblMachineState.Invoke(new Action(() =>
                {
                    case MachineState.Alarm:
                        btnStartMeasure.BackColor = lblMachineState.BackColor = Color.Red;
                        btnStartMeasure.ForeColor = lblMachineState.ForeColor = Color.White;
                        lblMachineState.Text = "警报";
                    switch (state)
                    {
                        case MachineState.Alarm:
                            btnStartMeasure.BackColor = lblMachineState.BackColor = Color.Red;
                            btnStartMeasure.ForeColor = lblMachineState.ForeColor = Color.White;
                            lblMachineState.Text = "警报";
                        btnStartMeasure.Enabled = false;
                        break;
                    case MachineState.Pause:
                        btnStartMeasure.BackColor = lblMachineState.BackColor = Color.Yellow;
                        btnStartMeasure.ForeColor = lblMachineState.ForeColor = Color.Black;
                        lblMachineState.Text = "暂停";
                            btnStartMeasure.Text = "开始测量";
                            btnStartMeasure.Enabled = false;
                            break;
                        case MachineState.Pause:
                            btnStartMeasure.BackColor = lblMachineState.BackColor = Color.Yellow;
                            btnStartMeasure.ForeColor = lblMachineState.ForeColor = Color.Black;
                            lblMachineState.Text = "暂停";
                        //btnStartMeasure.Text = "暂停测量";
                        btnStartMeasure.Enabled = true;
                        break;
                    case MachineState.Ready:
                        btnStartMeasure.BackColor = lblMachineState.BackColor = Color.Lime;
                        btnStartMeasure.ForeColor = lblMachineState.ForeColor = Color.White;
                        lblMachineState.Text = "就绪";
                            btnStartMeasure.Text = "继续测量";
                            btnStartMeasure.Enabled = true;
                            break;
                        case MachineState.Ready:
                            btnStartMeasure.BackColor = lblMachineState.BackColor = Color.Lime;
                            btnStartMeasure.ForeColor = lblMachineState.ForeColor = Color.Black;
                            lblMachineState.Text = "就绪";
                        btnStartMeasure.Enabled = true;
                        break;
                    case MachineState.Running:
                        btnStartMeasure.BackColor = lblMachineState.BackColor = Color.Lime;
                        btnStartMeasure.ForeColor = lblMachineState.ForeColor = Color.DarkGreen;
                        lblMachineState.Text = "运行";
                            btnStartMeasure.Text = "开始测量";
                            btnStartMeasure.Enabled = true;
                            break;
                        case MachineState.Running:
                            btnStartMeasure.BackColor = lblMachineState.BackColor = Color.Transparent;
                            btnStartMeasure.ForeColor = lblMachineState.ForeColor = Color.DarkGreen;
                            lblMachineState.Text = "运行";
                        btnStartMeasure.Enabled = true;
                        break;
                    case MachineState.Unknown:
                        btnStartMeasure.BackColor = lblMachineState.BackColor = SystemColors.Control;
                        btnStartMeasure.ForeColor = lblMachineState.ForeColor = SystemColors.ControlText;
                        lblMachineState.Text = "未知";
                        break;
                }
            }));
                            btnStartMeasure.Text = "暂停测量";
                            btnStartMeasure.Enabled = true;
                            break;
                        case MachineState.Unknown:
                            btnStartMeasure.BackColor = lblMachineState.BackColor = SystemColors.Control;
                            btnStartMeasure.ForeColor = lblMachineState.ForeColor = SystemColors.ControlText;
                            lblMachineState.Text = "未知";
                            btnStartMeasure.Enabled = false;
                            break;
                    }
                }));
            }
            await Task.Delay(100);
        }
@@ -326,14 +445,20 @@
                    lblProductionState.ForeColor = Color.White;
                    lblProductionState.Text = "OK";
                }
                else
                else if (result == 0)
                {
                    lblProductionState.BackColor = Color.Red;
                    lblProductionState.ForeColor = Color.White;
                    lblProductionState.Text = "NG";
                }
                else
                {
                    lblProductionState.BackColor = Color.White;
                    lblProductionState.ForeColor = Color.Red;
                    lblProductionState.Text = "NA";
                }
                btnStartMeasure.Text = "开始测量";
                //btnStartMeasure.Text = "开始测量";
            }));
            await Task.Delay(100);
        }
@@ -347,6 +472,15 @@
                lblProductionState.Text = "测试中";
                lblCT.Text = "";
                cvImage.Elements.ToList().ForEach(e =>
                {
                    if (e is KeyIndicator indicator)
                    {
                        indicator.Text = "";
                        indicator.ResultState = null;
                    }
                });
            }));
            await Task.Delay(100);
        }
@@ -356,9 +490,6 @@
        private async void btnStartMeasure_Click(object sender, EventArgs e)
        {
            btnStartMeasure.Enabled = false;
            bool isBreak = false;
            if (btnStartMeasure.Text == "开始测量")
            {
                await Task.Run(() =>
@@ -369,31 +500,19 @@
                    }
                    catch (Exception ex)
                    {
                        isBreak = true;
                        LogAsync(DateTime.Now, "流程异常", ex.Message);
                        Process_M071.ExceptionRaisedInMonitor(ex);
                        btnStartMeasure.Text = "开始测量";
                    }
                });
                if (!isBreak)
                    btnStartMeasure.Text = "暂停测量";
            }
            else if (btnStartMeasure.Text == "继续测量")
            {
                Process_M071.SwitchJobStatus(new OperationConfigBase() { InputPara = new List<int>() { 0 } }, null, null);
                btnStartMeasure.Text = "暂停测量";
                Process_M071.SwitchJobStatus(new OperationConfigBase() { InputPara = new List<int>() { 10 } }, null, null);
            }
            else
            {
                Process_M071.SwitchJobStatus(new OperationConfigBase() { InputPara = new List<int>() { 1 } }, null, null);
                btnStartMeasure.Text = "继续测量";
                Process_M071.SwitchJobStatus(new OperationConfigBase() { InputPara = new List<int>() { 11 } }, null, null);
            }
            btnStartMeasure.Enabled = true;
        }
        #region 复位操作
@@ -414,7 +533,7 @@
                Process_M071.Reset(null, null, null);
            }
            Process_M071.ResetTimer.Change(-1, 1000 * 5);
            Process_M071.ResetTimer.Change(1000 * Config.FullResetRequiredDuration, -1);
        }
        private void btnReset_MouseUp(object sender, MouseEventArgs e)
@@ -422,5 +541,95 @@
            Process_M071.ResetTimer.Change(-1, -1);
        }
        #endregion
        private void timer1_Tick(object sender, EventArgs e)
        {
            //cvImage.Refresh();
        }
        private void lblOperator_DoubleClick(object sender, EventArgs e)
        {
            InputWindow inputFrm = new InputWindow("请输入操作员工号");
            if (inputFrm.ShowDialog() == DialogResult.OK)
            {
                Process_M071.OperatorCode = inputFrm.Input;
            }
        }
        private void OnOperatorLogin(string operatorCode)
        {
            this.Invoke(new Action(() =>
            {
                lblOperator.Text = operatorCode;
            }));
        }
        private async void OnCheckHintUpload(string hintMsg, bool isAlarm)
        {
            await Task.Run(() =>
            {
                this.Invoke(new Action(() =>
                {
                    if (!string.IsNullOrWhiteSpace(hintMsg))
                    {
                        stStripHint.Visible = true;
                        tsslError.Text = hintMsg;
                        tsslError.BackColor = isAlarm ? Color.Red : Color.Green;
                    }
                    else
                    {
                        stStripHint.Visible = false;
                        tsslError.Text = "";
                    }
                }));
            });
        }
        bool isStart = true;
        private void button1_Click(object sender, EventArgs e)
        {
            Task.Run(async () =>
            {
                try
                {
                    for (int j = 0; j < 10000; j++)
                    {
                        int i = 0;
                        if (isStart)
                        {
                            cvImage.Elements.ToList().ForEach(ele =>
                            {
                                if (ele is KeyIndicator indicator)
                                {
                                    indicator.Text = "";
                                    indicator.ResultState = null;
                                }
                            });
                        }
                        else
                        {
                            cvImage.Elements.ToList().ForEach(ele =>
                            {
                                if (ele is KeyIndicator indicator)
                                {
                                    indicator.Text = (i++).ToString();
                                    indicator.ResultState = true;
                                }
                            });
                        }
                        isStart = !isStart;
                        cvImage.BeginInvoke(new Action(() => Refresh()));
                        LogAsync(DateTime.Now, $"UI Refresh {j}", "");
                        await Task.Delay(300);
                    }
                }
                catch (Exception ex)
                {
                }
            });
        }
    }
}