quanzhou
2025-10-21 745d1ce73a3b27d2a3507843c8060755a0c95765
src/Bro.M141_AOI1.Process/AOI1Process.cs
@@ -9,10 +9,12 @@
using HalconDotNet;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NPOI.OpenXmlFormats.Vml;
using NPOI.POIFS.Crypt.Dsig;
using NPOI.SS.Formula.Functions;
using NPOI.XSSF.Streaming.Values;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using static Bro.Common.Helper.EnumHelper;
@@ -105,6 +107,75 @@
        }
        [ProcessMethod("ImageCheck", "CheckProduction", "产品点检", InvokeType.TestInvoke)]
        public ResponseMessage CheckProduction(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        {
            ResponseMessage msg = new ResponseMessage();
            if (config is IImageCheckOperationConfig opConfig)
            {
                var results1 = GetSpecListFromConfigSelection(opConfig.SpecCollection);
                IImageSet imageSet = null;
                string Head = "时间,";
                string data = $"{DateTime.Now.ToString("yyyyMMddHHmmss")}T,";
                try
                {
                    var messure = M141Config.MeasureBindCollection_Check.FirstOrDefault(u => u.CameraId == invokeDevice.Id);
                    var camera = DeviceCollection.FirstOrDefault(u => u.Id == messure.CameraId) as CameraBase;
                    try
                    {
                        imageSet = CollectHImage(camera, messure.SnapshotOpConfig);
                    }
                    catch (Exception exx)
                    {
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"点检功能获取图像失败{exx.ToString()}");
                    }
                    var tool = GetHalconTool(null, "", opConfig.AlgorithemPath);
                    var ret = tool.RunProcedure(null, new Dictionary<string, HalconDotNet.HObject>() { { "INPUT_Image", imageSet.HImage } }, new List<string>() { "OUTPUT_Results" }, null);
                    //var ret = tool.RunProcedure(null, null, new List<string>() { "OUTPUT_Results" }, null);
                    List<IShapeElement> eleList = new List<IShapeElement>();
                    TextDisplay text = new TextDisplay();
                    text.LineLimit = M141Config.LineLimit_p;
                    text.FontSize = M141Config.FontSize_p;
                    eleList.Add(text);
                    text.StartX = text.StartY = 0;
                    text.AddText("点检运行成功", Color.Lime, Color.Transparent);
                    var b = ret.Item2["OUTPUT_Results"].HTupleToDouble();
                    if (ret != null&& results1.Count== b.Count())
                    {
                        var itemDict = results1.ToDictionary(u => u.OutputIndex);
                        b.ForEach(u =>
                        {
                            int index = b.IndexOf(u); // 获取当前值的索引
                            if (itemDict.TryGetValue(index, out var item))
                            {
                                double diff = u - item.StandardValue;
                                bool isInTolerance = (diff - item.Tolrenance_Positive) *(diff - item.Tolrenance_Negative) <= 0;
                                text.AddText($"{item.Code} {u}", isInTolerance == true ? Color.Lime: Color.Red, Color.Transparent);
                                 Head += $"{item.Code},";
                                 data += $"{u},";
                            }
                        });
                    }
                    camera.SaveFitImage(eleList, imageSet);
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"点检功能运行成功");
                }
                catch (Exception ex)
                {
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"点检功能失败,{ex.ToString()}");
                }
                LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"点检图片开始释放");
                imageSet.HImage?.Dispose();
                imageSet.HImage = null;
                imageSet.Dispose();
                imageSet = null;
                LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"图片已释放");
                //报表输出
                CSVRecordAsync($"相机{invokeDevice.Name}_点检数据记录.csv", data, Head);
            }
            return msg;
        }
        [ProcessMethod("ImageCheck", "ReadBarcode", "读码", InvokeType.TestInvoke)]
        public ResponseMessage ReadBarcode(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        {
@@ -140,7 +211,6 @@
                            {
                                codestr = "10";
                            }
                            if (codestr.Equals(mh.ToString()))
                            {
                                ret.Add(1);
@@ -158,7 +228,6 @@
                    }
                    FillSpecResults(results[0].PID, results[0].Specs, ret, opConfig.Products[0].SEQUENCE);
                    opConfig.Products[0].SN = barcode;
                    opConfig.Products[0].PID = barcode + "_1";
@@ -179,7 +248,6 @@
            }
            return msg;
        }
        [ProcessMethod("ImageCheck", "ReadBarcode2", "读栏具码", InvokeType.TestInvoke)]
        public ResponseMessage ReadBarcode2(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
@@ -295,7 +363,6 @@
            return msg;
        }
        private string BarcodeScannerCommunicate(TcpClientWrapBase client)
        {
            string barcode = "";
@@ -326,7 +393,6 @@
            return "NOREAD";
        }
        private bool CheckBarcodeValid(string barcode)
        {
            bool isBarcodeValid = true;
@@ -336,7 +402,6 @@
            }
            return isBarcodeValid;
        }
        [ProcessMethod("ImageCheck", "CheckLineProfile", "检测产品线轮廓度", InvokeType.TestInvoke)]
        public ResponseMessage CheckLineProfile(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
@@ -500,7 +565,6 @@
            return msg;
        }
        [ProcessMethod("ImageCheck", "GetMatrix", "获取矩阵", InvokeType.TestInvoke)]
        public ResponseMessage GetMatrix(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        {
@@ -529,10 +593,147 @@
            return msg;
        }
        [ProcessMethod("", "MESupdata", "MES正常上传OK产品获取条码", InvokeType.TestInvoke)]
        public ResponseMessage MESupdata(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        {
            ResponseMessage msg = new ResponseMessage();
            Plc3 = invokeDevice as PLCBase;
            ///产品1上传
            var plcnum = Plc3.Read(2100, 1, out _);
            if (plcnum[0] != 0)
            {
                var plist = mysqlhelper.GetProductList(plcnum + "_1");
                ProductModel newp = new ProductModel();
                newp.SEQUENCE = plist[0].SEQUENCE;
                newp.PID = plist[0].PID;
                newp.BasketCode = plist[0].BasketCode;
                newp.Zword = plist[0].Zword;
                newp.Result = "OK";
                newp.SN = plist[0].SN;
                var tems = Task.Run(() => mqtt.MESForProduceAsync(newp, M141Config.mesnum2.ToString(), M141Config.numpro)).Result;
                M141Config.mesnum2++;
                if (tems == null)
                {
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"产品{plist[0].PID}数据上传MES异常 返回数据为null");
                }
                else
                {
                    try
                    {
                        var obj = JsonConvert.DeserializeObject<AutoLineMacBarcodeQueueBak>(tems);
                        if (obj.zstatus == "200")
                        {
                            Plc1.WriteSingleAddress(2120, 1, out _);
                            LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"产品{plist[0].PID}数据上传MES成功  {tems}");
                        }
                        else
                        {
                            LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"产品{plist[0].PID}数据上传MES失败  {tems}");
                        }
                    }
                    catch (Exception)
                    {
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"产品{plist[0].PID}数据上传MES失败  {tems}");
                    }
                }
                ///产品1上传
                var plcnum1 = Plc3.Read(2102, 1, out _);
                if (plcnum1[0] != 0)
                {
                    var plist1 = mysqlhelper.GetProductList(plcnum1 + "_1");
                    ProductModel newp1 = new ProductModel();
                    newp1.SEQUENCE = plist[0].SEQUENCE;
                    newp1.PID = plist[0].PID;
                    newp1.BasketCode = plist[0].BasketCode;
                    newp1.Zword = plist[0].Zword;
                    newp1.Result = "OK";
                    newp1.SN = plist[0].SN;
                    var tems1 = Task.Run(() => mqtt.MESForProduceAsync(newp1, M141Config.mesnum2.ToString(), M141Config.numpro)).Result;
                    M141Config.mesnum2++;
                    if (tems1 == null)
                    {
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"产品{plist[0].PID}数据上传MES异常 返回数据为null");
                    }
                    else
                    {
                        try
                        {
                            var obj1 = JsonConvert.DeserializeObject<AutoLineMacBarcodeQueueBak>(tems1);
                            if (obj1.zstatus == "200")
                            {
                                Plc1.WriteSingleAddress(2120, 1, out _);
                                LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"产品{plist[0].PID}数据上传MES成功  {tems}");
                            }
                            else
                            {
                                LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"产品{plist[0].PID}数据上传MES失败  {tems}");
                            }
                        }
                        catch (Exception)
                        {
                            LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"产品{plist[0].PID}数据上传MES失败  {tems}");
                        }
                    }
                }
            }
            return msg;
        }
        [ProcessMethod("", "MESupFinallydata", "MES上传尾盘OK产品获取条码", InvokeType.TestInvoke)]
        public ResponseMessage MESupFinallydata(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
        {
            ResponseMessage msg = new ResponseMessage();
            Plc3 = invokeDevice as PLCBase;
            ///产品1上传
            var plcnum = Plc3.Read(2100, 1, out _);
            var plist = mysqlhelper.GetProductList(plcnum+"_1");
            ProductModel newp = new ProductModel();
            newp.SEQUENCE = plist[0].SEQUENCE;
            newp.PID = plist[0].PID;
            newp.BasketCode = plist[0].BasketCode;
            newp.Zword = plist[0].Zword;
            newp.Result = "OK";
            newp.SN = plist[0].SN;
            //上传尾盘数据
            var tems = Task.Run(() => mqtt.MESForProduceAsync(newp, M141Config.mesnum2.ToString(), M141Config.numpro,"Y")).Result;
            M141Config.mesnum2 = 0;
            if (tems == null)
            {
                LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"产品{plist[0].PID}数据上传MES异常 返回数据为null");
            }
            else
            {
                try
                {
                    var obj = JsonConvert.DeserializeObject<AutoLineMacBarcodeQueueBak>(tems);
                    if (obj.zstatus == "200")
                    {
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"产品{plist[0].PID}数据上传MES成功  {tems}");
                        //打印获取的条码
                        if (M141Config.Isprint && !string.IsNullOrEmpty(obj.tary_label))
                        {
                            StartPrint(obj.tary_label);
                        }
                    }
                    else
                    {
                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"产品{plist[0].PID}数据上传MES失败  {tems}");
                    }
                }
                catch (Exception)
                {
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"产品{plist[0].PID}数据上传MES失败  {tems}");
                }
            }
            return msg;
        }
        [ProcessMethod("", "MESup1", "MES上传进料口空篮", InvokeType.TestInvoke)]