Files
1440WGJ/JY.Inspection/Mes/MESDataCombin.cs
T
2026-07-14 13:55:17 +08:00

315 lines
12 KiB
C#

using JY.MES;
using JY.MES.MES;
using JY.Model;
using JY.Utility;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
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>
MesResponse ProductResultParameters(BlankingData m);
/// <summary>
/// 产品进站(包装)
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
RespArrivalStation PostProductArrivalStationData(ReqArrivalStation req);
/// <summary>
/// 产品出站(包装)
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
RespExitStation PostProductExitStationData(ReqExitStation req);
}
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 = "admin",
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 }
};
string p = JsonConvert.SerializeObject(gradingParam);
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、结果加工参数
public MesResponse ProductResultParameters(BlankingData m)
{
MesResponse mesResponse = new MesResponse();
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 = "admin";
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;
// 结果采集项
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 = "电池总结果"
});
//多个
pl.tagDataVOList = tdvls;
prp.payload = JsonConvert.SerializeObject(pl);
string p = JsonConvert.SerializeObject(prp);
var sw = new Stopwatch();
sw.Start();
string mesRes = MESApiHelper.HttpPostJsonAPI(Global.systemConfig.ResultProcessMesUrl, p, Global.systemConfig.MesRequestTime);
sw.Stop();
var res = JsonConvert.DeserializeObject<dynamic>(mesRes);
mesResponse.success = res.success.Value;
if (!res.success.Value)
{
mesResponse.code = res.code.Value;
mesResponse.message = res.message.Value;
if (mesRes.Contains("category"))
mesResponse.category = res.category.Value;
if (res.error != null)
mesResponse.error = (int)res.error.Value;
}
new MesLog().LogProductResult(nowTime, p, mesRes, sw.ElapsedMilliseconds);
}
catch (Exception ex)
{
mesResponse.success = false;
mesResponse.error = 9;
mesResponse.message = "结果加工参数上传异常:" + ex.Message;
}
return mesResponse;
}
#endregion
#region 登录
#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
{
string reqJsonStr = JsonConvert.SerializeObject(req);
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
{
string reqJsonStr = JsonConvert.SerializeObject(req);
var sw = new Stopwatch();
sw.Start();
string mesRes = MESApiHelper.HttpPostJsonAPI(
reqUrl,
reqJsonStr,
Global.systemConfig.MesRequestTime
);
sw.Stop();
resp = JsonConvert.DeserializeObject<RespExitStation>(mesRes);
new MesLog().LogArrivalStation(currentTime, reqJsonStr, mesRes, sw.ElapsedMilliseconds);
}
catch (Exception ex)
{
resp.Success = false;
resp.Message = "出站数据上传异常:" + ex.Message;
}
return resp;
}
}
}