From 9a2f369bcee572ba086ca83d76a562a0ecaa587b Mon Sep 17 00:00:00 2001
From: xcd <834800634@qq.com>
Date: 星期四, 16 七月 2020 17:22:09 +0800
Subject: [PATCH] 板卡修复回原点bug 主流程注释部分log信息,添加部分图片对象主动释放操作 修复Excel输出Alignment最后一项第一次无数据bug 添加平面补偿

---
 src/Bro.Process/ProcessControl.cs |  102 +++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 79 insertions(+), 23 deletions(-)

diff --git a/src/Bro.Process/ProcessControl.cs b/src/Bro.Process/ProcessControl.cs
index 1c146c5..ca587d2 100644
--- a/src/Bro.Process/ProcessControl.cs
+++ b/src/Bro.Process/ProcessControl.cs
@@ -21,7 +21,7 @@
 using System.Threading.Tasks;
 using static Bro.Common.Helper.EnumHelper;
 
-namespace Bro.Process
+namespace Bro.Process  
 {
     [Process("", DeviceAttributeType.Device)]
     public partial class ProcessControl : IProcess
@@ -342,6 +342,31 @@
             }
         }
 
+        public void CreateNewConfig(IProcessConfig config, string newProductionCode)
+        {
+            try
+            {
+                if (config == null)
+                    throw new ProcessException("淇濆瓨鐨勯厤缃俊鎭笉鑳戒负绌�");
+                ProductionCode = newProductionCode;
+                //鐢熸垚config.json
+                string newConfig = JsonConvert.SerializeObject(config, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All });
+                using (StreamWriter writer = new StreamWriter(_configPath, false, System.Text.Encoding.UTF8))
+                {
+                    writer.Write(newConfig);
+                    writer.Flush();
+                    writer.Close();
+                }
+
+                //娣诲姞鍒癝etting.json
+                SettingHelper.AddNewProductionCode(ProductionCode);
+            }
+            catch (Exception ex)
+            {
+                throw new ProcessException(ex.Message, null);
+            }
+        }
+
         private void SaveBackupConfig()
         {
             string backPath = Path.GetDirectoryName(_configPath);
@@ -398,7 +423,7 @@
         #endregion
 
         #region Halcon绠楀瓙璁剧疆
-        private void InitialHalconTool()
+        protected virtual void InitialHalconTool()
         {
             foreach (HDevEngineTool tool in _halconToolDict.Values)
             {
@@ -413,20 +438,25 @@
                 {
                     toolPath.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[monitorSet.Id + "|" + path] = tool;
-                        }
+                        LoadHalconTool(path, monitorSet.Id);
                     });
                 }
             });
+        }
+
+        protected void LoadHalconTool(string algorithemPath, string prefix)
+        {
+            if (!string.IsNullOrWhiteSpace(algorithemPath))
+            {
+                string directoryPath = Path.GetDirectoryName(algorithemPath);
+                string fileName = Path.GetFileNameWithoutExtension(algorithemPath);
+
+                HDevEngineTool tool = new HDevEngineTool(directoryPath);
+                tool.LoadProcedure(fileName);
+
+                //浣跨敤鈥渱鈥濅綔涓洪棿闅旂
+                _halconToolDict[prefix + "|" + algorithemPath] = tool;
+            }
         }
 
         /// <summary>
@@ -479,7 +509,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;
 
@@ -490,11 +520,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();
@@ -548,6 +581,8 @@
                         }
 
                         LogAsync(DateTime.Now, methodCode + "寮傚父淇℃伅", ex.GetExceptionMessage());
+
+                        ExceptionRaisedInMonitor(ex);
                     }
                 }
 
@@ -568,15 +603,32 @@
             #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 = "")
         {
-            ImageSet set = null;
+            IImageSet set = null;
 
             if (IConfig.IsImageOffline)
             {
@@ -623,8 +675,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)
@@ -637,9 +688,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)
             {
@@ -668,6 +719,11 @@
 
             OnAlarmUpdate?.BeginInvoke(alarmMsg, null, null);
         }
+
+        public virtual void RaisedAlarm(string alarmMsg)
+        {
+            OnAlarmUpdate?.Invoke(alarmMsg);
+        }
         #endregion
 
         #region ILogger

--
Gitblit v1.8.0