领胜LDS 键盘AOI检测项目
wells.liu
2020-07-06 f9c7928bff92596686e05a15fef21d499e954088
src/Bro.M071.Process/M071Process.cs
@@ -14,6 +14,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static Bro.Common.Helper.EnumHelper;
namespace Bro.M071.Process
{
@@ -36,6 +37,8 @@
        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()
@@ -44,21 +47,21 @@
            base.Open();
            CheckMachineState();
        }
            SwitchBeep(false);
            SwitchLightGreen(false);
            SwitchLightRed(false);
            SwitchLightYellow(false);
        /// <summary>
        /// 检查设备状态
        /// </summary>
        private void CheckMachineState()
        {
            //throw new NotImplementedException();
            Reset(null, null, null);
            FullReset(null);
        }
        private void InitialSetting()
        {
            //数据库迁移检查
            DatabaseInitialize.Initialize();
            MotionCardSettingCheck();
            Config.SnapshotPointCollection.ForEach(u =>
            {
@@ -129,29 +132,23 @@
            });
        }
        #region InitialHalconTool
        //protected override void InitialHalconTool()
        //{
        //    base.InitialHalconTool();
        //    Config.SnapshotPointCollection.ForEach(u =>
        //    {
        //        u.GetHalconToolPathList().ForEach(path =>
        //        {
        //            if (!string.IsNullOrWhiteSpace(path))
        //            {
        //                string directoryPath = Path.GetDirectoryName(path);
        //                string fileName = Path.GetFileNameWithoutExtension(path);
        //                HDevEngineTool tool = new HDevEngineTool(directoryPath);
        //                tool.LoadProcedure(fileName);
        //                //使用“|”作为间隔符
        //                _halconToolDict[u.Id + "|" + path] = tool;
        //            }
        //        });
        //    });
        //}
        #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; }
@@ -161,12 +158,16 @@
        [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();
@@ -179,6 +180,7 @@
            {
                Barcode = BarCode,
                Measurements = measurements,
                StartTime = DateTime.Now,
            };
            var existedProduction = productionList.FirstOrDefault(u => u.Barcode == pMeasure.Barcode);
@@ -210,14 +212,25 @@
                      var response = motionDevice.Run(s.MotionOp.OpConfig);
                      if (!response.Result)
                      {
                          throw new ProcessException($"{device.Name}异常,{response.Message}", null, ExceptionLevel.Info);
                          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;
                      HImage hImage = CollectHImage(camera, s.CameraOp.OpConfig, out string imgSetId);
                      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;
@@ -227,9 +240,64 @@
                  });
            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)
@@ -292,18 +360,28 @@
                    {
                        return;
                    }
                    //MES输出 todo
                    //Excel报表输出 todo
                    //数据库保存 todo
                    SaveWholeImage(pMeasure);
                    productionList.RemoveAll(p => p.Barcode == pMeasure.Barcode);
                    pMeasure.Dispose();
                }
                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();
            }
        }