M182轴承端盖外观缺陷AOI
kingno
2025-05-26 5a405c7dce20d8c79a733c9c786cc42eb59fe81c
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
using Bro.Common.Base;
using Bro.Common.Helper;
using Bro.Common.Interface;
using Bro.Common.Model;
using Bro.M135.Common;
using Newtonsoft.Json;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Bro.M141.Process
{
    public partial class M141Process
    {
        public event Action<ProductModel> UploadProductForStatistic;
        public List<ISpec> GetSpecHeads()
        {
            return Config.GetSpecList().Where(u => u.IsEnabled).OrderBy(u => u.Code).ToList();
        }
 
        object _pListForStatisticLock = new object();
        public List<ProductModel> ProductListForStatistic { get; set; } = new List<ProductModel>();
 
        private async void HandleProductStatistc(ProductModel product)
        {
            await Task.Run(() =>
             {
                 if (product == null)
                     return;
 
                 lock (_pListForStatisticLock)
                 {
                     //foreach (var item in product.ProductResultCollection)
                     //{
                     //    item.Value.PID = product.PID;
                     //    item.Value.ProductSeq = item.Key;
                     //    item.Value.EndTime = DateTime.Now;
 
                     SummaryOKRate(product);
                     ProductListForStatistic.Insert(0, product);
 
                     UploadProductForStatistic?.Invoke(product);
                     //}
 
                     while (ProductListForStatistic.Count > M141Config.MaxProductNumForStatistic)
                     {
                         ProductListForStatistic.RemoveAt(ProductListForStatistic.Count - 1);
                     }
                 }
 
                 //UploadProductForStatistic?.Invoke(product);
             });
        }
 
        #region 良率统计
        public SummaryData SummaryData { get; set; } = new SummaryData();
 
        object _okRateLock = new object();
        public OKRateModel OKRateModel => SummaryData.OKRateModel;
        public List<ShiftSummary> ShiftSummaryDatas => SummaryData.ShiftSummaryDatas;
 
        private void SummaryOKRate(ProductModel singleResult)
        {
            var results = singleResult.Result;
 
            if (results == "NG")
            {
                List<string> ngitem = new List<string>();
                foreach (var v in singleResult.Details)
                {
                    ngitem.AddRange(v.ResultList.GetDefectDescList());
                    ngitem.AddRange(v.DefectList);
                }
                ngitem = ngitem.Distinct().ToList();
                List<string> ngitemD = ngitem.Where(u => u.ToUpper().Contains("FAI")).ToList();
                //List<string> ngitemC = ngitem.Where(u => !u.ToUpper().Contains("FAI")).ToList();
                if (ngitemD != null && ngitemD.Count > 0)
                {
                    results = "CNG";
                }
                else
                {
                    results = "WNG";
                }
 
            }
 
 
 
 
 
            lock (_okRateLock)
            {
                OKRateModel.TotalNum++;
 
                bool isSpecOK = true;
                bool isDefectOK = true;
 
                if (results.ToUpper().Equals("CNG"))
                {
                    OKRateModel.Spec_NG++;
                    isSpecOK = false;
                }
 
                if (results.ToUpper().Equals("WNG"))
                {
                    OKRateModel.Defect_NG++;
                    isDefectOK = false;
                }
 
                if (isSpecOK && isDefectOK)
                {
                    OKRateModel.OKNum++;
                }
 
 
                ShiftSummary? shift = InitialCurrentShift(singleResult.EndTime??DateTime.Now);
 
                var hourData = shift.HourDatas.FirstOrDefault(u => u.HourTime <= singleResult.EndTime && u.HourTime.AddHours(1) > singleResult.EndTime);
                if (hourData == null)
                {
                    LogAsync(DateTime.Now, Common.Helper.EnumHelper.LogLevel.Error, $"产品{singleResult.PID},结束时间{(singleResult.EndTime??DateTime.Now).ToString("MM/dd HH:mm")}未能统计到合适的时段");
                }
                else
                {
                    hourData.OKRateModel.TotalNum++;
                    if (!isSpecOK)
                    {
                        hourData.OKRateModel.Spec_NG++;
                    }
 
                    if (!isDefectOK)
                    {
                        hourData.OKRateModel.Defect_NG++;
                    }
 
                    if (isSpecOK && isDefectOK)
                    {
                        hourData.OKRateModel.OKNum++;
                    }
                }
 
                _saveSummaryDataTimer.Change(15 * 1000, -1);
            }
        }
 
        private ShiftSummary InitialCurrentShift(DateTime dtNow)
        {
            var shift = ShiftSummaryDatas.FirstOrDefault(u => u.StartTime <= dtNow && u.EndTime > dtNow);
            if (shift == null)
            {
                DateTime shiftStartTime = DateTime.Now;
                if (dtNow.Hour >= 8 && dtNow.Hour < 20)
                {
                    shiftStartTime = dtNow.Date.AddHours(8);
                }
                else if (dtNow.Hour >= 20 && dtNow.Hour < 0)
                {
                    shiftStartTime = dtNow.Date.AddHours(20);
                }
                else
                {
                    shiftStartTime = dtNow.AddDays(-1).AddHours(20);
                }
 
                shift = new ShiftSummary()
                {
                    StartTime = shiftStartTime,
                    EndTime = shiftStartTime.AddHours(12),
                };
                shift.Initial();
                ShiftSummaryDatas.Add(shift);
 
                while (ShiftSummaryDatas.Count > 2)
                {
                    ShiftSummaryDatas.RemoveAt(0);
                }
 
                while (ShiftSummaryDatas.Count < 2)
                {
                    ShiftSummary tempShift = new ShiftSummary()
                    {
                        StartTime = ShiftSummaryDatas[ShiftSummaryDatas.Count - 1].EndTime,
                        EndTime = ShiftSummaryDatas[ShiftSummaryDatas.Count - 1].EndTime.AddHours(12),
                    };
                    tempShift.Initial();
 
                    ShiftSummaryDatas.Add(tempShift);
                }
            }
 
            return shift;
        }
 
        public void ResetOKRate()
        {
            lock (_okRateLock)
            {
                OKRateModel.TotalNum = OKRateModel.OKNum = OKRateModel.Spec_NG = OKRateModel.Defect_NG = 0;
            }
        }
 
        System.Threading.Timer _saveSummaryDataTimer = null;
        public void InitialSummaryData()
        {
            if (_saveSummaryDataTimer == null)
            {
                _saveSummaryDataTimer = new System.Threading.Timer(SaveSummaryData, null, -1, -1);
            }
 
            string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SummaryData.json");
            if (File.Exists(filePath))
            {
                try
                {
                    using (StreamReader reader = new StreamReader(filePath, System.Text.Encoding.UTF8))
                    {
                        var dataStr = reader.ReadToEnd();
 
                        SummaryData = JsonConvert.DeserializeObject<SummaryData>(dataStr);
                        if (SummaryData == null)
                        {
                            SummaryData = new SummaryData();
                        }
                    }
                }
                catch (Exception ex)
                {
                    SummaryData = new SummaryData();
                    LogAsync(DateTime.Now, EnumHelper.LogLevel.Error, $"统计汇总信息读取失败,初始化统计汇总");
                }
            }
 
            InitialCurrentShift(DateTime.Now);
        }
 
 
        private void SaveSummaryData(object? state)
        {
            if (!SaveSummaryData(out string error))
            {
                LogAsync(DateTime.Now, EnumHelper.LogLevel.Error, $"统计汇总信息保存失败,{error}");
            }
        }
 
        public bool SaveSummaryData(out string error)
        {
            error = "";
 
            try
            {
                using (StreamWriter sw = new StreamWriter(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SummaryData.json"), false, System.Text.Encoding.UTF8))
                {
                    sw.Write(JsonConvert.SerializeObject(SummaryData));
                }
            }
            catch (Exception ex)
            {
                error = ex.GetExceptionMessage();
                return false;
            }
 
            return true;
        }
        #endregion
 
        //[ProcessMethod("TestHandleProductStatistic", "TestHandleProductStatistic", "处理产品统计测试方法", InvokeType.TestInvoke)]
        //public ResponseMessage TestHandleProductStatistic(IOperationConfig config, IDevice executeDevice, IDevice sourceDevice)
        //{
        //    ProductModel productModel = new ProductModel();
        //    SingleProductResultCollection resultCollection = new SingleProductResultCollection();
        //    productModel.ProductResultCollection.Add(0, resultCollection);
 
        //    DetectResult result = new DetectResult();
        //    result.ResultState = EnumHelper.ResultState.OK;
        //    resultCollection.AddPositionSnapshotResult(1, new List<DetectResult>() { result });
 
        //    if (config is M141Process.HandleProductStatisticTestOpConfig opConfig)
        //    {
        //        if (opConfig.Defects.Count > 0)
        //        {
        //            opConfig.Defects.ForEach(d =>
        //            {
        //                result.AddNetResult(new NetResult()
        //                {
        //                    DetectDetails = new List<DefectDetail>()
        //                    {
        //                        new DefectDetail()
        //                        {
        //                            ClassName=d,
        //                            FinalResult=EnumHelper.ResultState.NG,
        //                        }
        //                    }
        //                });
        //            });
        //        }
 
        //        if (opConfig.FAINGs.Count > 0)
        //        {
        //            opConfig.FAINGs.ForEach(f =>
        //            {
        //                result.Specs.Add(new Spec()
        //                {
        //                    Code = f,
        //                    ActualValue = 999,
        //                    MeasureResult = false,
        //                });
        //            });
        //        }
 
        //        result.SetResult();
        //    }
 
        //    HandleProductStatistc(productModel);
 
        //    return new ResponseMessage();
        //}
 
        //[Device("TestHandleProductStatistic", "处理产品统计测试方法配置", EnumHelper.DeviceAttributeType.OperationConfig)]
        //public class HandleProductStatisticTestOpConfig : OperationConfigBase
        //{
        //    [Category("缺陷信息")]
        //    [Description("缺陷NG信息")]
        //    [DisplayName("缺陷NG信息")]
        //    [TypeConverter(typeof(SimpleCollectionConvert<string>))]
        //    [Editor(typeof(SimpleCollectionEditor<string>), typeof(UITypeEditor))]
        //    public List<string> Defects { get; set; } = new List<string>();
 
        //    [Category("缺陷信息")]
        //    [Description("尺寸NG信息")]
        //    [DisplayName("尺寸NG信息")]
        //    [TypeConverter(typeof(SimpleCollectionConvert<string>))]
        //    [Editor(typeof(SimpleCollectionEditor<string>), typeof(UITypeEditor))]
        //    public List<string> FAINGs { get; set; } = new List<string>();
        //}
    }
}
 
public class SummaryData
{
    public OKRateModel OKRateModel { get; set; } = new OKRateModel();
    public List<ShiftSummary> ShiftSummaryDatas { get; set; } = new List<ShiftSummary>();
}
 
public class OKRateModel
{
    public int TotalNum { get; set; } = 0;
    public int OKNum { get; set; } = 0;
    public int Spec_NG { get; set; } = 0;
    public int Defect_NG { get; set; } = 0;
}
 
public class ShiftSummary
{
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }
 
    public List<HourData> HourDatas { get; set; } = new List<HourData>();
 
    public class HourData
    {
        public DateTime HourTime { get; set; }
        public OKRateModel OKRateModel { get; set; } = new OKRateModel();
    }
 
    public void Initial()
    {
        HourDatas.Clear();
 
        for (int i = 0; i < 12; i++)
        {
            HourDatas.Add(new HourData()
            {
                HourTime = StartTime.AddHours(i),
            });
        }
    }
 
 
 
 
 
}
 
//public class SingleProductResultCollection
//{
//    public string PID { get; set; } = "";
//    public int ProductSeq { get; set; } = 0;
//    public DateTime EndTime { get; set; }
//    //public List<List<DetectResult>> DetectResults { get; set; } = new List<List<DetectResult>>();
//    public Dictionary<int, List<List<DetectResult>>> DetectResults { get; set; } = new Dictionary<int, List<List<DetectResult>>>();
 
//    object _resultLock = new object();
//    public void AddPositionSnapshotResult(int positionNum, List<DetectResult> results)
//    {
//        lock (_resultLock)
//        {
//            if (!DetectResults.ContainsKey(positionNum))
//            {
//                DetectResults[positionNum] = new List<List<DetectResult>>();
//            }
 
//            DetectResults[positionNum].Add(results);
//        }
//    }
//}