using Bro.Common.Helper; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace Bro.M071.Process { public partial class M071Process { #region MES //WebApiHelper _webApiHelper = new WebApiHelper(); public Action OnCheckHintUpload { get; set; } public Action OnOperatorLogin { get; set; } #endregion private string barcode = ""; public string BarCode { get => barcode; set { barcode = value; if (!string.IsNullOrWhiteSpace(barcode)) { if (!CheckIncoming(barcode)) { barcode = ""; } } } } private string operatorCode = ""; public string OperatorCode { get => operatorCode; set { operatorCode = value; if (!string.IsNullOrWhiteSpace(operatorCode)) { OnOperatorLogin?.Invoke(operatorCode); OnCheckHintUpload?.Invoke($"操作员登录:{operatorCode}", false); } } } #region WebService Method MethodInfo incomingCheckMethod = null; MethodInfo dataUploadMethod = null; const string IncomingCheckMethodName = "CheckFormerData"; const string DataUploadMethodName = "HighLowKey_DataUpLoad"; private object IncomingCheckObj = null; public MethodInfo IncomingCheckMethod { get { if (incomingCheckMethod == null) { incomingCheckMethod = InitialMESWebServiceMethod(IncomingCheckMethodName, out IncomingCheckObj); } return incomingCheckMethod; } set => incomingCheckMethod = value; } private object DataUploadObj = null; public MethodInfo DataUploadMethod { get { if (incomingCheckMethod == null) { incomingCheckMethod = InitialMESWebServiceMethod(DataUploadMethodName, out DataUploadObj); } return incomingCheckMethod; } set => incomingCheckMethod = value; } private MethodInfo InitialMESWebServiceMethod(string methodName, out object invokeClass) { invokeClass = null; if (!Config.IsEnableMESUpload) return null; // 读取配置文件,获取配置信息 var urlParas = Config.MESURL.Split(new char[] { '/', '.' }, StringSplitOptions.RemoveEmptyEntries); string className = urlParas[urlParas.Length - 2]; string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "MESAPI"); if (Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } // 调用WebServiceHelper WebServiceHelper.CreateWebServiceDLL(Config.MESURL, className, methodName, filePath); // 读取dll内容 byte[] filedata = File.ReadAllBytes(filePath + className + "_" + methodName + ".dll"); // 加载程序集信息 Assembly asm = Assembly.Load(filedata); Type t = asm.GetType(className); // 创建实例 invokeClass = Activator.CreateInstance(t); return t.GetMethod(methodName); } #endregion private bool CheckIncoming(string barcode) { if (!Config.IsEnableMESUpload) return true; //dynamic paras = new //{ // FGcode = barcode, // station = Config.StationCode, //}; //string url = $"{Config.MESURL}CheckFormerData"; //string paraStr = JsonConvert.SerializeObject(paras); //LogAsync(DateTime.Now, $"MES入料检测 {url}", paraStr); IncomingCheckMethod = InitialMESWebServiceMethod(IncomingCheckMethodName, out IncomingCheckObj); Stopwatch sw = new Stopwatch(); sw.Start(); //var result = _webApiHelper.dooPost(url, paraStr); object[] paras = new object[] { barcode, Config.StationCode }; var result = IncomingCheckMethod?.Invoke(IncomingCheckObj, paras).ToString(); var mesReturn = (MESIncomingCheckReturn)int.Parse(result); sw.Stop(); LogAsync(DateTime.Now, $"{barcode}产品条码检测,{result}|{mesReturn.ToString()}", $"耗时:{sw.ElapsedMilliseconds}ms"); bool isOK = mesReturn == MESIncomingCheckReturn.检测PASS; OnCheckHintUpload?.Invoke("条码检测" + mesReturn.ToString(), !isOK); return isOK; } private void MESCheckDataUpload(ProductionMeasurement pMeasure) { if (!Config.IsEnableMESUpload) return; //dynamic paras = new //{ // checkTimes = Config.IsDisableMultipleCheckTimes, // Project = Config.ProjectCode, // FGcode = pMeasure.Barcode, // CheckTime = pMeasure.EndTime.Value.ToString("yyyy-MM-dd HH:mm:ss"), // ProcessCode = Config.StationCode, // Result = pMeasure.PResult == "OK" ? "PASS" : "FAIL", // Line = Config.LineCode, // SlantCheckData = GetMESSlantData(pMeasure), // AlignmentCheckData = GetMESAlignmentData(pMeasure), //}; //string url = $"{Config.MESURL}HighLowKey_DataUpload"; //string paraStr = JsonConvert.SerializeObject(paras); //LogAsync(DateTime.Now, $"MES数据上传 {url}", paraStr); DataUploadMethod = InitialMESWebServiceMethod(DataUploadMethodName, out DataUploadObj); Stopwatch sw = new Stopwatch(); sw.Start(); //var result = _webApiHelper.dooPost(url, paraStr); DateTime checkTime = pMeasure.EndTime.Value; object[] paras = new object[] { Config.IsDisableMultipleCheckTimes, Config.ProjectCode, pMeasure.Barcode, checkTime.ToString("yyyy-MM-dd HH:mm:ss"), Config.StationCode, pMeasure.PResult == "OK" ? "PASS" : "FAIL", Config.LineCode, GetMESSlantData(pMeasure), GetMESAlignmentData(pMeasure) }; //LogAsync(DateTime.Now, $"{barcode}产品检测数据上传", JsonConvert.SerializeObject(paras)); var result = DataUploadMethod?.Invoke(DataUploadObj, paras).ToString(); var mesReturn = (MESUploadReturn)int.Parse(result); sw.Stop(); LogAsync(DateTime.Now, $"{barcode}产品检测数据上传,{result}|{mesReturn.ToString()}", $"耗时:{sw.ElapsedMilliseconds}ms"); OnCheckHintUpload?.Invoke("检测数据" + mesReturn.ToString(), mesReturn != MESUploadReturn.上传成功); } private string GetMESAlignmentData(ProductionMeasurement pMeasure) { return string.Join(",", pMeasure.Measurements.Where(u => u.MeasureType == "Alignment").Select(u => $"{u.Name}:{u.Spec.ActualValue.Value.ToString(_precision)}")); } private string GetMESSlantData(ProductionMeasurement pMeasure) { return string.Join(",", pMeasure.Measurements.Where(u => u.MeasureType == "Slant").Select(u => $"{u.Name}:{u.Spec.ActualValue.Value.ToString(_precision)}")); } } public enum MESIncomingCheckReturn { 上传结果未知 = -3, 未查询到工序信息 = -2, 未查询到SN信息 = -1, 检测FAIL = 0, 检测PASS = 1, } public enum MESUploadReturn { 上传结果未知 = -1, 上传失败 = 0, 上传成功 = 1, 工序检测异常 = 2, 检测次数异常 = 3, } }