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 System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Net; namespace JY.Inspection.Mes { public interface IMes { /// /// 分档查询 /// /// /// /// MesResponse GetMesIn(string code, string code2); /// /// 结果加工参数 /// /// /// MesCallResult ProductResultParameters(BlankingData m); /// /// 产品进站(包装) /// /// /// RespArrivalStation PostProductArrivalStationData(ReqArrivalStation req); /// /// 产品出站(包装) /// /// /// RespExitStation PostProductExitStationData(ReqExitStation req); MesCallResult LoginMes(ReqLoginMes 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() { 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(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 rowsls = new List(); 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 ProductResultParameters(BlankingData m) { MesCallResult resp = new MesCallResult(); 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 tdvls = new List(); 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(); resp = MESApiHelper.HttpPostJsonAPI(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 #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(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(mesRes); new MesLog().LogArrivalStation(currentTime, reqJsonStr, mesRes, sw.ElapsedMilliseconds); } catch (Exception ex) { resp.Success = false; resp.Message = "出站数据上传异常:" + ex.Message; } return resp; } public MesCallResult LoginMes(ReqLoginMes req) { if (req == null) { throw new ArgumentNullException("MES员工登录请求信息为空"); } // TODO MES登录请求地址 DateTime currentTime = DateTime.Now; string reqUrl = Global.systemConfig.LoginMesUrl; MesCallResult resp = new MesCallResult(); try { string reqJsonStr = JsonConvert.SerializeObject(req); var sw = new Stopwatch(); sw.Start(); string mesRes = MESApiHelper.HttpPostJsonAPI( reqUrl, reqJsonStr, Global.systemConfig.MesRequestTime ); resp = MESApiHelper.HttpPostJsonAPI(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; } } }