领胜LDS 键盘AOI检测项目
wells.liu
2020-07-06 f9c7928bff92596686e05a15fef21d499e954088
src/Bro.M071.Process/UI/M071_MainForm.cs
@@ -1,4 +1,5 @@
using Bro.Common.Helper;
using Bro.Common.Base;
using Bro.Common.Helper;
using Bro.UI.Model.Winform;
using System;
using System.Collections.Generic;
@@ -9,14 +10,16 @@
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static Bro.Common.Helper.EnumHelper;
namespace Bro.M071.Process.UI
{
    [MenuNode("M071_MainForm", "键盘检测主界面", 3, "M071Node", true)]
    [MenuNode("M071_MainForm", "运行界面", 1, "M071Node", true)]
    public partial class M071_MainForm : MenuFrmBase
    {
        Canvas cvImage = new Canvas();
        M071Config Config => Process?.IConfig as M071Config;
        M071Process Process_M071 => Process as M071Process;
        public M071_MainForm()
        {
@@ -26,7 +29,7 @@
            tsmiShowStatusBar.Checked = cvImage.IsShowStatusBar = false;
            tsmiShowToolBar.Checked = cvImage.IsShowToolBar = false;
            cvImage.Dock = DockStyle.Fill;
            this.Controls.Add(cvImage);
            plImage.Controls.Add(cvImage);
            tscEditLocation.Visible = tsmiShowEditor.Checked = false;
        }
@@ -66,17 +69,27 @@
                item.Tag = u.Id;
                lvMeasures.Items.Add(item);
            });
        }
        private void lvMeasures_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvMeasures.SelectedItems.Count <= 0)
                return;
            var ele = cvImage.Elements.FirstOrDefault(u => u.ID == lvMeasures.SelectedItems[0].Tag.ToString());
            propGridKeyIndicator.SelectedObject = ele;
            txtBarcode.ReadOnly = Config.IsBarcodeManulInputBlocked;
            Process_M071.OnClearBarcode -= M071_MainForm_OnClearBarcode;
            Process_M071.OnClearBarcode += M071_MainForm_OnClearBarcode;
            Process_M071.OnUpdateResult -= M071_MainForm_UpdateResult;
            Process_M071.OnUpdateResult += M071_MainForm_UpdateResult;
            Process_M071.OnUpdateCT -= M071_MainForm_UpdateCT;
            Process_M071.OnUpdateCT += M071_MainForm_UpdateCT;
            Process_M071.OnMeasureStart -= M071_MainForm_MeasureStart;
            Process_M071.OnMeasureStart += M071_MainForm_MeasureStart;
            Process_M071.OnMachineStateChanged -= M071_MainForm_MachineStateChanged;
            Process_M071.OnMachineStateChanged += M071_MainForm_MachineStateChanged;
            Process_M071.OnFullResetDone -= Process_M071_FullResetDone;
            Process_M071.OnFullResetDone += Process_M071_FullResetDone;
        }
        #region 图片区右键菜单
        private void tsmiShowToolBar_CheckedChanged(object sender, EventArgs e)
        {
            cvImage.IsShowToolBar = tsmiShowToolBar.Checked;
@@ -90,6 +103,18 @@
        private void tsmiShowEditor_CheckedChanged(object sender, EventArgs e)
        {
            tscEditLocation.Visible = tsmiShowEditor.Checked;
            tscEditLocation.BringToFront();
        }
        #endregion
        #region 标签编辑区
        private void lvMeasures_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvMeasures.SelectedItems.Count <= 0)
                return;
            var ele = cvImage.Elements.FirstOrDefault(u => u.ID == lvMeasures.SelectedItems[0].Tag.ToString());
            propGridKeyIndicator.SelectedObject = ele;
        }
        private void btnCancelEdit_Click(object sender, EventArgs e)
@@ -109,7 +134,7 @@
                lvMeasures.Items.Add(item);
            });
            //tscEditLocation.Visible = false;
            MessageBox.Show("取消标签修改");
        }
        private void btnConfirmEdit_Click(object sender, EventArgs e)
