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
using Autofac;
using Bro.Common.Helper;
using Bro.Common.Interface;
using Bro.Common.Model;
using Bro.M135.Common;
using Bro.Process;
using Bro.UI.Model.Winform;
using System.Data;
using Bro.M141.Process;
using Windows.Gaming.Input;
 
namespace Bro.M141.Process.UI
{
    //[MenuNode("ProductList", "产品列表", 2, EnumHelper.TopMenu.SystemInfo, MenuNodeType.Form)]
    public partial class FrmProductList : MenuFormBase
    {
        M141Process M141Process => Process as M141Process;
        M141Config M141Config => Process.IConfig as M141Config;
 
        bool IsSetTableHead = true;
        List<string> InitializeDataText = new List<string>();
        List<string> HeadText = new List<string>();
        List<List<string>> DataText = new List<List<string>>();
 
 
        List<ProductModel> DisplayProductList = new List<ProductModel>();
 
 
        public FrmProductList()
        {
            InitializeComponent();
            dataGridView1_ProductDisplayList.AutoGenerateColumns = false;
 
            this.Load += (s, e) =>
            {
                checkBox1_IsAutoRefresh.Checked = true;
                checkBox1_IsAutoRefresh.CheckedChanged -= AutoRefresh_ProductDisplayList;
                checkBox1_IsAutoRefresh.CheckedChanged += AutoRefresh_ProductDisplayList;
 
                timerRefresh.Enabled = true;
                timerRefresh.Interval = 1000;
                timerRefresh.Tick -= timerRefresh_Tick;
                timerRefresh.Tick += timerRefresh_Tick;
            };
        }
 
