From beb0f2c826515c0a2abc9ee6eb296dd5d7199b3a Mon Sep 17 00:00:00 2001
From: xcd <834800634@qq.com>
Date: 星期一, 06 七月 2020 13:24:44 +0800
Subject: [PATCH] 机台暂停/继续操作

---
 src/Bro.M071.Process/M071Process.cs |  555 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 554 insertions(+), 1 deletions(-)

diff --git a/src/Bro.M071.Process/M071Process.cs b/src/Bro.M071.Process/M071Process.cs
index 33827af..52d0f48 100644
--- a/src/Bro.M071.Process/M071Process.cs
+++ b/src/Bro.M071.Process/M071Process.cs
@@ -1,11 +1,20 @@
-锘縰sing Bro.Common.Helper;
+锘縰sing Bro.Common.Base;
+using Bro.Common.Helper;
+using Bro.Common.Interface;
 using Bro.Common.Model;
+using Bro.M071.Model;
 using Bro.Process;
+using HalconDotNet;
 using System;
 using System.Collections.Generic;
+using System.ComponentModel;
+using System.Drawing;
+using System.IO;
 using System.Linq;
 using System.Text;
+using System.Threading;
 using System.Threading.Tasks;
+using static Bro.Common.Helper.EnumHelper;
 
 namespace Bro.M071.Process
 {
@@ -23,5 +32,549 @@
             get => IConfig as M071Config;
         }
         #endregion
