Ver Fonte

调整螺丝机Mes结果上传

844184169 há 6 meses atrás
pai
commit
6a7e1286c2

+ 12 - 5
TeamAAS-VM/Core/Management.cs

@@ -1291,6 +1291,7 @@ namespace TeamAAS_VP.Core
         string[] wo { get; set; } = new string[10];//产品wo
         string[] color { get; set; } = new string[10];//产品color
         Dictionary<string, string> dataToMes = new Dictionary<string, string>();//上传mes的数据
+        List<string> resultToMes = new List<string>();//上传mes的数据
         /// <summary>
         /// PLC命令触发时
         /// </summary>
@@ -1877,13 +1878,14 @@ namespace TeamAAS_VP.Core
                             if (lockResult.Number == 1)
                             {
                                 dataToMes.Clear();
+                                resultToMes.Clear();
                                 lockResultsAll.Clear();
                             }
 
                             //基于当前的锁付结果和产品的规格参数判断锁付结果是否合格
                             var stationConfig = _configService.GetDeviceInfo(0);
                             bool torqueRes = false, turnRes = false, pressureRes = false;
-                            int lastRes = 2;//默认不合格,只有当扭力、圈数、压力都在设定的上下限范围内时,才判定为锁付合格
+                            bool lastRes = false;//默认不合格,只有当扭力、圈数、压力都在设定的上下限范围内时,才判定为锁付合格
                             if (lockResult.LockTorque >= lockResult.TargetTorqueLowerLimit && lockResult.LockTorque <= lockResult.TargetTorqueUpperLimit)
                             {
                                 torqueRes = true;
@@ -1898,13 +1900,17 @@ namespace TeamAAS_VP.Core
                             }
                             if (torqueRes == true && turnRes == true && pressureRes == true)//当扭力、圈数、压力都在设定的上下限范围内时,才判定为锁付合格
                             {
-                                lastRes = 1;
+                                lastRes = true;
                             }
                             dataToMes.Add($"Torque{lockResult.Number}", $"{lockResult.LockTorque:F2}");
                             dataToMes.Add($"Turn{lockResult.Number}", $"{lockResult.LockTurns:F2}");
                             dataToMes.Add($"Pressure{lockResult.Number}", $"{lockResult.Pressure:F2}");
