From 1f2fa3f54d4a7b5a2f601c4c947a06476342e3bd Mon Sep 17 00:00:00 2001
From: patrick.xu <patrick.xu@broconcentric.com>
Date: 星期五, 22 一月 2021 09:23:06 +0800
Subject: [PATCH] 1. gocator驱动将system.stop移到system.ReceiveData之后 2. 修改测量完成后状态切换逻辑,避免安全光线等干扰 3. 添加设备异常输出日志记录 4. 产品结果输出时做硬盘文件记录

---
 src/Bro.Process/ProcessControl.cs |   87 +++++++++++++++++++++++++++++++++++++------
 1 files changed, 75 insertions(+), 12 deletions(-)

diff --git a/src/Bro.Process/ProcessControl.cs b/src/Bro.Process/ProcessControl.cs
index 0edf765..9519754 100644
--- a/src/Bro.Process/ProcessControl.cs
+++ b/src/Bro.Process/ProcessControl.cs
@@ -45,7 +45,6 @@
 
         #region Event
         public event Action<string, Bitmap, string> OnBitmapOutput;
-        //public event Action<string, object> OnObjectOutput;
         public Action<DateTime, Exception> OnExceptionOccured { get; set; }
         public event Action<DeviceState> OnProcessStateChanged;
         public event Action<DateTime, string, string> OnLog;
@@ -209,10 +208,17 @@
                     d.OnLog -= OnDeviceLog;
                     d.OnLog += OnDeviceLog;
 
+                    d.OnExceptionOccured = OnDeviceExceptionRaised;
+
                     d.StateChange(DeviceState.DSInit);
                     d.StateChange(DeviceState.DSOpen);
                 }
             });
+        }
+
+        private void OnDeviceExceptionRaised(DateTime dt, Exception ex)
+        {
+            LogAsync(dt, "璁惧寮傚父", ex.GetExceptionMessage());
         }
         #endregion
 
@@ -342,6 +348,37 @@
             }
         }
 
+        public void CreateNewConfig(IProcessConfig config, string newProductionCode)
+        {
+            try
+            {
+                if (config == null)
+                    throw new ProcessException("淇濆瓨鐨勯厤缃俊鎭笉鑳戒负绌�");
+
+                string newConfigPath = Path.Combine(Path.GetDirectoryName(_configPath), $"Config_{newProductionCode}.json");
+                if (File.Exists(newConfigPath))
+                {
+                    throw new ProcessException($"{newProductionCode}閰嶇疆鏂囦欢宸茬粡瀛樺湪");
+                }
+
+                //鐢熸垚config.json
+                string newConfig = JsonConvert.SerializeObject(config, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All });
+                using (StreamWriter writer = new StreamWriter(newConfigPath, false, System.Text.Encoding.UTF8))
+                {
+                    writer.Write(newConfig);
+                    writer.Flush();
+                    writer.Close();
+                }
+
+                //娣诲姞鍒癝etting.json
+                SettingHelper.AddNewProductionCode(newProductionCode);
+            }
+            catch (Exception ex)
+            {
+                throw new ProcessException(ex.Message, null);
+            }
+        }
+
         private void SaveBackupConfig()
         {
             string backPath = Path.GetDirectoryName(_configPath);
@@ -440,14 +477,14 @@
         /// <param name="config">鎿嶄綔閰嶇疆锛岀敤鏉ョ‘璁ょ洃鍚潵婧愬拰绠楁硶璺緞</param>
         /// <param name="algorithemPath">绠楁硶璺緞锛岄粯璁ゆ槸閰嶇疆涓殑绗竴涓畻娉曡矾寰�</param>
         /// <returns>Halcon绠楁硶</returns>
-        protected HDevEngineTool GetHalconTool(IOperationConfig config, string algorithemPath = "")
+        protected HDevEngineTool GetHalconTool(IOperationConfig config, string monitorSetId = "", string algorithemPath = "")
         {
             if (string.IsNullOrWhiteSpace(algorithemPath))
             {
                 algorithemPath = (config as IHalconToolPath)?.GetHalconToolPathList()[0];
             }
 
-            string key = config.MonitorSetId + "|" + algorithemPath;
+            string key = (string.IsNullOrWhiteSpace(monitorSetId) ? config.MonitorSetId : monitorSetId) + "|" + algorithemPath;
             if (_halconToolDict.ContainsKey(key))
             {
                 return _halconToolDict[key];
@@ -484,7 +521,7 @@
         #endregion
 
         #region IMonitor鐩戝惉
-        protected virtual void OnMonitorInvoke(DateTime dt, string deviceId, IDevice sourceDevice, MonitorSet monitorSet)
+        protected virtual void OnMonitorInvoke(DateTime dt, string deviceId, IDevice sourceDevice, IMonitorSet monitorSet)
         {
             string methodCode = monitorSet.MethodCode;
 
@@ -495,11 +532,14 @@
                 return;
             }
 
+            if (!ProcessInvokePreCheck())
+                return;
+
             IOperationConfig config = monitorSet.OpConfig;
             object res = null;
             int reTryTimes = config.ReTryTimes;
             IDevice device = DeviceCollection.FirstOrDefault(u => u.Id == deviceId);
-            LogAsync(DateTime.Now, $"{device.Name}璋冪敤{methodCode}寮�濮�", "");
+            LogAsync(DateTime.Now, $"{device?.Name}璋冪敤{methodCode}寮�濮�", "");
 
             Stopwatch sw = new Stopwatch();
             sw.Start();
@@ -553,6 +593,8 @@
                         }
 
                         LogAsync(DateTime.Now, methodCode + "寮傚父淇℃伅", ex.GetExceptionMessage());
+
+                        ExceptionRaisedInMonitor(ex);
                     }
                 }
 
