385 lines
15 KiB
C#
385 lines
15 KiB
C#
using JY.MES;
|
|
using JY.MES.Entity;
|
|
using JY.MES.Enum;
|
|
using JY.MES.Exceptions;
|
|
using JY.MES.MES;
|
|
using JY.Model;
|
|
using JY.Utility;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Serialization;
|
|
using NPOI.OpenXmlFormats.Spreadsheet;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Net;
|
|
|
|
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>
|
|
MesCallResult<RespProductResult> 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);
|
|
|
|
MesCallResult<RespLoginMes> LoginMes(ReqLoginMes req);
|
|
|
|
// TODO 上传设备状态信息
|
|
|
|
// TODO 上传设备报警信息
|
|
|
|
// TODO 能耗
|
|
|
|
// TODO 设备打标
|
|
}
|
|
|
|
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,
|
|
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 = JsonHelper.Serialize(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 MesCallResult<RespProductResult> ProductResultParameters(BlankingData m)
|
|
{
|
|
MesCallResult<RespProductResult> resp = new MesCallResult<RespProductResult>();
|
|
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;
|
|
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)
|
|
{
|
|
// 判断m中的属性名是否存在it.HCPropName
|
|
// 如果存在生成TagDataVoListItem
|
|
var prop = m.GetType().GetProperty(item.HCPropName);
|
|
if (prop != null)
|
|
{
|
|
tdvls.Add(new TagDataVOListItem() {
|
|
tagCode = item.MesItemCode,
|
|
tagValue = prop.GetValue(m).ToString(),
|
|
tagTime = m.OutTime,
|
|
tagCalculateResult = "Y",
|
|
tagRemark = item.MesItemName
|
|
});
|
|
}
|
|
}
|
|
|
|
//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 = JsonHelper.Serialize(pl);
|
|
string p = JsonHelper.Serialize(prp);
|
|
|
|
var sw = new Stopwatch();
|
|
sw.Start();
|
|
resp = MESApiHelper.HttpPostJsonAPI<RespProductResult>(Global.systemConfig.ResultProcessMesUrl, p, 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().LogProductResult(nowTime, p, resp.OriRespJsonStr, sw.ElapsedMilliseconds);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
resp.IsSuccess = false;
|
|
resp.ErrorType = MesErrorType.ClientError;
|
|
resp.ErrorMessage = "结果加工参数上传异常:" + ex.Message;
|
|
}
|
|
return resp;
|
|
}
|
|
#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 = JsonHelper.Serialize(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 = JsonHelper.Serialize(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().LogExitStation(currentTime, reqJsonStr, mesRes, sw.ElapsedMilliseconds);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
resp.Success = false;
|
|
resp.Message = "出站数据上传异常:" + ex.Message;
|
|
}
|
|
|
|
return resp;
|
|
}
|
|
|
|
public MesCallResult<RespLoginMes> LoginMes(ReqLoginMes req)
|
|
{
|
|
if (req == null)
|
|
{
|
|
throw new ArgumentNullException("MES员工登录请求信息为空");
|
|
}
|
|
|
|
// MES登录请求地址
|
|
DateTime currentTime = DateTime.Now;
|
|
string reqUrl = Global.systemConfig.LoginMesUrl;
|
|
MesCallResult<RespLoginMes> resp = new MesCallResult<RespLoginMes>();
|
|
|
|
try
|
|
{
|
|
string reqJsonStr = JsonHelper.Serialize(req);
|
|
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;
|
|
}
|
|
}
|
|
}
|