From 2ecfd549b3819fc92244f67adfbf00902d2e0062 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?liming=20=E8=94=A1?= Date: Tue, 14 Jul 2026 16:09:25 +0800 Subject: [PATCH] =?UTF-8?q?mes=E9=9B=86=E6=88=90=E5=A2=9E=E5=8A=A0MesCallR?= =?UTF-8?q?esult?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JY.Inspection/HomeForm.cs | 11 +++--- JY.Inspection/JY.Inspection.csproj | 4 +- JY.Inspection/Mes/MESDataCombin.cs | 38 +++++++++--------- JY.MES/Entity/MesCallResult.cs | 34 ++++++++++++++++ JY.MES/Enum/MesErrorType.cs | 18 +++++++++ JY.MES/Exceptions/MesNetworkException.cs | 17 ++++++++ JY.MES/JY.MES.csproj | 5 +++ JY.MES/MES/RespArrivalStation.cs | 20 +--------- JY.MES/MES/RespBase.cs | 34 ++++++++++++++++ JY.MES/MES/RespExitStation.cs | 2 +- JY.MES/MES/RespProductResult.cs | 15 ++++++++ JY.MES/MESApiHelper.cs | 49 ++++++++++++++++++++++-- 12 files changed, 198 insertions(+), 49 deletions(-) create mode 100644 JY.MES/Entity/MesCallResult.cs create mode 100644 JY.MES/Enum/MesErrorType.cs create mode 100644 JY.MES/Exceptions/MesNetworkException.cs create mode 100644 JY.MES/MES/RespBase.cs create mode 100644 JY.MES/MES/RespProductResult.cs diff --git a/JY.Inspection/HomeForm.cs b/JY.Inspection/HomeForm.cs index 74d42b3..6a473a2 100644 --- a/JY.Inspection/HomeForm.cs +++ b/JY.Inspection/HomeForm.cs @@ -12,6 +12,7 @@ using JY.Inspection.Frm; using JY.Inspection.Mes; using JY.MES; using JY.MES.Entity; +using JY.MES.Enum; using JY.MES.MES; using JY.Model; using JY.Model.Excel; @@ -2434,17 +2435,17 @@ namespace JY.Inspection if (m != null) { #region 3、产品结果加工参数 - MesResponse resMes = ToMesData.ProductResultParameters(m); - if (!resMes.success) + MesCallResult resMes = ToMesData.ProductResultParameters(m); + if (!resMes.IsSuccess) { - if (resMes.error == 99) + if (resMes.ErrorType == MesErrorType.NetworkError || resMes.ErrorType == MesErrorType.NetworkTimeout) { omronPLCCom.lstMcUI[0].WriteDReg("W249", (short)1);//MES网络异常 LogManagerControl.AddLog("结果数据上传MES超时或无法访问MES服务器", LogAddtype.local, Logtype.Error); } else { - LogManagerControl.AddLog("结果数据上传MES失败NG" + resMes.message, LogAddtype.local, Logtype.Error); + LogManagerControl.AddLog("结果数据上传MES失败NG" + resMes.OriResponse.Message, LogAddtype.local, Logtype.Error); } } #endregion @@ -2469,7 +2470,7 @@ namespace JY.Inspection } } }; - RespArrivalStation resp = ToMesData.PostProductExitStationData(req); + RespExitStation resp = ToMesData.PostProductExitStationData(req); if (resp == null || !resp.Success) { diff --git a/JY.Inspection/JY.Inspection.csproj b/JY.Inspection/JY.Inspection.csproj index 34c31c4..31256c3 100644 --- a/JY.Inspection/JY.Inspection.csproj +++ b/JY.Inspection/JY.Inspection.csproj @@ -487,6 +487,8 @@ - + + + \ No newline at end of file diff --git a/JY.Inspection/Mes/MESDataCombin.cs b/JY.Inspection/Mes/MESDataCombin.cs index ab76860..c0177db 100644 --- a/JY.Inspection/Mes/MESDataCombin.cs +++ b/JY.Inspection/Mes/MESDataCombin.cs @@ -1,4 +1,7 @@ using JY.MES; +using JY.MES.Entity; +using JY.MES.Enum; +using JY.MES.Exceptions; using JY.MES.MES; using JY.Model; using JY.Utility; @@ -7,6 +10,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; +using System.Net; namespace JY.Inspection.Mes { @@ -25,7 +29,7 @@ namespace JY.Inspection.Mes /// /// /// - MesResponse ProductResultParameters(BlankingData m); + MesCallResult ProductResultParameters(BlankingData m); /// /// 产品进站(包装) @@ -131,9 +135,9 @@ namespace JY.Inspection.Mes #endregion #region 2、结果加工参数 - public MesResponse ProductResultParameters(BlankingData m) + public MesCallResult ProductResultParameters(BlankingData m) { - MesResponse mesResponse = new MesResponse(); + MesCallResult resp = new MesCallResult(); DateTime nowTime = DateTime.Now; string josnTxtMsg = string.Empty; try @@ -184,7 +188,8 @@ namespace JY.Inspection.Mes } // 生成TagDataVoListItem - tdvls.Add(new TagDataVOListItem() { + tdvls.Add(new TagDataVOListItem() + { tagCode = cfgItem.MesItemCode, tagValue = item.Value, tagTime = m.OutTime, @@ -209,30 +214,25 @@ namespace JY.Inspection.Mes var sw = new Stopwatch(); sw.Start(); - string mesRes = MESApiHelper.HttpPostJsonAPI(Global.systemConfig.ResultProcessMesUrl, p, Global.systemConfig.MesRequestTime); + var mesCallResult = MESApiHelper.HttpPostJsonAPI(Global.systemConfig.ResultProcessMesUrl, p, Global.systemConfig.MesRequestTime); sw.Stop(); - var res = JsonConvert.DeserializeObject(mesRes); - mesResponse.success = res.success.Value; - if (!res.success.Value) + if (mesCallResult?.OriResponse?.Success != true) { - mesResponse.code = res.code.Value; - mesResponse.message = res.message.Value; - if (mesRes.Contains("category")) - mesResponse.category = res.category.Value; - if (res.error != null) - mesResponse.error = (int)res.error.Value; + resp.IsSuccess = false; + resp.ErrorType = MesErrorType.BusinessError; + resp.ErrorMessage = mesCallResult?.OriResponse.Message ?? "MES业务处理失败"; } - new MesLog().LogProductResult(nowTime, p, mesRes, sw.ElapsedMilliseconds); + new MesLog().LogProductResult(nowTime, p, mesCallResult.OriRespJsonStr, sw.ElapsedMilliseconds); } catch (Exception ex) { - mesResponse.success = false; - mesResponse.error = 9; - mesResponse.message = "结果加工参数上传异常:" + ex.Message; + resp.IsSuccess = false; + resp.ErrorType = MesErrorType.ClientError; + resp.ErrorMessage = "结果加工参数上传异常:" + ex.Message; } - return mesResponse; + return resp; } #endregion diff --git a/JY.MES/Entity/MesCallResult.cs b/JY.MES/Entity/MesCallResult.cs new file mode 100644 index 0000000..d1a204e --- /dev/null +++ b/JY.MES/Entity/MesCallResult.cs @@ -0,0 +1,34 @@ +using JY.MES.Enum; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JY.MES.Entity +{ + public class MesCallResult + { + public bool IsSuccess { get; set; } + + /// + /// 错误类型枚举 + /// + public MesErrorType ErrorType { get; set; } + + /// + /// 错误描述 + /// + public string ErrorMessage { get; set; } + + /// + /// mes实际返回的响应 + /// + public T OriResponse { get; set; } + + /// + /// mes实际返回的响应json字符串 + /// + public string OriRespJsonStr { get; set; } + } +} diff --git a/JY.MES/Enum/MesErrorType.cs b/JY.MES/Enum/MesErrorType.cs new file mode 100644 index 0000000..857c67c --- /dev/null +++ b/JY.MES/Enum/MesErrorType.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JY.MES.Enum +{ + public enum MesErrorType + { + None, // 无错误 + NetworkError, // 网络错误(DNS、连接拒绝等) + NetworkTimeout, // 网络超时 + BusinessError, // 业务错误(200但业务失败) + ParseError, // 响应解析失败 + ClientError, // 客户端错误(400) + } +} diff --git a/JY.MES/Exceptions/MesNetworkException.cs b/JY.MES/Exceptions/MesNetworkException.cs new file mode 100644 index 0000000..3c6f3b8 --- /dev/null +++ b/JY.MES/Exceptions/MesNetworkException.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JY.MES.Exceptions +{ + public class MesNetworkException : Exception + { + public MesNetworkException(string message) : base(message) + { } + + public MesNetworkException() : base("MES网络异常") + { } + } +} diff --git a/JY.MES/JY.MES.csproj b/JY.MES/JY.MES.csproj index 62328a7..e31590b 100644 --- a/JY.MES/JY.MES.csproj +++ b/JY.MES/JY.MES.csproj @@ -48,6 +48,7 @@ + @@ -56,6 +57,8 @@ + + @@ -71,7 +74,9 @@ + + diff --git a/JY.MES/MES/RespArrivalStation.cs b/JY.MES/MES/RespArrivalStation.cs index 9e2752c..692ba21 100644 --- a/JY.MES/MES/RespArrivalStation.cs +++ b/JY.MES/MES/RespArrivalStation.cs @@ -9,26 +9,8 @@ namespace JY.MES.MES /// /// 产品进站响应信息 /// - public class RespArrivalStation + public class RespArrivalStation : RespBase { - /// - /// 错误代码,成功不返回 - /// - public string Code { get; set; } - /// - /// 成功标识,false失败 true 成功 - /// - public bool Success { get; set; } - - /// - /// 数据处理消息,成功不返回 - /// - public string Message { get; set; } - - /// - /// 报错消息类型 A:停线、B:排出、C:忽略(需警示) - /// - public string Category { get; set; } } } diff --git a/JY.MES/MES/RespBase.cs b/JY.MES/MES/RespBase.cs new file mode 100644 index 0000000..41adf16 --- /dev/null +++ b/JY.MES/MES/RespBase.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JY.MES.MES +{ + /// + /// 1440项目对应MES响应消息基类 + /// + public abstract class RespBase + { + /// + /// 错误代码,成功不返回 + /// + public string Code { get; set; } + + /// + /// 成功标识,false失败 true 成功 + /// + public bool Success { get; set; } + + /// + /// 数据处理消息,成功不返回 + /// + public string Message { get; set; } + + /// + /// 报错消息类型 A:停线、B:排出、C:忽略(需警示) + /// + public string Category { get; set; } + } +} diff --git a/JY.MES/MES/RespExitStation.cs b/JY.MES/MES/RespExitStation.cs index bf8738c..032ca73 100644 --- a/JY.MES/MES/RespExitStation.cs +++ b/JY.MES/MES/RespExitStation.cs @@ -9,7 +9,7 @@ namespace JY.MES.MES /// /// 产品出站响应信息 /// - public sealed class RespExitStation : RespArrivalStation + public sealed class RespExitStation : RespBase { } } diff --git a/JY.MES/MES/RespProductResult.cs b/JY.MES/MES/RespProductResult.cs new file mode 100644 index 0000000..7eca991 --- /dev/null +++ b/JY.MES/MES/RespProductResult.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JY.MES.MES +{ + /// + /// 结果加工参数响应信息 + /// + public class RespProductResult : RespBase + { + } +} diff --git a/JY.MES/MESApiHelper.cs b/JY.MES/MESApiHelper.cs index 3d0fb0d..6aac187 100644 --- a/JY.MES/MESApiHelper.cs +++ b/JY.MES/MESApiHelper.cs @@ -1,6 +1,10 @@ using JY.MES.Entity; +using JY.MES.Enum; +using JY.MES.Exceptions; +using JY.MES.MES; using Newtonsoft.Json; using System; +using System.CodeDom; using System.Collections.Generic; using System.IO; using System.Linq; @@ -31,11 +35,48 @@ namespace JY.MES } catch (Exception ex) { - mesResponse.success = false; - mesResponse.message = $"massage:[{ex.InnerException}],mes: [MES反馈超时或无法访间MES服务器]"; - mesResponse.error = 99; - return JsonConvert.SerializeObject(mesResponse); + throw new MesNetworkException($"massage:[{ex.InnerException}],mes: [MES反馈超时或无法访间MES服务器]"); } } + + public static MesCallResult HttpPostJsonAPI(string url, string strJosnData, double MesRequestTime) + { + var result = new MesCallResult() { IsSuccess = false }; + + try + { + StringContent stringContent = new StringContent(strJosnData, Encoding.UTF8, "application/json"); + string respJsonStr = new HttpClient() + { + Timeout = TimeSpan.FromSeconds(MesRequestTime) + }.PostAsync(url, (HttpContent)stringContent).Result.Content.ReadAsStringAsync().Result; + + var mesResp = JsonConvert.DeserializeObject(respJsonStr); + + result.IsSuccess = true; + result.OriResponse = mesResp; + result.OriRespJsonStr = respJsonStr; + } + catch (TimeoutException ex) + { + result.ErrorType = MesErrorType.NetworkTimeout; + result.ErrorMessage = $"MES请求超时: {ex.Message}"; + return result; + } + catch (HttpRequestException ex) + { + result.ErrorType = MesErrorType.NetworkError; + result.ErrorMessage = $"MES网络请求失败: {ex.Message}"; + return result; + } + catch (JsonException ex) + { + result.ErrorType = MesErrorType.ParseError; + result.ErrorMessage = $"MES响应解析失败: {ex.Message}"; + return result; + } + + return result; + } } }