@@ -573,13 +615,30 @@
             #endregion
 
             sw.Stop();
-            LogAsync(DateTime.Now, $"{device.Name}璋冪敤{methodCode}瀹屾垚锛岃�楁椂{sw.ElapsedMilliseconds}ms", "");
-            TimeRecordCSV(DateTime.Now, device.Name, $"{methodCode}璋冪敤瀹屾垚", (int)sw.ElapsedMilliseconds);
+            LogAsync(DateTime.Now, $"{device?.Name}璋冪敤{methodCode}瀹屾垚锛岃�楁椂{sw.ElapsedMilliseconds}ms", "");
+            TimeRecordCSV(DateTime.Now, device?.Name, $"{methodCode}璋冪敤瀹屾垚", (int)sw.ElapsedMilliseconds);
+        }
+
+        /// <summary>
+        /// 娴佺▼鎵ц鐨勯妫�鏌�
+        /// </summary>
+        /// <returns></returns>
+        public virtual bool ProcessInvokePreCheck()
+        {
+            return true;
+        }
+
+        /// <summary>
+        /// 鐩戝惉娴佺▼涓紓甯告姏鍑� 鍦ㄥ叿浣撲笟鍔′腑鍏蜂綋澶勭悊
+        /// </summary>
+        /// <param name="ex">鐩戝惉娴佺▼涓紓甯稿璞�</param>
+        public virtual void ExceptionRaisedInMonitor(Exception ex)
+        {
         }
         #endregion
 
         #region 鍥惧儚澶勭悊
-        protected HImage CollectHImage(CameraBase camera, IOperationConfig opConfig, out string imgSetId, [CallerMemberName]string methodCode = "")
+        protected IImageSet CollectHImage(CameraBase camera, IOperationConfig opConfig, [CallerMemberName] string methodCode = "")
         {
             IImageSet set = null;
 
@@ -628,8 +687,7 @@
                 TimeRecordCSV(DateTime.Now, camera.Name, methodCode + "閲囧浘", (int)sw.ElapsedMilliseconds);
             }
 
-            imgSetId = set?.Id;
-            return set.HImage;
+            return set;
         }
 
         protected async void CameraUpdateImage(CameraBase camera, Bitmap image, string setId)
@@ -642,9 +700,9 @@
         #endregion
 
         #region 鎶ヨ鍜孌ownTime
-        ObservableCollection<WarningSet> _warningRemains = new ObservableCollection<WarningSet>();
+        ObservableCollection<IWarningSet> _warningRemains = new ObservableCollection<IWarningSet>();
 
-        protected virtual void OnMonitorAlarm(DateTime dt, IDevice device, WarningSet warning)
+        protected virtual void OnMonitorAlarm(DateTime dt, IDevice device, IWarningSet warning)
         {
             if (warning.CurrentStatus)
             {
@@ -673,6 +731,11 @@
 
             OnAlarmUpdate?.BeginInvoke(alarmMsg, null, null);
         }
+
+        public virtual void RaisedAlarm(string alarmMsg)
+        {
+            OnAlarmUpdate?.Invoke(alarmMsg);
+        }
         #endregion
 
         #region ILogger

--
Gitblit v1.8.0