From 4cb676856f0c791ffcbef569c0ca8195bf8b0938 Mon Sep 17 00:00:00 2001
From: wells.liu <wells.liu@broconcentric.com>
Date: 星期六, 04 七月 2020 18:05:47 +0800
Subject: [PATCH] 数据库操作

---
 src/Bro.M071.Process/M071Process.cs |  110 +++++++++++++++++++++++-------------------------------
 1 files changed, 47 insertions(+), 63 deletions(-)

diff --git a/src/Bro.M071.Process/M071Process.cs b/src/Bro.M071.Process/M071Process.cs
index 2667c4f..f76d19a 100644
--- a/src/Bro.M071.Process/M071Process.cs
+++ b/src/Bro.M071.Process/M071Process.cs
@@ -2,6 +2,7 @@
 using Bro.Common.Helper;
 using Bro.Common.Interface;
 using Bro.Common.Model;
+using Bro.M071.Model;
 using Bro.Process;
 using HalconDotNet;
 using System;
@@ -33,7 +34,7 @@
 
         #region 浜嬩欢
         public event Action OnMeasureStart;
-        public event Action<string> OnBarcodeChanged;
+        public event Action OnClearBarcode;
         public event Action<IShapeElement> OnElementUpdated;
         #endregion
 
@@ -42,10 +43,23 @@
             InitialSetting();
 
             base.Open();
+
+            CheckMachineState();
+        }
+
+        /// <summary>
+        /// 妫�鏌ヨ澶囩姸鎬�
+        /// </summary>
+        private void CheckMachineState()
+        {
+            //throw new NotImplementedException();
         }
 
         private void InitialSetting()
         {
+            //鏁版嵁搴撹縼绉绘鏌�
+            DatabaseInitialize.Initialize();
+
             Config.SnapshotPointCollection.ForEach(u =>
             {
                 u.GetHalconToolPathList().ForEach(path =>
@@ -100,7 +114,7 @@
                     if (keyRespone.Count == 0)
                         throw new ProcessException($"{u.GetDisplayText()}瀵瑰簲鐨勫崟閿笉瀛樺湪鎴栦笉鍙敤");
 
-                    if (b.KeyResult == "All")
+                    if (b.KeyResultId == "All")
                     {
                         keyRespone.SelectMany(kr => kr.KeyResultList).ToList().ForEach(r =>
                           {
@@ -109,7 +123,7 @@
                     }
                     else
                     {
-                        b.MeasureValueDict[b.KeyResult] = null;
+                        b.MeasureValueDict[b.KeyResultId] = null;
                     }
                 });
             });
@@ -140,25 +154,16 @@
         //}
         #endregion
 
-        private string barCode = "";
-        public string BarCode
-        {
-            get => barCode;
-            set
-            {
-                if (barCode != value)
-                {
-                    barCode = value;
-                    OnBarcodeChanged?.Invoke(value);
-                }
-            }
-        }
+        public string BarCode { get; set; }
+
+        List<ProductionMeasurement> productionList = new List<ProductionMeasurement>();
 
         [ProcessMethod("", "StartJob", "寮�濮嬫壂鎻�", InvokeType.TestInvoke)]
         public ProcessResponse StartJob(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice)
         {
             if (string.IsNullOrWhiteSpace(BarCode))
             {
+                OnClearBarcode?.Invoke();
                 throw new ProcessException("鏈緭鍏ヤ骇鍝佹潯鐮侊紝璇峰嬁寮�濮嬫祴閲�");
             }
 
@@ -176,6 +181,16 @@
                 Measurements = measurements,
             };
 
+            var existedProduction = productionList.FirstOrDefault(u => u.Barcode == pMeasure.Barcode);
+            if (existedProduction != null)
+            {
+                productionList.Remove(existedProduction);
+                existedProduction.Dispose();
+                existedProduction = null;
+            }
+
+            productionList.Add(pMeasure);
+
             pMeasure.InitialMeasurementsPropertyChanged();
             pMeasure.PropertyChanged += MeasureProduction_PropertyChanged;
 
@@ -183,27 +198,24 @@
                   {
                       _pauseHandle.WaitHandle.WaitOne();
 
-                      IDevice device = DeviceCollection.FirstOrDefault(u => u.Id == s.MotionDevice);
+                      IDevice device = DeviceCollection.FirstOrDefault(u => u.Id == s.MotionOp.Device);
                       if (device == null)
                           throw new ProcessException($"{s.Name}鎷嶇収鐐逛綅鏈缃繍鍔ㄨ澶�");
 
-                      IMotion motionDevice = device as IMotion;
+                      IMotionCard motionDevice = device as IMotionCard;
 
                       if (motionDevice == null)
                           throw new ProcessException($"{s.Name}鎷嶇収鐐逛綅璁剧疆{device.Name}涓嶆槸杩愬姩璁惧");
 
-                      if (!motionDevice.MoveToPoint(null))
+                      var response = motionDevice.Run(s.MotionOp.OpConfig);
+                      if (!response.Result)
                       {
-                          throw new ProcessException("杩愬姩涓", null, ExceptionLevel.Info);
+                          throw new ProcessException($"{device.Name}寮傚父锛寋response.Message}", null, ExceptionLevel.Info);
                       }
 
                       CameraBase camera = DeviceCollection.FirstOrDefault(u => u.Id == s.CameraOp.Device) as CameraBase;
                       if (camera == null)
                           return;
-
-                      //IImageSet imgSet = camera.Snapshot(s.CameraOp.OpConfig);
-                      //if (imgSet == null)
-                      //    return;
 
                       HImage hImage = CollectHImage(camera, s.CameraOp.OpConfig, out string imgSetId);
                       if (string.IsNullOrWhiteSpace(imgSetId))
@@ -215,40 +227,8 @@
                   });
 
             BarCode = "";
+
             return new ProcessResponse(true);
-        }
-
-        /// <summary>
-        /// 鏆傚仠鏍囧織  
-        /// WaitHandle 鏆傚仠鍙ユ焺  榛樿涓洪潪闃诲 鍙墽琛�
-        /// WaitResult 鏆傚仠鏍囧織 true 姝e父鎵ц  false 鏆傚仠涓�
-        /// </summary>
-        ManualWaitConfirm _pauseHandle = new ManualWaitConfirm()
-        {
-            WaitHandle = new ManualResetEvent(true),
-            WaitResult = true,
-        };
-
-        [ProcessMethod("", "PauseJob", "鏆傚仠娴佺▼", InvokeType.TestInvoke)]
-        public ProcessResponse PauseJob(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice)
-        {
-            if (_pauseHandle.WaitResult)
-            {
-                #region 鏉垮崱鏆傚仠鍔ㄤ綔
-                #endregion
-
-                _pauseHandle.WaitHandle.Reset();
-            }
-            else
-            {
-                #region 鏉垮崱鎭㈠鍔ㄤ綔
-                #endregion
-
-                _pauseHandle.WaitHandle.Set();
-            }
-
-            _pauseHandle.WaitResult = !_pauseHandle.WaitResult;
-            return new ProcessResponse(_pauseHandle.WaitResult);
         }
 
         #region 绉佹湁鏂规硶
