From 745d1ce73a3b27d2a3507843c8060755a0c95765 Mon Sep 17 00:00:00 2001
From: quanzhou <quan.zhou@broconcentric.com>
Date: 星期二, 21 十月 2025 19:06:36 +0800
Subject: [PATCH] 打印机条码格式修改,增添一个点检功能方法

---
 src/Bro.M141.Process/M141Process_ImageCheck.cs |  256 ++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 234 insertions(+), 22 deletions(-)

diff --git a/src/Bro.M141.Process/M141Process_ImageCheck.cs b/src/Bro.M141.Process/M141Process_ImageCheck.cs
index 3b69ea8..f054fd9 100644
--- a/src/Bro.M141.Process/M141Process_ImageCheck.cs
+++ b/src/Bro.M141.Process/M141Process_ImageCheck.cs
@@ -2,13 +2,17 @@
 using Bro.Common.Helper;
 using Bro.Common.Interface;
 using Bro.Common.Model;
+using Bro.DataBase.Model;
 using Bro.M135.Common;
 using Bro.M135.DBManager;
 using HalconDotNet;
 using Microsoft.VisualBasic;
 using Newtonsoft.Json;
 using Newtonsoft.Json.Linq;
+using NPOI.SS.Formula.Functions;
+using NPOI.SS.UserModel;
 using NPOI.Util;
+using NPOI.XSSF.UserModel;
 using Sunny.UI;
 using System.Data;
 using System.Data.SqlTypes;
@@ -32,7 +36,7 @@
         ManualResetEvent set2 = new ManualResetEvent(false);
 
 
-        public event Action<string> StartPrinter;
+     
 
         [ProcessMethod("ImageCheck", "ImageCheckOperation", "閫氱敤鍥剧墖妫�娴嬫搷浣�", InvokeType.TestInvoke)]
         public ResponseMessage ImageCheckOperation(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
@@ -441,11 +445,14 @@
         }
 
 
