using Bro.Common.Helper; using Bro.Common.Interface; using Bro.Common.Model; using Newtonsoft.Json; using System.ComponentModel; using System.Diagnostics; using System.Drawing.Design; namespace Bro.M135.Common { public class MESHelper { public List AllDefects { get; set; } = new List(); WebApiHelper apiHelper = new WebApiHelper(); public bool UploadDataToMES(string mesURL, string stationCode, bool isOK, ProductModel p) { try { Stopwatch sw = new Stopwatch(); sw.Start(); string data = GetDefectDataStrForMESUpload(stationCode, isOK, p); CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Detail, $"产品{p.PID} {p.SN}检测上传信息{data}"); var replyData = apiHelper.dooPost(mesURL, data); sw.Stop(); CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"产品{p.PID} {p.SN}检测上传信息完成,反馈{replyData},耗时{sw.ElapsedMilliseconds}ms"); return true; } catch (Exception ex) { CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"工位{stationCode}上传信息异常,{ex.GetExceptionMessage()}"); return false; } } const string MES_OK = "Passed"; const string MES_NG = "Failed"; private string GetDefectDataStrForMESUpload(string stationCode, bool isOK, ProductModel p) { MESDataFrame dataFrame = new MESDataFrame(); dataFrame.workPlace = stationCode; snData data = new snData(); data.sn = ""; data.dateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); data.result = isOK ? MES_OK : MES_NG; var specList = p.Details.SelectMany(u => u.ResultList.SelectMany(u => u.Specs)).ToList(); specList.AddRange(p.Details.SelectMany(u => u.SpecList ?? new List())); specList.ForEach(s => { measurement mm = new measurement(); mm.reference = s.Code; mm.type = "1"; mm.status = s.MeasureResult == true ? MES_OK : MES_NG; mm.value = s.GetMeasureValueStr(4); mm.ucl = (s.StandardValue + s.Tolrenance_Positive).ToString("f4"); mm.lcl = (s.StandardValue - s.Tolrenance_Negative).ToString("f4"); data.measurements.Add(mm); }); var allDefects = p.Details.SelectMany(u => u.ResultList).ToList().GetDefectDescList(); allDefects.AddRange(p.Details.SelectMany(u => u.DefectList ?? new List())); allDefects = allDefects.Distinct().ToList(); var assignedDefects = new List(); AllDefects.ForEach(s => { measurement mm = new measurement(); mm.reference = s; mm.type = "2"; mm.status = allDefects.Contains(s) ? MES_NG : MES_OK; data.measurements.Add(mm); }); allDefects.ForEach(s => { if (!data.measurements.Any(u => u.reference == s)) { measurement mm = new measurement(); mm.reference = s; mm.type = "2"; mm.status = MES_NG; data.measurements.Add(mm); } }); dataFrame.snData.Add(data); return JsonConvert.SerializeObject(dataFrame); } } public class MESDataFrame { public string workPlace { get; set; } = ""; [TypeConverter(typeof(CollectionCountConvert))] [Editor(typeof(ComplexCollectionEditor), typeof(UITypeEditor))] public List snData { get; set; } = new List(); } public class snData : IComplexDisplay { public string sn { get; set; } = ""; public string dateTime { get; set; } = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); public string result { get; set; } = ""; [TypeConverter(typeof(CollectionCountConvert))] [Editor(typeof(ComplexCollectionEditor), typeof(UITypeEditor))] public List measurements { get; set; } = new List(); //[TypeConverter(typeof(CollectionCountConvert))] //[Editor(typeof(ComplexCollectionEditor), typeof(UITypeEditor))] //public List apperErr { get; set; } = new List(); public string GetDisplayText() { return $"{sn} {result}"; } } public class measurement : IComplexDisplay { public string reference { get; set; } = ""; public string type { get; set; } = "1"; public string status { get; set; } = ""; public string value { get; set; } = ""; public string ucl { get; set; } = ""; public string lcl { get; set; } = ""; public string GetDisplayText() { return $"{reference} {status} {value}"; } } //public class apperErr : IComplexDisplay //{ // public string apper { get; set; } = ""; // public string GetDisplayText() // { // return apper; // } //} }