Files
1258/JinYuan.MES/MesDataProcess.cs
T
2026-08-04 18:36:40 +08:00

2195 lines
109 KiB
C#

using JinYuan.Helper;
using JinYuan.MES.Enums;
using JinYuan.MES.Models;
using JinYuan.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.Remoting.Messaging;
using System.Threading;
using System.Threading.Tasks;
using static System.Runtime.CompilerServices.RuntimeHelpers;
namespace JinYuan.MES
{
public class MesDataProcess : IMes
{
#region 1、登录验证
/// 账号密码登录(异步方法)
/// </summary>
/// <param name="url">API地址</param>
/// <param name="siteCode">站点代码</param>
/// <param name="lineCode">线体代码</param>
/// <param name="equipNum">设备编号</param>
/// <param name="userName">用户名</param>
/// <param name="passWord">密码</param>
/// <param name="UserCardID">用户卡ID</param>
/// <returns>返回结果(是否成功、消息)</returns> userCardID = UserCardID
public async Task<(bool Success, string Message, string MesUserLevel)> LoginAsync(string url, string siteCode, string lineCode, string equipNum, string userName, string passWord)
{
bool res = false; // 操作结果
string resJson = ""; // 日志信息
string message = ""; // 消息
string mesUserName = "";//mes返回登陆权限
try
{
// 创建请求模型
var model = new LoginEntity
{
siteCode = siteCode,
lineCode = lineCode,
equipNum = equipNum,
userName = userName,
passWord = passWord,
};
// 序列化请求数据
var nowDate = DateTime.Now;
var postData = JsonConvert.SerializeObject(model);
// 记录请求开始时间
var sw = Stopwatch.StartNew();
// 调用异步API
string result = await MESApiHelper.SendRequestStringAsync(url, HttpMethod.Post, postData, false);
// 记录请求结束时间
sw.Stop();
// 如果返回结果为空,记录日志并返回
if (string.IsNullOrEmpty(result))
{
string resData = result == "" ? "" : result;
resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用员工权限校验接口:{url},请求数据为:{postData},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\员工权限校验\{nowDate.ToString("HH")}.txt", resJson);
return (res, message, mesUserName);
}
// 解析返回结果
var res1 = JsonConvert.DeserializeObject<MesResponseLogin>(result);
// 如果操作成功
if (res1 != null && res1.success && string.IsNullOrEmpty(res1.code) && string.IsNullOrEmpty(res1.message))
{
//mes登陆权限赋值
//if (res1.rows[0].username == userName) { mesUserName = res1.rows[0].permissionLevel; }
if (res1.rows[0] == userName) { mesUserName = res1.rows[0]; }
res = true;
message = res1.message;
resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用员工权限校验接口:{url},请求数据为:{postData}\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{JsonConvert.SerializeObject(result)}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\员工权限校验\{nowDate.ToString("HH")}.txt", resJson);
}
else
{
// 如果操作失败,记录日志
var resData = res1 == null ? "" : JsonConvert.SerializeObject(res1);
message = res1?.message ?? "未知错误";
resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用员工权限校验接口:{url},请求数据为:{postData}\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\员工权限校验\{nowDate.ToString("HH")}.txt", resJson);
}
}
catch (Exception ex)
{
// 捕获异常并记录日志
message = "调用员工权限校验接口发生异常";
resJson = $"调用员工权限校验接口发生异常:{ex.Message}";
LogHelper.Instance.WriteEX("调用员工权限校验接口", ex);
}
// 返回结果
return (res, message, mesUserName);
}
/// <summary>
/// 刷卡登录
/// </summary>
/// <param name="url"></param>
/// <param name="siteCode"></param>
/// <param name="lineCode"></param>
/// <param name="equipNum"></param>
/// <param name="UserCardID"></param>
/// <returns></returns>
public async Task<(bool Success, string Message)> LoginAsync(string url, string siteCode, string lineCode, string equipNum, string UserCardID)
{
bool res = false; // 操作结果
string resJson = ""; // 日志信息
string message = ""; // 消息
try
{
// 创建请求模型
var model = new LoginEntity
{
siteCode = siteCode,
lineCode = lineCode,
equipNum = equipNum,
userCardID = UserCardID,
};
// 序列化请求数据
var nowDate = DateTime.Now;
var postData = JsonConvert.SerializeObject(model);
// 记录请求开始时间
var sw = Stopwatch.StartNew();
// 调用异步API
string result = await MESApiHelper.SendRequestStringAsync(url, HttpMethod.Post, postData, false);
// 记录请求结束时间
sw.Stop();
// 如果返回结果为空,记录日志并返回
if (string.IsNullOrEmpty(result))
{
string resData = result == "" ? "" : result;
resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用员工权限校验接口:{url},请求数据为:{postData},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\员工权限校验\{nowDate.ToString("HH")}.txt", resJson);
return (res, message);
}
// 解析返回结果
var res1 = JsonConvert.DeserializeObject<MesResponse>(result);
// 如果操作成功
if (res1 != null && res1.success && string.IsNullOrEmpty(res1.code) && string.IsNullOrEmpty(res1.message))
{
res = true;
message = res1.message;
resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用员工权限校验接口:{url},请求数据为:{postData}\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{JsonConvert.SerializeObject(result)}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\员工权限校验\{nowDate.ToString("HH")}.txt", resJson);
}
else
{
// 如果操作失败,记录日志
var resData = res1 == null ? "" : JsonConvert.SerializeObject(res1);
message = res1?.message ?? "未知错误";
resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用员工权限校验接口:{url},请求数据为:{postData}\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\员工权限校验\{nowDate.ToString("HH")}.txt", resJson);
}
}
catch (Exception ex)
{
// 捕获异常并记录日志
message = "调用员工权限校验接口发生异常";
resJson = $"调用员工权限校验接口发生异常:{ex.Message}";
LogHelper.Instance.WriteEX("调用员工权限校验接口", ex);
}
// 返回结果
return (res, message);
}
#endregion
#region 2、获取MES参数请求
/// <summary>
/// 获取MES参数请求
/// </summary>
/// <param name="url"></param>
/// <param name="equipNum"></param>
/// <param name="siteCode"></param>
/// <param name="lineCode"></param>
/// <param name="materialCode"></param>
/// <param name="userName"></param>
/// <param name="resJson"></param>
/// <returns></returns>
public async Task<(ParamReturnData Data, string Mesage)> GetParamSetRequestAsync(string url, string siteCode, string lineCode, string equipNum, string materialCode, string userName)
{
string resJson = "";
string mesage = ""; // 用于存储消息
try
{
var model = new
{
siteCode = siteCode,
lineCode = lineCode,
equipNum = equipNum,
materialCode = materialCode,
userName = userName
};
var nowDate = DateTime.Now;
var postcontent = JsonConvert.SerializeObject(model);
var sw = new Stopwatch();
sw.Start();
string result = await MESApiHelper.SendRequestStringAsync(url, HttpMethod.Post, postcontent, false);
sw.Stop();
if (string.IsNullOrEmpty(result))
{
string resData = result == "" ? "" : result;
resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用设备参数设定值请求接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\设备参数设定值请求\{nowDate.ToString("HH")}.txt", resJson);
return (null, mesage);
}
var res1 = ReturnMesMsg<ParamReturnData>(result);
if (res1.success)
{
resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用设备参数设定值请求接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{result}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\设备参数设定值请求\{nowDate.ToString("HH")}.txt", resJson);
}
else
{
var resData = res1 == null ? "" : result;
mesage = "MES返回结果为空";
resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用设备参数设定值请求接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\设备参数设定值请求\{nowDate.ToString("HH")}.txt", resJson);
}
return (res1, mesage);
}
catch (Exception ex)
{
mesage = $"设备参数设定值请求接口发生异常:{ex.Message}";
LogHelper.Instance.WriteEX("设备参数设定值请求接口", ex);
return (null, mesage);
}
}
#endregion
#region 3、参数变更请求
/// <summary>
/// 参数变更请求
/// </summary>
/// <param name="url"></param>
/// <param name="siteCode"></param>
/// <param name="lineCode"></param>
/// <param name="equipNum"></param>
/// <param name="materialCode"></param>
/// <param name="userName"></param>
/// <param name="paramChangeItems"></param>
/// <param name="Mesage"></param>
/// <returns></returns>
public async Task<bool> ParamChange(string url, string siteCode, string lineCode, string equipNum, string materialCode, string userName, List<TagListItem> paramChangeItems)
{
bool res = false;
string resJson = "";
try
{
var model = new ParamChange
{
siteCode = siteCode,
lineCode = lineCode,
equipNum = equipNum,
materialCode = materialCode,
userName = userName,
};
var nowDate = DateTime.Now;
var postcontent = JsonConvert.SerializeObject(model);
var sw = new Stopwatch();
sw.Start();
string result = await MESApiHelper.SendRequestStringAsync(url, HttpMethod.Post, postcontent, false);
sw.Stop();
var resData = result == "" ? "" : result;
resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用参数设定值变更接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
TxtHelper.WriteTxt($@"D:\APILog\MesLogs\{nowDate.ToString("yyyyMMdd")}\参数设定值变更\{nowDate.ToString("HH")}.txt", resJson);
}
catch (Exception ex)
{
resJson = $"参数设定值变更接口发生异常:{ex.Message}";
LogHelper.Instance.WriteEX("参数设定值变更接口", ex);
}
return res;
}
#endregion
#region 根据料框码获取电池型号、电池条码等数据
/// <summary>
/// 根据料框码获取电池型号、电池条码等数据
/// </summary>
/// <param name="url">API地址 /api/public/equip/auto/gradequery</param>
/// <param name="siteCode">工厂代码</param>
/// <param name="lineCode">产线编号</param>
/// <param name="equipCode">设备编号</param>
/// <param name="recordDate">记录时间</param>
/// <param name="qty">数量</param>
/// <param name="containerCode">托盘码</param>
/// <param name="materialCode">物料代码</param>
/// <param name="userName">操作人</param>
/// <param name="materiallotCodeList">电芯信息列表</param>
/// <returns>返回结果(是否成功、消息列表、响应类型列表)</returns>
public async Task<(bool Success, string mesMessage, MesResType mesResType, TrayInfo trayInfo)> QueryMesTrayInfosAsync(string url, string siteCode, string lineCode, string equipNum, string recordDate, string containerCode, string materialCode, string userName)
{
bool success = false; // 操作结果
string message = ""; // 消息列表
MesResType mesResType = MesResType.C;
TrayInfo trayInfo = new TrayInfo();
try
{
// 序列化请求数据
var nowDate = DateTime.Now;
// 记录请求开始时间
var sw = Stopwatch.StartNew();
// 调用异步API
string result = await MESApiHelper.SendRequestStringAsync($"{url}", HttpMethod.Get, containerCode, false);
// 记录请求结束时间
sw.Stop();
// 如果请求耗时超过1秒,记录日志
long outTime = sw.ElapsedMilliseconds;
if (outTime > 1000)
{
LogHelper.Instance.WriteLog($"查询料盘信息PC<=>MES耗时:{outTime}ms,料框码:{containerCode}", "MESTime");
}
// 记录请求日志
string requestLog = $"{nowDate:yyyy-MM-dd HH:mm:ss.fff}:调用查询料盘信息接口:{url}/{containerCode},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds):yyyy-MM-dd HH:mm:ss.fff}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{result}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate:yyyyMMdd}\查询料盘信息\{nowDate:HH}.txt", requestLog);
// 如果返回结果为空,记录日志并返回
if (string.IsNullOrEmpty(result))
{
//string resData = result == "" ? "" : result;
//resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用查询电芯档位接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
//TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\查询电芯档位\{nowDate.ToString("HH")}.txt", resJson);
message = "MES接口返回空结果";
return (success, message, mesResType, trayInfo);
}
// 解析返回结果
var mesResponse = JsonConvert.DeserializeObject<MesResponseTray>(result);
// 如果操作成功
if (mesResponse != null && mesResponse.code == 200 && mesResponse.data != null)
{
success = true;
ProcessTrayResponse(mesResponse, ref message, ref mesResType, ref trayInfo); // 处理返回结果
//resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用查询电芯档位接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{result}";
//TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\查询电芯档位\{nowDate.ToString("HH")}.txt", resJson);
if (success && mesResType == MesResType.D)
{
message += $"查询料盘信息成功,共获取 {trayInfo.cellList.Count} 个电池信息";
//CommonMethods.AddDataMonitorLog(0, message);
//return (success, message, mesResType, trayInfo);
}
else
{
message += $"查询料盘信息失败";
//return (success, message, mesResType, trayInfo);
}
}
else
{
// 如果操作失败,记录日志
//var resData = res1 == null ? "" : result;
//resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用查询电芯档位接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
//TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\查询电芯档位\{nowDate.ToString("HH")}.txt", resJson);
// 如果操作失败
message += mesResponse?.msg ?? "MES接口返回失败状态";
//mesResType = GetMesResType(mesResponse?.category);
//CommonMethods.AddDataMonitorLog(1, $"查询电芯档位失败:{message}");
//return (success, message, mesResType, trayInfo);
}
}
catch (Exception ex)
{
// 捕获异常并记录日志
message = $"查询料盘信息接口发生异常:{ex.Message}";
LogHelper.Instance.WriteEX("查询料盘信息接口", ex);
}
// 返回结果
return (success, message, mesResType, trayInfo);
}
/// <summary>
/// 料盘信息查询返回数据处理
/// </summary>
/// <param name="res1"></param>
/// <param name="BarCode"></param>
/// <param name="MesMessage"></param>
/// <param name="mesResType"></param>
private void ProcessTrayResponse(MesResponseTray res, ref string mesMessage, ref MesResType mesResType, ref TrayInfo trayInfo)
{
if (res != null)
{
trayInfo = res.data ?? new TrayInfo();
mesMessage = res.msg;
if (res.code == 200)
{
mesResType = MesResType.D;
mesMessage = "MES料盘信息查询成功 ";
}
else
{
mesResType = MesResType.B;
//mesMessage = "MES料盘信息查询失败 ";
}
}
else
{
mesMessage = "响应数据为空";
mesResType = MesResType.B;
}
}
#endregion
#region 电芯档位获取
/// <summary>
/// 电芯档位获取
/// </summary>
/// <param name="url">API地址 /api/public/equip/auto/gradequery</param>
/// <param name="siteCode">工厂代码</param>
/// <param name="lineCode">产线编号</param>
/// <param name="equipCode">设备编号</param>
/// <param name="recordDate">记录时间</param>
/// <param name="qty">数量</param>
/// <param name="containerCode">托盘码</param>
/// <param name="materialCode">物料代码</param>
/// <param name="userName">操作人</param>
/// <param name="materiallotCodeList">电芯信息列表</param>
/// <returns>返回结果(是否成功、消息列表、响应类型列表)</returns>
public async Task<(bool Success, List<MaterialGrade> materialGrades)> QueryGradeAsync(string url, string siteCode, string lineCode, string equipNum, string recordDate, string qty, string materialCode, string userName, List<MaterialLot> listCode)
{
bool success = false; // 操作结果
string rawJson = "";
string message = ""; // 消息列表
MesResType mesResType = MesResType.C;
List<MaterialGrade> materialGrades = new List<MaterialGrade>();
try
{
// 创建请求模型
var model = new QueryGradeParam
{
siteCode = siteCode,
lineCode = lineCode,
equipNum = equipNum,
recordDate = recordDate,
qty = qty,
materialCode = materialCode,
actionType = "IN",
userName = userName,
batteryTrayBindVOList = listCode
};
// 序列化请求数据
var nowDate = DateTime.Now;
var postcontent = JsonConvert.SerializeObject(model);
// 记录请求开始时间
var sw = Stopwatch.StartNew();
// 调用异步API
string result = await MESApiHelper.SendRequestStringAsync(url, HttpMethod.Post, postcontent, false);
// 记录请求结束时间
sw.Stop();
// 如果请求耗时超过1秒,记录日志
long outTime = sw.ElapsedMilliseconds;
if (outTime > 1000)
{
LogHelper.Instance.WriteLog($"查询电芯档位PC<=>MES耗时:{outTime}ms,电芯码:{string.Join(",", listCode)}", "MESTime");
}
// 记录请求日志
string requestLog = $"{nowDate:yyyy-MM-dd HH:mm:ss.fff}:调用查询电芯档位接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds):yyyy-MM-dd HH:mm:ss.fff}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{result}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate:yyyyMMdd}\查询电芯档位\{nowDate:HH}.txt", requestLog);
// 如果返回结果为空,记录日志并返回
if (string.IsNullOrEmpty(result))
{
string resData = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用查询电芯档位接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{result}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\查询电芯档位\{nowDate.ToString("HH")}.txt", resData);
message = "MES接口返回空结果";
return (success, materialGrades);
}
// 解析返回结果
var mesResponse = JsonConvert.DeserializeObject<MesResponseGrade>(result);
// 如果操作成功
if (mesResponse != null && mesResponse.success)
{
success = true;
ProcessGradeResponse(mesResponse, listCode, ref message, ref materialGrades); // 处理返回结果
//resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用查询电芯档位接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{result}";
//TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\查询电芯档位\{nowDate.ToString("HH")}.txt", resJson);
if (success)
{
message = $"查询电芯档位成功,共获取 {materialGrades.Count} 个档位信息";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\查询电芯档位\{nowDate.ToString("HH")}.txt", message);
return (success, materialGrades);
}
else
{
return (success, materialGrades);
}
}
else
{
// 如果操作失败,记录日志
var resData = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用查询电芯档位接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{result}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\查询电芯档位\{nowDate.ToString("HH")}.txt", result);
return (success, materialGrades);
}
}
catch (Exception ex)
{
// 捕获异常并记录日志
message = $"查询电芯档位接口发生异常:{ex.Message}";
LogHelper.Instance.WriteEX("查询电芯档位接口", ex);
}
// 返回结果
return (success, materialGrades);
}
/// <summary>
/// 电芯档位查询返回数据处理
/// </summary>
/// <param name="res1"></param>
/// <param name="BarCode"></param>
/// <param name="MesMessage"></param>
/// <param name="mesResType"></param>
private void ProcessGradeResponse(MesResponseGrade res, List<MaterialLot> barCodes, ref string mesMessage, ref List<MaterialGrade> materialGrades)
{
if (res != null)
{
materialGrades = res.rows ?? new List<MaterialGrade>();
if (res.success)
{
// 检查数据完整性
if (materialGrades.Count != res.total)
{
mesMessage = $"数据数量不匹配:返回{materialGrades.Count}条,总数{res.total}";
}
// 检查条码匹配
var requestBarcodes = barCodes.Select(b => b.identification).ToHashSet();
var responseBarcodes = materialGrades.Select(m => m.identification).ToHashSet();
var unmatchedBarcodes = requestBarcodes.Except(responseBarcodes).ToList();
if (unmatchedBarcodes.Any())
{
mesMessage = $"存在 {unmatchedBarcodes.Count} 个未匹配的条码";
}
}
}
else
{
mesMessage = "响应数据为空";
}
}
#endregion
/// <summary>
/// 将MES返回的category转换为MesResType
/// </summary>
private MesResType GetMesResType(string category)
{
if (string.IsNullOrEmpty(category))
return MesResType.B;
switch (category.ToUpper())
{
case "A":
return MesResType.A; // 停线
case "B":
return MesResType.B; // 排出
case "C":
return MesResType.C; // 忽略(需警示)
case "D":
return MesResType.D; // 成功
default:
return MesResType.B; // 默认排出
}
}
#region 解绑所有料框码
/// <summary>
/// 料框码与电池解绑
/// </summary>
public async Task<(bool Success, string mesMessage)> UnbindTrayInfosAsync(string url, string equipNum, string trayCode)
{
bool success = false; // 操作结果
string rawJson = ""; // 日志信息
string message = ""; // 消息列表
MesResType mesResType = MesResType.C;
try
{
if (string.IsNullOrEmpty(trayCode))
{
message = "料框码为空";
return (success, message);
}
// 创建请求模型
var model = new unbindParam
{
equipNum = equipNum,
trayCode = trayCode,
};
var postcontent = JsonConvert.SerializeObject(model);
// 序列化请求数据
var nowDate = DateTime.Now;
// 记录请求开始时间
var sw = Stopwatch.StartNew();
// 调用异步API
string result = await MESApiHelper.SendRequestStringAsync(url, HttpMethod.Post, postcontent, false);
// 记录请求结束时间
sw.Stop();
rawJson = result;
// 如果请求耗时超过1秒,记录日志
long outTime = sw.ElapsedMilliseconds;
if (outTime > 1000)
{
//LogHelper.Instance.WriteLog($"进站PC<=>MES上传耗时:{outTime}ms,电芯码:{listBar[0]},{listBar[1]},{listBar[2]},{listBar[3]}", "MESTime");
LogHelper.Instance.WriteLog($"解绑料框码PC<=>MES耗时:{outTime}ms,料框码:{trayCode}", "MESTime");
}
// 记录请求日志
string requestLog = $"{nowDate:yyyy-MM-dd HH:mm:ss.fff}:调用解绑料框码接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds):yyyy-MM-dd HH:mm:ss.fff}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{result}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate:yyyyMMdd}\调用解绑料框码接口\{nowDate:HH}.txt", requestLog);
// 如果返回结果为空,记录日志并返回
if (string.IsNullOrEmpty(result))
{
//string resData = result == "" ? "" : result;
//resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用卡板电芯信息上传(装箱)接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
//TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\卡板电芯信息上传(装箱)\{nowDate.ToString("HH")}.txt", resJson);
//return (res, mesMessage, mesResType, materialPackInfos);
message = "MES接口返回空结果";
return (success, message);
}
// 解析返回结果
var response = JsonConvert.DeserializeObject<unBindMesResponse>(result);
// 如果操作成功
if (response != null && response.flag)
{
success = true;
return (success, response.msg);
}
else
{
string msg = response == null? $"{result}": response.msg;
return (false, msg);
}
}
catch (Exception ex)
{
// 捕获异常并记录日志
//resJson = $"卡板电芯信息上传(装箱)接口发生异常:{ex.Message}";
//LogHelper.Instance.WriteEX("卡板电芯信息上传(装箱)接口", ex);
// 捕获异常并记录日志
message = $"解绑料框码接口发生异常:{ex.Message}";
LogHelper.Instance.WriteEX("解绑料框码", ex);
return (success, message);//MesResult<List<MaterialPackInfo>>.Fail(message, MesResType.B, rawJson);
}
}
#endregion
#region 卡板电芯信息上传(装箱)
/// <summary>
/// 半成品电池绑定(装箱)
/// </summary>
/// <param name="url">API地址 /api/public/equip/auto/pack</param>
/// <param name="siteCode">工厂代码</param>
/// <param name="equipNum">设备编号</param>
/// <param name="innerContainerCode">卡板码(箱唛号)</param>
/// <param name="level">档位</param>
/// <param name="batteryInfos">电芯信息</param>
/// <returns>返回结果(是否成功、消息列表、响应类型列表)</returns>
public async Task<(bool success, string mesMessage, MesResType mesResType, List<MaterialPackInfo> materialPackInfos)> PackLoadAsync(string url, string siteCode, string lineCode, string equipNum, string ContainerCode, int capacity, string grade, string materialCode, List<BatteryInfo> cellDataVOList)
{
bool success = false; // 操作结果
string rawJson = ""; // 日志信息
string message = ""; // 消息列表
MesResType mesResType = MesResType.C;
List<MaterialPackInfo> materialPackInfos = new List<MaterialPackInfo>();
try
{
// 参数验证
if (cellDataVOList == null || !cellDataVOList.Any())
{
message = "电池信息列表为空";
return (success, message, mesResType, materialPackInfos);
}
if (string.IsNullOrEmpty(ContainerCode))
{
message = "容器码为空";
return (success, message, mesResType, materialPackInfos);
}
ContainerInfo containerInfo = new ContainerInfo
{
containerCode = ContainerCode,
productDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
//containerTypeCode = "XM_BCP",
packQty = capacity,
groupNum = "1",
//supplierCode= "EVEDL",
materialName = "MB36-0.5P-V1.1 电芯注液半成品",
materialCode = materialCode,
batteryInfos = cellDataVOList,
};
List<ContainerInfo> containerBarcodeInfos = new List<ContainerInfo>
{ };
containerBarcodeInfos.Add(containerInfo);
// 创建请求模型
var model = new PackParam
{
siteCode = siteCode,
lineCode = lineCode,
containerBarcodeInfos = containerBarcodeInfos,
equipNum = equipNum,
ContainerCode = ContainerCode,
capacity = capacity,
grade = grade,
materialCode = materialCode,
cellDataVOList = cellDataVOList
};
List<string> barcodeList = cellDataVOList.Select(BatteryInfo => BatteryInfo.identification).ToList();
// 序列化请求数据
var nowDate = DateTime.Now;
var postcontent = JsonConvert.SerializeObject(model);
// 记录请求开始时间
var sw = Stopwatch.StartNew();
// 调用异步API
string result = await MESApiHelper.SendRequestStringAsync(url, HttpMethod.Post, postcontent, false);
// 记录请求结束时间
sw.Stop();
rawJson = result;
// 如果请求耗时超过1秒,记录日志
long outTime = sw.ElapsedMilliseconds;
if (outTime > 1000)
{
//LogHelper.Instance.WriteLog($"进站PC<=>MES上传耗时:{outTime}ms,电芯码:{listBar[0]},{listBar[1]},{listBar[2]},{listBar[3]}", "MESTime");
LogHelper.Instance.WriteLog($"半成品电池绑定PC<=>MES耗时:{outTime}ms,电芯码:{string.Join(",", barcodeList)}", "MESTime");
}
// 记录请求日志
string requestLog = $"{nowDate:yyyy-MM-dd HH:mm:ss.fff}:调用半成品电池绑定接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds):yyyy-MM-dd HH:mm:ss.fff}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{result}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate:yyyyMMdd}\半成品电池绑定\{nowDate:HH}.txt", requestLog);
// 如果返回结果为空,记录日志并返回
if (string.IsNullOrEmpty(result))
{
//string resData = result == "" ? "" : result;
//resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用卡板电芯信息上传(装箱)接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
//TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\卡板电芯信息上传(装箱)\{nowDate.ToString("HH")}.txt", resJson);
//return (res, mesMessage, mesResType, materialPackInfos);
message = "MES接口返回空结果";
return (success, message, mesResType, materialPackInfos);
}
// 解析返回结果
var response = JsonConvert.DeserializeObject<MesResponsePack>(result);
// 如果操作成功
if (response != null && response.success)
{
success = true;
ProcessPackResponse(response, barcodeList, cellDataVOList, ref message, ref mesResType, ref materialPackInfos); // 处理返回结果
//resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用卡板电芯信息上传(装箱)接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{result}";
//TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\卡板电芯信息上传(装箱)\{nowDate.ToString("HH")}.txt", resJson);
//if (success && mesResType == MesResType.D)
{
//message = $"卡板电芯信息上传成功,共处理 {materialPackInfos?.Count ?? 0} 个电芯";
return (success, message, mesResType, materialPackInfos);
}
//else
//{
// return (success, message, mesResType, materialPackInfos);
//}
}
else
{
// 如果操作失败,记录日志
//var resData = res1 == null ? "" : result;
//resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用卡板电芯信息上传(装箱)接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
//TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\卡板电芯信息上传(装箱)\{nowDate.ToString("HH")}.txt", resJson);
// 如果操作失败
message = response?.message ?? "MES接口返回失败状态";
mesResType = MesResType.C;//GetMesResType(response?.category);
return (success, message, mesResType, materialPackInfos);//MesResult<List<MaterialPackInfo>>.Fail(message, mesResType, rawJson);
}
}
catch (Exception ex)
{
// 捕获异常并记录日志
//resJson = $"卡板电芯信息上传(装箱)接口发生异常:{ex.Message}";
//LogHelper.Instance.WriteEX("卡板电芯信息上传(装箱)接口", ex);
// 捕获异常并记录日志
message = $"半成品电池绑定接口发生异常:{ex.Message}";
LogHelper.Instance.WriteEX("半成品电池绑定接口", ex);
return (success, message, mesResType, materialPackInfos);//MesResult<List<MaterialPackInfo>>.Fail(message, MesResType.B, rawJson);
}
// 返回结果
//return (success, message, mesResType, materialPackInfos);
}
/// <summary>
/// 卡板电芯信息上传(装箱)返回数据处理
/// </summary>
/// <param name="res1"></param>
/// <param name="BarCode"></param>
/// <param name="MesMessage"></param>
/// <param name="mesResType"></param>
private void ProcessPackResponse(MesResponsePack response, List<string> barcodeList, List<BatteryInfo> cellDataVOList, ref string message, ref MesResType mesResType, ref List<MaterialPackInfo> materialPackInfos)
{
if (response != null)
{
//materialPackInfos = new List<MaterialPackInfo>();
message = response.message;
if (response.success)
{
mesResType = MesResType.D;
//message = "卡板电芯信息上传成功";
//// 检查数据完整性
//if (materialPackInfos.Count != cellDataVOList.Count)
//{
// message = $"返回数据数量不匹配:请求{cellDataVOList.Count}个,返回{materialPackInfos.Count}个";
// mesResType = MesResType.B;
// return;
//}
//// 检查条码匹配
//var responseBarcodes = materialPackInfos.Select(m => m.identification).ToHashSet();
//var unmatchedBarcodes = barcodeList.Except(responseBarcodes).ToList();
//if (unmatchedBarcodes.Any())
//{
// message = $"存在 {unmatchedBarcodes.Count} 个未匹配的条码";
// mesResType = MesResType.B;
// return;
//}
//// 检查绑定失败的条码
//var failedBarcodes = materialPackInfos
// .Where(m => !m.packResult)
// .Select(m => m.identification)
// .ToList();
//if (failedBarcodes.Any())
//{
// message = $"存在 {failedBarcodes.Count} 个绑定失败的电芯";
// mesResType = MesResType.B;
// return;
//}
}
else
{
mesResType = MesResType.C; //GetMesResType(response.category);
}
}
else
{
message = "响应数据为空";
mesResType = MesResType.B;
}
}
/// <summary>
/// 成品电池绑定
/// </summary>
/// <param name="url">API地址 /api/public/equip/auto/pack</param>
/// <param name="siteCode">工厂代码</param>
/// <param name="equipNum">设备编号</param>
/// <param name="innerContainerCode">卡板码(箱唛号)</param>
/// <param name="level">档位</param>
/// <param name="batteryInfos">电芯信息</param>
/// <returns>返回结果(是否成功、消息列表、响应类型列表)</returns>
public async Task<(bool success, string mesMessage, MesResType mesResType, List<MaterialPackInfo> materialPackInfos)> PackLoadFinishAsync(string url, string siteCode, string lineCode, string equipNum, string ContainerCode, int capacity, string grade, string materialCode, List<BatteryInfo> cellDataVOList)
{
bool success = false; // 操作结果
string rawJson = ""; // 日志信息
string message = ""; // 消息列表
MesResType mesResType = MesResType.C;
List<MaterialPackInfo> materialPackInfos = new List<MaterialPackInfo>();
try
{
// 参数验证
if (cellDataVOList == null || !cellDataVOList.Any())
{
message = "电池信息列表为空";
return (success, message, mesResType, materialPackInfos);
}
if (string.IsNullOrEmpty(ContainerCode))
{
message = "容器码为空";
return (success, message, mesResType, materialPackInfos);
}
ContainerInfo containerInfo = new ContainerInfo
{
containerCode = ContainerCode,
productDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
//containerTypeCode = "XM_BCP",
packQty = capacity,
groupNum = "1",
//supplierCode = "EVEDL",
materialName = "MB36-0.5P-V1.1 电池成品",
materialCode = materialCode,
batteryInfos = cellDataVOList,
};
List<ContainerInfo> containerBarcodeInfos = new List<ContainerInfo>(){ };
containerBarcodeInfos.Add(containerInfo);
// 创建请求模型
var model = new FinishPackParam
{
siteCode = siteCode,
lineCode = lineCode,
containerBarcodeInfos = containerBarcodeInfos,
equipCode = equipNum,
outerContainerCode = ContainerCode
};
List<string> barcodeList = cellDataVOList.Select(BatteryInfo => BatteryInfo.identification).ToList();
// 序列化请求数据
var nowDate = DateTime.Now;
var postcontent = JsonConvert.SerializeObject(model);
// 记录请求开始时间
var sw = Stopwatch.StartNew();
// 调用异步API
string result = await MESApiHelper.SendRequestStringAsync(url, HttpMethod.Post, postcontent, false);
// 记录请求结束时间
sw.Stop();
rawJson = result;
// 如果请求耗时超过1秒,记录日志
long outTime = sw.ElapsedMilliseconds;
if (outTime > 1000)
{
//LogHelper.Instance.WriteLog($"进站PC<=>MES上传耗时:{outTime}ms,电芯码:{listBar[0]},{listBar[1]},{listBar[2]},{listBar[3]}", "MESTime");
LogHelper.Instance.WriteLog($"成品电池绑定(装箱)PC<=>MES耗时:{outTime}ms,电芯码:{string.Join(",", barcodeList)}", "MESTime");
}
// 记录请求日志
string requestLog = $"{nowDate:yyyy-MM-dd HH:mm:ss.fff}:调用成品电池绑定接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds):yyyy-MM-dd HH:mm:ss.fff}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{result}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate:yyyyMMdd}\成品电池绑定\{nowDate:HH}.txt", requestLog);
// 如果返回结果为空,记录日志并返回
if (string.IsNullOrEmpty(result))
{
//string resData = result == "" ? "" : result;
//resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用成品电池绑定接口:{url},请求数据为:{JsonConvert.SerializeObject(model)},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
//TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\成品电池绑定\{nowDate.ToString("HH")}.txt", resJson);
message = "MES接口返回空结果";
return (success, message, mesResType, materialPackInfos);
}
// 解析返回结果
var response = JsonConvert.DeserializeObject<MesResponseFinishPack>(result);
// 如果操作成功
if (response != null && response.success)
{
success = true;
ProcessFinishPackResponse(response, barcodeList, cellDataVOList, ref message, ref mesResType, ref materialPackInfos); // 处理返回结果
//resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用卡板电芯信息上传(装箱)接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{result}";
//TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\卡板电芯信息上传(装箱)\{nowDate.ToString("HH")}.txt", resJson);
if (success && mesResType == MesResType.D)
{
message = $"卡板电芯信息上传成功,共处理 {materialPackInfos?.Count ?? 0} 个电芯";
}
else
{
//return (success, message, mesResType, materialPackInfos);
}
}
else
{
// 如果操作失败,记录日志
//var resData = res1 == null ? "" : result;
//resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用卡板电芯信息上传(装箱)接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
//TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\卡板电芯信息上传(装箱)\{nowDate.ToString("HH")}.txt", resJson);
// 如果操作失败
message = response?.message ?? "MES接口返回失败状态";
mesResType = GetMesResType(response?.category);
//return (success, message, mesResType, materialPackInfos);//MesResult<List<MaterialPackInfo>>.Fail(message, mesResType, rawJson);
}
}
catch (Exception ex)
{
// 捕获异常并记录日志
message = $"卡板电芯信息上传(装箱)接口发生异常:{ex.Message}";
LogHelper.Instance.WriteEX("卡板电芯信息上传(装箱)接口", ex);
//return (success, message, mesResType, materialPackInfos);//MesResult<List<MaterialPackInfo>>.Fail(message, MesResType.B, rawJson);
}
// 返回结果
return (success, message, mesResType, materialPackInfos);
}
/// <summary>
/// 成品电池绑定返回数据处理
/// </summary>
/// <param name="res1"></param>
/// <param name="BarCode"></param>
/// <param name="MesMessage"></param>
/// <param name="mesResType"></param>
private void ProcessFinishPackResponse(MesResponseFinishPack response, List<string> barcodeList, List<BatteryInfo> cellDataVOList, ref string message, ref MesResType mesResType, ref List<MaterialPackInfo> materialPackInfos)
{
if (response != null)
{
materialPackInfos = response.rows ?? new List<MaterialPackInfo>();
message = response.message;
if (response.success)
{
mesResType = MesResType.D;
message = "卡板电芯信息上传成功";
// 检查数据完整性
if (materialPackInfos.Count != cellDataVOList.Count)
{
message = $"返回数据数量不匹配:请求{cellDataVOList.Count}个,返回{materialPackInfos.Count}个";
mesResType = MesResType.B;
return;
}
// 检查条码匹配
var responseBarcodes = materialPackInfos.Select(m => m.identification).ToHashSet();
var unmatchedBarcodes = barcodeList.Except(responseBarcodes).ToList();
if (unmatchedBarcodes.Any())
{
message = $"存在 {unmatchedBarcodes.Count} 个未匹配的条码";
mesResType = MesResType.B;
return;
}
// 检查绑定失败的条码
var failedBarcodes = materialPackInfos
.Where(m => !m.packResult)
.Select(m => m.identification)
.ToList();
if (failedBarcodes.Any())
{
message = $"存在 {failedBarcodes.Count} 个绑定失败的电芯";
mesResType = MesResType.B;
return;
}
}
else
{
mesResType = GetMesResType(response.category);
}
}
else
{
message = "响应数据为空";
mesResType = MesResType.B;
}
}
#endregion
#region 箱唛信息获取
/// <summary>
/// 箱唛信息获取
/// </summary>
/// <param name="url">API地址 /api/public/equip/auto/cartonquery</param>
/// <param name="siteCode">工厂代码</param>
/// <param name="lineCode">产线编号</param>
/// <param name="equipNum">设备编号</param>
/// <param name="containerCode">卡板码(箱唛号)</param>
/// <returns>返回结果(是否成功、消息列表、响应类型列表)</returns>
public async Task<(bool success, string mesMessage, MesResType mesResType, List<TagInfo> cartonTagInfos)> QueryCartonAsync(string url, string siteCode, string lineCode, string equipNum, string containerCode)
{
bool success = false; // 操作结果
string rawJson = ""; //
string message = ""; // 消息列表
MesResType mesResType = MesResType.C;
List<TagInfo> cartonTagInfos = new List<TagInfo>();
try
{
// 参数验证
if (string.IsNullOrEmpty(containerCode))
{
message = "容器码为空";
return (success, message, mesResType, cartonTagInfos);
}
// 创建请求模型
var model = new QueryCartonPalletParam
{
siteCode = siteCode,
equipNum = equipNum,
lineCode = lineCode,
containerCode = containerCode
};
// 序列化请求数据
var nowDate = DateTime.Now;
var postcontent = JsonConvert.SerializeObject(model);
// 记录请求开始时间
var sw = Stopwatch.StartNew();
// 调用异步API
string result = await MESApiHelper.SendRequestStringAsync(url, HttpMethod.Post, postcontent, false);
// 记录请求结束时间
sw.Stop();
rawJson = result;
// 如果请求耗时超过1秒,记录日志
long outTime = sw.ElapsedMilliseconds;
if (outTime > 1000)
{
//LogHelper.Instance.WriteLog($"进站PC<=>MES上传耗时:{outTime}ms,电芯码:{listBar[0]},{listBar[1]},{listBar[2]},{listBar[3]}", "MESTime");
LogHelper.Instance.WriteLog($"箱唛信息获取PC<=>MES耗时:{outTime}ms", "MESTime");
}
// 记录请求日志
string requestLog = $"{nowDate:yyyy-MM-dd HH:mm:ss.fff}:调用箱唛信息获取接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds):yyyy-MM-dd HH:mm:ss.fff}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{result}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate:yyyyMMdd}\箱唛信息获取\{nowDate:HH}.txt", requestLog);
// 如果返回结果为空,记录日志并返回
if (string.IsNullOrEmpty(result))
{
//string resData = result == "" ? "" : result;
//resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用箱唛信息获取接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
//TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\箱唛信息获取\{nowDate.ToString("HH")}.txt", resJson);
//return (success, mesMessage, mesResType, cartonTagInfos);
message = "MES接口返回空结果";
return (success, message, mesResType, cartonTagInfos);
}
// 解析返回结果
var response = JsonConvert.DeserializeObject<MesResponseCarton>(result);
// 如果操作成功
if (response != null && response.success)
{
success = true;
ProcessCartonResponse(response, ref message, ref mesResType, ref cartonTagInfos); // 处理返回结果
//resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用箱唛信息获取接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{result}";
//TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\箱唛信息获取\{nowDate.ToString("HH")}.txt", resJson);
if (success && mesResType == MesResType.D)
{
message = $"箱唛信息获取成功,共获取 {cartonTagInfos?.Count ?? 0} 个标签信息";
return (success, message, mesResType, cartonTagInfos);
}
else
{
return (success, message, mesResType, cartonTagInfos);
}
}
else
{
//// 如果操作失败,记录日志
//var resData = response == null ? "" : result;
//resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用箱唛信息获取接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
//TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\箱唛信息获取\{nowDate.ToString("HH")}.txt", resJson);
// 如果操作失败
message = response?.message ?? "MES接口返回失败状态";
mesResType = GetMesResType(response?.category);
return (success, message, mesResType, cartonTagInfos);//MesResult<List<TagInfo>>.Fail(message, mesResType, rawJson);
}
}
catch (Exception ex)
{
// 捕获异常并记录日志
message = $"箱唛信息获取接口发生异常:{ex.Message}";
LogHelper.Instance.WriteEX("箱唛信息获取接口", ex);
}
// 返回结果
return (success, message, mesResType, cartonTagInfos);
}
/// <summary>
/// 箱唛信息获取返回数据处理
/// </summary>
/// <param name="res1"></param>
/// <param name="BarCode"></param>
/// <param name="MesMessage"></param>
/// <param name="mesResType"></param>
private void ProcessCartonResponse(MesResponseCarton response, ref string message, ref MesResType mesResType, ref List<TagInfo> cartonTagInfos)
{
if (response != null)
{
cartonTagInfos = response.rows ?? new List<TagInfo>();
message = response.message;
if (response.success)
{
mesResType = MesResType.D;
message = "MES箱唛信息获取成功 ";
// 检查数据完整性
if (cartonTagInfos.Count == 0)
{
message = "未获取到箱唛标签信息";
mesResType = MesResType.B;
}
}
else
{
mesResType = GetMesResType(response.category);
}
}
else
{
message = "响应数据为空";
mesResType = MesResType.B;
}
}
#endregion
#region 栈板获取
/// <summary>
/// 栈板信息获取
/// </summary>
/// <param name="url">API地址 /api/public/equip/auto/palletquery</param>
/// <param name="siteCode">工厂代码</param>
/// <param name="lineCode">产线编号</param>
/// <param name="equipNum">设备编号</param>
/// <param name="containerCode">卡板码(箱唛号)</param>
/// <returns>返回结果(是否成功、消息列表、响应类型列表)</returns>
public async Task<(bool success, string mesMessage, MesResType mesResType, List<TagInfo> palletTagInfos)> QueryPalletAsync(string url, string siteCode, string lineCode, string equipNum, string containerCode)
{
bool success = false; // 操作结果
string rawJson = ""; // 日志信息
string message = ""; // 消息列表
MesResType mesResType = MesResType.C;
List<TagInfo> palletTagInfos = new List<TagInfo>();
try
{
// 参数验证
if (string.IsNullOrEmpty(containerCode))
{
message = "容器码为空";
return (success, message, mesResType, palletTagInfos);
}
// 创建请求模型
var model = new QueryCartonPalletParam
{
siteCode = siteCode,
lineCode = lineCode,
equipNum = equipNum,
containerCode = containerCode
};
// 序列化请求数据
var nowDate = DateTime.Now;
var postcontent = JsonConvert.SerializeObject(model);
// 记录请求开始时间
var sw = Stopwatch.StartNew();
// 调用异步API
string result = await MESApiHelper.SendRequestStringAsync(url, HttpMethod.Post, postcontent, false);
// 记录请求结束时间
sw.Stop();
rawJson = result;
// 如果请求耗时超过1秒,记录日志
long outTime = sw.ElapsedMilliseconds;
if (outTime > 1000)
{
//LogHelper.Instance.WriteLog($"进站PC<=>MES上传耗时:{outTime}ms,电芯码:{listBar[0]},{listBar[1]},{listBar[2]},{listBar[3]}", "MESTime");
LogHelper.Instance.WriteLog($"栈板信息获取PC<=>MES耗时:{outTime}ms", "MESTime");
}
// 记录请求日志
string requestLog = $"{nowDate:yyyy-MM-dd HH:mm:ss.fff}:调用栈板信息获取接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds):yyyy-MM-dd HH:mm:ss.fff}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{result}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate:yyyyMMdd}\栈板信息获取\{nowDate:HH}.txt", requestLog);
// 如果返回结果为空,记录日志并返回
if (string.IsNullOrEmpty(result))
{
//string resData = result == "" ? "" : result;
//resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用栈板信息获取接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
//TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\栈板信息获取\{nowDate.ToString("HH")}.txt", resJson);
//return (res, mesMessage, mesResType, palletTagInfos);
message = "MES接口返回空结果";
//return MesResult<List<TagInfo>>.Fail(message, MesResType.B, rawJson);
return (success, message, mesResType, palletTagInfos);
}
// 解析返回结果
var response = JsonConvert.DeserializeObject<MesResponsePallet>(result);
// 如果操作成功
if (response != null && response.success)
{
success = true;
ProcessPalletResponse(response, ref message, ref mesResType, ref palletTagInfos); // 处理返回结果
//resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用栈板信息获取接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{result}";
//TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\栈板信息获取\{nowDate.ToString("HH")}.txt", resJson);
if (success && mesResType == MesResType.D)
{
message = $"栈板信息获取成功,共获取 {palletTagInfos?.Count ?? 0} 个标签信息";
//return MesResult<List<TagInfo>>.Ok(palletTagInfos ?? new List<TagInfo>(), message, rawJson);
return (success, message, mesResType, palletTagInfos);
}
else
{
//return MesResult<List<TagInfo>>.Fail(message, mesResType, rawJson);
return (success, message, mesResType, palletTagInfos);
}
}
else
{
//// 如果操作失败,记录日志
//var resData = res1 == null ? "" : result;
//resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用栈板信息获取接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
//TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\栈板信息获取\{nowDate.ToString("HH")}.txt", resJson);
// 如果操作失败
message = response?.message ?? "MES接口返回失败状态";
mesResType = GetMesResType(response?.category);
//return MesResult<List<TagInfo>>.Fail(message, mesResType, rawJson);
}
}
catch (Exception ex)
{
// 捕获异常并记录日志
message = $"栈板信息获取接口发生异常:{ex.Message}";
LogHelper.Instance.WriteEX("栈板信息获取接口", ex);
}
// 返回结果
return (success, message, mesResType, palletTagInfos);
}
/// <summary>
/// 栈板信息获取返回数据处理
/// </summary>
/// <param name="res1"></param>
/// <param name="BarCode"></param>
/// <param name="MesMessage"></param>
/// <param name="mesResType"></param>
private void ProcessPalletResponse(MesResponsePallet response, ref string message, ref MesResType mesResType, ref List<TagInfo> palletTagInfos)
{
if (response != null)
{
palletTagInfos = response.rows ?? new List<TagInfo>();
message = response.message;
if (response.success)
{
mesResType = MesResType.D;
message = "MES栈板信息获取成功 ";
if (response.success)
{
mesResType = MesResType.D;
message = "栈板信息获取成功";
// 检查数据完整性
if (palletTagInfos.Count == 0)
{
message = "未获取到栈板标签信息";
mesResType = MesResType.B;
}
}
else
{
mesResType = GetMesResType(response.category);
}
}
}
else
{
message = "响应数据为空";
mesResType = MesResType.B;
}
}
#endregion
#region 客户标签与系统标签绑定
/// <summary>
/// 客户标签与系统标签绑定
/// </summary>
/// <param name="url">API地址 /api/public/equip/auto/</param>
/// <param name="siteCode">工厂代码</param>
/// <param name="lineCode">产线编号</param>
/// <param name="equipNum">设备编号</param>
/// <param name="containerCode">卡板码(箱唛号)</param>
/// <param name="palletCode">栈板码</param>
/// <param name="custcartonCode">客户箱唛码</param>
/// <param name="custpalletCode">客户栈板码</param>
/// <param name="custcarton">客户箱唛信息</param>
/// <param name="custpallet">客户栈板信息</param>
/// <returns>返回结果(是否成功、消息列表、响应类型列表)</returns>
public async Task<(bool success, string mesMessage, MesResType mesResType)> BindingAsync(string url, string siteCode, string lineCode, string equipNum, string containerCode, string palletCode, string custcartonCode, string custpalletCode, List<TagInfo> custcarton, List<TagInfo> custpallet)
{
bool success = false; // 操作结果
string rawJson = ""; // 日志信息
string message = ""; // 消息列表
MesResType mesResType = MesResType.C;
try
{
// 参数验证
if (string.IsNullOrEmpty(containerCode))
{
message = "容器码为空";
return (success, message, mesResType);
}
// 创建请求模型
var model = new BindingParam
{
siteCode = siteCode,
lineCode = lineCode,
equipNum = equipNum,
containerCode = containerCode,
palletCode = palletCode,
custcartonCode = custcartonCode,
custpalletCode = custpalletCode,
custcarton = custcarton,
custpallet = custpallet
};
// 序列化请求数据
var nowDate = DateTime.Now;
var postcontent = JsonConvert.SerializeObject(model);
// 记录请求开始时间
var sw = Stopwatch.StartNew();
// 调用异步API
string result = await MESApiHelper.SendRequestStringAsync(url, HttpMethod.Post, postcontent, false);
// 记录请求结束时间
sw.Stop();
rawJson = result;
// 如果请求耗时超过1秒,记录日志
long outTime = sw.ElapsedMilliseconds;
if (outTime > 1000)
{
//LogHelper.Instance.WriteLog($"进站PC<=>MES上传耗时:{outTime}ms,电芯码:{listBar[0]},{listBar[1]},{listBar[2]},{listBar[3]}", "MESTime");
LogHelper.Instance.WriteLog($"客户标签与系统标签绑定PC<=>MES耗时:{outTime}ms", "MESTime");
}
// 记录请求日志
string requestLog = $"{nowDate:yyyy-MM-dd HH:mm:ss.fff}:调用客户标签与系统标签绑定接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds):yyyy-MM-dd HH:mm:ss.fff}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{result}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate:yyyyMMdd}\客户标签与系统标签绑定\{nowDate:HH}.txt", requestLog);
// 如果返回结果为空,记录日志并返回
if (string.IsNullOrEmpty(result))
{
//string resData = result == "" ? "" : result;
//resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用客户标签与系统标签绑定接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
//TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\客户标签与系统标签绑定\{nowDate.ToString("HH")}.txt", resJson);
//return (res, mesMessage, mesResType);
message = "MES接口返回空结果";
//return MesResult<bool>.Fail(message, MesResType.B, rawJson);
return (success, message, mesResType);
}
// 解析返回结果
var response = JsonConvert.DeserializeObject<MesResponse>(result);
// 如果操作成功
if (response != null && response.success)
{
success = true;
ProcessBindingResponse(response, ref message, ref mesResType); // 处理返回结果
//resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用客户标签与系统标签绑定接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{result}";
//TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\卡板电芯信息上传(装箱)\{nowDate.ToString("HH")}.txt", resJson);
if (success && mesResType == MesResType.D)
{
message = "客户标签与系统标签绑定成功";
//return MesResult<bool>.Ok(true, message, rawJson);
return (success, message, mesResType);
}
else
{
//return MesResult<bool>.Fail(message, mesResType, rawJson);
return (success, message, mesResType);
}
}
else
{
//// 如果操作失败,记录日志
//var resData = res1 == null ? "" : result;
//resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用客户标签与系统标签绑定接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
//TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\客户标签与系统标签绑定\{nowDate.ToString("HH")}.txt", resJson);
// 如果操作失败
message = response?.message ?? "MES接口返回失败状态";
//mesResType = GetMesResType(response?.category);
//return MesResult<bool>.Fail(message, mesResType, rawJson);
return (success, message, mesResType);
}
}
catch (Exception ex)
{
//// 捕获异常并记录日志
//resJson = $"客户标签与系统标签绑定接口发生异常:{ex.Message}";
//LogHelper.Instance.WriteLog($"客户标签与系统标签绑定接口{ex}");
// 捕获异常并记录日志
message = $"客户标签与系统标签绑定接口发生异常:{ex.Message}";
LogHelper.Instance.WriteEX("客户标签与系统标签绑定接口", ex);
//return MesResult<bool>.Fail(message, MesResType.B, rawJson);
return (success, message, mesResType);
}
// 返回结果
//return (res, mesMessage, mesResType);
}
/// <summary>
/// 客户标签与系统标签绑定返回数据处理
/// </summary>
/// <param name="res1"></param>
/// <param name="BarCode"></param>
/// <param name="MesMessage"></param>
/// <param name="mesResType"></param>
private void ProcessBindingResponse(MesResponse response, ref string message, ref MesResType mesResType)
{
if (response != null)
{
message = response.message;
if (response.success)
{
mesResType = MesResType.D;
message = "MES客户标签与系统标签绑定成功";
}
else
{
//mesResType = GetMesResType(response.category);
}
}
else
{
message = "响应数据为空";
mesResType = MesResType.B;
}
}
#endregion
/// <summary>
/// 产品结果加工参数接口
/// </summary>
public async Task<bool> ProducResultParamAsync(string url, string siteCode, string lineCode, string equipNum, string materialCode, string userName, string containerCode, string palletId, List<BatteryInfo> batteryInfos, MesUploadType unloadType)
{
var nowDate = DateTime.Now;
bool res = false;
string resJson = "";
#region 结果上传MES
try
{
string sNowDate = nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff");
var model = new ProductResultParameters
{
equipNum = equipNum,
type = unloadType.ToString(),
payload = ""
};
switch (unloadType)
{
case MesUploadType.DZ:
var payload1 = new Payload1
{
siteCode = siteCode,
lineCode = lineCode,
userName = userName,
materialCode = materialCode,
carCode = "",
collection = "JS", //BL 补录, JS 及时上传
recordDate = sNowDate,
qty = batteryInfos.Count,
containerCode = containerCode,
};
var Items = new List<IdentificationListItem>();
foreach(var battery in batteryInfos)
{
IdentificationListItem item = new IdentificationListItem()
{
identification = battery.identification,
qualityStatus = "Y", //m.CSBYHOUTSTATIONJUDGMENTRESULT,
tagDataVOList = GetTagDataVOList(battery, containerCode, palletId, sNowDate)
};
Items.Add(item);
}
payload1.identificationList = Items;
model.payload = JsonConvert.SerializeObject(payload1);
break;
}
var postcontent = JsonConvert.SerializeObject(model);
var sw = new Stopwatch();
sw.Start();
//string result = MESApiHelper.WepPostAPIAsync(url, postcontent);
string result = await MESApiHelper.SendRequestStringAsync(url, HttpMethod.Post, postcontent, false);
sw.Stop();
if (string.IsNullOrEmpty(result))
{
string resData = result == "" ? "" : result;
resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用产品结果加工参数接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\产品结果加工参数\{nowDate.ToString("HH")}.txt", resJson);
return res;
}
var res1 = ReturnMesMsg<MesResponse>(result);
if (!res1.success)
{
var resData = res1 == null ? "" : JsonConvert.SerializeObject(res1);
//Mesage = res1.message;
resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用产品结果加工参数接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\产品结果加工参数\{nowDate.ToString("HH")}.txt", resJson);
}
else
{
res = true;
//Mesage = res1.message;
resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用产品结果加工参数接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{JsonConvert.SerializeObject(result)}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\产品结果加工参数\{nowDate.ToString("HH")}.txt", resJson);
}
}
catch (Exception ex)
{
resJson = $"产品结果加工参数接口发生异常:{ex.Message}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\产品结果加工参数\{nowDate.ToString("HH")}.txt", resJson);
}
#endregion
return res;
}
private List<TagDataVOListItem> GetTagDataVOList(BatteryInfo m, string containerId, string palletId, string insertTime)
{
List<TagDataVOListItem> datavolist = new List<TagDataVOListItem>();
// 托盘号
datavolist.Add(new TagDataVOListItem()
{
tagCode = "TRAY_NUMBER_DBJJG",
tagValue = containerId,
tagTime = insertTime,
tagCalculateResult = "",
tagRemark = "托盘号"
});
// 栈板号
datavolist.Add(new TagDataVOListItem()
{
tagCode = "PALLET_NUMBER_DBJJG",
tagValue = palletId,
tagTime = insertTime,
tagCalculateResult = "",
tagRemark = "栈板号"
});
// 电池位置
datavolist.Add(new TagDataVOListItem()
{
tagCode = "BATTERY_LOCATION_DBJJG",
tagValue = m.locationRow,
tagTime = insertTime,
tagCalculateResult = "",
tagRemark = "电池位置"
});
// 设备编码
datavolist.Add(new TagDataVOListItem()
{
tagCode = "TOP_COVER_CODE_DBJJG",
tagValue = "",
tagTime = insertTime,
tagCalculateResult = "",
tagRemark = "电芯顶盖码"
});
return datavolist;
}
#region 12、设备状态
/// <summary>
/// 设备状态
/// </summary>
/// <param name="url">API URL</param>
/// <param name="siteCode">站点代码</param>
/// <param name="lineCode">生产线代码</param>
/// <param name="equipNum">设备编号</param>
/// <param name="materialCode">物料代码</param>
/// <param name="userName">用户名</param>
/// <param name="guid">唯一标识</param>
/// <param name="statusCode">状态代码</param>
/// <param name="faultCodes">故障代码列表</param>
/// <returns>返回操作结果和消息</returns>
public async Task<(bool Success, string ResJson)> UploadEquStatusAsync(string url, string siteCode, string lineCode, string equipNum, string materialCode, string userName, string guid, string statusCode, List<FaultCodeList> faultCodes)
{
bool res = false;
string resJson = "";
try
{
DateTime nowDate = DateTime.Now;
string sNowDate = nowDate.ToString("yyyy-MM-dd HH:mm:ss");
var parameters = new EqpStatusParam
{
siteCode = siteCode,
lineCode = lineCode,
equipNum = equipNum,
materialCode = materialCode,
userName = userName,
seatId = "",
recordDate = sNowDate,
statusCode = statusCode.ToString(),
uploadTime = sNowDate,
guid = guid
};
// 设置故障代码列表
parameters.faultCodeList = faultCodes;
var postcontent = JsonConvert.SerializeObject(parameters);
var sw = Stopwatch.StartNew();
// 异步调用
string result = await MESApiHelper.SendRequestStringAsync(url, HttpMethod.Post, postcontent, false);
sw.Stop();
if (string.IsNullOrEmpty(result))
{
string resData = result == "" ? "" : result;
resJson = $"{sNowDate}:调用设备运行状态接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\设备运行状态\{nowDate.ToString("HH")}.txt", resJson);
return (res, resJson);
}
var res1 = ReturnMesMsg<MesResponse>(result);
if (!res1.success)
{
var resData = res1 == null ? "" : JsonConvert.SerializeObject(res1);
resJson = $"{sNowDate}:调用设备运行状态接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\设备运行状态\{nowDate.ToString("HH")}.txt", resJson);
}
else
{
res = true;
resJson = $"{sNowDate}:调用设备运行状态接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{JsonConvert.SerializeObject(result)}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\设备运行状态\{nowDate.ToString("HH")}.txt", resJson);
}
}
catch (Exception ex)
{
resJson = $"设备运行状态接口发生异常:{ex.Message}";
LogHelper.Instance.WriteEX("设备运行状态接口", ex);
}
return (res, resJson);
}
#endregion
#region 13、设备报警上传接口
/// <summary>
/// 设备报警上传接口
/// </summary>
/// <param name="url">API URL</param>
/// <param name="siteCode">站点代码</param>
/// <param name="lineCode">生产线代码</param>
/// <param name="equipNum">设备编号</param>
/// <param name="materialCode">物料代码</param>
/// <param name="userName">用户名</param>
/// <param name="alarmObj">报警数据列表</param>
/// <param name="guid">唯一标识</param>
/// <returns>返回操作结果和消息</returns>
public async Task<(bool Success, string Mesage)> UploadEquAlarmAsync(string url, string siteCode, string lineCode, string equipNum, string materialCode, string userName, List<AlarmData> alarmObj, string guid)
{
bool res = false;
string resJson = "";
string mesage = ""; // 用于存储消息
try
{
var nowDate = DateTime.Now;
if (alarmObj.Count > 0)
{
Console.WriteLine($"{nowDate}: 开始处理报警数据,数量:{alarmObj.Count}");
var model = new UploadAlarmParam
{
siteCode = siteCode,
lineCode = lineCode,
equipNum = equipNum,
materialCode = materialCode,
userName = userName,
recordDate = nowDate.ToString("yyyy-MM-dd HH:mm:ss"),
guid = guid,
seatId = ""
};
model.alarmLineList = new List<AlarmLineListItem>();
for (int j = 0; j < alarmObj.Count; j++)
{
AlarmLineListItem alarmList = new AlarmLineListItem
{
alarmEndTime = System.Convert.ToString(alarmObj[j].EndTime),
alarmType = alarmObj[j].AlarmType,
alarmName = alarmObj[j].AlarmContent,
alarmStartTime = System.Convert.ToString(alarmObj[j].StartTime),
faultCode = alarmObj[j].AlarmCode
};
model.alarmLineList.Add(alarmList);
Console.WriteLine($"{nowDate}: 添加报警数据 - 报警类型:{alarmObj[j].AlarmType}, 报警内容:{alarmObj[j].AlarmContent}");
}
var postcontent = JsonConvert.SerializeObject(model);
Console.WriteLine($"{nowDate}: 请求数据已序列化:{postcontent}");
var sw = Stopwatch.StartNew();
Console.WriteLine($"{nowDate}: 开始调用设备报警信息接口");
// 异步调用
string result = await MESApiHelper.SendRequestStringAsync(url, HttpMethod.Post, postcontent, false);
sw.Stop();
Console.WriteLine($"{nowDate}: 接口调用完成,耗时:{sw.ElapsedMilliseconds}ms");
if (string.IsNullOrEmpty(result))
{
string resData = result == "" ? "" : result;
resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用设备报警信息接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\报警信息\{nowDate.ToString("HH")}.txt", resJson);
Console.WriteLine($"{nowDate}: 接口返回结果为空");
return (res, mesage);
}
var res1 = ReturnMesMsg<MesResponse>(result);
if (!res1.success)
{
var resData = res1 == null ? "" : JsonConvert.SerializeObject(res1);
mesage = res1.message;
resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用设备报警信息接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\报警信息\{nowDate.ToString("HH")}.txt", resJson);
Console.WriteLine($"{nowDate}: 接口调用失败,消息:{mesage}");
}
else
{
res = true;
mesage = res1.message;
resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用设备报警信息接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{JsonConvert.SerializeObject(result)}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\报警信息\{nowDate.ToString("HH")}.txt", resJson);
Console.WriteLine($"{nowDate}: 接口调用成功,消息:{mesage}");
}
}
else
{
Console.WriteLine($"{nowDate}: 报警数据为空,无需处理");
}
}
catch (Exception ex)
{
resJson = $"调用设备报警信息接口发生异常:{ex.Message}";
LogHelper.Instance.WriteEX("调用设备报警信息接口", ex);
Console.WriteLine($"{DateTime.Now}: 发生异常:{ex.Message}");
}
return (res, mesage);
}
#endregion
#region 14、上传设备能耗数据
/// <summary>
/// 上传设备能耗数据
/// </summary>
/// <param name="url">API URL</param>
/// <param name="siteCode">站点代码</param>
/// <param name="lineCode">生产线代码</param>
/// <param name="equipNum">设备编号</param>
/// <param name="userName">用户名</param>
/// <param name="electricMeterNum">电表编号列表</param>
/// <param name="wattrs">能耗数据列表</param>
/// <returns>返回操作结果和日志信息</returns>
public async Task<(bool Success, string Mesage)> UploadEquEnergyAsync(string url, string siteCode, string lineCode, string equipNum, string userName, List<string> electricMeterNum, List<WattrMeter> wattrs)
{
bool res = false;
string mesage = "";
try
{
var nowDate = DateTime.Now;
Console.WriteLine($"{nowDate}: 开始处理设备能耗数据");
// 序列化请求数据
var postcontent = JsonConvert.SerializeObject(wattrs);
Console.WriteLine($"{nowDate}: 请求数据已序列化:{postcontent}");
var sw = Stopwatch.StartNew();
Console.WriteLine($"{nowDate}: 开始调用设备能耗信息接口");
// 异步调用
string result = await MESApiHelper.SendRequestStringAsync(url, HttpMethod.Post, postcontent, false);
sw.Stop();
Console.WriteLine($"{nowDate}: 接口调用完成,耗时:{sw.ElapsedMilliseconds}ms");
if (string.IsNullOrEmpty(result))
{
string resData = result == "" ? "" : result;
mesage = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用设备能耗信息接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\设备能耗信息\{nowDate.ToString("HH")}.txt", mesage);
Console.WriteLine($"{nowDate}: 接口返回结果为空");
return (res, mesage);
}
var res1 = ReturnMesMsg<MesResponse>(result);
if (!res1.success)
{
var resData = res1 == null ? "" : JsonConvert.SerializeObject(res1);
mesage = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用设备能耗信息接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\设备能耗信息\{nowDate.ToString("HH")}.txt", mesage);
Console.WriteLine($"{nowDate}: 接口调用失败,返回结果:{resData}");
}
else
{
res = true;
mesage = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用设备能耗信息接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{JsonConvert.SerializeObject(result)}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\设备能耗信息\{nowDate.ToString("HH")}.txt", mesage);
Console.WriteLine($"{nowDate}: 接口调用成功,返回结果:{result}");
}
}
catch (Exception ex)
{
mesage = $"调用设备能耗信息接口发生异常:{ex.Message}";
LogHelper.Instance.WriteEX("调用设备能耗信息接口", ex);
Console.WriteLine($"{DateTime.Now}: 发生异常:{ex.Message}");
}
return (res, mesage);
}
#endregion
#region 15、风速仪数据上传至MES
/// <summary>
/// 风速仪数据上传至MES
/// </summary>
/// <param name="url">API URL</param>
/// <param name="equipNum">设备编号</param>
/// <param name="antiDustAirSpeedNum">风速仪编号列表</param>
/// <param name="collectData">采集数据列表</param>
/// <returns>返回操作结果和消息</returns>
public async Task<(bool Success, string Mesage)> UploadDeviceCollectDataAsync(string url, string equipNum, List<string> antiDustAirSpeedNum, List<float> collectData)
{
bool res = false;
string resJson = "";
string mesage = ""; // 用于存储消息
try
{
if (collectData == null || collectData.Count == 0)
{
mesage = "采集数据为空,无需处理";
Console.WriteLine($"{DateTime.Now}: {mesage}");
return (res, mesage);
}
var nowDate = DateTime.Now;
Console.WriteLine($"{nowDate}: 开始处理风速仪数据");
// 创建请求模型
UploadDeviceCollectData uploadDevice = new UploadDeviceCollectData
{
appid = "e1012966a936170c6fcab98e24042d10",
device_code = equipNum,
collect_time = GetUnixTimestamp()
};
// 填充采集数据
List<Collect_code_value_list> list = new List<Collect_code_value_list>();
for (int i = 0; i < collectData.Count; i++)
{
Collect_code_value_list _Value_List = new Collect_code_value_list
{
target_code = 5,
target_value = collectData[i].ToString(),
target_value_tag = antiDustAirSpeedNum[i]
};
list.Add(_Value_List);
Console.WriteLine($"{nowDate}: 添加采集数据 - 风速仪编号:{antiDustAirSpeedNum[i]}, 采集值:{collectData[i]}");
}
uploadDevice.collect_code_value_list = list;
// 序列化请求数据
string postcontent = JsonConvert.SerializeObject(uploadDevice);
Console.WriteLine($"{nowDate}: 请求数据已序列化:{postcontent}");
var sw = Stopwatch.StartNew();
Console.WriteLine($"{nowDate}: 开始调用风速接口");
// 异步调用
string result = await MESApiHelper.SendRequestStringAsync(url, HttpMethod.Post, postcontent, false);
sw.Stop();
Console.WriteLine($"{nowDate}: 接口调用完成,耗时:{sw.ElapsedMilliseconds}ms");
if (string.IsNullOrEmpty(result))
{
string resData = result == "" ? "" : result;
resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用风速接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\风速信息\{nowDate.ToString("HH")}.txt", resJson);
mesage = "接口返回结果为空";
Console.WriteLine($"{nowDate}: {mesage}");
return (res, mesage);
}
var res1 = ReturnMesMsg<MesResponse>(result);
if (!res1.success)
{
var resData = res1 == null ? "" : JsonConvert.SerializeObject(res1);
mesage = res1.message;
resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用风速接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{resData}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\风速信息\{nowDate.ToString("HH")}.txt", resJson);
Console.WriteLine($"{nowDate}: 接口调用失败,消息:{mesage}");
}
else
{
res = true;
mesage = res1.message;
resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用风速接口:{url},请求数据为:{postcontent},\n{nowDate.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{JsonConvert.SerializeObject(result)}";
TxtHelper.WriteTxt($@"D:\APILog\Logs\MesLogs\{nowDate.ToString("yyyyMMdd")}\风速信息\{nowDate.ToString("HH")}.txt", resJson);
Console.WriteLine($"{nowDate}: 接口调用成功,消息:{mesage}");
}
}
catch (Exception ex)
{
resJson = $"调用风速接口发生异常:{ex.Message}";
LogHelper.Instance.WriteEX("调用风速接口", ex);
Console.WriteLine($"{DateTime.Now}: 发生异常:{ex.Message}");
}
return (res, mesage);
}
#endregion
/// <summary>
/// 序列化各类返回
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="ret"></param>
/// <returns></returns>
private T ReturnMesMsg<T>(string ret)
{
try
{
return JsonConvert.DeserializeObject<T>(ret);
}
catch (System.Exception ex)
{
return default(T);
}
}
public List<MesResType> GetMesType(int Num)
{
List<MesResType> mesResTypes = new List<MesResType>();
for (int i = 0; i < Num; i++)
{
mesResTypes.Add(MesResType.B);
}
return mesResTypes;
}
public List<string> GetListString(int Num, string str)
{
List<string> mesResTypes = new List<string>();
for (int i = 0; i < Num; i++)
{
mesResTypes.Add(str);
}
return mesResTypes;
}
/// <summary>
///本地时间为Unix时间戳
/// </summary>
/// <returns></returns>
public static long GetUnixTimestamp()
{
return (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
}
public Task<(bool Success, string ResJson)> ParamChangeAsync(string url, string siteCode, string lineCode, string equipNum, string materialCode, string userName, List<TagListItem> paramChangeItems)
{
throw new NotImplementedException();
}
//#endregion
}
}