领胜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
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;
 
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)]
        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>
        /// 监听地址索引 按照PLC监听地址从0开始的索引
        /// </summary>
        [Category("监听设置")]
        [Description("监听地址索引 按照PLC监听地址从0开始的索引")]
        [DisplayName("触发索引")]
        public int TriggerIndex { get; set; } = -1;
 
        /// <summary>
        /// 触发值
        /// </summary>
        [Category("监听设置")]
        [Description("触发值,设置为-999时变化即触发")]
        [DisplayName("触发值")]
        public int TriggerValue { get; set; } = -1;
 
        /// <summary>
        /// 传入数据地址的索引 按照PLC监听地址从0开始的索引集合
        /// </summary>
        [Category("监听设置")]
        [Description("传入数据地址的索引 按照PLC监听地址从0开始的索引")]
        [DisplayName("传入数据索引")]
        [TypeConverter(typeof(SimpleCollectionConvert<int>))]
        public List<int> InputDataIndex { get; set; } = new List<int>();
 
        /// <summary>
        /// 数据地址 实际PLC寄存器的地址,例如 40012
        /// </summary>
        [Category("回传设置")]
        [Description("回传数据地址 实际PLC寄存器的地址,10进制,例如 40012")]
        [DisplayName("回传数据地址")]
        public int ReplyDataAddress { get; set; } = -1;
 
        /// <summary>
        /// 通知地址 实际PLC寄存器的地址,例如 40012
        /// </summary>
        [Category("回传设置")]
        [Description("通知地址 实际PLC寄存器的地址,10进制,例如 40012")]
        [DisplayName("通知地址")]
        public int NoticeAddress { get; set; } = -1;
 
        [Browsable(false)]
        [JsonIgnore]
        public ProcessResponse Response { get; set; } = new ProcessResponse();
 
        public MonitorSet() { }
 
        public override string GetDisplayText()
        {
            string desc = base.GetDisplayText();
 
            desc += $"_{TriggerIndex}:{TriggerValue}";
 
            return desc;
        }
    }
 
    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);
        }
    }
}