using Bro.Common.Base; using Bro.Common.Helper; using Bro.Common.Interface; using Bro.Common.Model; using Bro.M135.Common; using Bro.M135.DBManager; using Bro.UI.Model.Winform; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Bro.M135.Process { public partial class M141_WS1_Process : IUpdateProductData { #region Constructor public M141_WS1_Process() : base() { } public M141_WS1_Process(string productCode) : base(productCode) { } #endregion M135Config M135Config => Config as M135Config; object _productListLock = new object(); List ProductList = new List(); MachineLearningBase ML = null; TcpListenerWrap TcpListener = null; PointLaser_OmronDriver PointLaser_Upper = null; PointLaser_OmronDriver PointLaser_Down = null; Spec _errorSpec = null; int _backgroundImageWidth = 2448; public event Action> OnPositionResultUpdated; public event Action OnUpdateProductData; string STATION_NAME => string.IsNullOrWhiteSpace(M135Config.StationCode) ? "M135_Size" : M135Config.StationCode; public override void Open() { base.Open(); //if (_manager_P_Product == null) //{ // try // { // _manager_P_Product = new Manager_P_PRODUCT(); // } // catch (Exception ex) // { // LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"数据库初始化异常,{ex.GetExceptionMessage()}"); // throw ex; // } //} ML = DeviceCollection.FirstOrDefault(u => u is MachineLearningBase) as MachineLearningBase; if (ML == null) { LogAsync(DateTime.Now, EnumHelper.LogLevel.Information, $"未设置ML实例"); } TcpListener = DeviceCollection.FirstOrDefault(u => u is TcpListenerWrap) as TcpListenerWrap; if (TcpListener == null) { LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"未设置TCP监听实例"); } _errorSpec = M135Config.SpecCollection.FirstOrDefault(u => u.Code == M135Config.CheckErrorSpecCode) as Spec; if (_errorSpec == null) { LogAsync(DateTime.Now, EnumHelper.LogLevel.Warning, $"未设置检测异常Spec"); } PointLaser_Upper = DeviceCollection.FirstOrDefault(u => u.Id == M135Config.PointLaserId_Upper) as PointLaser_OmronDriver; if (PointLaser_Upper == null) { LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"未设置或选择上方点激光设备"); } PointLaser_Down = DeviceCollection.FirstOrDefault(u => u.Id == M135Config.PointLaserId_Down) as PointLaser_OmronDriver; if (PointLaser_Down == null) { LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"未设置或选择下方点激光设备"); } _csvHead = ""; _contourPointDataHead = ""; _pointHeightHead = ""; _triggerSource_P1 = ""; InitialProductList(); if (File.Exists(M135Config.BackgroundImageFilePath)) { Bitmap bkImage = new Bitmap(M135Config.BackgroundImageFilePath); _backgroundImageWidth = bkImage.Width; } } public override void InitialProcessMethods() { base.InitialProcessMethods(); OldDataClear.Instance.RunClearOldData(M135Config.DBDataTimeLimit); } public override void ProcessRunStateChanged() { base.ProcessRunStateChanged(); if (CurrentState == EnumHelper.RunState.Running) { OldDataClear.Instance.SetAllowFlag(false, M135Config.DBDataTimeLimit); } else { OldDataClear.Instance.SetAllowFlag(true, M135Config.DBDataTimeLimit); } } private void NewProductIntoList(ProductModel_M135 p) { lock (_productListLock) { ProductList.Insert(0, p); while (ProductList.Count > 200) { ProductList.RemoveAt(ProductList.Count - 1); } } //Task.Run(() => //{ var isNewDone = _manager_P_Product.NewProduct(p, p.Details, out string msg); if (!isNewDone) { LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"产品{p.PID}数据库新增异常,{msg}"); } //}); } private ProductModel_M135 FindProductModelByBarcode(string barcode) { var p = ProductList.FirstOrDefault(u => u.SN == barcode); if (p == null && M135Config.IsAllowTempProduct) { p = new ProductModel_M135(); p.SN = barcode; ProductList.Add(p); } return p; } protected void FillSpecResults(List detectSpec, List results) { detectSpec.ForEach(s => { if (!s.IsEnabled) return; if (results.Count > s.OutputIndex) { s.ActualValue = results[s.OutputIndex]; LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"{s.Code}赋值{s.GetMeasureValueStr()},结果{s.GetMeasureResultStr()}"); } }); } private List ConvertXY2Data(List xList, List yList) { List list = new List(); for (int i = 0; i < xList.Count && i < yList.Count; i++) { list.Add(xList[i]); list.Add(yList[i]); } return list; } private void ConvertData2XY(List data, out List xList, out List yList) { xList = new List(); yList = new List(); for (int i = 0; i < data.Count; i += 2) { xList.Add(data[i]); yList.Add(data[i + 1]); } } private List GetSpecListFromConfigSelection(List specSelectors) { List specList = new List(); specSelectors.ForEach(u => { var spec = M135Config.SpecCollection.FirstOrDefault(s => s.Code == u.SpecCode); if (spec != null) { specList.Add(spec.Copy()); } }); return specList; } private async void UpdatePositionResultDisplay(P_PRODUCT_DETAIL detail, bool isOK, string positionName) { await Task.Run(() => { if (detail == null) { LogAsync(DateTime.Now, EnumHelper.LogLevel.Warning, $"无法显示检测明细信息"); return; } List eleList = new List(); TextDisplay txt = new TextDisplay(); eleList.Add(txt); txt.StartX = txt.StartY = (int)(_backgroundImageWidth * GlobalVar.WIDTH_RATIO); txt.AddText($"{detail.SN} {(isOK ? "OK" : "NG")}", isOK ? Color.Lime : Color.White, isOK ? Color.Transparent : Color.Red); txt.AddText(" ", Color.Transparent, Color.Transparent); var ngSpecCode = new List(); detail.SpecList.ForEach(s => { GetIndicatorFromSpec(s, eleList, txt, ngSpecCode); }); detail.ResultList.SelectMany(u => u.Specs).ToList().ForEach(s => { GetIndicatorFromSpec(s, eleList, txt, ngSpecCode); }); var defects = new List(detail.DefectList); defects.AddRange(detail.ResultList.GetDefectDescList()); defects = defects.Except(ngSpecCode).Distinct().ToList(); defects.ForEach(u => { txt.AddText(u, Color.Red, Color.Transparent); var indicator = M135Config.DefectIndicatorCollection.FirstOrDefault(d => d.DefectName == u); if (indicator != null) { var cloneIndicator = indicator.Clone() as DefectRectangleIndicator; eleList.Add(cloneIndicator); } }); OnPositionResultUpdated?.Invoke(positionName, eleList); }); } private void GetIndicatorFromSpec(ISpec s, List eleList, TextDisplay txt, List ngSpecCode) { txt.AddText($"{s.Code} {s.GetMeasureValueStr()}", s.MeasureResult == true ? Color.Lime : Color.Red, Color.Transparent); if (s.MeasureResult != true) { var indicator = M135Config.DefectIndicatorCollection.FirstOrDefault(u => u.DefectName == s.Code); if (indicator != null) { var cloneIndicator = indicator.Clone() as DefectRectangleIndicator; cloneIndicator.DefectName += $" {s.GetMeasureValueStr()}"; eleList.Add(cloneIndicator); } ngSpecCode.Add(s.Code); } } private async void UpdateOverAllProductResultToUI(ProductModel_M135 product, bool isOK) { await Task.Run(() => { List eleList = new List(); TextDisplay txt = new TextDisplay(); eleList.Add(txt); txt.StartX = txt.StartY = (int)(_backgroundImageWidth * GlobalVar.WIDTH_RATIO); txt.AddText($"{product.SN} {product.Result}", isOK ? Color.Lime : Color.White, isOK ? Color.Transparent : Color.Red); txt.AddText(" ", Color.Transparent, Color.Transparent); var ngSpecCode = new List(); product.Details.ForEach(d => { d.SpecList.ForEach(s => { GetIndicatorFromSpec(s, eleList, txt, ngSpecCode); }); d.ResultList.SelectMany(u => u.Specs).ToList().ForEach(s => { GetIndicatorFromSpec(s, eleList, txt, ngSpecCode); }); var defects = new List(d.DefectList); defects.AddRange(d.ResultList.GetDefectDescList()); defects = defects.Except(ngSpecCode).Distinct().ToList(); defects.ForEach(u => { txt.AddText(u, Color.Red, Color.Transparent); var indicator = M135Config.DefectIndicatorCollection.FirstOrDefault(d => d.DefectName == u); if (indicator != null) { var cloneIndicator = indicator.Clone() as DefectRectangleIndicator; eleList.Add(cloneIndicator); } }); }); OnPositionResultUpdated?.Invoke("", eleList); }); } [ProcessMethod("", "UpdateProdcutModelToListTest", "上传至产品列表测试", InvokeType.TestInvoke)] public ResponseMessage UpdateProdcutModelToListTest(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice) { ProductModel p = new ProductModel(); p.SN = "123456"; p.Result = "Ng"; Spec spec = new Spec() { Code = "SpecTest", StandardValue = 100, Tolrenance_Negative = 10, Tolrenance_Positive = 10 }; spec.ActualValue = 101; P_PRODUCT_DETAIL detail = new P_PRODUCT_DETAIL() { PositionName = "S1", STATION_CODE = "M135" }; detail.SpecList = new List(); for (int i = 0; i < 10; i++) { var specClone = (Spec)spec.Copy(); specClone.Code = $"FAI_{i}"; specClone.ActualValue = new Random().Next(80, 120); detail.SpecList.Add(specClone); } detail.DefectList = new List() { "压伤" }; p.Details.Add(detail); OnUpdateProductData?.Invoke(p); return new ResponseMessage(); } } }