From 78289c64a16dd02cc9fec595bf758a9e30a30926 Mon Sep 17 00:00:00 2001
From: patrick.xu <patrick.xu@broconcentric.com>
Date: 星期六, 20 二月 2021 10:51:11 +0800
Subject: [PATCH] gocator修改异步模式下图片获取操作

---
 src/Bro.M071.Process/M071Process.cs |  140 +++++++++++++++++++++++++++++++++++++---------
 1 files changed, 112 insertions(+), 28 deletions(-)

diff --git a/src/Bro.M071.Process/M071Process.cs b/src/Bro.M071.Process/M071Process.cs
index cc95119..f7b3f92 100644
--- a/src/Bro.M071.Process/M071Process.cs
+++ b/src/Bro.M071.Process/M071Process.cs
@@ -19,6 +19,7 @@
 using System.Drawing.Imaging;
 using System.IO;
 using System.Linq;
+using System.Runtime.CompilerServices;
 using System.Text;
 using System.Threading;
 using System.Threading.Tasks;
@@ -47,6 +48,7 @@
         public event Action<IShapeElement> OnElementUpdated;
         public event Action<MachineState> OnMachineStateChanged;
         public event Action OnFullResetDone;
+        public event Action OnResetDone;
         #endregion
 
         string _precision = "f3";
@@ -193,7 +195,11 @@
         }
         #endregion
 
+        object productionLock = new object();
         List<ProductionMeasurement> productionList = new List<ProductionMeasurement>();
+
+        bool _isdoing = false;
+        object _doingLock = new object();
 
         [ProcessMethod("", "StartJob", "寮�濮嬫壂鎻�", InvokeType.TestInvoke)]
         public ProcessResponse StartJob(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice)
@@ -204,12 +210,13 @@
             }
 
             string hint = "";
-            if (MachineState != MachineState.Ready)
-            {
-                hint = "鏈哄彴鏈氨缁紝璇峰嬁寮�濮嬫祴閲�";
-                OnCheckHintUpload?.Invoke(hint, true);
-                throw new ProcessException(hint, null, ExceptionLevel.Fatal);
-            }
+
+            //if (MachineState != MachineState.Ready)
+            //{
+            //    hint = "鏈哄彴鏈氨缁紝璇峰嬁寮�濮嬫祴閲�";
+            //    OnCheckHintUpload?.Invoke(hint, true);
+            //    throw new ProcessException(hint, null, ExceptionLevel.Warning);
+            //}
 
             if (string.IsNullOrWhiteSpace(BarCode))
             {
@@ -217,6 +224,17 @@
                 OnClearBarcode?.Invoke();
                 OnCheckHintUpload?.Invoke(hint, true);
                 throw new ProcessException(hint);
+            }
+
+            lock (_doingLock)
+            {
+                if (_isdoing)
+                {
+                    LogAsync(DateTime.Now, "璁惧姝e湪杩愯涓�", "");
+                    return new ProcessResponse(true);
+                }
+
+                _isdoing = true;
             }
 
             MachineState = MachineState.Running;
@@ -247,20 +265,25 @@
 
             BarCode = "";
 
-            var existedProduction = productionList.FirstOrDefault(u => u.Barcode == pMeasure.Barcode);
-            if (existedProduction != null)
+            lock (productionLock)
             {
-                productionList.Remove(existedProduction);
-                existedProduction.Dispose();
-                existedProduction = null;
-            }
+                var existedProduction = productionList.FirstOrDefault(u => u.Barcode == pMeasure.Barcode);
+                if (existedProduction != null)
+                {
+                    productionList.Remove(existedProduction);
+                    existedProduction.Dispose();
+                    existedProduction = null;
+                }
 
-            productionList.Add(pMeasure);
+                productionList.Add(pMeasure);
+            }
 
             pMeasure.InitialMeasurementsPropertyChanged();
             pMeasure.PropertyChanged += MeasureProduction_PropertyChanged;
 
-            Config.SnapshotPointCollection.Where(u => u.IsEnabled).ToList().ForEach(s =>
+            try
+            {
+                Config.SnapshotPointCollection.Where(u => u.IsEnabled).ToList().ForEach(s =>
                   {
                       _pausedHandle.Wait();
 
@@ -309,23 +332,43 @@
 
                       RunImageHandle(s.CameraOp.OpConfig, set, s.Id, s.Name, pMeasure.Measurements);
                   });
+            }
+            catch (Exception ex)
+            {
+                throw ex;
+            }
+            finally
+            {
+                LogAsync(DateTime.Now, $"{pMeasure.Barcode}娴嬮噺鍔ㄤ綔瀹屾垚", "");
+                _isdoing = false;
 
-            LogAsync(DateTime.Now, $"{pMeasure.Barcode}娴嬮噺鍔ㄤ綔瀹屾垚", "");
-
-            GC.Collect(0, GCCollectionMode.Optimized);
+                GC.Collect(0, GCCollectionMode.Optimized);
+            }
 
             return new ProcessResponse(true);
         }
 
         #region 鍙屾墜鍚姩
