领胜LDS 键盘AOI检测项目
wells.liu
2020-07-04 65faad811883efc4264e3a5ca2c4e64340c198f5
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
using A036.Process;
using Bro.Common.Helper;
using Bro.Common.Interface;
using Bro.Common.Model;
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;
 
namespace Bro.Device.Station.Forms
{
    public partial class MonitorSetBindFrm : Form
    {
        private Dictionary<string, MonitorSet> monitorSetBind = new Dictionary<string, MonitorSet>();
        public Dictionary<string, MonitorSet> MonitorSetBind
        {
            get
            {
                return monitorSetBind;
            }
            set
            {
                if (value != null)
                {
                    monitorSetBind = value;
                    RefreshBindView();
                }
            }
        }
 
        //private string stationCode = "";
        //public string StationCode
        //{
        //    get
        //    {
        //        return stationCode;
        //    }
        //    set
        //    {
        //        if (!string.IsNullOrWhiteSpace(value))
        //        {
        //            stationCode = value;
        //            LoadMethodCodes();
        //        }
        //    }
        //}
 
        //List<ProcessMethodAttribute> methods = new List<ProcessMethodAttribute>();
 
        public MonitorSetBindFrm()
        {
            InitializeComponent();
        }
 
        public MonitorSetBindFrm(Dictionary<string, MonitorSet> bind)
        {
            InitializeComponent();
 
            LoadMethodCodes();
 
            if (bind != null)
            {
                //MonitorSetBind = bind;
 
                methods.ForEach(u =>
                {
                    bool isMatch = false;
                    foreach (var pair in bind)
                    {
                        if (pair.Key == u.MethodCode)
                        {
                            isMatch = true;
                            MonitorSetBind.Add(pair.Key, pair.Value);
                            break;
                        }
                    }
 
                    if (!isMatch)
                    {
                        MonitorSetBind.Add(u.MethodCode, new MonitorSet());
                    }
                });
 
                var keyList = MonitorSetBind.Select(u => u.Key).ToList();
                keyList = keyList.OrderBy(u => u).ToList();
                Dictionary<string, MonitorSet> tempDict = new Dictionary<string, MonitorSet>();
                keyList.ForEach(k =>
                {
                    tempDict[k] = MonitorSetBind[k];
                });
 
                MonitorSetBind = tempDict;
 
                RefreshBindView();
            }
 
            propGridMonitorSet.PropertyValueChanged += PropGridMonitorSet_PropertyValueChanged;
        }
 
        private void PropGridMonitorSet_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            var ms = propGridMonitorSet.SelectedObject as MonitorSet;
            lvBinds.Items[selectedIndex].Text = ms.TriggerIndex + "-" + ms.TriggerValue + "-" + String.Join(",", ms.InputDataIndex) + "-" + ms.ReplyDataAddress + "-" + ms.NoticeAddress;
        }
 
        private void RefreshBindView()
        {
            lvBinds.Items.Clear();
 
            foreach (var pair in MonitorSetBind)
            {
                var ms = pair.Value;
                ListViewItem item = new ListViewItem(ms.TriggerIndex + "-" + ms.TriggerValue + "-" + String.Join(",", ms.InputDataIndex) + "-" + ms.ReplyDataAddress + "-" + ms.NoticeAddress);
 
                item.SubItems.Add(pair.Key);
 
                lvBinds.Items.Add(item);
            }
        }
 
        private void LoadMethodCodes()
        {
            IProcess sp = new ProcessControl();
 
            methods = sp.ProcessMethodCollection;
            var pms = methods.ConvertAll(u =>
            {
                return new { Desc = u.MethodCode + "--" + u.MethodDesc, Value = u.MethodCode };
            });
 
            //cboMethods.DataSource = pms;
            //cboMethods.DisplayMember = "Desc";
            //cboMethods.ValueMember = "Value";
        }
 
        int selectedIndex = -1;
        private void lvBinds_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvBinds.SelectedIndices.Count > 0)
            {
                selectedIndex = lvBinds.SelectedIndices[0];
 
                string ms = MonitorSetBind.Keys.ElementAt(selectedIndex);
 
                propGridMonitorSet.SelectedObject = MonitorSetBind[ms];
 
                var method = methods.FirstOrDefault(u => u.MethodCode == ms);
                lblMethodCode.Text = method.MethodCode + "\r\n" + method.MethodDesc;
 
                //cboMethods.SelectedValue = MonitorSetBind[ms];
            }
        }
 
        private void btnAddBind_Click(object sender, EventArgs e)
        {
            MonitorSet ms = new MonitorSet();
            MonitorSetBind.Add("", ms);
 
            MonitorSetBind = MonitorSetBind;
        }
 
        private void btnRemoveBind_Click(object sender, EventArgs e)
        {
            if (lvBinds.SelectedIndices.Count > 0)
            {
                int index = lvBinds.SelectedIndices[0];
 
                var ms = MonitorSetBind.Keys.ElementAt(index);
 
                MonitorSetBind.Remove(ms);
            }
            else
            {
                MessageBox.Show("请选择需要删除的绑定关系");
            }
        }
 
        private void cboMethods_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (cboMethods.SelectedValue != null)
            //{
            //    var ms = propGridMonitorSet.SelectedObject as MonitorSet;
            //    if (ms != null && MonitorSetBind.Values.Contains(ms))
            //    {
            //        MonitorSetBind[ms] = cboMethods.SelectedValue.ToString();
 
            //        lvBinds.Items[selectedIndex].SubItems[1].Text = cboMethods.SelectedValue.ToString();
            //    }
            //}
        }
    }
}