kingno
2025-04-03 a97ac998301461e6284595b1cf2c7b40ce5b2459
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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<string> AllDefects { get; set; } = new List<string>();
 
        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<ISpec>()));
 
            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<string>()));
            allDefects = allDefects.Distinct().ToList();
            var assignedDefects = new List<string>();
 
            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<snData>), typeof(UITypeEditor))]
        public List<snData> snData { get; set; } = new List<snData>();
    }
 
    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<measurement>), typeof(UITypeEditor))]
        public List<measurement> measurements { get; set; } = new List<measurement>();
 
        //[TypeConverter(typeof(CollectionCountConvert))]
        //[Editor(typeof(ComplexCollectionEditor<apperErr>), typeof(UITypeEditor))]
        //public List<apperErr> apperErr { get; set; } = new List<apperErr>();
 
        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;
    //    }
    //}
}