        protected override void Dispose(bool disposing)
        {
            if (M141Process != null)
            {
                M141Process.OnNewProductEnqueued -= Process_OnNewProductEnqueued;
                M141Process.OnProductDequeued -= Process_OnProductDequeued;
            }
 
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        public override void OnProcessUpdated()
        {
            base.OnProcessUpdated();
 
            //初始化表头
            InitialTableHead();
 
            M141Process.OnNewProductEnqueued -= Process_OnNewProductEnqueued;
            M141Process.OnProductDequeued -= Process_OnProductDequeued;
            M141Process.OnNewProductEnqueued += Process_OnNewProductEnqueued;
            M141Process.OnProductDequeued += Process_OnProductDequeued;
        }
 
        private void button1_ClearProductDisplayList_Click(object sender, EventArgs e)
        {
            IsSetTableHead = false;
            DisplayProductList.Clear();
            DataText.Clear();
            RefreshProductView();
        }
 
        //初始化表格
        #region 初始化DataGridView表格
        public void InitialTableHead()
        {
            //根据内容自动调节所有单元格的行的高度
            //dataGridView1_ProductDisplayList.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
 
            //获取标准检测项和ML检测项
            var specCollection = (((GlobalVar.Container.BeginLifetimeScope()).Resolve<IProcessConfig>()) as ProcessConfigBase).SpecCollection;
 
            //计算所有检测项的数量,确定表格列数
            int num_SpecCollection = specCollection.Count();
            //添加列
            for (int i = 0; i < num_SpecCollection + 4; i++)
            {
                InitializeDataText.Add("-");
            }
            //确定表头并添加
            HeadText.Add("PID");
            HeadText.Add("SN");
            HeadText.Add("Result");
            //添加网络检测项名称
            HeadText.Add("NetResults");
            //添加标准检测项的名称
            foreach (var item in specCollection)
            {
                HeadText.Add(item.Code);
            }
 
            RefreshProductView();
        }
        #endregion
 
        //设置是否自动刷新表格
        public void AutoRefresh_ProductDisplayList(object? sender, EventArgs e)
        {
            timerRefresh.Enabled = checkBox1_IsAutoRefresh.Checked;
        }
 
        //加入新的产品数据
        private async void Process_OnNewProductEnqueued(ProductModel pRecord)
        {
            await Task.Run(() =>
            {
                lock (_dataViewLock)
                {
                    DisplayProductList.Insert(0, pRecord);
 
                    //筛选检测项
                    List<string> tempDataString = new List<string>(InitializeDataText);
                    var specsList = (pRecord.Details.SelectMany(p => p.ResultList).ToList()).SelectMany(d => d.Specs).ToList();
                    var defects = string.Join(",", (pRecord.Details.SelectMany(p => p.ResultList.SelectMany(r => r.NetResults.SelectMany(n => n.DetectDetails.Select(t => t.ClassName))).Distinct().SkipWhile(u => string.Equals(u, "ok", StringComparison.OrdinalIgnoreCase)))));
 
                    tempDataString[0] = pRecord.PID;
                    tempDataString[1] = pRecord.SN;
                    tempDataString[2] = pRecord.Result;
                    tempDataString[3] = defects;
                    for (int i = 4; i < HeadText.Count(); i++)
                    {
                        //tempDataString[i] = specsList.FirstOrDefault(s => s.Code == HeadText[i] ? s.ActualValue.ToString() : "-");
 
                        tempDataString[i] = specsList.FirstOrDefault(s => s.Code == HeadText[i])?.GetMeasureValueStr() ?? "-";
                    }
 
                    DataText.Insert(0, tempDataString);
                }
            });
        }
 
        //如果当前产品已存在,更新数据并出队操作
        private void Process_OnProductDequeued(ProductModel pRecord)
        {
            lock (_dataViewLock)
            {
                var p = DisplayProductList.FirstOrDefault(u => u.PID == pRecord.PID);
                if (p != null)
                {
                    int index = DisplayProductList.IndexOf(p);
                    DisplayProductList[index] = pRecord;
 
                    //筛选检测项
                    var tempDataString = InitializeDataText;
                    var specsList = (pRecord.Details.SelectMany(p => p.ResultList).ToList()).SelectMany(d => d.Specs).ToList();
                    var defects = string.Join(",", (pRecord.Details.SelectMany(p => p.ResultList.SelectMany(r => r.NetResults.SelectMany(n => n.DetectDetails.Select(t => t.ClassName))).Distinct().SkipWhile(u => string.Equals(u, "ok", StringComparison.OrdinalIgnoreCase)))));
 
                    tempDataString[0] = pRecord.PID;
                    tempDataString[1] = pRecord.SN;
                    tempDataString[2] = pRecord.Result;
                    tempDataString[3] = defects;
                    for (int i = 4; i < HeadText.Count(); i++)
                    {
                        //tempDataString[i] = specsList.FirstOrDefault(s => s.Code == HeadText[i] ? s.ActualValue.ToString() : "-");
 
                        tempDataString[i] = specsList.FirstOrDefault(s => s.Code == HeadText[i])?.GetMeasureValueStr() ?? "-";
                    }
 
                    DataText[index] = tempDataString;
                }
            }
        }
 
        //时间刷新
        private void timerRefresh_Tick(object sender, EventArgs e)
        {
            timerRefresh.Interval = M141Config.DataViewRefreshTime;
            timerRefresh.Enabled = false;
            this.Invoke(() =>
            {
                RefreshProductView();
            });
 
            timerRefresh.Enabled = true;
        }
 
        //刷新数据(将收集的数据显示到界面表格中)
        static object _dataViewLock = new object();
        private void RefreshProductView()
        {
            lock (_dataViewLock)
            {
                DataText = DataText.Take(M141Config.DataViewCount).ToList();
            }
            this.Invoke(() =>
            {
                //按行刷新显示
                if (M141Config.RefreshModeSelect == RefreshMode.行)
                {
                    //添加列,并加入列头
                    if (IsSetTableHead)
                    {
                        int countColumn = 0;
                        HeadText.ForEach(h =>
                        {
                            dataGridView1_ProductDisplayList.Columns.Add(new DataGridViewTextBoxColumn());
                            dataGridView1_ProductDisplayList.Columns[countColumn].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                            dataGridView1_ProductDisplayList.Columns[countColumn++].HeaderText = h;
                        });
 
                        IsSetTableHead = false;
                    }
 
                    //添加行
                    dataGridView1_ProductDisplayList.Rows.Clear();
                    if (DataText.Count() != 0)
                    {
                        int countRow = 0;
                        DataText.ForEach(d1 =>
                        {
                            int countColumn = 0;
                            dataGridView1_ProductDisplayList.Rows.Add();
                            d1.ForEach(d2 =>
                            {
                                dataGridView1_ProductDisplayList.Rows[countRow].Cells[countColumn++].Value = d2;
                            });
                            countRow++;
                        });
                    }
                }
                //按列刷新显示
                else
                {
 
                }
            });
        }
    }
}