+
+        #region 浜嬩欢
+        public event Action OnMeasureStart;
+        public event Action OnClearBarcode;
+        public event Action<IShapeElement> OnElementUpdated;
+        public event Action<MachineState> OnMachineStateChanged;
+        public event Action OnFullResetDone;
+        #endregion
+
+        public override void Open()
+        {
+            InitialSetting();
+
+            base.Open();
+
+            SwitchBeep(false);
+            SwitchLightGreen(false);
+            SwitchLightRed(false);
+            SwitchLightYellow(false);
+
+            Reset(null, null, null);
+            FullReset(null);
+        }
+
+        private void InitialSetting()
+        {
+            //鏁版嵁搴撹縼绉绘鏌�
+            DatabaseInitialize.Initialize();
+
+            MotionCardSettingCheck();
+
+            Config.SnapshotPointCollection.ForEach(u =>
+            {
+                u.GetHalconToolPathList().ForEach(path =>
+                {
+                    LoadHalconTool(path, u.Id);
+                });
+            });
+
+            Config.KeyUnitCollection.ForEach(u =>
+            {
+                if (!u.IsEnabled)
+                    return;
+
+                var snapshotPoint = Config.SnapshotPointCollection.FirstOrDefault(s => s.Id == u.SnapshotPointId && s.IsEnabled);
+                if (snapshotPoint == null)
+                    throw new ProcessException($"{u.AlignName}鏈缃彲鐢ㄦ媿鐓х偣浣�");
+
+                if (u.ImageSeq < 1)
+                    throw new ProcessException($"{u.AlignName}鍥剧墖搴忓彿灏忎簬1");
+
+                var algo = Config.KeyAlgorithemCollection.FirstOrDefault(a => a.Id == u.KeyAlgorithemId);
+                if (algo == null)
+                    throw new ProcessException($"{u.AlignName}鏈缃娴嬬畻娉�");
+
+                u.KeyAlgorithemPath = algo.AlgorithemPath;
+                LoadHalconTool(u.KeyAlgorithemPath, u.AlignName);
+
+                var resultSet = Config.KeyResultCollection.FirstOrDefault(r => r.Id == u.KeyResultId);
+                if (resultSet == null)
+                    throw new ProcessException($"{u.AlignName}鏈缃娴嬬粨鏋滈厤缃�");
+
+                u.KeyResultList = new List<string>(resultSet.Results);
+            });
+
+            Config.MeasurementUnitCollection.ForEach(u =>
+            {
+                if (!u.IsEnabled)
+                    return;
+
+                var spec = Config.MeasureTypeCollection.FirstOrDefault(s => s.Code == u.MeasureType);
+                if (spec == null)
+                    throw new ProcessException($"{u.GetDisplayText()}鏈缃娴嬬被鍨�");
+
+                u.Spec = new MeasureType();
+                u.Spec.DataFrom(spec);
+
+                LoadHalconTool(u.Spec.AlgorithemPath, u.Id);
+
+                u.KeyUnitCollection.ForEach(b =>
+                {
+                    var keyRespone = Config.KeyUnitCollection.Where(ku => ku.IsEnabled && ku.Key == b.Key).ToList();
+                    if (keyRespone.Count == 0)
+                        throw new ProcessException($"{u.GetDisplayText()}瀵瑰簲鐨勫崟閿笉瀛樺湪鎴栦笉鍙敤");
+
+                    if (b.KeyResultId == "All")
+                    {
+                        keyRespone.SelectMany(kr => kr.KeyResultList).ToList().ForEach(r =>
+                          {
+                              b.MeasureValueDict[r] = null;
+                          });
+                    }
+                    else
+                    {
+                        b.MeasureValueDict[b.KeyResultId] = null;
+                    }
+                });
+            });
+        }
+
+        #region 娴佺▼涓姏鍑哄紓甯�
+        public override void ExceptionRaisedInMonitor(Exception ex)
+        {
+            if (ex is ProcessException pEx)
+            {
+                if (pEx.Level >= ExceptionLevel.Fatal)
+                {
+                    RaisedAlarm(pEx.Message);
+                    MachineState = MachineState.Alarm;
+                }
+            }
+            else
+            {
+                RaisedAlarm(ex.Message);
+                MachineState = MachineState.Alarm;
+            }
+        }
+        #endregion
+
+        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 (MachineState != MachineState.Ready)
+                throw new ProcessException("鏈哄彴鏈氨缁紝璇峰嬁寮�濮嬫祴閲�", null, ExceptionLevel.Fatal);
+
+            if (string.IsNullOrWhiteSpace(BarCode))
+            {
+                OnClearBarcode?.Invoke();
+                throw new ProcessException("鏈緭鍏ヤ骇鍝佹潯鐮侊紝璇峰嬁寮�濮嬫祴閲�");
+            }
+
+            MachineState = MachineState.Running;
+            OnMeasureStart?.BeginInvoke(null, null);
+
+            var measurements = Config.MeasurementUnitCollection.Where(u => u.IsEnabled).ToList().DeepSerializeClone();
+            measurements.ForEach(m =>
+            {
+                m.InitialKeyUnitMeasureChanged();
+            });
+
+            var pMeasure = new ProductionMeasurement()
+            {
+                Barcode = BarCode,
+                Measurements = measurements,
+                StartTime = DateTime.Now,
+            };
+
+            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;
+
+            Config.SnapshotPointCollection.Where(u => u.IsEnabled).ToList().ForEach(s =>
+                  {
+                      _pauseHandle.WaitHandle.WaitOne();
+
+                      IDevice device = DeviceCollection.FirstOrDefault(u => u.Id == s.MotionOp.Device);
+                      if (device == null)
+                          throw new ProcessException($"{s.Name}鎷嶇収鐐逛綅鏈缃繍鍔ㄨ澶�");
+
+                      IMotionCard motionDevice = device as IMotionCard;
+
+                      if (motionDevice == null)
+                          throw new ProcessException($"{s.Name}鎷嶇収鐐逛綅璁剧疆{device.Name}涓嶆槸杩愬姩璁惧");
+
+                      var response = motionDevice.Run(s.MotionOp.OpConfig);
+                      if (!response.Result)
+                      {
+                          throw new ProcessException($"{device.Name}寮傚父锛寋response.Message}", null, ExceptionLevel.Fatal);
+                      }
+
+                      CameraBase camera = DeviceCollection.FirstOrDefault(u => u.Id == s.CameraOp.Device) as CameraBase;
+                      if (camera == null)
+                          return;
+
+                      string imgSetId = "";
+                      HImage hImage = null;
+                      try
+                      {
+                          hImage = CollectHImage(camera, s.CameraOp.OpConfig, out imgSetId);
+                      }
+                      catch (ProcessException pEx)
+                      {
+                          pEx.Level = ExceptionLevel.Fatal;
+                          throw pEx;
+                      }
+
+                      if (string.IsNullOrWhiteSpace(imgSetId))
+                      {
+                          return;
+                      }
+
+                      RunImageHandle(camera, s.CameraOp.OpConfig, hImage, s.Id, s.Name, pMeasure.Measurements);
+                  });
+
+            BarCode = "";
+            LogAsync(DateTime.Now, $"{pMeasure.Barcode}娴嬮噺鍔ㄤ綔瀹屾垚", "");
+
+            return new ProcessResponse(true);
+        }
+
+        #region 鍙屾墜鍚姩
+        bool isLeftStart = false;
+        bool IsLeftStart
+        {
+            get => isLeftStart;
+            set
+            {
+                isLeftStart = value;
+                StartCheck();
+            }
+        }
+
+        bool isRightStart = false;
+        bool IsRightStart
+        {
+            get => isRightStart;
+            set
+            {
+                isRightStart = value;
+                StartCheck();
+            }
+        }
+
+        private void StartCheck()
+        {
+            if (isRightStart && isLeftStart)
+            {
+                StartJob(null, null, null);
+            }
+        }
+
+        [ProcessMethod("", "Start_Left", "宸︽墜鍚姩", InvokeType.TestInvoke)]
+        public ProcessResponse Start_Left(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice)
+        {
+            if (opConfig.InputPara != null && opConfig.InputPara.Count > 0)
+            {
+                IsLeftStart = opConfig.InputPara[0] == 1;
+            }
+
+            return new ProcessResponse();
+        }
+
+        [ProcessMethod("", "Start_Right", "鍙虫墜鍚姩", InvokeType.TestInvoke)]
+        public ProcessResponse Start_Right(IOperationConfig opConfig, IDevice invokeDevice, IDevice sourceDevice)
+        {
+            if (opConfig.InputPara != null && opConfig.InputPara.Count > 0)
+            {
+                IsRightStart = opConfig.InputPara[0] == 1;
+            }
+
+            return new ProcessResponse();
+        }
+        #endregion
+
+        #region 绉佹湁鏂规硶
+        private void MeasureProduction_PropertyChanged(object sender, PropertyChangedEventArgs e)
+        {
+            if (sender is ProductionMeasurement pMeasure)
+            {
+                lock (pMeasure)
+                {
+                    //妫�鏌ユ槸鍚﹀叏閮ㄥ畬鎴�
+                    pMeasure.Measurements.ForEach(m =>
+                    {
+                        if (m.KeyUnitCollection.All(k => k.IsDone != null))
+                        {
+                            if (!m.IsUpdated)
+                            {
+                                if (m.KeyUnitCollection.Any(k => k.IsDone == false))
+                                {
+                                    m.Spec.ActualValue = -999;
+                                }
+                                else
+                                {
+                                    string toolKey = m.Id + "|" + m.Spec.AlgorithemPath;
+                                    if (!_halconToolDict.ContainsKey(toolKey))
+                                    {
+                                        LogAsync(DateTime.Now, $"{m.GetDisplayText()}{m.Spec.Code}绠楁硶鏈垵濮嬪寲", "");
+                                        m.Spec.ActualValue = -999;
+                                    }
+                                    else
+                                    {
+                                        var array = m.KeyUnitCollection.SelectMany(u => u.MeasureValueDict.Values.ToList().ConvertAll(v => v ?? -999)).ToArray();
+                                        _halconToolDict[toolKey].InputTupleDic["INPUT_Params"] = new HTuple(array);
+                                        if (!_halconToolDict[toolKey].RunProcedure(out string error))
+                                        {
+                                            LogAsync(DateTime.Now, $"{m.GetDisplayText()}{m.Spec.Code}绠楁硶寮傚父,{error}", "");
+                                            m.Spec.ActualValue = -999;
+                                        }
+                                        else
+                                        {
+                                            m.Spec.ActualValue = _halconToolDict[toolKey].GetResultTuple("OUTPUT_Result").D;
+                                            LogAsync(DateTime.Now, $"{m.GetDisplayText()}鏁版嵁{m.Spec.ActualValue}锛岀粨鏋渰(m.Spec.MeasureResult == null ? "TBD" : (m.Spec.MeasureResult == true ? "OK" : "NG"))}", "");
+                                        }
+                                    }
+                                }
+
+                                LogAsync(DateTime.Now, $"{m.GetDisplayText()}妫�娴嬬粨鏋�", $"{((m.Spec.MeasureResult ?? false) ? "OK" : "NG")}");
+
+                                IShapeElement indicator = null;
+                                pMeasure.ElementList.Add(indicator);
+                                //杈撳嚭鍥惧舰鍩哄厓鍒扮晫闈� todo
+                                OnElementUpdated?.BeginInvoke(indicator, null, null);
+
+                                SaveKeyImages(pMeasure.Barcode, m);
+
+                                m.IsUpdated = true;
+                            }
+                        }
+                    });
+
+                    if (!pMeasure.Measurements.All(m => m.IsUpdated))
+                    {
+                        return;
+                    }
+                }
+
+                pMeasure.EndTime = DateTime.Now;
+                bool pResult = pMeasure.Measurements.All(u => u.Spec.MeasureResult == true);
+                OnUpdateResult?.Invoke(DateTime.Now, pResult ? 1 : 0);
+                OnUpdateCT?.Invoke((float)(pMeasure.EndTime.Value - pMeasure.StartTime.Value).TotalSeconds);
+
+                LogAsync(DateTime.Now, $"{pMeasure.Barcode} 妫�娴嬪畬鎴愶紝缁撴灉 {(pResult ? "OK" : "NG")}", "");
+
+                if (MachineState == MachineState.Running)
+                    MachineState = MachineState.Ready;
+
+                //MES杈撳嚭 todo
+
+                //Excel鎶ヨ〃杈撳嚭 todo
+
+                //鏁版嵁搴撲繚瀛� todo
+
+                SaveWholeImage(pMeasure);
+
+                productionList.RemoveAll(p => p.Barcode == pMeasure.Barcode);
+                pMeasure.Dispose();
+            }
+        }
+
+        #region 鍥惧儚淇濆瓨
+        private void SaveWholeImage(ProductionMeasurement pMeasure)
+        {
+            try
+            {
+                Bitmap backImage = (Bitmap)Bitmap.FromFile(Config.BackgroundImagePath);
+
+                Bitmap map = new Bitmap(backImage.Width, backImage.Height);
+                using (Graphics g = Graphics.FromImage(map))
+                {
+                    g.DrawImage(backImage, new PointF(0, 0));
+
+                    pMeasure.ElementList.ForEach(e =>
+                    {
+                        e.Draw(g);
+                    });
+                }
+
+                string dir = Path.Combine(Config.ImageSaveFolder, "TopView", DateTime.Now.ToString("yyyyMMdd"));
+                if (!Directory.Exists(dir))
+                {
+                    Directory.CreateDirectory(dir);
+                }
+
+                map.Save(Path.Combine(dir, $"{pMeasure.Barcode}_{DateTime.Now.ToString("HHmmss")}.bmp"));
+            }
+            catch (Exception ex)
+            {
+                LogAsync(DateTime.Now, "鏁翠綋鍥剧墖淇濆瓨寮傚父", ex.GetExceptionMessage());
+            }
+        }
+
+        private void SaveKeyImages(string barCode, MeasurementUnit measureUnit)
+        {
+            string measureName = measureUnit.GetDisplayText();
+            if (Config.ImageSaveOption.IsSaveOriginImage)
+            {
+                measureUnit.KeyUnitCollection.ForEach(u => u.ImageSaveStatus++);
+
+                string dir = Path.Combine(Config.ImageSaveFolder, "Origin", DateTime.Now.ToString("yyyyMMdd"), barCode, measureUnit.MeasureType);
+                if (!Directory.Exists(dir))
+                {
+                    Directory.CreateDirectory(dir);
+                }
+
+                SaveKeyImages(measureUnit, measureName, dir);
+            }
+
+            string result = (measureUnit.Spec.MeasureResult ?? false) ? "OK" : "NG";
+            if (Config.ImageSaveOption.AddtionalSaveType.ToUpper().Contains(result))
+            {
+                measureUnit.KeyUnitCollection.ForEach(u => u.ImageSaveStatus++);
+
+                string dir = Path.Combine(Config.ImageSaveFolder, result, DateTime.Now.ToString("yyyyMMdd"), barCode, measureUnit.MeasureType);
+                if (!Directory.Exists(dir))
+                {
+                    Directory.CreateDirectory(dir);
+                }
+
+                SaveKeyImages(measureUnit, measureName, dir);
+            }
+        }
+
+        private async void SaveKeyImages(MeasurementUnit measureUnit, string measureName, string dir)
+        {
+            await Task.Run(() =>
+            {
+                measureUnit.KeyUnitCollection.ForEach(u =>
+                {
+                    int i = 0;
+                    u.KeyImages?.ForEach(image =>
+                    {
+                        string fileName = Path.Combine(dir, $"{measureName}_{u.Key}{(i == 0 ? "" : $"-{i}")}_{DateTime.Now.ToString("HHmmss")}.tiff");
+                        image.WriteImage("tiff", 0, fileName);
+                        i++;
+                    });
+
+                    u.ImageSaveStatus--;
+                });
+            });
+        }
+        #endregion
+
+        private async void RunImageHandle(CameraBase camera, IOperationConfig opConfig, HImage hImage, string snapshotId, string snapshotName, List<MeasurementUnit> measureList)
+        {
+            await Task.Run(() =>
+             {
+                 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();
+
+                 string toolKey = (opConfig as CameraOprerationConfigBase).AlgorithemPath;
+                 HObject images = hImage;
+
+                 if (!string.IsNullOrWhiteSpace(toolKey))
+                 {
+                     toolKey = snapshotId + "|" + toolKey;
+                     if (!_halconToolDict.ContainsKey(toolKey))
+                     {
+                         LogAsync(DateTime.Now, $"{snapshotName}鏈垵濮嬪寲鍙栧浘绠楁硶閰嶇疆", "");
+                         keyBindCollection.ForEach(k => k.FillKeyValues(null));
+                         return;
+                     }
+
+                     _halconToolDict[toolKey].InputImageDic["INPUT_Image"] = hImage;
+                     if (!_halconToolDict[toolKey].RunProcedure(out string error))
+                     {
+                         LogAsync(DateTime.Now, $"{snapshotName}鍙栧浘绠楁硶寮傚父锛寋error}", "");
+                         keyBindCollection.ForEach(k => k.FillKeyValues(null));
+                         return;
+                     }
+
+                     images = _halconToolDict[toolKey].GetResultObject("OUTPUT_Images");
+                 }
+
+                 HOperatorSet.CountObj(images, out HTuple count);
+
+                 if (count == 0)
+                 {
+                     LogAsync(DateTime.Now, $"{snapshotName}鍙栧浘绠楁硶鏈緭鍑哄浘鍍�", "");
+                     keyBindCollection.ForEach(k => k.FillKeyValues(null));
+                     return;
+                 }
+
+                 var excludeKeys = keys.Where(u => u.ImageSeq > count).ToList();
+                 if (excludeKeys.Count > 0)
+                 {
+                     LogAsync(DateTime.Now, $"{string.Join(" ", excludeKeys.Select(u => u.AlignName))}鏈湪鍥剧墖鑾峰彇搴忓垪涓�", "");
+                     keyBindCollection.Where(k => excludeKeys.Any(u => u.Key == k.Key)).ToList().ForEach(k =>
+                          {
+                              k.FillKeyValues(null);
+                          });
+                 }
+
+                 Parallel.For(1, count.I, (i) =>
+                 {
+                     HOperatorSet.SelectObj(images, out HObject image, i);
+
+                     keys.Where(u => u.ImageSeq == i).ToList().ForEach(k =>
+                         {
+                             Dictionary<string, double> resultDict = null;
+
+                             var keyBindList = keyBindCollection.Where(u => u.Key == k.Key).ToList();
+
+                             string keyToolKey = k.AlignName + "|" + k.KeyAlgorithemPath;
+                             if (!_halconToolDict.ContainsKey(keyToolKey))
+                             {
+                                 LogAsync(DateTime.Now, $"{k.AlignName}妫�娴嬬畻娉曟湭鍒濆鍖�", "");
+                             }
+                             else
+                             {
+                                 _halconToolDict[keyToolKey].InputImageDic["INPUT_Image"] = image;
+                                 if (!_halconToolDict[keyToolKey].RunProcedure(out string error))
+                                 {
+                                     LogAsync(DateTime.Now, $"{k.AlignName}妫�娴嬬畻娉曞紓甯革紝{error}", "");
+                                 }
+                                 else
+                                 {
+                                     var results = _halconToolDict[keyToolKey].GetResultTuple("OUTPUT_Results").HTupleToDouble();
+                                     if (results.Count == 0 || results.Any(u => u < 0))
+                                     {
+                                         LogAsync(DateTime.Now, $"{k.AlignName}妫�娴嬬粨鏋滃紓甯�", "");
+                                     }
+                                     else
+                                     {
+                                         resultDict = k.KeyResultList.ToDictionary(u => u, u =>
+                                              {
+                                                  int index = k.KeyResultList.IndexOf(u);
+                                                  return results[index];
+                                              });
+                                     }
+                                 }
+                             }
+
+                             keyBindList.ForEach(kb =>
+                             {
+                                 kb.KeyImages.Add(image.Clone() as HImage);
+                                 kb.FillKeyValues(resultDict);
+                             });
+                         });
+
+                     image.Dispose();
+                 });
+
+                 if (count.I != 1)
+                 {
+                     hImage?.Dispose();
+                     hImage = null;
+                 }
+             });
+        }
+        #endregion
     }
 }

--
Gitblit v1.8.0