+
+
         [ProcessMethod("", "ProductDataUpload", "浜у搧鏁版嵁姹囨��", InvokeType.TestInvoke)]
         public ResponseMessage ProductDataUpload(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
         {
             ResponseMessage msg = new ResponseMessage();
-            string inputSequence = Plc1.Read(1536, 1, out _)[0].ToString();
+            int numplca = Plc1.Read(1536, 1, out _)[0];
+            string inputSequence = numplca.ToString();
 
             var productList = new List<int>() { 1 }.Select(u =>
             {
@@ -453,6 +460,10 @@
                 return FindProductBySequence(sequence, true);
             }).ToList();
 
+            if (PlcNumForAll == -1)
+            {
+                PlcNumForAll = numplca;
+            }
 
             int waitInterval = 300;
             int repeatTime = M141Config.DetectTimeout / waitInterval;
@@ -477,29 +488,220 @@
             } while (true);
 
 
-            var Defaultclass = M141Config.DefectClassCollection.FirstOrDefault(u => u.IsDefault);
-            productList.ForEach(p =>
+            var Defaultclass = M141Config.DefectClassCollection.FirstOrDefault(u => u.ClassName == productList[0].Result);
+
+            if (M141Config.StationCode == "S4" && productList[0].Result == "OK")
             {
-                if (Defaultclass == null)
+                ////mysqlhelper.GetS2Result(productList[0].SEQUENCE);
+                Plc1.WriteSingleAddress(1526, mysqlhelper.GetS2Result(productList[0].SEQUENCE) ? 1 : 2, out _);
+
+                LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"浜у搧{string.Join(",", productList.Select(u => u.PID))}妫�娴嬪弽棣坽mysqlhelper.GetS2Result(productList[0].SEQUENCE)}");
+            }
+            else
+            {
+                Plc1.WriteSingleAddress(1526, Defaultclass?.ClassValue ?? 2, out _);
+            }
+
+            Plc1.WriteSingleAddress(1516, 1, out _);
+
+            //if (M141Config.ISupMES && (M141Config.MESchannel == -1 || M141Config.MESchannel == 1))
+            //{
+            //    if (M141Config.numpro >= 50)
+            //    {
+            //        M141Config.numpro = 0;
+            //    }
+
+            //    if (productList[0].Result == "OK")
+            //    {
+            //        M141Config.numpro++;
+            //    }
+
+            //    //mqtt.demes(productList[0], M141Config.zwoid);
+            //    string Msgreceice = Task.Run(() => mqtt.MESForProduceAsync(productList[0], M141Config.zwoid, M141Config.numpro)).Result;
+            //    if (Msgreceice == null)
+            //    {
+            //        LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"浜у搧{productList[0].PID}鏁版嵁涓婁紶MES寮傚父 杩斿洖鏁版嵁涓簄ull");
+            //    }
+            //    else
+            //    {
+            //        try
+            //        {
+            //            var obj = JsonConvert.DeserializeObject<AutoLineMacBarcodeQueueBak>(Msgreceice);
+
+            //            if (obj.zstatus == "200")
+            //            {
+            //                LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"浜у搧{productList[0].PID}鏁版嵁涓婁紶MES鎴愬姛  {Msgreceice}");
+            //            }
+            //            else
+            //            {
+            //                LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"浜у搧{productList[0].PID}鏁版嵁涓婁紶MES澶辫触  {Msgreceice}");
+            //            }
+            //        }
+            //        catch
+            //        {
+            //            LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"浜у搧{productList[0].PID}鏁版嵁涓婁紶MES寮傚父  {Msgreceice}");
+            //        }
+            //    }
+
+
+
+            //    lock (plcnumlock)
+            //    {
+            //        int differ = numplca - PlcNumForAll;
+            //        if (differ > 1)
+            //        {
+            //            for (int i = PlcNumForAll + 1; i < numplca; i++)
+            //            {
+            //                try
+            //                {
+            //                    var plist = mysqlhelper.GetProductList(i + "_1");
+            //                    ProductModel newp = new ProductModel();
+            //                    newp.SEQUENCE = plist[0].SEQUENCE;
+            //                    newp.PID = plist[0].PID;
+            //                    newp.BasketCode = plist[0].BasketCode;
+            //                    newp.Result = "NG";
+            //                    newp.SN = plist[0].SN;
+            //                    var tems = Task.Run(() => mqtt.MESForProduceAsync(newp, M141Config.zwoid, M141Config.numpro)).Result;
+
+            //                }
+            //                catch
+            //                {
+
+            //                }
+            //            }
+            //        }
+            //        else if (differ != -29998)
+            //        {
+            //            for (int i = PlcNumForAll + 1; i <= 29999; i++)
+            //            {
+            //                try
+            //                {
+            //                    var plist = mysqlhelper.GetProductList(i + "_1");
+            //                    ProductModel newp = new ProductModel();
+            //                    newp.SEQUENCE = plist[0].SEQUENCE;
+            //                    newp.PID = plist[0].PID;
+            //                    newp.BasketCode = plist[0].BasketCode;
+            //                    newp.Result = "NG";
+            //                    newp.SN = plist[0].SN;
+            //                    var tems = Task.Run(() => mqtt.MESForProduceAsync(newp, M141Config.zwoid, M141Config.numpro)).Result;
+
+            //                }
+            //                catch
+            //                {
+
+            //                }
+            //            }
+
+
+
+            //            for (int i = 1; i < numplca; i++)
+            //            {
+            //                try
+            //                {
+            //                    var plist = mysqlhelper.GetProductList(i + "_1");
+            //                    ProductModel newp = new ProductModel();
+            //                    newp.SEQUENCE = plist[0].SEQUENCE;
+            //                    newp.PID = plist[0].PID;
+            //                    newp.BasketCode = plist[0].BasketCode;
+            //                    newp.Result = "NG";
+            //                    newp.SN = plist[0].SN;
+            //                    var tems = Task.Run(() => mqtt.MESForProduceAsync(newp, M141Config.zwoid, M141Config.numpro)).Result;
+
+            //                }
+            //                catch
+            //                {
+
+            //                }
+            //            }
+
+
+            //        }
+            //        PlcNumForAll = numplca;
+            //    }
+
+            //}
+
+            return msg;
+        }
+
+        object plcnumlock = new object();
+        int PlcNumForAll = -1;
+
+        private bool _isDemoStarted = false;
+        [ProcessMethod("OfflineDemo", "OfflineDemo", "绂荤嚎娴嬭瘯", InvokeType.CalibInvoke)]
+        public ResponseMessage OfflineDemo(IOperationConfig config, IDevice invokeDevice, IDevice sourceDevice)
+        {
+            if (config is OfflineDemoOperationConfig opConfig)
+            {
+                if (_isDemoStarted)
                 {
-                    if (string.IsNullOrEmpty(p.Result))
-                    {
-                        p.Result = "NG";
-                    }
+                    _isDemoStarted = false;
                 }
                 else
                 {
-                    if (string.IsNullOrEmpty(p.Result))
-                    {
-                        p.Result = Defaultclass.ClassName;
-                    }
+                    _isDemoStarted = true;
+                    OfflineDemoAsync(opConfig.ImageFolder, opConfig.IsOK, opConfig.SaveImageTime);
                 }
+            }
 
-                Plc1.WriteSingleAddress(1526, p.Result.ToUpper().Contains("NG") ? 2 : 1, out _);
+            return new ResponseMessage();
+        }
+
+        private async void OfflineDemoAsync(string imageFolder, bool isok, int saveimagetime)
+        {
+            await Task.Run(() =>
+            {
+                var imageFileNames = new DirectoryInfo(imageFolder).GetFiles().Select(u => u.FullName).ToList();
+
+                for (int i = 0; i < imageFileNames.Count; i++)
+                {
+                    if (!_isDemoStarted)
+                    {
+                        return;
+                    }
+                    var imageFile = Path.GetFileNameWithoutExtension(imageFileNames[i]);
+                    if (imageFile.EndsWith("Fit"))
+                    {
+                        continue;
+                    }
+                    if (imageFile.EndsWith("OK") & isok == false)
+                    {
+                        continue;
+                    }
+                    if (imageFile.EndsWith("NG") & isok == true)
+                    {
+                        continue;
+                    }
+                    var nameDatas = imageFile.Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries).ToList();
+                    if (nameDatas.Count != 5)
+                    {
+                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Error, $"鏂囦欢{imageFile}鍛藉悕涓嶇鍚堣鑼冿紝涓嶆墽琛岀绾挎祴璇�");
+                        continue;
+                    }
+
+                    var sn = nameDatas[0] + "_1";
+                    var imageSeq = nameDatas[2][^1].ToString();
+                    var measureBind = M141Config.MeasureBindCollection.FirstOrDefault(u => u.ImageSaveSeq == imageSeq);
+
+                    if (measureBind == null || !measureBind.IsFixed)
+                    {
+                        LogAsync(DateTime.Now, EnumHelper.LogLevel.Assist, $"绂荤嚎娴嬭瘯锛屽伐浣峽(measureBind == null ? "鏈尮閰�" : $"{measureBind.WorkPosition}鏈紑鍚�")}");
+                        continue;
+                    }
+
+                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"绂荤嚎娴嬭瘯锛屼骇鍝亄sn}寮�濮嬪伐浣峽measureBind.WorkPosition}绂荤嚎妫�娴�");
+                    MLImageSet imgSet = new MLImageSet();
+
+                    imgSet.HImage = new HalconDotNet.HImage(imageFileNames[i]);
+                    imgSet.PID = sn;
+
+                    var detectResults = ML.RunMLDetectionSync(imgSet, null, measureBind.DetectionId);
+                    Thread.Sleep(saveimagetime);
+                    imgSet.HImage?.Dispose();
+                    imgSet.HImage = null;
+
+                }
             });
