From 66e0168fe68ae5641041b2bceb3f0bb9640df09f Mon Sep 17 00:00:00 2001
From: quanzhou <quan.zhou@broconcentric.com>
Date: 星期五, 22 八月 2025 09:21:39 +0800
Subject: [PATCH] 添加MES超时队列匹配功能,vision错误自动累加和清零动作

---
 src/Bro.M141.Process/MyMQTT.cs           |   92 +++++++++++++++++-----
 src/Bro.M141_AOI1.Process/AOI1Process.cs |  127 +++++++++++++++++++++++++++++--
 2 files changed, 188 insertions(+), 31 deletions(-)

diff --git a/src/Bro.M141.Process/MyMQTT.cs b/src/Bro.M141.Process/MyMQTT.cs
index eba6af5..84ea30a 100644
--- a/src/Bro.M141.Process/MyMQTT.cs
+++ b/src/Bro.M141.Process/MyMQTT.cs
@@ -8,6 +8,7 @@
 using RabbitMQ.Client;
 using RabbitMQ.Client.Events;
 using SourceGrid.Cells.Editors;
+
 using Sunny.UI.Win32;
 using System;
 using System.Collections.Concurrent;
@@ -16,6 +17,7 @@
 using System.Text;
 using System.Threading.Tasks;
 using ZXing;
+
 
 namespace Bro.M141.Process
 {
@@ -32,6 +34,10 @@
 
         private readonly string _queue1 = "auto_line_mac_queue_bak";
         private readonly string _queue2 = "auto_line_mac_barcode_queue_bak";
+
+        List<MSGClass> MSGClasses = new List<MSGClass>();
+
+
 
         public RabbitMQHelper(string host = "10.2.20.114", int port = 15672, string user = "aoiUser", string pwd = "Chia@aoi")
         {
@@ -105,6 +111,15 @@
                 }
                 else
                 {
+                    if (MSGClasses.Any(u => u.zguid == obj?.zbguid))
+                    {
+                        var tem = MSGClasses.FirstOrDefault(u => u.zguid == obj?.zbguid);
+                        if (!string.IsNullOrEmpty(tem.receive))
+                        {
+                            tem.receive = message;
+                        }
+                    }
+
                     CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Information, $"RabbitMQ 绡叿 鏀跺埌鏈尮閰� zbguid锛歿obj?.zbguid}");
                 }
             }
@@ -133,12 +148,37 @@
                 CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"RabbitMQ 鐗╂枡 瑙f瀽寮傚父锛歿ex.Message}");
             }
         }
+        //Dictionary<string, Task> dic_ispass = new Dictionary<string, Task>();///value涓烘敹鍒版暟鎹�
 
         public async Task<string> MESForBasketAsync(string ztype, string zlsn, string zlpn, string zstatus, string zversion, int timeoutMes = 30000)
         {
+
+            string keystr = ztype + "_" + zlsn + "_" + zlpn + "_" + zstatus;
+
+            if (MSGClasses.Any(u => u.key == keystr))
+            {
+                var tem = MSGClasses.FirstOrDefault(u => u.key == keystr);
+
+                DateTime dt = DateTime.Now;
+                while ((DateTime.Now - dt).TotalMilliseconds < timeoutMes)
+                {
+                    if (!string.IsNullOrEmpty(tem.receive))
+                    {
+                        return tem.receive;
+                    }
+                    Thread.Sleep(1000);
+                }
+                MSGClasses.RemoveAll(u => u.key == keystr);
+                return tem.receive;
+            }
+
+
+
+
             var guid = Guid.NewGuid().ToString();
             var tcs = new TaskCompletionSource<string>(TaskCreationOptions.RunContinuationsAsynchronously);
             _pendingTask1[guid] = tcs;
+
 
             var msgObj = new AutoLineMacQueue
             {
@@ -158,15 +198,32 @@
             _sendChannel.BasicPublish("", "auto_line_mac_queue", null, body);
 
             var completed = await Task.WhenAny(tcs.Task, Task.Delay(timeoutMes));
+
+
             _pendingTask1.TryRemove(guid, out _);
             if (completed == tcs.Task)
+            {
                 return await tcs.Task;
+            }
             else
             {
+                MSGClasses.Add(new MSGClass()
+                {
+                    key = keystr,
+                    zguid = guid,
+                });
+
+
                 CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"RabbitMQ 绡叿 杩斿洖瓒呮椂1锛寊bguid={guid}");
                 return null;
             }
         }
