Files
1440WGJ/JY.Inspection/Mes/MESDataCombin.cs
T

385 lines
15 KiB
C#
Raw Normal View History

2026-07-14 13:55:17 +08:00
using JY.MES;
2026-07-14 16:09:25 +08:00
using JY.MES.Entity;
using JY.MES.Enum;
using JY.MES.Exceptions;
2026-07-14 13:55:17 +08:00
using JY.MES.MES;
using JY.Model;
using JY.Utility;
using Newtonsoft.Json;
2026-07-16 20:25:59 +08:00
using Newtonsoft.Json.Serialization;
using NPOI.OpenXmlFormats.Spreadsheet;
2026-07-14 13:55:17 +08:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
2026-07-14 16:09:25 +08:00
using System.Net;
2026-07-14 13:55:17 +08:00
namespace JY.Inspection.Mes
{
public interface IMes
{
/// <summary>
/// 分档查询
/// </summary>
/// <param name="code"></param>
/// <param name="code2"></param>
/// <returns></returns>
MesResponse GetMesIn(string code, string code2);
/// <summary>
/// 结果加工参数
/// </summary>
/// <param name="m"></param>
/// <returns></returns>
2026-07-14 16:09:25 +08:00
MesCallResult<RespProductResult> ProductResultParameters(BlankingData m);
2026-07-14 13:55:17 +08:00
/// <summary>
/// 产品进站(包装)
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
RespArrivalStation PostProductArrivalStationData(ReqArrivalStation req);
/// <summary>
/// 产品出站(包装)
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
RespExitStation PostProductExitStationData(ReqExitStation req);
2026-07-15 15:21:17 +08:00
MesCallResult<RespLoginMes> LoginMes(ReqLoginMes req);
2026-07-16 13:08:39 +08:00
// TODO 上传设备状态信息
// TODO 上传设备报警信息
// TODO 能耗
// TODO 设备打标
2026-07-14 13:55:17 +08:00
}
public class MESDataCombin : IMes
{
#region 1、 分档查询
public MesResponse GetMesIn(string code, string code2)
{
MesResponse mesResponse = new MesResponse();
DateTime nowTime = DateTime.Now;
string josnTxtMsg = string.Empty;
string BarCode = "";
try
{
if (code.Contains("ERROR"))
{
BarCode = code2;
}
else
{
BarCode = code;
}
GradingParam gradingParam = new GradingParam
{
siteCode = Global.systemConfig.siteCode,
lineCode = Global.systemConfig.lineCode,
userName = Global.systemConfig.userName,
2026-07-14 13:55:17 +08:00
equipCode = Global.systemConfig.equipCode,
recordDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
qty = 1,
containerCode = "",
materialCode = Global.systemConfig.materialCode,
materiallotCodeList = new List<string>() { BarCode }
};
2026-07-16 20:25:59 +08:00
string p = JsonHelper.Serialize(gradingParam);
2026-07-14 13:55:17 +08:00
var sw = new Stopwatch();
sw.Start();
string mesRes = MESApiHelper.HttpPostJsonAPI(Global.systemConfig.GradingMesUrl, p, Global.systemConfig.MesRequestTime);
sw.Stop();
var res = JsonConvert.DeserializeObject<MesResponse>(mesRes);
josnTxtMsg = $"{nowTime.ToString("yyyy-MM-dd HH:mm:ss.fff")}:调用电芯分档查询接口:{Global.systemConfig.GradingMesUrl},请求数据为:{p},\n{nowTime.AddMilliseconds(sw.ElapsedMilliseconds).ToString("yyyy-MM-dd HH:mm:ss.fff")}:接口耗时:{sw.ElapsedMilliseconds}ms,返回结果:{mesRes}";
TxtHelper.WriteTxt($@"D:\APILog\MesLogs\{nowTime.ToString("yyyyMMdd")}\调用电芯分档查询接口\{nowTime.ToString("HH")}.txt", josnTxtMsg);//前面是路径,后面是数据
mesResponse.success = res.success;
mesResponse.message = res.message;
mesResponse.error = res.error;
if (!res.success)
{
mesResponse.message = res.message;
mesResponse.error = res.error;
}
else
{
try
{
if (res.rows != null && res.rows.Count > 0)
{
List<MesResponse.rowsListItem> rowsls = new List<MesResponse.rowsListItem>();
MesResponse.rowsListItem rows = new MesResponse.rowsListItem();
rows.identification = res.rows[0].identification;
rows.level = res.rows[0].level;
rows.passage = res.rows[0].passage;
rows.rank = res.rows[0].rank;
rows.message = res.rows[0].message;
rowsls.Add(rows);
mesResponse.rows = rowsls;
}
}
catch (Exception ex)
{
}
}
}
catch (Exception ex)
{
mesResponse.success = false;
mesResponse.error = 9;
mesResponse.message = "电芯分档查询异常:" + ex.Message;
}
return mesResponse;
}
#endregion
#region 2、结果加工参数
2026-07-14 16:09:25 +08:00
public MesCallResult<RespProductResult> ProductResultParameters(BlankingData m)
2026-07-14 13:55:17 +08:00
{
2026-07-14 16:09:25 +08:00
MesCallResult<RespProductResult> resp = new MesCallResult<RespProductResult>();
2026-07-14 13:55:17 +08:00
DateTime nowTime = DateTime.Now;
string josnTxtMsg = string.Empty;
try
{
ProductResultParameters prp = new ProductResultParameters();
prp.equipNum = Global.systemConfig.equipCode;
prp.type = "DD";
Payload pl = new Payload();
pl.siteCode = Global.systemConfig.siteCode;
pl.lineCode = Global.systemConfig.lineCode;
pl.userName = Global.systemConfig.userName;
2026-07-14 13:55:17 +08:00
pl.materialCode = Global.systemConfig.materialCode;
pl.carCode = "";
pl.collection = "JS";
pl.recordDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
pl.qty = 1;
pl.containerCode = "";
IdentificationItem iil = new IdentificationItem();
if (m.DepartureBarCode.Contains("ERROR"))
iil.identification = m.ArrivalBarCode;
else
iil.identification = m.DepartureBarCode;
iil.qualityStatus = m.Result == "OK" ? "Y" : "N";
pl.identification = iil;
List<TagDataVOListItem> tdvls = new List<TagDataVOListItem>();
TagDataVOListItem tag1;
// 结果采集项
var otherCfgList = Global.systemConfig.CollectItemCfgList
.Where(it => it.IsEnable && !string.IsNullOrEmpty(it.HCPropName)).ToList();
foreach (var item in otherCfgList)
2026-07-14 13:55:17 +08:00
{
// 判断m中的属性名是否存在it.HCPropName
// 如果存在生成TagDataVoListItem
var prop = m.GetType().GetProperty(item.HCPropName);
if (prop != null)
2026-07-14 13:55:17 +08:00
{
tdvls.Add(new TagDataVOListItem() {
tagCode = item.MesItemCode,
tagValue = prop.GetValue(m).ToString(),
tagTime = m.OutTime,
tagCalculateResult = "Y",
tagRemark = item.MesItemName
});
2026-07-14 13:55:17 +08:00
}
}
//tag1 = new TagDataVOListItem() { tagCode = "WGJA001", tagValue = m.ArrivalBarCode, tagTime = m.OutTime, tagCalculateResult = "Y", tagRemark = "入站条码" }; tdvls.Add(tag1);
//tag1 = new TagDataVOListItem() { tagCode = "WGJA002", tagValue = m.DepartureBarCode, tagTime = m.OutTime, tagCalculateResult = "Y", tagRemark = "出站条码" }; tdvls.Add(tag1);
//tag1 = new TagDataVOListItem() { tagCode = "WGJA003", tagValue = m.WorkShift, tagTime = m.OutTime, tagCalculateResult = "Y", tagRemark = "班次" }; tdvls.Add(tag1);
//tag1 = new TagDataVOListItem() { tagCode = "WGJA004", tagValue = m.OutTime, tagTime = m.OutTime, tagCalculateResult = "Y", tagRemark = "出站时间" }; tdvls.Add(tag1);
//foreach (var item in m.PLCValDic)
//{
// // TODO 获取采集项配置
// var cfgItem = Global.systemConfig.CollectItemCfgList
// .Where(it => it.IsEnable && it.PLCRelAddress == item.Key)
// .FirstOrDefault();
// if (cfgItem == null)
// {
// continue;
// }
// // 生成TagDataVoListItem
// tdvls.Add(new TagDataVOListItem()
// {
// tagCode = cfgItem.MesItemCode,
// tagValue = item.Value,
// tagTime = m.OutTime,
// tagCalculateResult = "Y",
// tagRemark = cfgItem.MesItemName
// });
//}
//tdvls.Add(new TagDataVOListItem()
//{
// tagCode = "WGJA021",
// tagValue = m.Result,
// tagTime = m.OutTime,
// tagCalculateResult = "Y",
// tagRemark = "电池总结果"
//});
2026-07-14 13:55:17 +08:00
//多个
pl.tagDataVOList = tdvls;
2026-07-16 20:25:59 +08:00
prp.payload = JsonHelper.Serialize(pl);
string p = JsonHelper.Serialize(prp);
2026-07-14 13:55:17 +08:00
var sw = new Stopwatch();
sw.Start();
resp = MESApiHelper.HttpPostJsonAPI<RespProductResult>(Global.systemConfig.ResultProcessMesUrl, p, Global.systemConfig.MesRequestTime);
2026-07-14 13:55:17 +08:00
sw.Stop();
if (resp?.OriResponse != null && resp?.OriResponse.Success != true)
2026-07-14 13:55:17 +08:00
{
2026-07-14 16:09:25 +08:00
resp.IsSuccess = false;
resp.ErrorType = MesErrorType.BusinessError;
resp.ErrorMessage = resp?.OriResponse?.Message ?? "MES业务处理失败, 错误消息为空";
2026-07-14 13:55:17 +08:00
}
new MesLog().LogProductResult(nowTime, p, resp.OriRespJsonStr, sw.ElapsedMilliseconds);
2026-07-14 13:55:17 +08:00
}
catch (Exception ex)
{
2026-07-14 16:09:25 +08:00
resp.IsSuccess = false;
resp.ErrorType = MesErrorType.ClientError;
resp.ErrorMessage = "结果加工参数上传异常:" + ex.Message;
2026-07-14 13:55:17 +08:00
}
2026-07-14 16:09:25 +08:00
return resp;
2026-07-14 13:55:17 +08:00
}
#endregion
public RespArrivalStation PostProductArrivalStationData(ReqArrivalStation req)
{
if (req == null)
{
throw new ArgumentNullException("入站数据为空");
}
DateTime currentTime = DateTime.Now;
string reqUrl = Global.systemConfig.StationArrivalUrl;
RespArrivalStation resp = new RespArrivalStation();
try
{
2026-07-16 20:25:59 +08:00
string reqJsonStr = JsonHelper.Serialize(req);
2026-07-14 13:55:17 +08:00
var sw = new Stopwatch();
sw.Start();
string mesRes = MESApiHelper.HttpPostJsonAPI(
reqUrl,
reqJsonStr,
Global.systemConfig.MesRequestTime
);
sw.Stop();
resp = JsonConvert.DeserializeObject<RespArrivalStation>(mesRes);
new MesLog().LogArrivalStation(currentTime, reqJsonStr, mesRes, sw.ElapsedMilliseconds);
}
catch (Exception ex)
{
resp.Success = false;
resp.Message = "入站数据上传异常:" + ex.Message;
}
return resp;
}
public RespExitStation PostProductExitStationData(ReqExitStation req)
{
if (req == null)
{
throw new ArgumentNullException("出站请求数据为空");
}
DateTime currentTime = DateTime.Now;
string reqUrl = Global.systemConfig.StationExitUrl;
RespExitStation resp = new RespExitStation();
try
{
2026-07-16 20:25:59 +08:00
string reqJsonStr = JsonHelper.Serialize(req);
2026-07-14 13:55:17 +08:00
var sw = new Stopwatch();
sw.Start();
string mesRes = MESApiHelper.HttpPostJsonAPI(
reqUrl,
reqJsonStr,
Global.systemConfig.MesRequestTime
);
sw.Stop();
resp = JsonConvert.DeserializeObject<RespExitStation>(mesRes);
2026-07-16 20:25:59 +08:00
new MesLog().LogExitStation(currentTime, reqJsonStr, mesRes, sw.ElapsedMilliseconds);
2026-07-14 13:55:17 +08:00
}
catch (Exception ex)
{
resp.Success = false;
resp.Message = "出站数据上传异常:" + ex.Message;
}
return resp;
}
2026-07-15 15:21:17 +08:00
public MesCallResult<RespLoginMes> LoginMes(ReqLoginMes req)
{
if (req == null)
{
throw new ArgumentNullException("MES员工登录请求信息为空");
}
// TODO MES登录请求地址
DateTime currentTime = DateTime.Now;
2026-07-15 16:32:52 +08:00
string reqUrl = Global.systemConfig.LoginMesUrl;
2026-07-15 15:21:17 +08:00
MesCallResult<RespLoginMes> resp = new MesCallResult<RespLoginMes>();
try
{
2026-07-16 20:25:59 +08:00
string reqJsonStr = JsonHelper.Serialize(req);
2026-07-15 15:21:17 +08:00
var sw = new Stopwatch();
sw.Start();
string mesRes = MESApiHelper.HttpPostJsonAPI(
reqUrl,
reqJsonStr,
Global.systemConfig.MesRequestTime
);
resp = MESApiHelper.HttpPostJsonAPI<RespLoginMes>(reqUrl, reqJsonStr, Global.systemConfig.MesRequestTime);
sw.Stop();
if (resp?.OriResponse != null && resp?.OriResponse.Success != true)
{
resp.IsSuccess = false;
resp.ErrorType = MesErrorType.BusinessError;
resp.ErrorMessage = resp?.OriResponse?.Message ?? "MES业务处理失败, 错误消息为空";
}
new MesLog().LogLoginMes(currentTime, reqJsonStr, resp.OriRespJsonStr, sw.ElapsedMilliseconds);
}
catch (Exception ex)
{
resp.IsSuccess = false;
resp.ErrorType = MesErrorType.ClientError;
resp.ErrorMessage = "获取MES员工登录信息异常:" + ex.Message;
}
return resp;
}
2026-07-14 13:55:17 +08:00
}
}