-            Plc1.WriteSingleAddress(1516, 1, out _);
-
-            return msg;
         }
 
 
@@ -510,8 +712,8 @@
             ResponseMessage msg = new ResponseMessage();
 
             Plc2 = invokeDevice as PLCBase;
-            string message = "";
-            StartPrinter.Invoke(message);
+            string message = "nothing";
+            StartPrint(message);
             return msg;
         }
 
@@ -559,6 +761,9 @@
             });
             return specList;
         }
+
+
+
 
         protected void FillSpecResults(string pid, List<ISpec> detectSpec, List<double> results, string SEQUENCE)
         {
@@ -637,7 +842,7 @@
 
             UpdateDefectAsync(defects);
             var defectClass = GetDefectClassFromDefectList(defects);
-            UpdateResult(DateTime.Now, p.SN, defectClass.ClassName, "",p.ImagePaths);
+            UpdateResult(DateTime.Now, p.SN, defectClass.ClassName, "", p.ImagePaths);
 
             //浜у搧搴忓彿+1
             //Interlocked.Increment(ref _productIndex);
@@ -661,17 +866,20 @@
 
 
 
-        private void UpdateProductResultAsync(ProductModel p)
+        private void UpdateProductResultAsync(ProductModel p, string name)
         {
             UpdateProductResult(p, out bool isOK);
 
             _taskFactory.StartNew(() =>
             {
                 _csvHead = p.GetCSVHead(ref _specHeadList, ref _positionList);
-                CSVRecordAsync($"ProductRecord_{DateTime.Now.ToString("yyyyMMdd")}.csv", p.GetCSVData(_specHeadList, _positionList), _csvHead);
+                //CSVRecordAsync($"ProductRecord_{DateTime.Now.ToString("yyyyMMdd")}.csv", p.GetCSVData(_specHeadList, _positionList), _csvHead);
+                CSVRecordAsync(name, p.GetCSVData(_specHeadList, _positionList), _csvHead);
                 //_manager_P_Product.UpdateProductResult(p.ID, p.PID, p.SN, p.Result);
             });
 
+            //杩炵画NG鏁版嵁璁板綍
+            CheckContinuousNGAlarmAsync(p);
         }
 
 
@@ -783,6 +991,10 @@
 
 
 
+      
+
+
+
 
     }
 }

--
Gitblit v1.8.0