From c0b8009ef36746c7a14606267662570e8c59c2c6 Mon Sep 17 00:00:00 2001
From: xcd <834800634@qq.com>
Date: 星期四, 09 七月 2020 19:12:37 +0800
Subject: [PATCH] 部分模型修改

---
 src/Bro.M071.Process/UI/M071_MainForm.cs |  259 ++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 233 insertions(+), 26 deletions(-)

diff --git a/src/Bro.M071.Process/UI/M071_MainForm.cs b/src/Bro.M071.Process/UI/M071_MainForm.cs
index 97904b6..938e116 100644
--- a/src/Bro.M071.Process/UI/M071_MainForm.cs
+++ b/src/Bro.M071.Process/UI/M071_MainForm.cs
@@ -1,4 +1,5 @@
-锘縰sing Bro.Common.Helper;
+锘縰sing Bro.Common.Base;
+using Bro.Common.Helper;
 using Bro.UI.Model.Winform;
 using System;
 using System.Collections.Generic;
@@ -9,6 +10,7 @@
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
+using static Bro.Common.Helper.EnumHelper;
 
 namespace Bro.M071.Process.UI
 {
@@ -17,6 +19,7 @@
     {
         Canvas cvImage = new Canvas();
         M071Config Config => Process?.IConfig as M071Config;
+        M071Process Process_M071 => Process as M071Process;
 
         public M071_MainForm()
         {
@@ -29,6 +32,13 @@
             plImage.Controls.Add(cvImage);
 
             tscEditLocation.Visible = tsmiShowEditor.Checked = false;
+
+            this.Load += async (s, e) =>
+               {
+                   await Task.Delay(300);
+
+                   cvImage.SetScreenSize();
+               };
         }
 
         public override void OnProcessUpdated()
@@ -41,36 +51,53 @@
             if (string.IsNullOrWhiteSpace(Config.BackgroundImagePath))
                 return;
 
-            try
+            this.Invoke(new Action(() =>
             {
-                Bitmap image = (Bitmap)Image.FromFile(Config.BackgroundImagePath);
-                cvImage.LoadImage(image);
-            }
-            catch (Exception ex)
-            {
-                LogAsync(new LogMsg(DateTime.Now, "杞藉叆鑳屾櫙鍥惧紓甯�", ex.Message));
-                return;
-            }
-
-            cvImage.Elements.Clear();
-            lvMeasures.Items.Clear();
-            Config.MeasurementUnitCollection.ForEach(u =>
-            {
-                if (!u.IsEnabled)
+                try
+                {
+                    Bitmap image = (Bitmap)Image.FromFile(Config.BackgroundImagePath);
+                    cvImage.LoadImage(image);
+                    cvImage.SetScreenSize();
+                }
+                catch (Exception ex)
+                {
+                    LogAsync(new LogMsg(DateTime.Now, "杞藉叆鑳屾櫙鍥惧紓甯�", ex.Message));
                     return;
+                }
 
-                var ele = new KeyIndicator(u.Id, u.DisplayLocation);
-                cvImage.Elements.Add(ele);
+                cvImage.Elements.Clear();
+                lvMeasures.Items.Clear();
+                txtBarcode.ReadOnly = Config.IsBarcodeManulInputBlocked;
 
-                ListViewItem item = new ListViewItem(u.GetDisplayText());
-                item.Tag = u.Id;
-                lvMeasures.Items.Add(item);
-            });
+                Config.MeasurementUnitCollection.ForEach(u =>
+                {
+                    if (!u.IsEnabled)
+                        return;
 
-            txtBarcode.ReadOnly = Config.IsBarcodeManulInputBlocked;
+                    var ele = new KeyIndicator(u.Id, u.DisplayLocation);
+                    cvImage.Elements.Add(ele);
 
-            (Process as M071Process).OnClearBarcode -= M071_MainForm_OnClearBarcode;
-            (Process as M071Process).OnClearBarcode += M071_MainForm_OnClearBarcode;
+                    ListViewItem item = new ListViewItem(u.GetDisplayText());
+                    item.Tag = u.Id;
+                    lvMeasures.Items.Add(item);
+                });
+            }));
+
+            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 鍥剧墖鍖哄彸閿彍鍗�
@@ -134,6 +161,19 @@
 
             MessageBox.Show("鏍囩淇敼瀹屾垚");
         }
+
+        private void lvMeasures_DoubleClick(object sender, EventArgs e)
+        {
+            if (lvMeasures.SelectedItems.Count <= 0)
+                return;
+
+            var ele = cvImage.Elements.FirstOrDefault(u => u.ID == lvMeasures.SelectedItems[0].Tag.ToString());
+            if (ele != null)
+            {
+                ele.State = ElementState.Selected;
+                cvImage.Invalidate();
+            }
+        }
         #endregion
 
         #region 涓婃柟鐘舵�佸尯
@@ -153,11 +193,16 @@
                 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 as M071Process).BarCode = txtBarcode.Text.Trim();
+            Process_M071.BarCode = txtBarcode.Text.Trim();
         }
 
         private void M071_MainForm_OnClearBarcode()
@@ -166,7 +211,169 @@
         }
         #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
     }
 }

--
Gitblit v1.8.0