领胜LDS 键盘AOI检测项目
wells.liu
2020-07-01 e9f47e76b9932949c9df829e98b09938eb93e870
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
using Autofac;
using Bro.Common.Factory;
using Bro.Common.Helper;
using Bro.Common.Interface;
using Bro.Common.Model.Forms;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using static Bro.Common.Helper.EnumHelper;
 
namespace Bro.Common.Model
{
    public class SimpleMonitorSet : IMonitorSet, IComplexDisplay
    {
        [Browsable(false)]
        public string Id { get; set; } = Guid.NewGuid().ToString();
 
        [Category("调用配置")]
        [Description("调用设备")]
        [DisplayName("调用设备")]
        [TypeConverter(typeof(DeviceSelectorConverter<IDevice>))]
        public string InvokeDevice { get; set; } = "";
 
        [Browsable(false)]
        public string SourceDevice { get; set; } = "";
 
        [Category("调用配置")]
        [Description("调用方法")]
        [DisplayName("调用方法")]
        [TypeConverter(typeof(ProcessMethodSelectorConverter))]
        public string MethodCode { get; set; }
 
        [Category("调用配置")]
        [Description("调用方法描述")]
        [DisplayName("调用方法描述")]
        [ReadOnly(true)]
        public string MethodDesc
        {
            get
            {
                using (var scope = GlobalVar.Container.BeginLifetimeScope())
                {
                    List<ProcessMethodAttribute> methodList = scope.Resolve<List<ProcessMethodAttribute>>();
                    return methodList.FirstOrDefault(u => u.MethodCode == MethodCode)?.MethodDesc;
                }
            }
        }
 
        [Category("调用配置")]
        [Description("调用操作配置")]
        [DisplayName("调用操作配置")]
        [TypeConverter(typeof(ComplexObjectConvert))]
        [Editor(typeof(IOperationConfigEditor), typeof(UITypeEditor))]
        public IOperationConfig OpConfig { get; set; }
 
        [Category("个性化设置")]
        [Description("监听调用描述")]
        [DisplayName("监听名称")]
        public string Name { get; set; } = "";
 
        [Browsable(false)]
        [JsonIgnore]
        public ProcessResponse Response { get; set; } = new ProcessResponse();
 
        [Browsable(false)]
        public string DisplayText
        {
            get
            {
                return GetDisplayText();
            }
        }
 
        public SimpleMonitorSet() { }
 
        public virtual string GetDisplayText()
        {
            using (var scope = GlobalVar.Container.BeginLifetimeScope())
            {
                List<ProcessMethodAttribute> methodList = scope.Resolve<List<ProcessMethodAttribute>>();
                List<IDevice> deviceList = scope.Resolve<List<IDevice>>();
 
                string displayText = string.IsNullOrWhiteSpace(Name) ? "" : Name + "_";
 
                var device = deviceList.FirstOrDefault(u => u.Id == InvokeDevice);
                if (device != null)
                {
                    displayText += ($"{device.Name}");
                }
                else
                {
                    displayText += "无设备";
                }
 
                displayText += "  ";
                var method = methodList.FirstOrDefault(u => u.MethodCode == MethodCode);
                if (method != null)
                {
                    displayText += ($"{method.MethodCode}-{method.MethodDesc}");
                }
                else
                {
                    displayText += "无调用";
                }
 
                var sourceDevice = deviceList.FirstOrDefault(u => u.Id == SourceDevice);
                if (sourceDevice != null)
                {
                    displayText = sourceDevice.Name + " " + displayText;
                }
 
                return displayText;
            }
        }
    }
 
    /// <summary>
    /// PLC监听设置
    /// </summary>
    public class MonitorSet : SimpleMonitorSet
    {
        /// <summary>
        /// 监听地址索引 按照监听地址从0开始的索引
        /// </summary>
        [Category("监听设置")]
        [Description("监听地址索引 按照监听地址从0开始的索引")]
        [DisplayName("触发索引")]
        public int TriggerIndex { get; set; } = -1;
 
