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.Reflection.Emit; using System.Runtime.Remoting.Messaging; using System.Text; using System.Threading.Tasks; namespace JinYuan.MES { public class MesDataProcess : IMes { #region 1、登录验证 √ /// 账号密码登录(异步方法) /// /// API地址 /// 站点代码 /// 线体代码 /// 设备编号 /// 用户名 /// 密码 /// 用户卡ID /// 返回结果(是否成功、消息) userCardID = UserCardID public async Task<(bool Success, string Message, string MesUserLevel)> LoginEnyityAsync(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 LoginEnyity { 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; } 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)> LoginEnyityAsync(string url, string siteCode, string lineCode, string equipNum, string UserCardID) { bool res = false; // 操作结果 string resJson = ""; // 日志信息 string message = ""; // 消息 try { // 创建请求模型 var model = new LoginEnyity { 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, //changeTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), //tagGroupCode = "", //tagGroupDescription = "", }; //model.tagList = new List(); //model.tagList.Add(new TagListItem //{ // tagCode = "", // unitBefore = "", //}); 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 4、单个上传(只上传卷芯码,不上传托杯码) /// /// 单个上传(只上传卷芯码,不上传托杯码) /// /// /// /// /// /// /// /// /// {"rows":[{"code":null,"success":true,"message":null,"identification":"InsertAt20231218095755934","containerCode":null,"category":null}],"success":true,"total":1} /// public async Task<(bool Success, string Mesage, MesResType MesResType)> SingleCellInStationAsync(string url, string siteCode, string lineCode, string equipNum, string materialCode, string userName, string BarCode) { bool res = false; string resJson = ""; string mesage = ""; // 用于存储消息 MesResType mesResType = MesResType.D; // 默认为 OK try { var model = new SingleCellInStation { siteCode = siteCode, lineCode = lineCode, equipNum = equipNum, materialCode = materialCode, userName = userName, identification = BarCode, productType = "OK" }; 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(); long outTime = sw.ElapsedMilliseconds; if (outTime > 1000) { LogHelper.Instance.WriteLog($"钢壳进站PC<=>MES上传耗时:{outTime}ms,钢壳码:{BarCode}", "MESTime"); } 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, mesage, mesResType); } var res1 = ReturnMesMsg(result); if (res1.success) { res = true; GetInMesResType(res1, BarCode, ref mesage, 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); } else { var resData = res1 == null ? "" : result; mesage = res1.message; if (res1.category != "") { switch (res1.category) { case "A": mesResType = MesResType.A; break; case "B": mesResType = MesResType.B; break; case "C": mesResType = MesResType.C; break; case "D": mesResType = MesResType.D; break; default: break; } } 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); } } catch (Exception ex) { mesage = "调用进站接口超时或MES无返回信息!"; mesResType = MesResType.B; resJson = $"产品进站信息(组装)接口发生异常:{ex.Message}"; LogHelper.Instance.WriteEX("产品进站信息(组装)接口", ex); } return (res, mesage, mesResType); } #endregion #region 5、复投单个上传(只上传条码,不上传卷芯码) /// 复投单个上传(异步方法) /// /// API地址 /// 站点代码 /// 线体代码 /// 设备编号 /// 物料代码 /// 用户名 /// 条码 /// 返回结果(是否成功、日志信息、响应类型) public async Task<(bool, string, MesResType)> SingleFTCellInStationAsync(string url, string siteCode, string lineCode, string equipNum, string materialCode, string userName, string BarCode) { bool res = false; // 操作结果 string resJson = ""; // 日志信息 MesResType mesResType = new MesResType(); // 响应类型 try { // 创建请求模型 var model = new ManyCellInStation { siteCode = siteCode, lineCode = lineCode, equipNum = equipNum, //materialCode = materialCode, userName = userName, identificationList = new List { BarCode }, // 电芯条码 containerCodeList = new List() // 电芯托杯码(为空) }; // 序列化请求数据 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,钢壳码:{BarCode}", "MESTime"); } // 如果返回结果为空,记录日志并返回 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, resJson, mesResType); } // 解析返回结果 var res1 = JsonConvert.DeserializeObject(result); // 如果操作成功 if (res1.success) { res = true; GetInMesResType(res1, BarCode, ref resJson, 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); } 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); } } catch (Exception ex) { // 捕获异常并记录日志 resJson = $"产品进站信息(组装)接口发生异常:{ex.Message}"; LogHelper.Instance.WriteEX("产品进站信息(组装)接口", ex); } // 返回结果 return (res, resJson, mesResType); } #endregion #region 原材料上传 /// /// 原材料上传 /// /// /// /// /// /// /// /// public async Task<(bool Success, string MesMessage, MesResType MesResType)> RawMaterialStationAsync(string url, string MaterialCode, string equipNum, string BarCode) { bool res = false; string resJson = ""; string mesMessage = "失败"; // 消息列表 MesResType mesResType = MesResType.B; // 响应类型列表 try { JObject jo4 = new JObject(); jo4.Add("materialCode", MaterialCode);//物料编码 jo4.Add("workOrderNum", "");//工单编号 jo4.Add("mesEquipNum", equipNum);//设备编号 JArray jo5 = new JArray(); jo5.Add(BarCode);//原材料条码 jo4.Add("materialLotCodeList", jo5); var nowDate = DateTime.Now; var sw = new Stopwatch(); sw.Start(); string result = await MESApiHelper.SendRequestStringAsync(url, HttpMethod.Post, jo4, false); sw.Stop(); if (string.IsNullOrEmpty(result)) { mesMessage = "原材料接口失败,MES服务器或网络异常,请联系MES维护人员处理!"; string resData = result == "" ? "" : result; resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用原材料接口:{url},请求数据为:{jo4.ToString()},\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); } var res1 = ReturnMesMsg(result); if (!res1.success) { var resData = res1 == null ? "" : JsonConvert.SerializeObject(res1); resJson = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用原材料接口:{url},请求数据为:{jo4.ToString()},\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 = $"{nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用原材料接口:{url},请求数据为:{jo4.ToString()},\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); } if (res1 != null) { if (res1.success) { mesResType = MesResType.D; mesMessage = "原材料上传成功"; } else { mesResType = MesResType.B; mesMessage = res1.message; } } else { mesResType = MesResType.UnKnow; mesMessage = "原材料接口调用失败,请联系MES维护人员!"; } } catch (Exception ex) { resJson = $"原材料接口发生异常:{ex.Message}"; LogHelper.Instance.WriteEX("原材料接口", ex); } // 返回结果 return (res, mesMessage, mesResType); } #endregion #region 多个电池进站(异步方法)(进站)√ /// /// 多个电池进站(异步方法) /// /// API地址 /// 站点代码 /// 线体代码 /// 设备编号 /// 物料代码 /// 用户名 /// 条码列表 /// 返回结果(是否成功、消息列表、响应类型列表) public async Task<(bool Success, List MesMessage, List MesResType)> ManyCellInStationAsync(string url, string siteCode, string lineCode, string equipNum, string materialCode, string userName, List listBar) { bool res = false; // 操作结果 string resJson = ""; // 日志信息 List mesMessage = GetListString(listBar.Count, "进站失败"); // 消息列表 List mesResType = GetMesType(listBar.Count); // 响应类型列表 try { // 创建请求模型 var model = new ManyCellInStation { siteCode = siteCode, lineCode = lineCode, equipNum = equipNum, // materialCode = materialCode,//取消 userName = userName, productType = "G11",//待定 identificationList = new List(listBar) // 电芯条码 }; // 序列化请求数据 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,电芯码:{listBar[0]},{listBar[1]},{listBar[2]},{listBar[3]}", "MESTime"); LogHelper.Instance.WriteLog($"进站PC<=>MES上传耗时:{outTime}ms,电芯码:{string.Join(",", listBar)}", "MESTime"); } // 如果返回结果为空,记录日志并返回 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); } // 修改解析部分 var response = JObject.Parse(result); bool overallSuccess = response["success"]?.Value() ?? false; if (overallSuccess) { res = true; // 单个电池的结果处理 for (int i = 0; i < listBar.Count; i++) { // 对于单个电池,直接使用整体结果 mesMessage[i] = response["message"]?.Value() ?? "进站成功"; mesResType[i] = MesResType.D; } 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 { // 如果操作失败 string errorMessage = response["message"]?.Value() ?? "进站失败"; List rows = null; if (errorMessage.Contains("详情看rows")) { rows = response["rows"].ToObject>(); } for (int i = 0; i < listBar.Count; i++) { var detail = GetDetail(rows, listBar[i]); mesMessage[i] = detail ?? errorMessage; } 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); } } catch (Exception ex) { // 捕获异常并记录日志 resJson = $"产品进站信息(组装)接口发生异常:{ex.Message}"; LogHelper.Instance.WriteEX("产品进站信息(组装)接口", ex); } // 返回结果 return (res, mesMessage, mesResType); } private string GetDetail(List rows, string barcode) { var item = rows.FirstOrDefault(r => r.identification == barcode); return item?.moveInErrorInfo??""; } #endregion #region 7、单个电池出站 √ /// /// 单个电池出站(异步方法) /// /// API地址 /// 站点代码 /// 线体代码 /// 设备编号 /// 物料代码 /// 用户名 /// 电池实体 /// 类型 /// 物料批次代码定位器 /// 返回结果(是否成功、消息、响应类型) public async Task<(bool Success, string Mesage, MesResType MesResType)> SingleCellOutStationAsync( string url, string siteCode, string lineCode, string equipNum, string modelName, string materialCode, string userName, BGearEntity m, int type, string materialLotCodeLocator) { bool res = false; // 操作结果 string resJson = ""; // 日志信息 string mesage = ""; // 消息 MesResType mesResType = MesResType.D; // 默认响应类型 try { // 创建请求数据 JArray jaIdentiftyList = new JArray(); // identiftyList JObject joIdentifty = new JObject(); joIdentifty.Add("identification", m.BarCode); // 卷心码 joIdentifty.Add("qualityStatus", m.Result); // 质量状态 joIdentifty.Add("qrCode", ""); // 二维码 joIdentifty.Add("ngMessage", ""); // NG信息 joIdentifty.Add("ngCode", new JArray { "" }); // NG代码 // 创建 materialLotList JArray jaMaterialLotList = new JArray(); JObject joMaterialLot = new JObject(); joMaterialLot.Add("materialLotCode", ""); // 物料批次码 joMaterialLot.Add("materialLotCodeLocator", ""); // 物料批次码定位 jaMaterialLotList.Add(joMaterialLot); joIdentifty.Add("materialLotList", jaMaterialLotList); // 将 materialLotList 添加到 identiftyList jaIdentiftyList.Add(joIdentifty); // 将 identiftyList 添加到主列表 JArray containerCodeAndIdentificationList = new JArray(); JObject joContainerCodeAndIdentification = new JObject(); joContainerCodeAndIdentification.Add("containerCode", ""); joContainerCodeAndIdentification.Add("identiftyList", jaIdentiftyList); containerCodeAndIdentificationList.Add(joContainerCodeAndIdentification); // 创建主 JSON 对象 JObject joRequest = new JObject(); joRequest.Add("siteCode", siteCode); // 站点代码 joRequest.Add("lineCode", lineCode); // 线代码 joRequest.Add("equipNum", equipNum); // 设备编号 joRequest.Add("userName", userName); // 用户名 joRequest.Add("materialCode", materialCode); // 物料代码 joRequest.Add("productType", modelName); // 产品类型 joRequest.Add("completeQty", "1"); // 完成数量 joRequest.Add("type", "1"); // 类型 //joRequest.Add("identiftyList", jaIdentiftyList); // 添加 identiftyList joRequest.Add("containerCodeAndIdentificationList", containerCodeAndIdentificationList); // 序列化请求数据 var nowDate = DateTime.Now; var postcontent = JsonConvert.SerializeObject(joRequest); // 记录请求开始时间 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,卷芯码:{m.BarCode}", "MESTime"); } // 如果返回结果为空,记录日志并返回 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, mesage, mesResType); } // 解析返回结果 var res1 = JsonConvert.DeserializeObject(result); // 如果操作成功 if (res1.success) { res = true; GetOutMesResType(res1, m.BarCode, ref mesage, 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); } else { // 如果操作失败,记录日志 var resData = res1 == null ? "" : result; mesage = res1.rows[0].message; mesResType = MesResType.B; 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); } } catch (Exception ex) { // 捕获异常并记录日志 mesage = "产品出站信息(组装)接口发生异常"; mesResType = MesResType.B; resJson = $"产品出站信息(组装)接口发生异常:{ex.Message}"; LogHelper.Instance.WriteEX("产品出站信息(组装)接口", ex); } // 返回结果 return (res, mesage, mesResType); } #endregion #region 7.5、多个电池出站 √ /// /// 多个电池出站(异步方法) /// /// API地址 /// 站点代码 /// 线体代码 /// 设备编号 /// 物料代码 /// 用户名 /// 电池实体 /// 类型 /// 物料批次代码定位器 /// 返回结果(是否成功、消息、响应类型) public async Task<(bool Success, List MesMessage, List MesResType)> ManyCellOutStationAsync( string url, string siteCode, string lineCode, string equipNum, string modelName, string materialCode, string userName, List m, int type, string materialLotCodeLocator) { bool res = false; // 操作结果 string resJson = ""; // 日志信息 //string mesage = ""; // 消息 //MesResType mesResType = MesResType.D; // 默认响应类型 List mesage = GetListString(m.Count, "出站失败"); // 消息列表 List mesResType = GetMesType(m.Count); // 响应类型列表 try { // 直接构建电池信息列表(对应assembleLineList) JArray assembleLineList = new JArray(); for (int i = 0; i < m.Count; i++) { JObject batteryInfo = new JObject(); batteryInfo.Add("identification", m[i].BarCode); //batteryInfo.Add("qualityStatus", "OK"); batteryInfo.Add("qualityStatus", m[i].Result); batteryInfo.Add("qrCode", ""); batteryInfo.Add("ngMessage", ""); batteryInfo.Add("ngCode", new JArray()); // 目标格式中是空数组 assembleLineList.Add(batteryInfo); } // 构建主请求对象(按目标格式) JObject joRequest = new JObject(); joRequest.Add("siteCode", siteCode); joRequest.Add("lineCode", lineCode); joRequest.Add("equipNum", equipNum); joRequest.Add("userName", userName); joRequest.Add("containerCode", ""); // 空字符串 joRequest.Add("productType", modelName); // 对应目标中的productType joRequest.Add("assembleLineList", assembleLineList); // 关键字段名变更 // 序列化请求数据 var nowDate = DateTime.Now; var postcontent = JsonConvert.SerializeObject(joRequest); // 记录请求开始时间 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,卷芯码:{m.BarCode}", "MESTime"); List listCode = GetListString(m.Count, ""); for (int i = 0; i < m.Count; i++) { listCode.Add(m[i].BarCode); } LogHelper.Instance.WriteLog($"卷芯出站PC<=>MES上传耗时:{outTime}ms,卷芯码:{string.Join(",", listCode)}", "MESTime"); } // 如果返回结果为空,记录日志并返回 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, mesage, mesResType); } //// 解析返回结果 //var res1 = JsonConvert.DeserializeObject(result); //// 如果操作成功 //if (res1.success) //{ // res = true; // GetManyOutMesResType(res1, m, ref mesage, 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); //} //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); //} // 解析返回结果 - 修改部分开始 var responseObj = JObject.Parse(result); bool overallSuccess = responseObj["success"]?.Value() ?? false; if (responseObj["message"] != null) { // 处理单个电池的结果(当只上传一个电池时) string successMessage = responseObj["message"]?.Value() ?? "出站失败"; for (int i = 0; i < m.Count; i++) { mesage[i] = successMessage; mesResType[i] = MesResType.B; } 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 { res = true; string errorMessage = responseObj["message"]?.Value() ?? "出站成功"; // 设置所有电池的错误消息 for (int i = 0; i < m.Count; i++) { mesage[i] = errorMessage; mesResType[i] = MesResType.D; } // 如果操作失败,记录日志 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); } } catch (Exception ex) { // 捕获异常并记录日志 resJson = $"产品出站信息(组装)接口发生异常:{ex.Message}"; LogHelper.Instance.WriteEX("产品出站信息(组装)接口", ex); } // 返回结果 return (res, mesage, mesResType); } #endregion #region 8、、单电池出站(异步方法)(出站)√ /// /// /// /// /// 工厂代码 /// 产线编号 /// 设备编号 /// 物料编码 /// 员工账号 /// BGearEntity /// 类型 /// 原材料上料位置 /// public async Task<(bool Success, string Mesage, MesResType MesResType)> SingleCellInAndOutStationAsync( string url, string siteCode, string lineCode, string equipNum, string materialCode, string userName, BGearEntity m, int type, string materialLotCodeLocator) { bool res = false; // 操作结果 string resJson = ""; // 日志信息 string mesage = ""; // 消息 MesResType mesResType = MesResType.D; // 默认响应类型 try { // 创建请求数据 JArray jaContainerCode = new JArray(); // containerCodeAndIdentificationList JArray jaMaterialLotCodeList = new JArray(); // materialLotCodeList JObject jo4 = new JObject(); jo4.Add("materialLotCode", ""); jo4.Add("materialLotCodeLocator", materialLotCodeLocator); jaMaterialLotCodeList.Add(jo4); JArray jaIdentiftyList = new JArray(); // identiftyList JObject jo3 = new JObject(); jo3.Add("identification", m.BarCode); // 卷心码 jo3.Add("qualityStatus", m.Result); jo3.Add("qrCode", ""); jo3.Add("ngMessage", m.Remark); jo3.Add("ngCode", "");//补 jo3.Add("materialLotList", jaMaterialLotCodeList); jaIdentiftyList.Add(jo3); JObject jo2 = new JObject(); jo2.Add("containerCode", ""); // 托杯码 jo2.Add("identiftyList", jaIdentiftyList); jaContainerCode.Add(jo2); JObject jo1 = new JObject(); jo1.Add("siteCode", siteCode); jo1.Add("lineCode", lineCode); jo1.Add("equipNum", equipNum); jo1.Add("materialCode", materialCode); jo1.Add("userName", userName); jo1.Add("productType", ""); jo1.Add("completeQty", ""); jo1.Add("type", 1); // 追溯标识替换 jo1.Add("containerCodeAndIdentificationList", jaContainerCode); // 序列化请求数据 var nowDate = DateTime.Now; var postcontent = JsonConvert.SerializeObject(jo1); // 记录请求开始时间 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,钢壳码:{m.BarCode}", "MESTime"); } // 如果返回结果为空,记录日志并返回 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, mesage, mesResType); } // 解析返回结果 var res1 = JsonConvert.DeserializeObject(result); // 如果操作成功 if (res1.success) { res = true; GetOutMesResType(res1, m.BarCode, ref mesage, 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); } 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); } } catch (Exception ex) { // 捕获异常并记录日志 mesage = "产品进出站一体信息(组装)接口发生异常"; mesResType = MesResType.B; resJson = $"产品进出站一体信息(组装)接口发生异常:{ex.Message}"; LogHelper.Instance.WriteEX("产品进出站一体信息(组装)接口", ex); } // 返回结果 return (res, mesage, mesResType); } #endregion #region 多个电芯上传 × /// /// 多个电芯上传 /// /// /// public async Task<(bool Success, List MesMessage, List MesResType)> OutboundInformationsAsync(string url, string siteCode, string lineCode, string equipNum, string materialCode, string userName, List listBar) { bool res = false; // 操作结果 string resJson = ""; // 日志信息 List mesMessage = GetListString(listBar.Count, "出站失败"); // 消息列表 List mesResType = GetMesType(listBar.Count); // 响应类型列表 try { // 创建请求模型 var model = new ManyCellOutStation { siteCode = siteCode, lineCode = lineCode, equipNum = equipNum, materialCode = materialCode, userName = userName, productType = "OK", completeQty = listBar.Count.ToString(), containerCode = "", type = 1 }; // 填充电芯条码信息 model.identiftyList = new List(); for (int i = 0; i < listBar.Count; i++) { model.identiftyList.Add(new identiftyList { identification = listBar[i].BarCode, materialLotList = new List(), materialLotCode = "", qualityStatus = listBar[i].Result.ToString(), qrCode = "", ngCode = new List(), ngMessage = "" }); } // 序列化请求数据 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,电芯码:{listBar[0].BarCode},{listBar[1].BarCode}", "MESTime"); } // 如果返回结果为空,记录日志并返回 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); } // 解析返回结果 var res1 = JsonConvert.DeserializeObject(result); // 如果操作成功 if (res1 != null && res1.success) { res = true; GetOutMesResType(res1, listBar, ref mesMessage, 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); } 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); } } catch (Exception ex) { // 捕获异常并记录日志 resJson = $"产品出站信息(组装)接口发生异常:{ex.Message}"; LogHelper.Instance.WriteEX("产品出站信息(组装)接口", ex); } // 返回结果 return (res, mesMessage, mesResType); } #endregion #region 10、 过程数据上传 /// /// 过程数据上传 /// /// /// /// /// /// /// /// /// /// /// /// public bool ProductProcessParam(string url, string siteCode, string userName, string equipNum, string lineCode, string materialCode, MesUploadType unloadType, List productProcessParam, ref string resJson) { throw new NotImplementedException(); } #endregion #region 11、结果参数上传(产品结果数据)√ /// /// 结果参数上传 /// /// /// /// /// /// /// /// /// /// /// public async Task<(bool Success, string ResJson)> ProducResultParamAsync(string url, string siteCode, string lineCode, string equipNum, string materialCode, string userName, BGearEntity m, MesUploadType unloadType) { bool res = false; string resJson = ""; try { var nowDate = DateTime.Now; 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.DD: var payload1 = new Payload1 { siteCode = siteCode, lineCode = lineCode, userName = userName, materialCode = materialCode, carCode = "", collection = "JS", recordDate = sNowDate, qty = 1, containerCode = "", }; payload1.identification = new IdentificationListItem { identification = m.BarCode, qualityStatus = "Y" }; payload1.tagDataVOList = GetTagDataVOList(m, equipNum); model.payload = JsonConvert.SerializeObject(payload1); break; } var postcontent = JsonConvert.SerializeObject(model); postcontent = postcontent.TrimEnd(','); 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 = $"{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, resJson); } var res1 = ReturnMesMsg(result); if (!res1.success) { var resData = res1 == null ? "" : JsonConvert.SerializeObject(res1); 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; 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}"; LogHelper.Instance.WriteEX("产品结果加工参数接口", ex); } return (res, resJson); } private List GetTagDataVOList(BGearEntity m, string equipNum) { //var AGearList = List datavolist = new List(); string strFileName = System.Environment.CurrentDirectory + "\\采集项目表.csv"; FileStream fileStream = new FileStream(strFileName, FileMode.Open, FileAccess.Read, FileShare.Read); StreamReader streamReader = new StreamReader(fileStream,Encoding.Default); _ = streamReader.ReadLine(); while (!streamReader.EndOfStream) { string line = streamReader.ReadLine(); string[] keys = line.Split(new string[] {","},StringSplitOptions.RemoveEmptyEntries); if (keys.Length == 3) { var temp = m.GetType().GetProperty(keys[1]).GetValue(m, null); datavolist.Add(new TagDataVOListItem() { tagCode = keys[0], tagValue = temp == null ? "0" : temp.ToString(),//m tagTime = DateTime.Now.ToString(), tagCalculateResult = "Y", tagRemark = keys[2] }); } } #region 本地字段没有,手动赋值 // 设备名称 datavolist.Add(new TagDataVOListItem() { tagCode = "MS_F040_RES_007", tagValue = equipNum,//m tagTime = DateTime.Now.ToString(), tagCalculateResult = "Y", tagRemark = "质谱仪(机台)" }); datavolist.Add(new TagDataVOListItem() { tagCode = "MS_F040_RES_004", tagValue = m.Result.Split(',')[0],//m tagTime = DateTime.Now.ToString(), tagCalculateResult = "Y", tagRemark = "质谱结果" }); // 操作员 //datavolist.Add(new TagDataVOListItem() //{ // tagCode = "MS_F080_RES_005", // tagValue = "",//m // tagTime = DateTime.Now.ToString(), // tagCalculateResult = "Y", // tagRemark = "操作员" //}); #endregion return datavolist; } /// /// 多个电池上传 /// /// /// /// /// /// /// /// /// /// public async Task<(bool Success, string ResJson)> ProducResultParamAsync(string url, string siteCode, string lineCode, string equipNum, string materialCode, string userName, List m, MesUploadType unloadType) { bool res = false; string resJson = ""; try { var nowDate = DateTime.Now; 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.DD: var payload1 = new Payload1 { siteCode = siteCode, lineCode = lineCode, userName = userName, materialCode = materialCode, carCode = "", collection = "JS", recordDate = sNowDate, qty = m.Count, // 数量改为传入的 BGearEntity 列表的数量 containerCode = "", }; payload1.identification = new IdentificationListItem { identification = m[0].BarCode, // 使用第一个 BGearEntity 的条码 qualityStatus = "Y" }; // 初始化 tagDataVOList payload1.tagDataVOList = new List(); // 遍历多个 BGearEntity,添加到 tagDataVOList foreach (var entity in m) { var tagData = new TagDataVOListItem { // 根据 BGearEntity 的属性初始化 TagDataVOListItem tagCode = "CSBYH_CODE_A", tagValue = entity.BarCode[0].ToString(), tagTime = entity.TestTime, tagCalculateResult = "Y", tagRemark = "A芯包条码" }; payload1.tagDataVOList.Add(tagData); // 可以根据需要添加更多 TagDataVOListItem } model.payload = JsonConvert.SerializeObject(payload1); break; } var postcontent = JsonConvert.SerializeObject(model); 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 = $"{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, resJson); } var res1 = ReturnMesMsg(result); if (!res1.success) { var resData = res1 == null ? "" : JsonConvert.SerializeObject(res1); 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; 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}"; LogHelper.Instance.WriteEX("产品结果加工参数接口", ex); } return (res, resJson); } #endregion #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 #region 16、数据处理逻辑 /// /// 单电池进站分类 /// /// /// /// /// private void GetInMesResType(MesResponseSingle res1, string BarCode, ref string MesMessage, ref MesResType mesResType) { if (res1 != null) { if (res1.success) { mesResType = MesResType.D; MesMessage = "MES进站成功 "; } else { if (res1.category == null) { mesResType = MesResType.B; MesMessage = res1.message; } else { switch (res1.category) { case "A": mesResType = MesResType.A; MesMessage = res1.message; break; case "B": mesResType = MesResType.B; MesMessage = res1.message; break; case "C": mesResType = MesResType.C; MesMessage = res1.message; break; default: mesResType = MesResType.UnKnow; MesMessage = "MES进站失败 "; break; } } } } } /// /// 多电池进站分类 /// /// /// /// /// private void GetInMesResType(MesResponseIn res1, List listBar, ref List MesMessage, ref List mesResType) { if (res1 != null) { for (int i = 0; i < res1.total; i++) { for (int j = 0; j < res1.total; j++) { if (res1.rows[j].identification == listBar[i]) { if (res1.rows[j].success) { mesResType[i] = MesResType.D; MesMessage[i] = "MES进站成功 "; } else { if (res1.rows[j].category == null) { mesResType[i] = MesResType.B; MesMessage[i] = res1.rows[j].message; } else { switch (res1.rows[j].category) { case "A": mesResType[i] = MesResType.A; MesMessage[i] = res1.rows[j].message; break; case "B": mesResType[i] = MesResType.B; MesMessage[i] = res1.rows[j].message; break; case "C": mesResType[i] = MesResType.C; MesMessage[i] = res1.rows[j].message; break; default: mesResType[i] = MesResType.UnKnow; MesMessage[i] = "MES进站失败 "; break; } } } } } } } } /// /// 单电池出站分类 /// /// /// /// /// private void GetOutMesResType(MesResponseOut res1, string BarCode, ref string MesMessage, ref MesResType mesResType) { if (res1 != null && res1.rows != null) { if (res1.rows[0].success) { mesResType = MesResType.D; MesMessage = "MES出站成功 "; } else { if (res1.rows[0].category == null) { mesResType = MesResType.B; MesMessage = res1.rows[0].message; } else { switch (res1.rows[0].category) { case "A": mesResType = MesResType.A; MesMessage = res1.rows[0].message; break; case "B": mesResType = MesResType.B; MesMessage = res1.rows[0].message; break; case "C": mesResType = MesResType.C; MesMessage = res1.rows[0].message; break; default: mesResType = MesResType.UnKnow; MesMessage = "MES出站失败 "; break; } } } } } /// /// 多电池出站分类 /// /// /// /// /// private void GetManyOutMesResType(MesResponseOut res1, List m, ref List MesMessage, ref List mesResType) { if (res1 != null) { for (int i = 0; i < res1.total; i++) { for (int j = 0; j < res1.total; j++) { if (res1.rows[j].identification == m[i].BarCode) { if (res1.rows[j].success) { mesResType[i] = MesResType.D; MesMessage[i] = "MES出站成功 "; } else { if (res1.rows[j].category == null) { mesResType[i] = MesResType.B; MesMessage[i] = res1.rows[j].message; } else { switch (res1.rows[j].category) { case "A": mesResType[i] = MesResType.A; MesMessage[i] = res1.rows[j].message; break; case "B": mesResType[i] = MesResType.B; MesMessage[i] = res1.rows[j].message; break; case "C": mesResType[i] = MesResType.C; MesMessage[i] = res1.rows[j].message; break; default: mesResType[i] = MesResType.UnKnow; MesMessage[i] = "MES出站失败 "; break; } } } } } } } } private void GetOutMesResType(MesResponse res1, string BarCode, ref string MesMessage, ref MesResType mesResType) { if (res1 != null) { if (res1.success) { mesResType = MesResType.D; MesMessage = "MES出站成功 "; } else { mesResType = MesResType.B; MesMessage = res1.message; } } } /// /// 多电池出站结果分类 /// /// /// /// /// private void GetOutMesResType(MesResponseOut res1, List list, ref List MesMessage, ref List mesResType) { if (res1 != null) { for (int i = 0; i < list.Count; i++) { for (int j = 0; j < res1.total; j++) { if (res1.rows[j].identification == list[i].BarCode) { if (res1.rows[j].success) { mesResType[i] = MesResType.D; MesMessage[i] = "MES出站成功 "; } else { if (res1.rows[j].category == null) { mesResType[i] = MesResType.B; MesMessage[i] = res1.rows[j].message; } else { switch (res1.rows[i].category) { case "A": mesResType[i] = MesResType.A; MesMessage[i] = res1.rows[j].message; break; case "B": mesResType[i] = MesResType.B; MesMessage[i] = res1.rows[j].message; break; case "C": mesResType[i] = MesResType.C; MesMessage[i] = res1.rows[j].message; break; default: mesResType[i] = MesResType.UnKnow; MesMessage[i] = "MES出站失败 "; break; } } } } } } } } /// /// 序列化各类返回 /// /// /// /// 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(); } public Task<(bool Success, string ResJson)> ProductProcessParamAsync(string url, string siteCode, string userName, string equipNum, string lineCode, string materialCode, MesUploadType unloadType, List productProcessParam) { throw new NotImplementedException(); } #endregion } }