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、登录验证
/// 账号密码登录(异步方法)
///
/// API地址
/// 站点代码
/// 线体代码
/// 设备编号
/// 用户名
/// 密码
/// 用户卡ID
/// 返回结果(是否成功、消息) 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(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);
}
///
/// 刷卡登录
///
///
///
///
///
///
///
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(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参数请求
///
/// 获取MES参数请求
///
///
///
///
///
///
///
///
///
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(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、参数变更请求
///
/// 参数变更请求
///
///
///
///
///
///
///
///
///
///
public async Task ParamChange(string url, string siteCode, string lineCode, string equipNum, string materialCode, string userName, List 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 根据料框码获取电池型号、电池条码等数据
///
/// 根据料框码获取电池型号、电池条码等数据
///
/// API地址 /api/public/equip/auto/gradequery
/// 工厂代码
/// 产线编号
/// 设备编号
/// 记录时间
/// 数量
/// 托盘码
/// 物料代码
/// 操作人
/// 电芯信息列表
/// 返回结果(是否成功、消息列表、响应类型列表)
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(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);
}
///
/// 料盘信息查询返回数据处理
///
///
///
///
///
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 电芯档位获取
///
/// 电芯档位获取
///
/// API地址 /api/public/equip/auto/gradequery
/// 工厂代码
/// 产线编号
/// 设备编号
/// 记录时间
/// 数量
/// 托盘码
/// 物料代码
/// 操作人
/// 电芯信息列表
/// 返回结果(是否成功、消息列表、响应类型列表)
public async Task<(bool Success, List materialGrades)> QueryGradeAsync(string url, string siteCode, string lineCode, string equipNum, string recordDate, string qty, string materialCode, string userName, List listCode)
{
bool success = false; // 操作结果
string rawJson = "";
string message = ""; // 消息列表
MesResType mesResType = MesResType.C;
List materialGrades = new List();
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(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);
}
///
/// 电芯档位查询返回数据处理
///
///
///
///
///
private void ProcessGradeResponse(MesResponseGrade res, List barCodes, ref string mesMessage, ref List materialGrades)
{
if (res != null)
{
materialGrades = res.rows ?? new List();
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
///
/// 将MES返回的category转换为MesResType
///
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 解绑所有料框码
///
/// 料框码与电池解绑
///
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(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>.Fail(message, MesResType.B, rawJson);
}
}
#endregion
#region 卡板电芯信息上传(装箱)
///
/// 半成品电池绑定(装箱)
///
/// API地址 /api/public/equip/auto/pack
/// 工厂代码
/// 设备编号
/// 卡板码(箱唛号)
/// 档位
/// 电芯信息
/// 返回结果(是否成功、消息列表、响应类型列表)
public async Task<(bool success, string mesMessage, MesResType mesResType, List materialPackInfos)> PackLoadAsync(string url, string siteCode, string lineCode, string equipNum, string ContainerCode, int capacity, string grade, string materialCode, List cellDataVOList)
{
bool success = false; // 操作结果
string rawJson = ""; // 日志信息
string message = ""; // 消息列表
MesResType mesResType = MesResType.C;
List materialPackInfos = new List();
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 containerBarcodeInfos = new List
{ };
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 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(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>.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>.Fail(message, MesResType.B, rawJson);
}
// 返回结果
//return (success, message, mesResType, materialPackInfos);
}
///
/// 卡板电芯信息上传(装箱)返回数据处理
///
///
///
///
///
private void ProcessPackResponse(MesResponsePack response, List barcodeList, List cellDataVOList, ref string message, ref MesResType mesResType, ref List materialPackInfos)
{
if (response != null)
{
//materialPackInfos = new List();
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;
}
}
///
/// 成品电池绑定
///
/// API地址 /api/public/equip/auto/pack
/// 工厂代码
/// 设备编号
/// 卡板码(箱唛号)
/// 档位
/// 电芯信息
/// 返回结果(是否成功、消息列表、响应类型列表)
public async Task<(bool success, string mesMessage, MesResType mesResType, List materialPackInfos)> PackLoadFinishAsync(string url, string siteCode, string lineCode, string equipNum, string ContainerCode, int capacity, string grade, string materialCode, List cellDataVOList)
{
bool success = false; // 操作结果
string rawJson = ""; // 日志信息
string message = ""; // 消息列表
MesResType mesResType = MesResType.C;
List materialPackInfos = new List();
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 containerBarcodeInfos = new List(){ };
containerBarcodeInfos.Add(containerInfo);
// 创建请求模型
var model = new FinishPackParam
{
siteCode = siteCode,
lineCode = lineCode,
containerBarcodeInfos = containerBarcodeInfos,
equipCode = equipNum,
outerContainerCode = ContainerCode
};
List 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(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>.Fail(message, mesResType, rawJson);
}
}
catch (Exception ex)
{
// 捕获异常并记录日志
message = $"卡板电芯信息上传(装箱)接口发生异常:{ex.Message}";
LogHelper.Instance.WriteEX("卡板电芯信息上传(装箱)接口", ex);
//return (success, message, mesResType, materialPackInfos);//MesResult>.Fail(message, MesResType.B, rawJson);
}
// 返回结果
return (success, message, mesResType, materialPackInfos);
}
///
/// 成品电池绑定返回数据处理
///
///
///
///
///
private void ProcessFinishPackResponse(MesResponseFinishPack response, List barcodeList, List cellDataVOList, ref string message, ref MesResType mesResType, ref List materialPackInfos)
{
if (response != null)
{
materialPackInfos = response.rows ?? new List();
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 箱唛信息获取
///
/// 箱唛信息获取
///
/// API地址 /api/public/equip/auto/cartonquery
/// 工厂代码
/// 产线编号
/// 设备编号
/// 卡板码(箱唛号)
/// 返回结果(是否成功、消息列表、响应类型列表)
public async Task<(bool success, string mesMessage, MesResType mesResType, List cartonTagInfos)> QueryCartonAsync(string url, string siteCode, string lineCode, string equipNum, string containerCode)
{
bool success = false; // 操作结果
string rawJson = ""; //
string message = ""; // 消息列表
MesResType mesResType = MesResType.C;
List cartonTagInfos = new List();
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(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>.Fail(message, mesResType, rawJson);
}
}
catch (Exception ex)
{
// 捕获异常并记录日志
message = $"箱唛信息获取接口发生异常:{ex.Message}";
LogHelper.Instance.WriteEX("箱唛信息获取接口", ex);
}
// 返回结果
return (success, message, mesResType, cartonTagInfos);
}
///
/// 箱唛信息获取返回数据处理
///
///
///
///
///
private void ProcessCartonResponse(MesResponseCarton response, ref string message, ref MesResType mesResType, ref List cartonTagInfos)
{
if (response != null)
{
cartonTagInfos = response.rows ?? new List();
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 栈板获取
///
/// 栈板信息获取
///
/// API地址 /api/public/equip/auto/palletquery
/// 工厂代码
/// 产线编号
/// 设备编号
/// 卡板码(箱唛号)
/// 返回结果(是否成功、消息列表、响应类型列表)
public async Task<(bool success, string mesMessage, MesResType mesResType, List palletTagInfos)> QueryPalletAsync(string url, string siteCode, string lineCode, string equipNum, string containerCode)
{
bool success = false; // 操作结果
string rawJson = ""; // 日志信息
string message = ""; // 消息列表
MesResType mesResType = MesResType.C;
List palletTagInfos = new List();
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>.Fail(message, MesResType.B, rawJson);
return (success, message, mesResType, palletTagInfos);
}
// 解析返回结果
var response = JsonConvert.DeserializeObject(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>.Ok(palletTagInfos ?? new List(), message, rawJson);
return (success, message, mesResType, palletTagInfos);
}
else
{
//return MesResult>.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>.Fail(message, mesResType, rawJson);
}
}
catch (Exception ex)
{
// 捕获异常并记录日志
message = $"栈板信息获取接口发生异常:{ex.Message}";
LogHelper.Instance.WriteEX("栈板信息获取接口", ex);
}
// 返回结果
return (success, message, mesResType, palletTagInfos);
}
///
/// 栈板信息获取返回数据处理
///
///
///
///
///
private void ProcessPalletResponse(MesResponsePallet response, ref string message, ref MesResType mesResType, ref List palletTagInfos)
{
if (response != null)
{
palletTagInfos = response.rows ?? new List();
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 客户标签与系统标签绑定
///
/// 客户标签与系统标签绑定
///
/// API地址 /api/public/equip/auto/
/// 工厂代码
/// 产线编号
/// 设备编号
/// 卡板码(箱唛号)
/// 栈板码
/// 客户箱唛码
/// 客户栈板码
/// 客户箱唛信息
/// 客户栈板信息
/// 返回结果(是否成功、消息列表、响应类型列表)
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 custcarton, List 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.Fail(message, MesResType.B, rawJson);
return (success, message, mesResType);
}
// 解析返回结果
var response = JsonConvert.DeserializeObject(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.Ok(true, message, rawJson);
return (success, message, mesResType);
}
else
{
//return MesResult.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.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.Fail(message, MesResType.B, rawJson);
return (success, message, mesResType);
}
// 返回结果
//return (res, mesMessage, mesResType);
}
///
/// 客户标签与系统标签绑定返回数据处理
///
///
///
///
///
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
///
/// 产品结果加工参数接口
///
public async Task ProducResultParamAsync(string url, string siteCode, string lineCode, string equipNum, string materialCode, string userName, string containerCode, string palletId, List 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();
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(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 GetTagDataVOList(BatteryInfo m, string containerId, string palletId, string insertTime)
{
List datavolist = new List();
// 托盘号
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、设备状态
///
/// 设备状态
///
/// API URL
/// 站点代码
/// 生产线代码
/// 设备编号
/// 物料代码
/// 用户名
/// 唯一标识
/// 状态代码
/// 故障代码列表
/// 返回操作结果和消息
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 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(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、设备报警上传接口
///
/// 设备报警上传接口
///
/// API URL
/// 站点代码
/// 生产线代码
/// 设备编号
/// 物料代码
/// 用户名
/// 报警数据列表
/// 唯一标识
/// 返回操作结果和消息
public async Task<(bool Success, string Mesage)> UploadEquAlarmAsync(string url, string siteCode, string lineCode, string equipNum, string materialCode, string userName, List 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();
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(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、上传设备能耗数据
///
/// 上传设备能耗数据
///
/// API URL
/// 站点代码
/// 生产线代码
/// 设备编号
/// 用户名
/// 电表编号列表
/// 能耗数据列表
/// 返回操作结果和日志信息
public async Task<(bool Success, string Mesage)> UploadEquEnergyAsync(string url, string siteCode, string lineCode, string equipNum, string userName, List electricMeterNum, List 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(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
///
/// 风速仪数据上传至MES
///
/// API URL
/// 设备编号
/// 风速仪编号列表
/// 采集数据列表
/// 返回操作结果和消息
public async Task<(bool Success, string Mesage)> UploadDeviceCollectDataAsync(string url, string equipNum, List antiDustAirSpeedNum, List 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 list = new 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(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
///
/// 序列化各类返回
///
///
///
///
private T ReturnMesMsg(string ret)
{
try
{
return JsonConvert.DeserializeObject(ret);
}
catch (System.Exception ex)
{
return default(T);
}
}
public List GetMesType(int Num)
{
List mesResTypes = new List();
for (int i = 0; i < Num; i++)
{
mesResTypes.Add(MesResType.B);
}
return mesResTypes;
}
public List GetListString(int Num, string str)
{
List mesResTypes = new List();
for (int i = 0; i < Num; i++)
{
mesResTypes.Add(str);
}
return mesResTypes;
}
///
///本地时间为Unix时间戳
///
///
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 paramChangeItems)
{
throw new NotImplementedException();
}
//#endregion
}
}