        /// <summary>
        /// 触发值
        /// </summary>
        [Category("监听设置")]
        [Description("触发值,设置为-999时变化即触发")]
        [DisplayName("触发值")]
        public int TriggerValue { get; set; } = -1;
 
        /// <summary>
        /// 传入数据地址的索引 按照监听地址从0开始的索引集合
        /// </summary>
        [Category("监听设置")]
        [Description("传入数据地址的索引 按照监听地址从0开始的索引")]
        [DisplayName("传入数据索引")]
        [TypeConverter(typeof(SimpleCollectionConvert<int>))]
        public List<int> InputDataIndex { get; set; } = new List<int>();
 
        /// <summary>
        /// 数据地址 实际寄存器的地址,例如 40012
        /// </summary>
        [Category("回传设置")]
        [Description("回传数据地址 实际PLC寄存器的地址,10进制,例如 40012")]
        [DisplayName("回传数据地址")]
        public int ReplyDataAddress { get; set; } = -1;
 
        /// <summary>
        /// 通知地址 实际寄存器的地址,例如 40012
        /// </summary>
        [Category("回传设置")]
        [Description("通知地址 实际寄存器的地址,10进制,例如 40012")]
        [DisplayName("通知地址")]
        public int NoticeAddress { get; set; } = -1;
 
        public MonitorSet() { }
 
        public override string GetDisplayText()
        {
            string desc = base.GetDisplayText();
 
            desc += $"_{TriggerIndex}:{TriggerValue}";
 
            return desc;
        }
    }
 
    /// <summary>
    /// 运动板卡监听配置对象
    /// </summary>
    public class MotionCardMonitorSet : SimpleMonitorSet
    {
        /// <summary>
        /// 监听类型
        /// </summary>
        [Category("监听设置")]
        [DisplayName("监听类型")]
        [Description("监听运动板卡 IO 类型(IN OUT)")]
        public IOModel MonitorIOModel { get; set; }
 
        /// <summary>
        /// 监听地址索引
        /// </summary>
        [Category("监听设置")]
        [Description("监听地址索引")]
        [DisplayName("触发索引")]
        public int TriggerIndex { get; set; } = -1;
 
        /// <summary>
        /// 触发值
        /// </summary>
        [Category("监听设置")]
        [Description("触发值,设置为-999时变化即触发")]
        [DisplayName("触发值")]
        public int TriggerValue { get; set; } = -1;
 
        /// <summary>
        /// 监听回传
        /// </summary>
        [Category("回传设置")]
        [DisplayName("监听回传")]
        [Description("监听运动板卡,并往指定的IO写入数据")]
        [TypeConverter(typeof(CollectionCountConvert))]
        [Editor(typeof(IOItem), typeof(UITypeEditor))]
        public List<IOItem> ReplyIODatas { get; set; } = new List<IOItem>();
    }
 
    public class IOperationConfigEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }
 
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
 
            if (edSvc != null)
            {
                IMonitorSet ms = context.Instance as IMonitorSet;
                if (value == null)
                {
                    string methodCode = ms.MethodCode;
                    using (var scope = GlobalVar.Container.BeginLifetimeScope())
                    {
                        List<ProcessMethodAttribute> methodList = scope.Resolve<List<ProcessMethodAttribute>>();
                        var method = methodList.FirstOrDefault(u => u.MethodCode == methodCode);
 
                        if (method != null)
                        {
                            value = ConfigFactory.GetOperationConfig(method.DeviceType);
                        }
                    }
                }
 
                FrmOpConfigEdit frm = new FrmOpConfigEdit(ms.MethodCode, ms.Id, value as IOperationConfig)
                {
                    StartPosition = FormStartPosition.CenterScreen
                };
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    return frm.OpConfig;
                }
                else
                {
                    return frm.BackupConfig;
                }
            }
 
            return base.EditValue(context, provider, value);
        }
    }
}