+
+
+
+
+
+
 
         public async Task<string> MESForProduceAsync(ProductModel pro, string zversion, int znonum, string endtray = "N", string line = "NQ216", int timeoutMs = 3000)
         {
@@ -219,7 +276,7 @@
                 ["鍒掍激"] = "SZ2021"
             };
             const string DefaultErrorCode = "S31006";
-           
+
             string zr = DefectCodeMap.TryGetValue(pro.Result, out var code) ? code : DefaultErrorCode;
 
 
@@ -268,8 +325,15 @@
         }
     }
 
+    public class MSGClass
+    {
+        public string key { get; set; }
 
+        public string zguid { get; set; }
 
+        public string receive { get; set; }
+
+    }
 
 
 
@@ -525,21 +589,6 @@
 
 
 
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
     public class AutoLineMacQueue
     {
         /// <summary>
@@ -596,9 +645,7 @@
         /// 2锛氶婊¤浇锛堣〃绀�4鍙蜂綅棰勬弧杞斤紝3鍙蜂綅寮�濮嬪線4鍙蜂綅鎼椂锛�
         /// </summary>
         public string zstatus { get; set; }
-
     }
-
     public class AutoLineMacQueueBak
     {
         public string zbguid { get; set; }
@@ -615,14 +662,12 @@
         /// </summary>
         public string zwoid { get; set; }
     }
-
     public class AutoLineMacBarcodeQueue
     {
         /// <summary>
         /// 鍞竴ID
         /// </summary>
         public string zbguid { get; set; } = Guid.NewGuid().ToString();
-
         /// <summary>
         /// 鏃堕棿鎴筹紝绮剧‘鍒拌�楃
         /// </summary>
@@ -655,7 +700,7 @@
         /// 榛樿锛歂;闆舵暟灏剧洏锛歒
         /// </summary>
         public string endtray { get; set; } = "N";
-              /// <summary>
+        /// <summary>
         /// 姣忎釜ztype涓嬪搴旂嫭绔嬩竴涓簭鍒楀彿锛屼竴涓笟鍔″姩浣滀竴涓紝浠�0寮�濮嬶紝
         /// 姣忔鍔�1锛屽埌杈鹃槇鍊煎悗鍙噸缃紙姣斿100鍚庝粠0寮�濮嬶級锛屽彧瑕佸拰涓婁竴
         /// 娆$殑涓嶄竴鏍峰嵆鍙�
@@ -683,6 +728,7 @@
         /// </summary>
         public string tary_label { get; set; }
     }
-
-
 }