-                            dataToMes.Add($"LastResult{lockResult.Number}", $"{lastRes}");
-                            SendTaskMessage($"螺丝序号:{lockResult.Number},扭力:{lockResult.LockTorque},压力:{lockResult.Pressure},圈数:{lockResult.LockTurns},结果:{lastRes}", MessageLevel.Info);
+                            //dataToMes.Add($"LastResult{lockResult.Number}", $"{lastRes}");
+                            if (!lastRes)//如果不合格,则在MES结果中添加"Screw{lockResult.Number}_Fail"
+                            {
+                                resultToMes.Add($"Screw{lockResult.Number}_Fail");
+                            }
+                            SendTaskMessage($"螺丝序号:{lockResult.Number},扭力:{lockResult.LockTorque},压力:{lockResult.Pressure},圈数:{lockResult.LockTurns}", MessageLevel.Info);
 
                             lockResultsAll.Add(lockResult);
                             App.Current.Dispatcher.Invoke(() =>
@@ -2472,7 +2478,8 @@ namespace TeamAAS_VP.Core
                                         SendTaskMessage($"mes开始ADD", MessageLevel.Info);
                                         //上传mes
                                         //这里现在上传的结果都是锁付站的,如果存在其他附属站的数据需要上传时,将不适用,要根据实际情况进行调整
-                                        (bool isSuccess, string response) = await _mesService.SubmitGetAsync(ProductMainSN[i], "", dataToMes, resultIndex == 1 ? true : false, _LastMesCheckTime[i], serverTimeResult.Now);
+                                        //(bool isSuccess, string response) = await _mesService.SubmitGetAsync(ProductMainSN[i], "", dataToMes, resultIndex == 1 ? true : false, _LastMesCheckTime[i], serverTimeResult.Now);
+                                        (bool isSuccess, string response) = await _mesService.SubmitGetExAsync(ProductMainSN[i], "", dataToMes, resultToMes, _LastMesCheckTime[i], serverTimeResult.Now);
                                         if (isSuccess)
                                         {
                                             LogHelper.WriteLogMes($"上传mes成功");

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

@@ -112,6 +112,8 @@ namespace TeamAAS_VP.Interfaces
         /// </remarks>
         Task<(bool IsSuccess, string Response)> SubmitGetAsync(string sn, string comp, Dictionary<string, string> data, bool isSuccess, DateTime start_time, DateTime stop_time, CancellationToken cancellationToken = default);
 
+        Task<(bool IsSuccess, string Response)> SubmitGetExAsync(string sn, string comp, Dictionary<string, string> data, List<string> results, DateTime start_time, DateTime stop_time, CancellationToken cancellationToken = default);
+
 
         /// <summary>
         /// 使用配置的提交(Submit)URL 发起 GET 请求,提交过站结果及时间信息,并使用指定的超时时间(秒)。

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

@@ -1,3 +1,4 @@
+using Cognex.VisionPro.QuickBuild.Implementation.Internal;
 using NPOI.SS.Formula.Functions;
 using Prism.Events;
 using Prism.Ioc;
@@ -640,6 +641,92 @@ namespace TeamAAS_VP.Services
             return result;
         }
 
+        public async Task<(bool IsSuccess, string Response)> SubmitGetExAsync(string sn, string comp, Dictionary<string, string> data, List<string> results, DateTime start_time, DateTime stop_time, 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 submit URL");
+            if (device.F == "PTL")
+            {
+                if (string.IsNullOrEmpty(device.comp))
+                {
+                    return (false, "PTL模式下comp不能为空");
+                }
+            }
+
+            //string res = isSuccess ? "Pass" : "Fail";
+            string res = "Pass";
+            if (results.Count > 0)//如果有多个结果,则用逗号连接起来传给MES, MES端再解析
+            {
+                res = string.Join(",", results);
+            }
+
+            StringBuilder sb = new StringBuilder();
+            if (device.F == "PTL")
+            {
+                sb.Append($"{device.MesUrl}?c=ADD_RECORD&line={device.Line}&station={device.Station}&fixtureid={device.FixtureId}&sn={sn}&f=PTL&comp={device.comp}:{comp}");
+                foreach (var item in data)
+                {
+                    sb.Append($"&{item.Key}={item.Value}");
+                }
+                sb.Append($"&result={res}&start_time={start_time.ToString("yyyy-MM-dd HH:mm:ss")}&stop_time={stop_time.ToString("yyyy-MM-dd HH:mm:ss")}");
+            }
+            else
+            {
+                sb.Append($"{device.MesUrl}?c=ADD_RECORD&line={device.Line}&station={device.Station}&fixtureid={device.FixtureId}&sn={sn}&f=PQC");
+                foreach (var item in data)
+                {
+                    sb.Append($"&{item.Key}={item.Value}");
+                }
+                sb.Append($"&result={res}&start_time={start_time.ToString("yyyy-MM-dd HH:mm:ss")}&stop_time={stop_time.ToString("yyyy-MM-dd HH:mm:ss")}");
+            }
+            string url = sb.ToString();
+
+            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, cancellationToken))
+                    {
+                        var text = await rsp.Content.ReadAsStringAsync();
+                        LogHelper.WriteLogMes($"【HTTP接收】{text}");
+                        SendTaskMessage($"【HTTP接收】{text}", MessageLevel.Info);
+                        // "SFC_OK" 代表和 MES 连接成功
+                        if (text.Contains("SFC_OK"))
+                        {
+                            // 如果有附加信息(多行),代表此 SN 数据有问题,视为失败并返回该文本
+                            if (text.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries).Length > 1)
+                            {
+                                return (false, text);
+                            }
+                            return (true, text);
+                        }
+                        result = (false, text);
+                    }
+                }
+                catch (OperationCanceledException)
+                {
+                    result = (false, "Canceled");
+                }
+                catch (Exception ex)
+                {
+                    result = (false, ex.Message);
+                }
+            }
+            return result;
+        }
+
         /// <summary>
         /// 向 MES 提交请求的超时重载,使用超时(秒)作为便捷参数。
         /// </summary>