@@ -123,7 +148,203 @@
                }
            });
            //tscEditLocation.Visible = false;
            MessageBox.Show("标签修改完成");
        }
        #endregion
        #region 上方状态区
        #region 条码
        string _barcode = "";
        private void M071_MainForm_KeyUp(object sender, KeyEventArgs e)
        {
            string keyStr = e.KeyCode.ToString();
            if (keyStr.Length == 1)
            {
                _barcode += keyStr.ToUpper();
            }
            if (e.KeyValue == 13)
            {
                txtBarcode.Text = _barcode;
                _barcode = "";
            }
        }
        private void txtBarcode_TextChanged(object sender, EventArgs e)
        {
            Process_M071.BarCode = txtBarcode.Text.Trim();
        }
        private void M071_MainForm_OnClearBarcode()
        {
            txtBarcode.BeginInvoke(new Action(() => txtBarcode.Clear()));
        }
        #endregion
        #region 机台状态
        private async void M071_MainForm_MachineStateChanged(MachineState state)
        {
            lblMachineState.BeginInvoke(new Action(() =>
            {
                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 = true;
                        break;
                    case MachineState.Ready:
                        btnStartMeasure.BackColor = lblMachineState.BackColor = Color.Lime;
                        btnStartMeasure.ForeColor = lblMachineState.ForeColor = Color.White;
                        lblMachineState.Text = "就绪";
                        btnStartMeasure.Enabled = true;
                        break;
                    case MachineState.Running:
                        btnStartMeasure.BackColor = lblMachineState.BackColor = Color.Lime;
                        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;
                }
            }));
            await Task.Delay(100);
        }
        #endregion
        #region 产品结果
        private async void M071_MainForm_UpdateCT(float ct)
        {
            lblCT.BeginInvoke(new Action(() =>
            {
                lblCT.Text = ct.ToString("f2") + " 秒";
            }));
            await Task.Delay(100);
        }
        private async void M071_MainForm_UpdateResult(DateTime arg1, int result)
        {
            this.BeginInvoke(new Action(() =>
            {
                if (result == 1)
                {
                    lblProductionState.BackColor = Color.Green;
                    lblProductionState.ForeColor = Color.White;
                    lblProductionState.Text = "OK";
                }
                else
                {
                    lblProductionState.BackColor = Color.Red;
                    lblProductionState.ForeColor = Color.White;
                    lblProductionState.Text = "NG";
                }
                btnStartMeasure.Text = "开始测量";
            }));
            await Task.Delay(100);
        }
        private async void M071_MainForm_MeasureStart()
        {
            this.BeginInvoke(new Action(() =>
            {
                lblProductionState.BackColor = SystemColors.Control;
                lblProductionState.ForeColor = SystemColors.ControlText;
                lblProductionState.Text = "测试中";
                lblCT.Text = "";
            }));
            await Task.Delay(100);
        }
        #endregion
        #endregion
        private async void btnStartMeasure_Click(object sender, EventArgs e)
        {
            btnStartMeasure.Enabled = false;
            bool isBreak = false;
            if (btnStartMeasure.Text == "开始测量")
            {
                await Task.Run(() =>
                {
                    try
                    {
                        Process_M071.StartJob(null, null, null);
                    }
                    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 = "暂停测量";
            }
            else
            {
                Process_M071.SwitchJobStatus(new OperationConfigBase() { InputPara = new List<int>() { 1 } }, null, null);
                btnStartMeasure.Text = "继续测量";
            }
            btnStartMeasure.Enabled = true;
        }
        #region 复位操作
        private async void Process_M071_FullResetDone()
        {
            btnStartMeasure.BeginInvoke(new Action(() =>
            {
                btnStartMeasure.Enabled = true;
                btnStartMeasure.Text = "开始测量";
            }));
            await Task.Delay(100);
        }
        private void btnReset_MouseDown(object sender, MouseEventArgs e)
        {
            if (Process_M071.MachineState != MachineState.Running)
            {
                Process_M071.Reset(null, null, null);
            }
            Process_M071.ResetTimer.Change(-1, 1000 * 5);
        }
        private void btnReset_MouseUp(object sender, MouseEventArgs e)
        {
            Process_M071.ResetTimer.Change(-1, -1);
        }
        #endregion
    }
}