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; using System.Threading.Tasks; using System.Windows.Forms; 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 (dataUploadMethod == null) { dataUploadMethod = InitialMESWebServiceMethod(DataUploadMethodName, out DataUploadObj); } return dataUploadMethod; } set => dataUploadMethod = 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, Config.ProjectCode }; if (Config.IsEnableMESLog) { LogAsync(DateTime.Now, $"MES入料检测", JsonConvert.SerializeObject(paras)); } string result = "-3"; int repeatTime = 3; do { try { result = IncomingCheckMethod?.Invoke(IncomingCheckObj, paras).ToString(); repeatTime = -1; } catch (Exception ex) { LogAsync(DateTime.Now, $"{barcode}产品条码检测异常", ex.GetExceptionMessage()); repeatTime--; Thread.Sleep(100); result = "-3"; } } while (repeatTime > 0); 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, Config.StationCode, pMeasure.PResult == "OK" ? "PASS" : "FAIL", Config.LineCode, GetMESSlantData(pMeasure), GetMESAlignmentData(pMeasure), GetNGKeys(pMeasure) }; if (Config.IsEnableMESLog) { LogAsync(DateTime.Now, $"{pMeasure.Barcode}产品检测数据上传", JsonConvert.SerializeObject(paras)); } string result = "-1"; int repeatTime = 3; do { try { result = DataUploadMethod?.Invoke(DataUploadObj, paras).ToString(); repeatTime = -1; } catch (Exception ex) { LogAsync(DateTime.Now, $"{barcode}产品检测数据上传异常", ex.GetExceptionMessage()); repeatTime--; Thread.Sleep(100); result = "-1"; } } while (repeatTime > 0); var mesReturn = (MESUploadReturn)int.Parse(result); sw.Stop(); LogAsync(DateTime.Now, $"{barcode}产品检测数据上传,{result}|{mesReturn.ToString()}", $"耗时:{sw.ElapsedMilliseconds}ms"); OnCheckHintUpload?.Invoke("检测数据" + mesReturn.ToString(), mesReturn != MESUploadReturn.上传成功); if (mesReturn != MESUploadReturn.上传成功) { MessageBox.Show($"{barcode}产品数据上传失败,{mesReturn.ToString()}\r\n请检查网络连接或确认问题后重新检测", "上传异常", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } private string GetMESAlignmentData(ProductionMeasurement pMeasure) { return string.Join(",", pMeasure.Measurements.Where(u => u.MeasureType.StartsWith("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.StartsWith("Slant")).Select(u => $"{u.Name}:{u.Spec.ActualValue.Value.ToString(_precision)}")); } private string GetNGKeys(ProductionMeasurement pMeasure) { string ngCodes = ""; Dictionary> ngCodeDict = new Dictionary>(); var ngList = pMeasure.Measurements.Where(m => m.Spec.MeasureResult != true).ToList(); if (ngList.Count == 0) return ""; ngList.GroupBy(m => m.MeasureType).ToList().ForEach(g => { var mType = Config.MeasureTypeCollection.FirstOrDefault(u => u.Code == g.Key); if (mType != null) { if (!ngCodeDict.ContainsKey(mType.NGCode)) { ngCodeDict[mType.NGCode] = new List(); } g.ToList().ForEach(k => { k.KeyUnitCollection.Select(kk => kk.Key).ToList().ForEach(key => { string uploadKeyCode = key; var keyCodeMap = Config.KeyCodeMappingCollection.FirstOrDefault(kcm => kcm.Key == key); if (keyCodeMap != null) { uploadKeyCode = keyCodeMap.KeyCode; } if (!ngCodeDict[mType.NGCode].Contains(uploadKeyCode)) { ngCodeDict[mType.NGCode].Add(uploadKeyCode); } }); }); } }); if (ngCodeDict.Count > 0) { ngCodes = string.Join(";", ngCodeDict.ToList().Select(u => $"{u.Key}:{string.Join(",", u.Value)}")); } return ngCodes; } } public enum MESIncomingCheckReturn { 上传结果未知 = -3, 未查询到工序信息 = -2, 未查询到SN信息 = -1, 检测FAIL = 0, 检测PASS = 1, } public enum MESUploadReturn { 上传结果未知 = -1, 上传失败 = 0, 上传成功 = 1, 工序检测异常 = 2, 检测次数异常 = 3, } }