using Bro.Common.Base; using Bro.Common.Helper; using Bro.Common.Interface; using Bro.Common.Model; using Bro.M135.Common; using Bro.M141.Process; using HalconDotNet; using NPOI.SS.Formula.Functions; using NPOI.XSSF.Streaming.Values; using System.Collections.Concurrent; using System.Net.Sockets; using System.Text.RegularExpressions; using static Org.BouncyCastle.Crypto.Engines.SM2Engine; using static Org.BouncyCastle.Math.EC.ECCurve; using static ScottPlot.Plottable.PopulationPlot; namespace Bro.M141_AOI1.Process { [Process("AOI1", EnumHelper.DeviceAttributeType.Device)] public class AOI1Process : M141Process { #region constructor public AOI1Process() : base() { } public AOI1Process(string productCode) : base(productCode) { } #endregion AOI1Config ConfigAOI1 => Config as AOI1Config; //Thread ThHeartPlc; public override void Open() { base.Open(); } string Productcode = DateTime.Now.ToString("yyyyMMddHHmmss"); Dictionary dicplctime = new Dictionary(); [ProcessMethod("", "PositionCheck_P1", "工位1检测", InvokeType.TestInvoke)] public ResponseMessage PositionCheck_P1(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice) { if (config.TriggerValue == 1) { Productcode = DateTime.Now.ToString("yyyyMMddHHmmssfff"); } int pronum = Plc1.Read(ConfigAOI1.Pronumaddress, 1, out _)[0]; LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"产品计时开始 plc料号 {pronum} "); dicplctime[pronum] = DateTime.Now; config.TriggerStr = "1#," + config.TriggerValue + ",Str," + Productcode; ResponseMessage msg = RunImageCheck_plc(config, pronum); msg.IsReply = false; if (msg.Result != 1) { ReplyPlcData(new List() { false, false, false }, pronum); } return msg; } public override void ReplyPlcData(List result, int pronum) { try { pronum = pronum * 10; if (result.Count == 3) { Plc1.WriteSingleAddress(ConfigAOI1.Finresult1, pronum + (result[0] ? 1 : 2), out _); Plc1.WriteSingleAddress(ConfigAOI1.Finresult2, pronum + (result[1] ? 1 : 2), out _); Plc1.WriteSingleAddress(ConfigAOI1.Finresult3, pronum + (result[2] ? 1 : 2), out _); Plc1.WriteSingleAddress(ConfigAOI1.Plcover, 1, out _); LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"产品计时结束 plc料号 {pronum} 结果{pronum + (result[0] ? 1 : 2)},{pronum + (result[1] ? 1 : 2)},{pronum + (result[2] ? 1 : 2)} 总用时 {(DateTime.Now - dicplctime[pronum / 10]).TotalMilliseconds}ms "); Thread.Sleep(100); Plc1.WriteSingleAddress(ConfigAOI1.Finresult1, 0, out _); Plc1.WriteSingleAddress(ConfigAOI1.Finresult2, 0, out _); Plc1.WriteSingleAddress(ConfigAOI1.Finresult3, 0, out _); } else { Plc1.WriteSingleAddress(ConfigAOI1.Finresult1, pronum + 2, out _); Plc1.WriteSingleAddress(ConfigAOI1.Finresult2, pronum + 2, out _); Plc1.WriteSingleAddress(ConfigAOI1.Finresult3, pronum + 2, out _); Plc1.WriteSingleAddress(ConfigAOI1.Plcover, 1, out _); LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"产品计时结束 plc料号 {pronum} 结果{pronum + 2},{pronum + 2},{pronum + 2} 总用时 {(DateTime.Now - dicplctime[pronum/10]).TotalMilliseconds}ms "); LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"测试结果数量异常!!!!!!!!"); Thread.Sleep(100); Plc1.WriteSingleAddress(ConfigAOI1.Finresult1, 0, out _); Plc1.WriteSingleAddress(ConfigAOI1.Finresult2, 0, out _); Plc1.WriteSingleAddress(ConfigAOI1.Finresult3, 0, out _); } } catch { } //else if (Postion.Contains("2")) //{ // Plc1.WriteSingleAddress(ConfigAOI1.P2over, result, out _); //} //else if (Postion.Contains("3")) //{ // Plc1.WriteSingleAddress(ConfigAOI1.P3over, result, out _); //} //else //{ // Plc1.WriteSingleAddress(ConfigAOI1.Finresult, result, out _); //} } //[ProcessMethod("", "PositionCheck_P1_F", "工位1检测飞拍", InvokeType.TestInvoke)] //public ResponseMessage PositionCheck_P1_F(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice) //{ // if (config.TriggerValue == 1) // { // Productcode = DateTime.Now.ToString("yyyyMMddHHmmss"); // } // config.TriggerStr = "1#,1,Str," + Productcode; // ResponseMessage msg = new ResponseMessage(); // msg.Result = 1; // msg.IsReply = false; // List measureBinds = new List(); // string inputSequence = ""; // try // { // RunImageCheckPreTreat_plc(config, out measureBinds, out inputSequence); // } // catch (Exception ex) // { // LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"检测预处理异常,{ex.GetExceptionMessage()}"); // msg.Result = -1; // msg.Message = ex.Message; // msg.IsReply = false; // return msg; // } // int measureNums = measureBinds.Count; // bool WaitSignalstate = true; // //_isLastCheckFlag = false; // measureBinds.GroupBy(u => u.CameraId).AsParallel().ForAll(c => // { // var camera = DeviceCollection.FirstOrDefault(u => u.Id == c.Key) as CameraBase; // foreach (var i in c.ToList().OrderBy(u => u.ImageIndex).ToList()) // { // string index = (i.ImageIndex + 1).ToString(); // if (ConfigAOI1.P1SFstate) // { // if (!WaitSignal(index)) // { // WaitSignalstate = false; // break; // } // } // var products = i.ProductIndices.Select(pi => // { // return FindProductBySequence($"{inputSequence}_{pi}", true); // }).ToList(); // IImageSet imgSet = null; // try // { // imgSet = CollectHImage(camera, i.SnapshotOpConfig); // lock (trigerStr) // { // trigerStr.Add(index + "over"); // } // if (imgSet == null) // { // LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"工位1{camera?.Name}取像{i?.ImageIndex}失败 图像为空"); // break; // } // } // catch (Exception ex) // { // LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"工位1{camera?.Name}取像{i?.ImageIndex}异常,{ex.ToString()}"); // break; // } // RunImageCheckAsync(products, config.TriggerStr, config.TriggerSource, imgSet, i).ContinueWith(t => // { // Interlocked.Decrement(ref measureNums); // }); // } // }); // if (!WaitSignalstate) // { // LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"图片采集信息不全"); // msg.IsReply = false; // return msg; // } // while (measureNums > 0) // { // Thread.Sleep(50); // } // //检测完成后清理图片缓存 // measureBinds.Select(u => u.CameraId).Distinct().ToList().ForEach(u => // { // var camera = DeviceCollection.FirstOrDefault(c => c.Id == u) as CameraBase; // camera.ClearImageBufferQueue(); // LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"{camera.Name}相机工位1检测后清理缓存"); // }); // string positionName = measureBinds[0].WorkPosition; // var pList = CheckPositionDoneAsync_plc(positionName, inputSequence, config, new List()).GetAwaiter().GetResult(); // return msg; //} //[ProcessMethod("", "PositionCheck_P1_photo", "工位1拍照", InvokeType.TestInvoke)] //public ResponseMessage PositionCheck_P1_photo(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice) //{ // ResponseMessage msg = new ResponseMessage(); // msg.IsReply = false; // int index = config.TriggerValue; // if (index == 1) // { // trigerStr.Clear(); // } // lock (trigerStr) // { // trigerStr.Add(index.ToString()); // } // WaitSignal(index + "over"); // ReplyPlcData("P1", index); // return msg; //} //[ProcessMethod("", "PositionCheck_P2_F", "工位2检测飞拍", InvokeType.TestInvoke)] //public ResponseMessage PositionCheck_P2_F(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice) //{ // config.TriggerStr = "2#,1,Str," + Productcode; // ResponseMessage msg = new ResponseMessage(); // msg.Result = 1; // msg.IsReply = false; // List measureBinds = new List(); // string inputSequence = ""; // try // { // RunImageCheckPreTreat_plc(config, out measureBinds, out inputSequence); // } // catch (Exception ex) // { // LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"检测预处理异常,{ex.GetExceptionMessage()}"); // msg.Result = -1; // msg.Message = ex.Message; // msg.IsReply = false; // return msg; // } // int measureNums = measureBinds.Count; // bool WaitSignalstate = true; // //_isLastCheckFlag = false; // measureBinds.GroupBy(u => u.CameraId).AsParallel().ForAll(c => // { // var camera = DeviceCollection.FirstOrDefault(u => u.Id == c.Key) as CameraBase; // foreach (var i in c.ToList().OrderBy(u => u.ImageIndex).ToList()) // { // string index = (i.ImageIndex + 1).ToString(); // if (ConfigAOI1.P2SFstate) // { // if (!WaitSignal(index, "p2")) // { // WaitSignalstate = false; // break; // } // } // var products = i.ProductIndices.Select(pi => // { // return FindProductBySequence($"{inputSequence}_{pi}", true); // }).ToList(); // IImageSet imgSet = null; // try // { // imgSet = CollectHImage(camera, i.SnapshotOpConfig); // lock (trigerStr2) // { // trigerStr2.Add(index + "over"); // } // if (imgSet == null) // { // LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"工位2{camera?.Name}取像{i?.ImageIndex}失败 图像为空"); // break; // } // } // catch (Exception ex) // { // LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"工位2{camera?.Name}取像{i?.ImageIndex}异常,{ex.ToString()}"); // break; // } // RunImageCheckAsync(products, config.TriggerStr, config.TriggerSource, imgSet, i).ContinueWith(t => // { // Interlocked.Decrement(ref measureNums); // }); // } // }); // if (!WaitSignalstate) // { // LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"图片采集信息不全"); // msg.IsReply = false; // return msg; // } // while (measureNums > 0) // { // Thread.Sleep(50); // } // //检测完成后清理图片缓存 // measureBinds.Select(u => u.CameraId).Distinct().ToList().ForEach(u => // { // var camera = DeviceCollection.FirstOrDefault(c => c.Id == u) as CameraBase; // camera.ClearImageBufferQueue(); // LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"{camera.Name}相机工位2检测后清理缓存"); // }); // string positionName = measureBinds[0].WorkPosition; // var pList = CheckPositionDoneAsync_plc(positionName, inputSequence, config, new List()).GetAwaiter().GetResult(); // return msg; //} //[ProcessMethod("", "PositionCheck_P2_photo", "工位2拍照", InvokeType.TestInvoke)] //public ResponseMessage PositionCheck_P2_photo(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice) //{ // ResponseMessage msg = new ResponseMessage(); // msg.IsReply = false; // int index = config.TriggerValue; // if (index == 1) // { // trigerStr2.Clear(); // } // lock (trigerStr2) // { // trigerStr2.Add(index.ToString()); // } // WaitSignal(index + "over", "p2"); // ReplyPlcData("P2", index); // return msg; //} //[ProcessMethod("", "PositionCheck_P3_F", "工位3检测飞拍", InvokeType.TestInvoke)] //public ResponseMessage PositionCheck_P3_F(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice) //{ // config.TriggerStr = "3#,1,Str," + Productcode; // ResponseMessage msg = new ResponseMessage(); // msg.Result = 1; // msg.IsReply = false; // List measureBinds = new List(); // string inputSequence = ""; // try // { // RunImageCheckPreTreat_plc(config, out measureBinds, out inputSequence); // } // catch (Exception ex) // { // LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"检测预处理异常,{ex.GetExceptionMessage()}"); // msg.Result = -1; // msg.Message = ex.Message; // msg.IsReply = false; // return msg; // } // int measureNums = measureBinds.Count; // bool WaitSignalstate = true; // //_isLastCheckFlag = false; // measureBinds.GroupBy(u => u.CameraId).AsParallel().ForAll(c => // { // var camera = DeviceCollection.FirstOrDefault(u => u.Id == c.Key) as CameraBase; // foreach (var i in c.ToList().OrderBy(u => u.ImageIndex).ToList()) // { // string index = (i.ImageIndex + 1).ToString(); // if (ConfigAOI1.P3SFstate) // { // if (!WaitSignal(index, "p3")) // { // WaitSignalstate = false; // break; // } // } // var products = i.ProductIndices.Select(pi => // { // return FindProductBySequence($"{inputSequence}_{pi}", true); // }).ToList(); // IImageSet imgSet = null; // try // { // imgSet = CollectHImage(camera, i.SnapshotOpConfig); // lock (trigerStr3) // { // trigerStr3.Add(index + "over"); // } // if (imgSet == null) // { // LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"工位1{camera?.Name}取像{i?.ImageIndex}失败 图像为空"); // break; // } // } // catch (Exception ex) // { // LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"工位1{camera?.Name}取像{i?.ImageIndex}异常,{ex.ToString()}"); // break; // } // RunImageCheckAsync(products, config.TriggerStr, config.TriggerSource, imgSet, i).ContinueWith(t => // { // Interlocked.Decrement(ref measureNums); // }); // } // }); // if (!WaitSignalstate) // { // LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"图片采集信息不全"); // msg.IsReply = false; // return msg; // } // while (measureNums > 0) // { // Thread.Sleep(50); // } // //检测完成后清理图片缓存 // measureBinds.Select(u => u.CameraId).Distinct().ToList().ForEach(u => // { // var camera = DeviceCollection.FirstOrDefault(c => c.Id == u) as CameraBase; // camera.ClearImageBufferQueue(); // LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"{camera.Name}相机工位1检测后清理缓存"); // }); // string positionName = measureBinds[0].WorkPosition; // var pList = CheckPositionDoneAsync_plc(positionName, inputSequence, config, new List()).GetAwaiter().GetResult(); // return msg; //} //[ProcessMethod("", "PositionCheck_P3_photo", "工位3拍照", InvokeType.TestInvoke)] //public ResponseMessage PositionCheck_P3_photo(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice) //{ // ResponseMessage msg = new ResponseMessage(); // msg.IsReply = false; // int index = config.TriggerValue; // if (index == 1) // { // trigerStr3.Clear(); // } // lock (trigerStr3) // { // trigerStr3.Add(index.ToString()); // } // WaitSignal(index + "over", "p3"); // ReplyPlcData("P3", index); // return msg; //} //public bool WaitSignal(string index, string p = "p1") //{ // DateTime dt = DateTime.Now; // while ((DateTime.Now - dt).TotalMilliseconds < ConfigAOI1.PhotolayoutTime) // { // if (p.Equals("p1")) // { // lock (trigerStr) // { // if (trigerStr.Contains(index)) // { // return true; // } // } // } // else if (p == "p2") // { // lock (trigerStr2) // { // if (trigerStr2.Contains(index)) // { // return true; // } // } // } // else if (p == "p3") // { // lock (trigerStr3) // { // if (trigerStr3.Contains(index)) // { // return true; // } // } // } // Thread.Sleep(50); // } // return false; //} //List trigerStr = new List(); //List trigerStr2 = new List(); //List trigerStr3 = new List(); //[ProcessMethod("", "PositionCheck_P2", "工位2检测", InvokeType.TestInvoke)] //public ResponseMessage PositionCheck_P2(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice) //{ // config.TriggerStr = "2#," + config.TriggerValue + ",Str," + Productcode; // ResponseMessage msg = RunImageCheck_plc(config); // msg.IsReply = false; // if (msg.Result != 1) // { // ReplyPlcData("P2", config.TriggerValue); // } // return msg; //} //[ProcessMethod("", "PositionCheck_P3", "工位3检测", InvokeType.TestInvoke)] //public ResponseMessage PositionCheck_P3(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice) //{ // config.TriggerStr = "3#," + config.TriggerValue + ",Str," + Productcode; // ResponseMessage msg = RunImageCheck_plc(config); // msg.IsReply = false; // if (msg.Result != 1) // { // ReplyPlcData("P3", config.TriggerValue); // } // return msg; //} //[ProcessMethod("", "PositionCheck_Fin", "结果反馈plc", InvokeType.TestInvoke)] //public ResponseMessage PositionCheck_Fin(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice) //{ // config.TriggerStr = "#,UpData," + Productcode; // ResponseMessage msg = new ResponseMessage(); // var triggerDatas = config.TriggerStr.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); // string inputSequence = triggerDatas[triggerDatas.Length - 1]; // var productList = new List() { 1 }.Select(u => // { // string sequence = $"{inputSequence}_{u}"; // return FindProductBySequence(sequence, true); // }).ToList(); // int waitInterval = 300; // int repeatTime = /*M141Config.DetectTimeout / waitInterval+*/2; // do // { // if (productList.All(p => !string.IsNullOrWhiteSpace(p.Result))) // { // break; // } // else // { // Thread.Sleep(waitInterval); // repeatTime--; // } // if (repeatTime < 0) // { // LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"产品{string.Join(",", productList.Select(u => u.PID))}检测获取汇总结果超时,使用当前结果"); // break; // } // } while (true); // var Defaultclass = M141Config.DefectClassCollection.FirstOrDefault(u => u.IsDefault); // productList.ForEach(p => // { // if (Defaultclass == null) // { // if (string.IsNullOrEmpty(p.Result)) // { // p.Result = "NG"; // } // } // else // { // if (string.IsNullOrEmpty(p.Result)) // { // p.Result = Defaultclass.ClassName; // } // } // var specList = p.Details.SelectMany(u => u.ResultList.SelectMany(r => r.Specs)).ToList(); // p.Details.SelectMany(u => u.SpecList).ToList().ForEach(s => // { // if (!specList.Any(u => u.Code == s.Code)) // { // specList.Add(s); // } // }); // List Pointdatas11 = new List(); // foreach (var item in specList) // { // try // { // Pointdatas11.Add(new Pointdata() // { // Code = item.Code, // Value = Convert.ToDouble(item.GetMeasureValueStr()), // Stand = item.StandardValue, // Min = item.StandardValue - item.Tolrenance_Negative, // Max = item.StandardValue + item.Tolrenance_Positive, // ispoint = false, // }); // } // catch // { // } // } // M141Codelist.Add(Pointdatas11); // ReplyPlcData("Fin", p.Result.ToUpper().Contains("NG") ? 2 : 1); // }); // return msg; //} } }