using Bro.Common.Base; using Bro.Common.Helper; using Bro.Common.Interface; using Bro.Common.Model; using Bro.M135.Common; using HalconDotNet; using Microsoft.VisualBasic; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Sunny.UI; using System.Data; using System.Data.SqlTypes; using System.Diagnostics; using System.Net; using System.Text; using System.Text.Encodings.Web; using System.Windows.Forms; using System.Xml.Linq; using static System.Net.Mime.MediaTypeNames; using static System.Net.WebRequestMethods; using static System.Windows.Forms.VisualStyles.VisualStyleElement.Tab; namespace Bro.M141.Process { public partial class M141Process { Dictionary> dicdate = new Dictionary>(); ManualResetEvent set1 = new ManualResetEvent(false); ManualResetEvent set2 = new ManualResetEvent(false); [ProcessMethod("ImageCheck", "ImageCheckOperation", "通用图片检测操作", InvokeType.TestInvoke)] public ResponseMessage ImageCheckOperation(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice) { ResponseMessage msg = new ResponseMessage(); if (config is IImageCheckOperationConfig opConfig) { var results = opConfig.Products.Select(u => { DetectResult result = new DetectResult(); result.PID = u.PID; result.Specs = GetSpecListFromConfigSelection(opConfig.SpecCollection); return result; }).ToList(); msg.DataObj = results; var tool = GetHalconTool(null, "", opConfig.AlgorithemPath); Tuple, Dictionary, string, int> ret = null; if (opConfig.BDstate) { List bdlist = M141Config.BDCollection.FirstOrDefault(u => u.CameraId == opConfig.ExecuteDevice && u.CheckIndex == opConfig.BDindex)?.BDList; if (bdlist == null) { LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"未找到标定配置,序号{opConfig.BDindex}"); return msg; } ret = tool.RunProcedure(new Dictionary() { { "INPUT_CalibrationData", bdlist.ToArray() } }, new Dictionary() { { "INPUT_Image", opConfig.ImageSet.HImage } }, new List() { "OUTPUT_Results_1", "OUTPUT_Results_2" }, null); } else { ret = tool.RunProcedure(null, new Dictionary() { { "INPUT_Image", opConfig.ImageSet.HImage } }, new List() { "OUTPUT_Results_1", "OUTPUT_Results_2" }, null); } //var ret = tool.RunProcedure(null, new Dictionary() { { "INPUT_Image", opConfig.ImageSet.HImage } }, new List() { "OUTPUT_Results_1", "OUTPUT_Results_2" }, null); if (!ret.Item1) { LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"脚本{opConfig.AlgorithemPath}运行异常,{ret.Item4}"); } else { //for (int i = 1; i <= 2; i++) //{ // var p = opConfig.Products.FirstOrDefault(u => u.SEQUENCE.EndsWith(i.ToString())); // if (p != null) // { // var pResult = results.FirstOrDefault(u => u.PID == p.PID); // var datas = ret.Item2[$"OUTPUT_Results_{i}"].HTupleToDouble(); // LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"当前脚本结果OUTPUT_Results_{i}:{string.Join(",", datas.Select(u => u.ToString("f3")))}"); // if (pResult != null && datas.Count > 0) // { // FillSpecResults(pResult.PID, pResult.Specs, datas); // } // } //} opConfig.Products.ForEach(p => { var pResult = results.FirstOrDefault(u => u.PID == p.PID); string i = p.SEQUENCE[p.SEQUENCE.Length - 1].ToString(); var datas = ret.Item2[$"OUTPUT_Results_{i}"].HTupleToDouble(); LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"当前脚本结果OUTPUT_Results_{i}:{string.Join(",", datas.Select(u => u.ToString("f3")))}"); if (pResult != null && datas.Count > 0) { FillSpecResults(pResult.PID, pResult.Specs, datas, p.SEQUENCE); } }); } } return msg; } protected List GetSpecListFromConfigSelection(List specSelectors) { List specList = new List(); specSelectors.ForEach(u => { var spec = M141Config.SpecCollection.FirstOrDefault(s => s.Code == u.SpecCode); if (spec != null) { var temp = spec.Copy(); temp.ActualValue = null; temp.MeasureResult = null; specList.Add(temp); } }); return specList; } protected void FillSpecResults(string pid, List detectSpec, List results, string SEQUENCE) { detectSpec.ForEach(s => { if (!s.IsEnabled) return; if (results.Count > s.OutputIndex) { s.ActualValue = results[s.OutputIndex]; if (s.IsEnableCompensation && s.CompensationValue.Count > 1) { string index1 = SEQUENCE.Split('_')[SEQUENCE.Split('_').Count() - 1]; if (index1 == "2") { s.ActualValue += s.CompensationValue[1]; } else { s.ActualValue += s.CompensationValue[0]; } } LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"产品{pid}检测项{s.Code}赋值{s.GetMeasureValueStr()},结果{s.GetMeasureResultStr()}"); } else { s.ActualValue = null; LogAsync(DateTime.Now, EnumHelper.LogLevel.Error, $"产品{pid}检测项{s.Code}未能赋值,结果{s.GetMeasureResultStr()}"); } }); } protected List GenerateSpecByMeasureItems(List measureItems) { List measureSpecs = new List(); measureItems.ForEach(m => { var originSpec = M141Config.SpecCollection.FirstOrDefault(u => u.Code == m.SpecCode); if (originSpec == null) { LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"{m.SpecCode}设置的检测标准未能在配置中获取"); return; } if (m.IsSubscribeSpecForPoints) { int index = 0; m.MeasurePointNameCollection.ForEach(mp => { var spec_p = originSpec.Copy(); spec_p.Code = mp.MeasurePointName; spec_p.OutputIndex = index; measureSpecs.Add(spec_p); index++; }); } else { var spec = originSpec.Copy(); if (!string.IsNullOrWhiteSpace(m.FAIName)) { spec.Code = m.FAIName; } measureSpecs.Add(spec); } }); return measureSpecs; } DateTime? _ct = null; string _csvHead = ""; List _specHeadList = null; List _positionList = new List(); private void UpdateProductResult(ProductModel p, out bool isOK) { isOK = false; int waitTimes = 1; if (p.Details.Any(u => u.IsDone == false) && waitTimes > 0) { waitTimes--; } if (waitTimes == 0) { var unfinishedPositions = p.Details.Where(u => u.IsDone == false).Select(u => u.PositionName).ToList(); if (unfinishedPositions.Count > 0) { LogAsync(DateTime.Now, EnumHelper.LogLevel.Error, $"产品{string.Join(",", unfinishedPositions)}站点结果未完成"); } } p.InitialDetailSpecs(); var resultList = p.Details.SelectMany(u => u.ResultList).ToList(); resultList.ForEach(u => u.SetResult()); var defects = resultList.GetDefectDescList(); var ngSpecCodes = p.Details.SelectMany(u => u.SpecList ?? new List()).Where(u => u.MeasureResult != true).Select(u => u.Code); var ngDefectDescList = p.Details.SelectMany(u => u.DefectList ?? new List()).ToList(); defects.AddRange(ngSpecCodes); defects.AddRange(ngDefectDescList); if (p.Details.Any(u => !u.IsDone)) { defects.Add("TBD"); } defects = defects.Distinct().ToList(); isOK = defects.Count <= 0; UpdateDefectAsync(defects); var defectClass = GetDefectClassFromDefectList(defects); UpdateResult(DateTime.Now, p.SN, defectClass.ClassName, ""); List specList = new List(); specList.AddRange(p.Details.SelectMany(u => u.SpecList).ToList().ConvertAll(u => (ISpec)u)); specList.AddRange(p.Details.SelectMany(u => u.ResultList.SelectMany(d => d.Specs))); //ReplyRealTimeData(p.DataTag as List, specList); p.Result = defectClass.ClassName; p.EndTime = DateTime.Now; LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"产品{p.PID} {p.SEQUENCE} {p.SN}检测结果{p.Result},缺陷明细:{string.Join(",", defects)}"); //} //catch (Exception e) //{ // LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, e.ToString()); //} } private void UpdateProductResultAsync(ProductModel p) { UpdateProductResult(p, out bool isOK); //_taskFactory.StartNew(() => //{ // if (M141Config.IsEnableOEEDataUpload) // { // OEEDataUpload(M141Config.OEEConfig_WorkPlace, p.SN, p.Details.SelectMany(u => u.ResultList).ToList()); // } //}); _taskFactory.StartNew(() => { if (string.IsNullOrWhiteSpace(_csvHead)) { _csvHead = p.GetCSVHead(ref _specHeadList, ref _positionList); } CSVRecordAsync($"ProductRecord_{DateTime.Now.ToString("yyyyMMdd")}.csv", p.GetCSVData(_specHeadList, _positionList), _csvHead); //_manager_P_Product.UpdateProductResult(p.ID, p.PID, p.SN, p.Result); }); } } }