From 3f6e94d7de9694378114773ab48d8a61bcc3a01e Mon Sep 17 00:00:00 2001
From: patrick.xu <patrick.xu@broconcentric.com>
Date: 星期一, 01 二月 2021 09:22:20 +0800
Subject: [PATCH] 1. 报警生成前后端异步调用 2. 安全光栅方法单线程调用 3. 复位完成时修改界面显示 4. 机台状态拆分执行状态

---
 src/Bro.M071.Process/UI/M071_MainForm.cs |  385 +++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 307 insertions(+), 78 deletions(-)

diff --git a/src/Bro.M071.Process/UI/M071_MainForm.cs b/src/Bro.M071.Process/UI/M071_MainForm.cs
index 63f429e..1266611 100644
--- a/src/Bro.M071.Process/UI/M071_MainForm.cs
+++ b/src/Bro.M071.Process/UI/M071_MainForm.cs
@@ -1,12 +1,16 @@
-锘縰sing Bro.Common.Helper;
+锘縰sing 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;
@@ -31,6 +35,21 @@
             plImage.Controls.Add(cvImage);
 
             tscEditLocation.Visible = tsmiShowEditor.Checked = false;
+
+            this.Load += async (s, e) =>
+               {
+                   await Task.Delay(300);
+
+                   cvImage.SetScreenSize();
+                   cvImage.OnElementChangedHandle -= CvImage_OnElementChangedHandle;
+                   cvImage.OnElementChangedHandle += CvImage_OnElementChangedHandle;
+
+                   timer1.Enabled = true;
+
+                   btnReset.Text = $"澶嶄綅锛堥暱鎸墈Config.FullResetRequiredDuration}绉掑ぇ澶嶄綅锛�";
+
+                   lblCT.Text = tsslError.Text = "";
+               };
         }
 
         public override void OnProcessUpdated()
@@ -45,31 +64,44 @@
 
             try
             {
-                Bitmap image = (Bitmap)Image.FromFile(Config.BackgroundImagePath);
-                cvImage.LoadImage(image);
+                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();
+
+                       txtBarcode.Enabled = !Config.IsBarcodeManualInputBlocked;
+
+                       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);
+                       });
+
+                       lblOperator.Text = string.IsNullOrWhiteSpace(Process_M071.OperatorCode) ? "NotLogin" : Process_M071.OperatorCode;
+                   }));
             }
             catch (Exception ex)
             {
-                LogAsync(new LogMsg(DateTime.Now, "杞藉叆鑳屾櫙鍥惧紓甯�", ex.Message));
-                return;
             }
-
-            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);
-            });
-
-            txtBarcode.ReadOnly = Config.IsBarcodeManulInputBlocked;
 
             Process_M071.OnClearBarcode -= M071_MainForm_OnClearBarcode;
             Process_M071.OnClearBarcode += M071_MainForm_OnClearBarcode;
@@ -86,6 +118,31 @@
 
             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)
+        {
+            txtBarcode.Enabled = !Config.IsBarcodeManualInputBlocked;
         }
 
         #region 鍥剧墖鍖哄彸閿彍鍗�
@@ -104,13 +161,69 @@
             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
 
         #region 鏍囩缂栬緫鍖�
+        private async void CvImage_OnElementChangedHandle(Common.Interface.IShapeElement ele)
+        {
+            this.Invoke(new Action(() =>
+            {
+                if (ele is KeyIndicator indicator)
+                {
+                    if (indicator.State == ElementState.Selected)
+                    {
+                        foreach (ListViewItem item in lvMeasures.Items)
+                        {
+                            if (item.Tag.ToString() == indicator.ID)
+                            {
+                                item.Selected = true;
+
+                                lvMeasures.EnsureVisible(item.Index);
+                                break;
+                            }
+                        }
+                    }
+                }
+            }));
+
+            await Task.Delay(100);
+        }
+
         private void lvMeasures_SelectedIndexChanged(object sender, EventArgs e)
         {
             if (lvMeasures.SelectedItems.Count <= 0)
                 return;
+
+            foreach (ListViewItem item in lvMeasures.Items)
+            {
+                item.BackColor = SystemColors.Control;
+            }
+
+            foreach (ListViewItem item in lvMeasures.SelectedItems)
+            {
+                item.BackColor = Color.Orange;
+            }
 
             var ele = cvImage.Elements.FirstOrDefault(u => u.ID == lvMeasures.SelectedItems[0].Tag.ToString());
             propGridKeyIndicator.SelectedObject = ele;
@@ -149,6 +262,57 @@
 
             MessageBox.Show("鏍囩淇敼瀹屾垚");
         }
+
+        private void lvMeasures_DoubleClick(object sender, EventArgs e)
+        {
+            if (lvMeasures.SelectedItems.Count <= 0)
+                return;
+
+            cvImage.Elements.ToList().ForEach(u => u.State = ElementState.Normal);
+            var ele = cvImage.Elements.FirstOrDefault(u => u.ID == lvMeasures.SelectedItems[0].Tag.ToString());
+            if (ele != null)
+            {
+                ele.State = ElementState.Selected;
+                //cvImage.Invalidate();
+            }
+        }
+        #endregion
+
+        #region 鏍囩缁撴灉鏄剧ず
+        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();
+                }));
+            }
+
+            await Task.Delay(100);
+        }
         #endregion
 
         #region 涓婃柟鐘舵�佸尯
@@ -162,17 +326,30 @@
             {
                 _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 = "";
+            }
+
+            if (e.KeyValue == 27)
+            {
+                cvImage.Elements.ToList().ForEach(ele => ele.State = ElementState.Normal);
             }
         }
 
         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()
@@ -184,46 +361,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);
         }
@@ -249,14 +434,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);
         }
@@ -270,6 +461,15 @@
                 lblProductionState.Text = "娴嬭瘯涓�";
 
                 lblCT.Text = "";
+
+                cvImage.Elements.ToList().ForEach(e =>
+                {
+                    if (e is KeyIndicator indicator)
+                    {
+                        indicator.Text = "";
+                        indicator.ResultState = null;
+                    }
+                });
             }));
             await Task.Delay(100);
         }
@@ -279,9 +479,6 @@
 
         private async void btnStartMeasure_Click(object sender, EventArgs e)
         {
-            btnStartMeasure.Enabled = false;
-            bool isBreak = false;
-
             if (btnStartMeasure.Text == "寮�濮嬫祴閲�")
             {
                 await Task.Run(() =>
@@ -292,31 +489,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.ResumeJob(null, null, null);
-                btnStartMeasure.Text = "鏆傚仠娴嬮噺";
+                Process_M071.SwitchJobStatus(new OperationConfigBase() { InputPara = new List<int>() { 10 } }, null, null);
             }
             else
             {
-                Process_M071.PauseJob(null, null, null);
-                btnStartMeasure.Text = "缁х画娴嬮噺";
+                Process_M071.SwitchJobStatus(new OperationConfigBase() { InputPara = new List<int>() { 11 } }, null, null);
             }
-
-            btnStartMeasure.Enabled = true;
         }
 
         #region 澶嶄綅鎿嶄綔
@@ -337,7 +522,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)
@@ -345,5 +530,49 @@
             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 = "";
+                    }
+                }));
+            });
+        }
     }
 }

--
Gitblit v1.8.0