@@ -294,8 +274,12 @@
                                     }
                                 }
 
+                                LogAsync(DateTime.Now, $"{m.GetDisplayText()}妫�娴嬬粨鏋�", $"{((m.Spec.MeasureResult ?? false) ? "OK" : "NG")}");
+
+                                IShapeElement indicator = null;
+                                pMeasure.ElementList.Add(indicator);
                                 //杈撳嚭鍥惧舰鍩哄厓鍒扮晫闈� todo
-                                OnElementUpdated?.BeginInvoke(null, null, null);
+                                OnElementUpdated?.BeginInvoke(indicator, null, null);
 
                                 SaveKeyImages(pMeasure.Barcode, m);
 
@@ -309,8 +293,6 @@
                         return;
                     }
 
-
-
                     //MES杈撳嚭 todo
 
                     //Excel鎶ヨ〃杈撳嚭 todo
@@ -318,6 +300,8 @@
                     //鏁版嵁搴撲繚瀛� todo
 
                     SaveWholeImage(pMeasure);
+
+                    productionList.RemoveAll(p => p.Barcode == pMeasure.Barcode);
                     pMeasure.Dispose();
                 }
             }
@@ -355,7 +339,7 @@
             }
         }
 
-        private void SaveKeyImages(string barCode, MeasurementUint measureUnit)
+        private void SaveKeyImages(string barCode, MeasurementUnit measureUnit)
         {
             string measureName = measureUnit.GetDisplayText();
             if (Config.ImageSaveOption.IsSaveOriginImage)
@@ -386,7 +370,7 @@
             }
         }
 
-        private async void SaveKeyImages(MeasurementUint measureUnit, string measureName, string dir)
+        private async void SaveKeyImages(MeasurementUnit measureUnit, string measureName, string dir)
         {
             await Task.Run(() =>
             {
@@ -406,7 +390,7 @@
         }
         #endregion
 
-        private async void RunImageHandle(CameraBase camera, IOperationConfig opConfig, HImage hImage, string snapshotId, string snapshotName, List<MeasurementUint> measureList)
+        private async void RunImageHandle(CameraBase camera, IOperationConfig opConfig, HImage hImage, string snapshotId, string snapshotName, List<MeasurementUnit> measureList)
         {
             await Task.Run(() =>
              {

--
Gitblit v1.8.0