领胜LDS 键盘AOI检测项目
xcd
2020-06-24 d6c577e17ee7bb5331dd51d803f9b42441b0f0e5
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
using Bro.Common.Base;
using Bro.Common.Factory;
using Bro.Common.Helper;
using Bro.Common.Interface;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
 
namespace Bro.Process.UI
{
    public partial class OperationConfigBindFrm : Form
    {
        //private Dictionary<string, IOperationConfig> opBinds = new Dictionary<string, IOperationConfig>();
        public Dictionary<string, IOperationConfig> OpBinds { get; set; } = new Dictionary<string, IOperationConfig>();
        //{
        //    get
        //    {
        //        return opBinds;
        //    }
        //    set
        //    {
        //        if (value != null)
        //        {
        //            opBinds = value;
        //            RefreshBindView();
        //        }
        //    }
        //}
 
        //private string stationCode = "";
        //public string StationCode
        //{
        //    get
        //    {
        //        return stationCode;
        //    }
        //    set
        //    {
        //        if (!string.IsNullOrWhiteSpace(value))
        //        {
        //            stationCode = value;
        //            LoadMethodCodes();
        //        }
        //    }
        //}
 
        Dictionary<string, Type> AllOpDict = new Dictionary<string, Type>();
        List<ProcessMethodAttribute> ProcessMethodList = new List<ProcessMethodAttribute>();
 
        public OperationConfigBindFrm()
        {
            InitializeComponent();
        }
 
        public OperationConfigBindFrm(Dictionary<string, IOperationConfig> bind)
        {
            InitializeComponent();
 
            GetAllOpConfig();
 
            //StationCode = scode;
            LoadMethodCodes();
 
            if (bind != null)
            {
                for (int i = 0; i < OpBinds.Keys.Count; i++)
                {
                    string s = OpBinds.Keys.ElementAt(i);
                    if (bind.Keys.Contains(s))
                    {
                        OpBinds[s] = bind[s];
                    }
                }
 
                RefreshBindView();
            }
 
            propGridIOP.PropertyValueChanged += PropGridMonitorSet_PropertyValueChanged;
        }
 
        private void PropGridMonitorSet_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            var config = propGridIOP.SelectedObject as IOperationConfig;
            lvBinds.Items[selectedIndex].SubItems[1].Text = JsonConvert.SerializeObject(config);
        }
 
        private void RefreshBindView()
        {
            lvBinds.Items.Clear();
 
            foreach (var pair in OpBinds)
            {
                var ms = pair.Key;
                ListViewItem item = new ListViewItem(pair.Key);
 
                item.SubItems.Add(JsonConvert.SerializeObject(pair.Value as IOperationConfig));
 
                lvBinds.Items.Add(item);
            }
        }
 
        private void GetAllOpConfig()
        {
            this.GetType().Assembly.GetReferencedAssemblies().ToList().ForEach(a =>
            {
                Assembly.Load(a).GetTypes().ToList().ForEach(t =>
                {
                    var attr = t.GetCustomAttribute<DeviceAttribute>();
                    if (attr != null && attr.AttrType == EnumHelper.DeviceAttributeType.OperationConfig)
                    {
                        AllOpDict[attr.TypeCode] = t;
                    }
                });
            });
 
            this.GetType().Assembly.GetTypes().ToList().ForEach(t =>
            {
                var attr = t.GetCustomAttribute<DeviceAttribute>();
                if (attr != null && attr.AttrType == EnumHelper.DeviceAttributeType.OperationConfig)
                {
                    AllOpDict[attr.TypeCode] = t;
                }
            });
 
            var deviceOpConfigs = ConfigFactory.GetAllOpConfigTypes();
 
            foreach (string s in deviceOpConfigs.Keys)
            {
                AllOpDict[s] = deviceOpConfigs[s];
            }
        }
 
        private void LoadMethodCodes()
        {
            IProcess sp = new ProcessControl();
            //switch (StationCode)
            //{
            //    case "S1":
            //        sp = new StationProcess_S1(false);
            //        break;
            //    case "S2":
            //        sp = new StationProcess_S2(false);
            //        break;
            //    case "S3":
            //        sp = new StationProcess_S3(false);
            //        break;
            //    case "S4":
            //        sp = new StationProcess_S4(false);
            //        break;
            //    case "S5":
            //        sp = new StationProcess_S5(false);
            //        break;
            //    case "S6":
            //        sp = new StationProcess_S6(false);
            //        break;
            //    case "S7":
            //        sp = new StationProcess_S7(false);
            //        break;
            //        //case "S0":
            //        //    sp = new StationProcess_S0(false);
            //        //    break;
            //}
 
            OpBinds = new Dictionary<string, IOperationConfig>();
 
            ProcessMethodList = sp.ProcessMethodCollection;
 
            ProcessMethodList.ForEach(u =>
            {
                if (!string.IsNullOrWhiteSpace(u.DeviceType))
                {
                    OpBinds[u.MethodCode] = Activator.CreateInstance(AllOpDict[u.DeviceType]) as IOperationConfig;
                }
                else
                {
                    OpBinds[u.MethodCode] = new OperationConfigBase();
                }
            });
        }
 
        int selectedIndex = -1;
        private void lvBinds_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvBinds.SelectedIndices.Count > 0)
            {
                selectedIndex = lvBinds.SelectedIndices[0];
 
                var ms = OpBinds.Keys.ElementAt(selectedIndex);
 
                propGridIOP.SelectedObject = null;
                propGridIOP.SelectedObject = OpBinds[ms];
            }
        }
 
        private void btnAddBind_Click(object sender, EventArgs e)
        {
            //MonitorSet ms = new MonitorSet();
            //OpBinds.Add("",);
 
            //OpBinds = OpBinds;
        }
 
        private void btnRemoveBind_Click(object sender, EventArgs e)
        {
            //if (lvBinds.SelectedIndices.Count > 0)
            //{
            //    int index = lvBinds.SelectedIndices[0];
 
            //    var ms = OpBinds.Keys.ElementAt(index);
 
            //    OpBinds.Remove(ms);
            //}
            //else
            //{
            //    MessageBox.Show("请选择需要删除的绑定关系");
            //}
        }
 
        private void cboMethods_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (cboMethods.SelectedValue != null)
            //{
            //    var ms = propGridIOP.SelectedObject as MonitorSet;
            //    if (ms != null && OpBinds.Keys.Contains(ms))
            //    {
            //        OpBinds[ms] = cboMethods.SelectedValue.ToString();
 
            //        lvBinds.Items[selectedIndex].SubItems[1].Text = cboMethods.SelectedValue.ToString();
            //    }
            //}
        }
 
        private void btnClearOpConfig_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否确认清空所有操作配置?", "清除提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                OpBinds.Clear();
                MessageBox.Show("清除成功!");
            }
        }
 
        private void btnResetCurrentOpConfig_Click(object sender, EventArgs e)
        {
            if (lvBinds.SelectedIndices.Count > 0)
            {
                if (MessageBox.Show("是否确认重置当前操作配置?", "重置提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    selectedIndex = lvBinds.SelectedIndices[0];
 
                    var ms = OpBinds.Keys.ElementAt(selectedIndex);
 
                    propGridIOP.SelectedObject = null;
                    propGridIOP.SelectedObject = OpBinds[ms] = Activator.CreateInstance(AllOpDict[ProcessMethodList.FirstOrDefault(u => u.MethodCode == ms).DeviceType]) as IOperationConfig;
 
                    MessageBox.Show("重置成功!");
                }
            }
            else
            {
                MessageBox.Show("请选择需要重置的操作");
            }
        }
    }
}