领胜LDS 键盘AOI检测项目
wells.liu
2020-07-04 642cd31f0d1586a2a5ca6f9a3b3364725f4f1ecd
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
using Bro.Common.Helper;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using static Bro.Common.Helper.EnumHelper;
 
namespace Bro.Common.Model
{
    public class IOItem : IComplexDisplay, IEqualityComparer
    {
        /// <summary>
        /// IO点编号
        /// </summary>
        [Category("IO配置")]
        [Description("IO点编号")]
        public virtual int IONum { get; set; }
 
        /// <summary>
        /// IO点的值
        /// </summary>
        [Category("IO配置")]
        [Description("IO状态")]
        [TypeConverter(typeof(EnumDescriptionConverter<IOValue>))]
        public virtual IOValue Value { get; set; }
 
        /// <summary>
        /// IO点是in还是out
        /// </summary>   
        [Category("IO配置")]
        [Description("IO类型")]
        public virtual IOType IOType { get; set; }
 
        public new bool Equals(object x, object y)
        {
            if (x is IOItem a && y is IOItem b)
            {
                return a.IOType == b.IOType && a.IONum == b.IONum;
            }
 
            return false;
        }
 
        public virtual string GetDisplayText()
        {
            return $"{IOType.GetEnumDescription()}-{IONum}-{Value.GetEnumDescription()}";
        }
 
        public int GetHashCode(object obj)
        {
            return obj.GetHashCode();
        }
    }
 
    public class IODefinition : IOItem
    {
        [Category("IO配置")]
        [Description("IO预定义")]
        [TypeConverter(typeof(EnumDescriptionConverter<IOPrestatement>))]
        public IOPrestatement IOPreStatement { get; set; } = IOPrestatement.Customized;
 
        [Category("IO配置")]
        [Description("IO用途描述")]
        public string IODesc { get; set; }
 
        [Category("IO配置")]
        [Description("备注说明")]
        public string Remark { get; set; }
 
        [Browsable(false)]
        [JsonIgnore]
        public override IOValue Value { get; set; }
 
        public override string GetDisplayText()
        {
            return $"{(IOPreStatement == IOPrestatement.Customized ? IODesc : IOPreStatement.GetEnumDescription())} {IOType.GetEnumDescription()} {IONum}";
        }
    }
 
    public class IORefrenceItem : IComplexDisplay
    {
        [Category("IO操作配置")]
        [Description("需要操作的IO")]
        [TypeConverter(typeof(IORefrenceItemSourceConverter))]
        public IODefinition IOItem { get; set; } = new IODefinition();
 
        [Category("IO操作配置")]
        [Description("需要操作的IO的状态")]
        public IOValue CheckValue
        {
            get => IOItem.Value;
            set => IOItem.Value = value;
        }
 
        [Browsable(false)]
        [JsonIgnore]
        public List<IODefinition> IOItemSource { get; set; } = new List<IODefinition>();
 
        public string GetDisplayText()
        {
            return IOItem.IODesc + "-" + CheckValue.GetEnumDescription();
        }
    }
 
    public class IORefrenceItemSourceConverter : TypeConverter
    {
        Dictionary<IODefinition, string> itemDict = new Dictionary<IODefinition, string>();
        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            if (context.Instance is IORefrenceItem item)
            {
                item.IOItemSource.ForEach(i =>
                {
                    itemDict[i] = i.IODesc + "-" + i.IOType.GetEnumDescription() + "-" + i.IONum;
                });
 
                return new StandardValuesCollection(itemDict.Keys);
            }
 
            return base.GetStandardValues(context);
        }
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true;
        }
 
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return true;
        }
 
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(String))
                return true;
 
            return base.CanConvertFrom(context, sourceType);
        }
 
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return base.CanConvertTo(context, destinationType);
        }
 
        public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
        {
            return base.CreateInstance(context, propertyValues);
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="culture"></param>
        /// <param name="value">string</param>
        /// <returns></returns>
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            var s = value.ToString().Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
 
            if (s.Length == 3)
            {
                foreach (KeyValuePair<IODefinition, string> pair in itemDict)
                {
                    if (pair.Value == value.ToString())
                    {
                        return pair.Key;
                    }
                }
 
                IODefinition item = new IODefinition();
                item.IODesc = s[0];
                item.IOType = (IOType)Enum.Parse(typeof(IOType), s[1]);
                item.IONum = int.Parse(s[2]);
                return item;
            }
 
            return base.ConvertFrom(context, culture, value);
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="culture"></param>
        /// <param name="value">IOItem</param>
        /// <param name="destinationType"></param>
        /// <returns></returns>
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (value is IODefinition item)
            {
                foreach (KeyValuePair<IODefinition, string> pair in itemDict)
                {
                    if (pair.Key.IOType == item.IOType && pair.Key.IONum == item.IONum)
                    {
                        return pair.Value;
                    }
                }
 
                return item.IODesc + "-" + item.IOType.GetEnumDescription() + "-" + item.IONum;
            }
 
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }
}