+
+
+
diff --git a/src/Bro.M141_AOI1.Process/AOI1Process.cs b/src/Bro.M141_AOI1.Process/AOI1Process.cs
index 6a88850..e2a093d 100644
--- a/src/Bro.M141_AOI1.Process/AOI1Process.cs
+++ b/src/Bro.M141_AOI1.Process/AOI1Process.cs
@@ -249,6 +249,25 @@
                                 }
                                 else
                                 {
+                                    try
+                                    {
+                                        if (zwordstr.Contains("version閿欒"))
+                                        {
+                                            Regex reg = new Regex("[0-9]+", RegexOptions.IgnoreCase | RegexOptions.Singleline, TimeSpan.FromSeconds(2));
+                                            MatchCollection matches = reg.Matches(obj.zerrmsg);
+                                            if (matches[0] != null && matches.Count == 3)
+                                            {
+                                                ConfigAOI1.mesnum2 = int.Parse(matches[0].Value)+1;
+                                            }
+                                            else
+                                            {
+                                                ConfigAOI1.mesnum2 = 0;
+                                            }
+                                        }
+                                    }
+                                    catch (Exception ex) 
+                                    {
+                                    }                                 
                                     Plc1.WriteSingleAddress(1524, 2, out _);
                                     Plc1.WriteSingleAddress(1514, 1, out _);
                                     LogAsync(DateTime.Now, EnumHelper.LogLevel.Information, $"RabbitMQ zwoid鑾峰彇澶辫触 {zwordstr} ");
@@ -547,6 +566,25 @@
                             }
                             else
                             {
+                                try
+                                {
+                                    if (Msg.Contains("version閿欒"))
+                                    {
+                                        Regex reg = new Regex("[0-9]+", RegexOptions.IgnoreCase | RegexOptions.Singleline, TimeSpan.FromSeconds(2));
+                                        MatchCollection matches = reg.Matches(obj.zerrmsg);
+                                        if (matches[0] != null && matches.Count == 3)
+                                        {
+                                            ConfigAOI1.mesnum1 = int.Parse(matches[0].Value)+ 1;
+                                        }
+                                        else
+                                        {
+                                            ConfigAOI1.mesnum1 = 0;
+                                        }
+                                    }
+                                }
+                                catch (Exception ex)
+                                {
+                                }
                                 Plc1.WriteSingleAddress(1590, 2, out _);
                             }
                             LogAsync(DateTime.Now, obj?.zstatus == "200" ? EnumHelper.LogLevel.Information : EnumHelper.LogLevel.Exception, $"RabbitMQ 绡叿  杩涙枡鍙g┖绡� 涓婁紶{(obj?.zstatus == "200" ? "鎴愬姛" : "澶辫触")}   {obj?.zstatus}");
@@ -554,6 +592,7 @@
                         }
                         catch
                         {
+                            
                             LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"RabbitMQ 绡叿  杩涙枡鍙g┖绡� 鑾峰彇杩斿洖鍊艰В鏋愬紓甯�  杩斿洖鏁版嵁{Msg} ");
                             Plc1.WriteSingleAddress(1590, 2, out _);
                         }
@@ -608,6 +647,25 @@
                             }
                             else
                             {
+                                try
+                                {
+                                    if (Msg.Contains("version閿欒"))
+                                    {
+                                        Regex reg = new Regex("[0-9]+", RegexOptions.IgnoreCase | RegexOptions.Singleline, TimeSpan.FromSeconds(2));
+                                        MatchCollection matches = reg.Matches(obj.zerrmsg);
+                                        if (matches[0] != null && matches.Count == 3)
+                                        {
+                                            ConfigAOI1.mesnum1 = int.Parse(matches[0].Value) + 1;
+                                        }
+                                        else
+                                        {
+                                            ConfigAOI1.mesnum1 = 0;
+                                        }
+                                    }
+                                }
+                                catch (Exception ex)
+                                {
+                                }
                                 Plc1.WriteSingleAddress(1590, 2, out _);
                             }
 
@@ -658,6 +716,25 @@
                         }
                         else
                         {
+                            try
+                            {
+                                if (Msg.Contains("version閿欒"))
+                                {
+                                    Regex reg = new Regex("[0-9]+", RegexOptions.IgnoreCase | RegexOptions.Singleline, TimeSpan.FromSeconds(2));
+                                    MatchCollection matches = reg.Matches(obj.zerrmsg);
+                                    if (matches[0] != null && matches.Count == 3)
+                                    {
+                                        ConfigAOI1.mesnum3 = int.Parse(matches[0].Value)+1;
+                                    }
+                                    else
+                                    {
+                                        ConfigAOI1.mesnum3 = 0;
+                                    }
+                                }
+                            }
+                            catch (Exception ex)
+                            {
+                            }
                             Plc1.WriteSingleAddress(1591, 2, out _);
                         }
                         LogAsync(DateTime.Now, obj?.zstatus == "200" ? EnumHelper.LogLevel.Information : EnumHelper.LogLevel.Exception, $"RabbitMQ 绡叿  涓嬫枡鍙g┖绡� 涓婁紶{(obj?.zstatus == "200" ? "鎴愬姛" : "澶辫触")}   {obj?.zstatus}");
