mes集成增加MesCallResult

This commit is contained in:
liming 蔡
2026-07-14 16:09:25 +08:00
parent 5d2c7afb9c
commit 2ecfd549b3
12 changed files with 198 additions and 49 deletions
+34
View File
@@ -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<T>
{
public bool IsSuccess { get; set; }
/// <summary>
/// 错误类型枚举
/// </summary>
public MesErrorType ErrorType { get; set; }
/// <summary>
/// 错误描述
/// </summary>
public string ErrorMessage { get; set; }
/// <summary>
/// mes实际返回的响应
/// </summary>
public T OriResponse { get; set; }
/// <summary>
/// mes实际返回的响应json字符串
/// </summary>
public string OriRespJsonStr { get; set; }
}
}
+18
View File
@@ -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)
}
}
+17
View File
@@ -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网络异常")
{ }
}
}
+5
View File
@@ -48,6 +48,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Entity\MesCallResult.cs" />
<Compile Include="Entity\UploadAlarmParam.cs" />
<Compile Include="Entity\UploadTestDataParam.cs" />
<Compile Include="Entity\UploadAlarmResult.cs" />
@@ -56,6 +57,8 @@
<Compile Include="Entity\CheckBarcodeParam.cs" />
<Compile Include="Entity\EqpStatusResult.cs" />
<Compile Include="Entity\EqpStatusParam.cs" />
<Compile Include="Enum\MesErrorType.cs" />
<Compile Include="Exceptions\MesNetworkException.cs" />
<Compile Include="FMS\FMS_Alarm.cs" />
<Compile Include="FMS\FMS_Materialln.cs" />
<Compile Include="FMS\FMS_Status.cs" />
@@ -71,7 +74,9 @@
<Compile Include="MES\ReqArrivalStation.cs" />
<Compile Include="MES\ReqExitStation.cs" />
<Compile Include="MES\RespArrivalStation.cs" />
<Compile Include="MES\RespBase.cs" />
<Compile Include="MES\RespExitStation.cs" />
<Compile Include="MES\RespProductResult.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
+1 -19
View File
@@ -9,26 +9,8 @@ namespace JY.MES.MES
/// <summary>
/// 产品进站响应信息
/// </summary>
public class RespArrivalStation
public class RespArrivalStation : RespBase
{
/// <summary>
/// 错误代码,成功不返回
/// </summary>
public string Code { get; set; }
/// <summary>
/// 成功标识,false失败 true 成功
/// </summary>
public bool Success { get; set; }
/// <summary>
/// 数据处理消息,成功不返回
/// </summary>
public string Message { get; set; }
/// <summary>
/// 报错消息类型 A:停线、B:排出、C:忽略(需警示)
/// </summary>
public string Category { get; set; }
}
}
+34
View File
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JY.MES.MES
{
/// <summary>
/// 1440项目对应MES响应消息基类
/// </summary>
public abstract class RespBase
{
/// <summary>
/// 错误代码,成功不返回
/// </summary>
public string Code { get; set; }
/// <summary>
/// 成功标识,false失败 true 成功
/// </summary>
public bool Success { get; set; }
/// <summary>
/// 数据处理消息,成功不返回
/// </summary>
public string Message { get; set; }
/// <summary>
/// 报错消息类型 A:停线、B:排出、C:忽略(需警示)
/// </summary>
public string Category { get; set; }
}
}
+1 -1
View File
@@ -9,7 +9,7 @@ namespace JY.MES.MES
/// <summary>
/// 产品出站响应信息
/// </summary>
public sealed class RespExitStation : RespArrivalStation
public sealed class RespExitStation : RespBase
{
}
}
+15
View File
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JY.MES.MES
{
/// <summary>
/// 结果加工参数响应信息
/// </summary>
public class RespProductResult : RespBase
{
}
}
+45 -4
View File
@@ -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<T> HttpPostJsonAPI<T>(string url, string strJosnData, double MesRequestTime)
{
var result = new MesCallResult<T>() { 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<T>(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;
}
}
}