patrick
2019-12-10 1c4426810c71eead57084be8a18ade8d314dd8c4
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 Bro.Common.Interface;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using System.Reflection;
using Bro.Common.Helper;
using Bro.Common.Base;
using Bro.Common.Factory;
using A032.Process;
using Bro.Common.Model;
using Autofac;
 
namespace Bro.Device.Station.Forms
{
    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(string scode, 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()
        {
            OpBinds = new Dictionary<string, IOperationConfig>();
 
            using (var scope = GlobalVar.Container.BeginLifetimeScope())
            {
                ProcessMethodList = scope.Resolve<List<ProcessMethodAttribute>>();
            }
 
            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("请选择需要重置的操作");
            }
        }
    }
}