@@ -703,7 +780,29 @@
                         {
                             ConfigAOI1.mesnum3++;
                         }
-                        LogAsync(DateTime.Now, obj?.zstatus == "200" ? EnumHelper.LogLevel.Information : EnumHelper.LogLevel.Exception, $"RabbitMQ 绡叿  涓嬫枡鍙f弧杞�1 涓婁紶{(obj?.zstatus == "200" ? "鎴愬姛" : "澶辫触")}   {obj?.zstatus}");
+                        else
+                        {
+                            try
+                            {
+                                if (Msg.Contains("version閿欒"))
+                                {
+                                    Regex reg = new Regex("[0-9]+", RegexOptions.IgnoreCase | RegexOptions.Singleline, TimeSpan.FromSeconds(2));
+                                    MatchCollection matches = reg.Matches(obj.zerrmsg);
+                                    if (matches[0] != null && matches.Count == 3)
+                                    {
+                                        ConfigAOI1.mesnum3 = int.Parse(matches[0].Value) + 1;
+                                    }
+                                    else
+                                    {
+                                        ConfigAOI1.mesnum3 = 0;
+                                    }
+                                }
+                            }
+                            catch (Exception ex)
+                            {
+                            }
+                        }
+                            LogAsync(DateTime.Now, obj?.zstatus == "200" ? EnumHelper.LogLevel.Information : EnumHelper.LogLevel.Exception, $"RabbitMQ 绡叿  涓嬫枡鍙f弧杞�1 涓婁紶{(obj?.zstatus == "200" ? "鎴愬姛" : "澶辫触")}   {obj?.zstatus}");
                     }
                     catch
                     {
@@ -714,8 +813,6 @@
                 {
                     LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"RabbitMQ 绡叿  涓嬫枡鍙f弧杞�1 鑾峰彇杩斿洖鍊艰В鏋愬紓甯�  杩斿洖鏁版嵁null ");
                 }
-
-
                 Thread.Sleep(1000);
                 //mqtt.Send("4", "DS02217", ConfigAOI1.Dicbasketcode.ContainsKey(num) ? ConfigAOI1.Dicbasketcode[num] : ConfigAOI1.basketcode, "1", num.ToString());
                 Msg = Task.Run(() => mqtt.MESForBasketAsync("4", "DS02217", ConfigAOI1.PlcAndBasketcodes.Any(u => u.id == num) ? ConfigAOI1.PlcAndBasketcodes.FirstOrDefault(u => u.id == num).code : "NoRead", "1", ConfigAOI1.mesnum4.ToString())).Result;
@@ -731,6 +828,25 @@
                         }
                         else
                         {
+                            try
+                            {
+                                if (Msg.Contains("version閿欒"))
+                                {
+                                    Regex reg = new Regex("[0-9]+", RegexOptions.IgnoreCase | RegexOptions.Singleline, TimeSpan.FromSeconds(2));
+                                    MatchCollection matches = reg.Matches(obj.zerrmsg);
+                                    if (matches[0] != null && matches.Count == 3)
+                                    {
+                                        ConfigAOI1.mesnum4 = int.Parse(matches[0].Value) + 1;
+                                    }
+                                    else
+                                    {
+                                        ConfigAOI1.mesnum4 = 0;
+                                    }
+                                }
+                            }
+                            catch (Exception ex)
+                            {
+                            }
                             Plc1.WriteSingleAddress(1591, 2, out _);
                         }
                         LogAsync(DateTime.Now, obj?.zstatus == "200" ? EnumHelper.LogLevel.Information : EnumHelper.LogLevel.Exception, $"RabbitMQ 绡叿  涓嬫枡鍙f弧杞� 涓婁紶{(obj?.zstatus == "200" ? "鎴愬姛" : "澶辫触")}   {obj?.zstatus}");
@@ -753,10 +869,5 @@
             }
             return msg;
         }
-
-
-
-
-
     }
 }

--
Gitblit v1.8.0