Jelajahi Sumber

扩展MES接口支持电池SN查询及日志优化

新增StationGetExAsync方法,支持通过电池SN查询MES产品SN,并在Management中替换原有MES查询调用。扫码结果统一转大写,增强日志记录,优化PLC状态同步处理。
徐孝锋 6 bulan lalu
induk
melakukan
075e3cfe99

+ 6 - 1
TeamAAS-VM/Core/Management.cs

@@ -2165,6 +2165,7 @@ namespace TeamAAS_VP.Core
                                     if (qrCodeResult && outputCollection.Contains("QR"))
                                     {
                                         code = outputCollection["QR"].Value as string;
+                                        code = code.ToUpper();
                                         if (!string.IsNullOrEmpty(code))
                                         {
                                             SendTaskMessage($"二维码扫码成功:{code}", MessageLevel.Info);
@@ -2201,7 +2202,7 @@ namespace TeamAAS_VP.Core
                                     _LastMesCheckTime[i] = DateTime.UtcNow;
                                     LogHelper.WriteLogMes($"【mes开始QUERY】");
                                     SendTaskMessage($"mes开始QUERY", MessageLevel.Info);
-                                    (bool isSuccess, string response) = await _mesService.StationGetAsync(code, "", 2);//询问mes
+                                    (bool isSuccess, string response) = await _mesService.StationGetExAsync(code, "", 2);//基于电池的SN获取产品SN,询问mes
                                     if (i == 0)
                                     {
                                         SendTaskMessage($"收到当前过站检查请求(当站是否生产)...", MessageLevel.Debug);
@@ -2258,6 +2259,10 @@ namespace TeamAAS_VP.Core
 
                                         LogHelper.WriteLogMes($"询问mes失败");
                                         SendTaskMessage($"询问mes失败:{response}", MessageLevel.Error);
+
+                                        //产品发生改变并且供料器发生了变化时,需要通知PLC进行取料更改
+                                        await plc.WriteNodeAsync(addressConfig.Out_ScrewFeederIsChanged.Address, (Int16)1); //不发生改变
+                                        await plc.WriteNodeAsync(string.Format(addressConfig.Out_MesStatus.Address, i), (Int16)2);
                                     }
                                     LogHelper.WriteLogMes($"【mes结束QUERY】");
                                     SendTaskMessage($"mes结束QUERY", MessageLevel.Info);

+ 2 - 0
TeamAAS-VM/Interfaces/IMesService.cs

@@ -45,6 +45,8 @@ namespace TeamAAS_VP.Interfaces
         /// </remarks>
         Task<(bool IsSuccess, string Response)> StationGetAsync(string sn,string comp, double timeoutInSeconds);
 
+        Task<(bool IsSuccess, string Response)> StationGetExAsync(string sn, string comp, double timeoutInSeconds);
+
         /// <summary>
         /// 使用配置的提交(Submit)URL 发起 GET 请求,提交过站结果及时间信息。
         /// </summary>

+ 81 - 0
TeamAAS-VM/Services/MesService.cs

@@ -146,6 +146,79 @@ namespace TeamAAS_VP.Services
             return result;
         }
 
+        /// <summary>
+        /// 向配置中指定的 MES 站点 URL 发起 GET 请求(用于过站查询)。
+        /// </summary>
+        /// <param name="sn">要查询的序列号(SN)。</param>
+        /// <param name="cancellationToken">可选的取消令牌,用于请求超时或取消。</param>
+        /// <returns>
+        /// 返回元组 (IsSuccess, Response):
+        /// - IsSuccess: 请求是否视为成功(即 MES 返回了预期的 "SFC_OK" 且包含 "unit_process_check=OK")。
+        /// - Response: 原始响应文本或错误信息说明(如 "MES功能未启用!"、"Missing MES station URL"、"Canceled" 等)。
+        /// </returns>
+        /// <remarks>
+        /// 行为说明:
+        /// - 从配置获取设备信息,若 MES 未启用则直接返回 IsSuccess=true 且 Response 为提示文本(不视为错误)。
+        /// - 若未配置 MES URL 则返回 IsSuccess=false 与错误文本。
+        /// - 使用配置的 URL(通过 <see cref="string.Format"/> 填充 sn)发起 GET 请求,最多重试 10 次。
+        /// - 只有当响应包含 "SFC_OK" 且包含 "unit_process_check=OK" 时视为成功并立即返回。
+        /// - 捕获 <see cref="OperationCanceledException"/> 返回 "Canceled",捕获其它异常则返回异常消息。
+        /// - 若实例已被释放则抛出 <see cref="ObjectDisposedException"/>。
+        /// </remarks>
+        public async Task<(bool IsSuccess, string Response)> StationGetExAsync(string sn, string comp, CancellationToken cancellationToken = default)
+        {
+            // 如果已经释放,则抛出异常,防止在已释放资源上继续操作。
+            if (_disposed) throw new ObjectDisposedException(nameof(MesService));
+
+            var device = _configService.GetDeviceInfo();
+            if (device == null || !device.EnableMES)
+            {
+                return (true, "MES功能未启用!");
+            }
+            if (string.IsNullOrWhiteSpace(device.MesUrl))
+                return (false, "Missing MES station URL");
+
+            string url = "";
+            url = $"{device.MesUrl}?c=QUERY_RECORD&line={device.Line}&station={device.Station}&fixtureid={device.FixtureId}&sn={sn}&p=sn,message";
+
+            LogHelper.WriteLogMes($"【询问URL】{url}");
+            SendTaskMessage($"【询问URL】{url}", MessageLevel.Info);
+            (bool IsSuccess, string Response) result = (false, string.Empty);
+
+            for (int i = 0; i < 3; i++)
+            {
+                try
+                {
+                    using (var rsp = await _httpClient.GetAsync(url))
+                    {
+                        var text = await rsp.Content.ReadAsStringAsync();
+                        LogHelper.WriteLogMes($"【HTTP接收】{text}");
+                        SendTaskMessage($"【HTTP接收】{text}", MessageLevel.Info);
+
+                        // "SFC_OK" 代表和 MES 连接成功
+                        if (text.Contains("SFC_OK"))
+                        {
+                            if (text.Contains("message=OK"))
+                            {
+                                return (true, text);
+                            }
+                        }
+                        result = (false, text);
+                    }
+                }
+                catch (OperationCanceledException)
+                {
+                    result = (false, "Canceled");
+                }
+                catch (Exception ex)
+                {
+                    result = (false, ex.Message);
+                }
+            }
+            return result;
+        }
+
+
         /// <summary>
         /// 向 MES 发起站点查询请求,使用超时(秒)作为便捷重载。
         /// </summary>
@@ -160,6 +233,14 @@ namespace TeamAAS_VP.Services
             }
         }
 
+        public async Task<(bool IsSuccess, string Response)> StationGetExAsync(string sn, string comp, double timeoutInSeconds)
+        {
+            using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(timeoutInSeconds)))
+            {
+                return await StationGetExAsync(sn, comp, cts.Token);
+            }
+        }
+
         /// <summary>
         /// 向配置中指定的 MES 提交 URL 发起 GET 请求(用于提交测试结果/记录)。
         /// </summary>