+        object _leftStartLock = new object();
+        object _rightStartLock = new object();
+
         bool isLeftStart = false;
         bool IsLeftStart
         {
             get => isLeftStart;
             set
             {
-                isLeftStart = value;
-                StartCheck();
+                if (isLeftStart != value)
+                {
+                    lock (_leftStartLock)
+                    {
+                        if (IsLeftStart != value)
+                        {
+                            isLeftStart = value;
+                            StartCheck();
+                        }
+                    }
+                }
             }
         }
 
@@ -335,15 +378,30 @@
             get => isRightStart;
             set
             {
-                isRightStart = value;
-                StartCheck();
+                if (isRightStart != value)
+                {
+                    lock (_rightStartLock)
+                    {
+                        if (isRightStart != value)
+                        {
+                            isRightStart = value;
+                            StartCheck();
+                        }
+                    }
+                }
             }
         }
 
         private void StartCheck()
         {
+
             if (isRightStart && isLeftStart)
             {
+                if (_isdoing)
+                {
+                    OnCheckHintUpload?.Invoke("璁惧杩愯涓�", false);
+                    return;
+                }
                 StartJob(null, null, null);
             }
         }
@@ -444,9 +502,13 @@
         {
             if (sender is ProductionMeasurement pMeasure)
             {
-                var production = productionList.FirstOrDefault(u => u.Barcode == pMeasure.Barcode);
-                if (production == null)
-                    return;
+                ProductionMeasurement production = null;
+                lock (productionLock)
+                {
+                    production = productionList.FirstOrDefault(u => u.Barcode == pMeasure.Barcode);
+                    if (production == null)
+                        return;
+                }
 
                 lock (production)
                 {
@@ -553,8 +615,20 @@
 
                 LogAsync(DateTime.Now, $"{pMeasure.Barcode} 妫�娴嬪畬鎴愶紝缁撴灉 {pMeasure.PResult}", "");
 
-                if (MachineState == MachineState.Running)
-                    MachineState = MachineState.Ready;
+                //if (MachineState == MachineState.Running)
+                //    MachineState = MachineState.Ready;
+
+                if (MachineState != MachineState.Alarm)
+                {
+                    if (MachineState == MachineState.Pause)
+                    {
+                        _machineStateBeforePause = MachineState.Ready;
+                    }
+                    else
+                    {
+                        MachineState = MachineState.Ready;
+                    }
+                }
 
                 var measurementUnitResultAndKeyUnitDataSet = GetMeasurementUnitResultAndKeyUnitData(pMeasure);
 
@@ -573,7 +647,10 @@
 
                 SaveWholeImage(pMeasure);
 
-                productionList.RemoveAll(p => p.Barcode == pMeasure.Barcode);
+                lock (productionLock)
+                {
+                    productionList.RemoveAll(p => p.Barcode == pMeasure.Barcode);
+                }
                 pMeasure.Dispose();
                 //LogAsync(DateTime.Now, $"{pMeasure.Barcode}浜у搧淇℃伅閲婃斁瀹屾垚", JsonConvert.SerializeObject(pMeasure));
             }
@@ -661,7 +738,7 @@
                     }
                     measurementUnitResult.MeasurementType = measurementUnit.MeasureType;
                     measurementUnitResult.MeasurementValue = measurementUnit.Spec.ActualValue == null ? "NA" : measurementUnit.Spec.ActualValue.Value.ToString(_precision);
-                    measurementUnitResult.MeasurementResult = measurementUnit.Spec.MeasureResult.Value ? "OK" : "NG";
+                    measurementUnitResult.MeasurementResult = measurementUnit.Spec.MeasureResult == null ? "NA" : measurementUnit.Spec.MeasureResult.Value ? "OK" : "NG";
 
                     measurementUnitResults.Add(measurementUnitResult);
 
@@ -1183,6 +1260,13 @@
                 var keys = Config.KeyUnitCollection.Where(u => u.IsEnabled && u.SnapshotPointId == snapshotId);
                 var keyBindCollection = measureList.SelectMany(u => u.KeyUnitCollection).Where(u => keys.Any(k => k.Key == u.Key)).ToList();
 
+                if (imgSet.HImage == null)
+                {
+                    LogAsync(DateTime.Now, $"{snapshotName}鏈幏鍙栨壂鎻忓浘鍍�", "");
+                    keyBindCollection.ForEach(k => k.FillKeyValues(null));
+                    return;
+                }
+
                 string toolKey = (opConfig as CameraOprerationConfigBase).AlgorithemPath;
 
                 HObject images = new HObject();

--
Gitblit v1.8.0