领胜LDS 键盘AOI检测项目
xcd
2020-08-01 c44c1b442fde596c1d20c0ad82e4d308f71ec806
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,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();
                }
                //添加到Setting.json
                SettingHelper.AddNewProductionCode(newProductionCode);
            }
            catch (Exception ex)
            {
                throw new ProcessException(ex.Message, null);
            }
        }
        private void SaveBackupConfig()
        {
            string backPath = Path.GetDirectoryName(_configPath);
@@ -398,7 +429,7 @@
        #endregion
        #region Halcon算子设置
        private void InitialHalconTool()
        protected virtual void InitialHalconTool()
        {
            foreach (HDevEngineTool tool in _halconToolDict.Values)
            {
@@ -413,20 +444,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 +515,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 +526,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 +587,8 @@
                        }
                        LogAsync(DateTime.Now, methodCode + "异常信息", ex.GetExceptionMessage());
                        ExceptionRaisedInMonitor(ex);
                    }
                }
@@ -568,15 +609,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 +681,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 +694,9 @@
        #endregion
        #region 报警和DownTime
        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 +725,11 @@
            OnAlarmUpdate?.BeginInvoke(alarmMsg, null, null);
        }
        public virtual void RaisedAlarm(string alarmMsg)
        {
            OnAlarmUpdate?.Invoke(alarmMsg);
        }
        #endregion
        #region ILogger