quanzhou
2025-08-22 66e0168fe68ae5641041b2bceb3f0bb9640df09f
添加MES超时队列匹配功能,vision错误自动累加和清零动作
2个文件已修改
219 ■■■■ 已修改文件
src/Bro.M141.Process/MyMQTT.cs 92 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/Bro.M141_AOI1.Process/AOI1Process.cs 127 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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 物料 解析异常:{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,zbguid={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 @@
        /// 默认:N;零数尾盘:Y
        /// </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; }
    }
}
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 篮具  进料口空篮 上传{(obj?.zstatus == "200" ? "成功" : "失败")}   {obj?.zstatus}");
@@ -554,6 +592,7 @@
                        }
                        catch
                        {
                            LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"RabbitMQ 篮具  进料口空篮 获取返回值解析异常  返回数据{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 篮具  下料口空篮 上传{(obj?.zstatus == "200" ? "成功" : "失败")}   {obj?.zstatus}");
@@ -703,7 +780,29 @@
                        {
                            ConfigAOI1.mesnum3++;
                        }
                        LogAsync(DateTime.Now, obj?.zstatus == "200" ? EnumHelper.LogLevel.Information : EnumHelper.LogLevel.Exception, $"RabbitMQ 篮具  下料口满载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 篮具  下料口满载1 上传{(obj?.zstatus == "200" ? "成功" : "失败")}   {obj?.zstatus}");
                    }
                    catch
                    {
@@ -714,8 +813,6 @@
                {
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"RabbitMQ 篮具  下料口满载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 篮具  下料口满载 上传{(obj?.zstatus == "200" ? "成功" : "失败")}   {obj?.zstatus}");
@@ -753,10 +869,5 @@
            }
            return msg;
        }
    }
}