-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoPropertyClass.cs
More file actions
419 lines (372 loc) · 17.6 KB
/
AutoPropertyClass.cs
File metadata and controls
419 lines (372 loc) · 17.6 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using System.Diagnostics;
namespace UHFReader288MPDemo
{
/// <summary>
/// 自动界面赋值取值对象;
/// </summary>
class AutoPropertyClass
{
private IntPtr _devHandle;
private PropertyParaMapClass _propertyParaMap;
/// <summary>
/// 构造函数;
/// </summary>
/// <param name="devHandle">设备句柄;</param>
public AutoPropertyClass(IntPtr devHandle)
{
this._devHandle = devHandle;
this._propertyParaMap = new PropertyParaMapClass();
}
/// <summary>
/// 添加控件与PARA_TYPES间的映射关系;
/// </summary>
/// <param name="propertyName">控件标识,使用控件的AccessibleName属性作为标识;</param>
/// <param name="paraType">DevControl.PARA_TYPES</param>
/// <returns>是否添加成功</returns>
public bool AddPropertyParaMap(string propertyName, DevControl.PARA_TYPES paraType)
{
//增加控件与设备参数间的映射关系;
return this._propertyParaMap.AddPropertyParaMap(propertyName, paraType);
}
/// <summary>
/// 从容器控件中读取已加入映射表的子控件的输入值;
/// </summary>
/// <param name="ownerPage">容器控件;</param>
/// <param name="channelNum">参数所属通道号;</param>
/// <param name="paraList">IntPtr(paralist);</param>
/// <returns>DevControl.tagErrorCode</returns>
private DevControl.tagErrorCode GetParameterFromProperty(Control ownerPage, int channelNum, IntPtr paraList)
{
IntPtr setParaList = paraList;
DevControl.tagErrorCode eCode = DevControl.tagErrorCode.DM_ERR_OK;
Debug.Assert(ownerPage != null);
Debug.Assert(paraList != IntPtr.Zero);
//遍历容器控件,通过控件名与设备参数间的映射关系,从配置界面的控件中读取配置设备的参数;
foreach (Control control in ownerPage.Controls)
{
//控件不使能则不需要填充数值(包括其下所有子控件)
if ((control.Enabled == false))
{
continue;
}
//控件为非叶节点控件,则使用递归继续向下遍历;
if (control.Controls.Count != 0)
{
eCode = GetParameterFromProperty(control, channelNum, paraList);
if (eCode != DevControl.tagErrorCode.DM_ERR_OK)
{
break;
}
}
//使用control.AccessibleName来标识参与映射的控件;
//可依据需求修改为其他标识;
if (control.AccessibleName != null)
{
bool isFind;
//查找控件映射的PARA_TYPES
DevControl.PARA_TYPES paraType = DevControl.PARA_TYPES.END_OF_PARA_TYPES;
isFind = this._propertyParaMap.FindParaByProperty(control.AccessibleName, ref paraType);
if (isFind == true)
{
//依据控件类型,从控件中读取输入参数值,按字符串格式读取;
StringBuilder bufferString = new StringBuilder();
Type controlType = control.GetType();
if (controlType == typeof(System.Windows.Forms.TextBox))
{
if (((System.Windows.Forms.TextBox)control).ReadOnly == true)
{
continue;
}
bufferString.Append(((System.Windows.Forms.TextBox)control).Text.Trim());
}
else if (controlType == typeof(System.Windows.Forms.CheckBox))
{
if (((System.Windows.Forms.CheckBox)control).Checked)
{
bufferString.Append('1');
}
else
{
bufferString.Append('0');
}
}
else if (controlType == typeof(System.Windows.Forms.ComboBox))
{
bufferString.Append(((System.Windows.Forms.ComboBox)control).SelectedIndex.ToString());
}
else if (controlType == typeof(System.Windows.Forms.NumericUpDown))
{
bufferString.Append(((System.Windows.Forms.NumericUpDown)control).Value.ToString());
}
else
{
Debug.Fail("must support new controlType!");
continue;
}
//将读取的字符串格式输入参数值转换为操作接口输入类型,然后检查输入参数的合法性,合法则加入参数列表;
byte[] valueBuf = new byte[100];
int valueLen = valueBuf.Length;
eCode = DevControl.DM_String2Value(paraType, bufferString, bufferString.Length, valueBuf, ref valueLen);
if (eCode == DevControl.tagErrorCode.DM_ERR_OK)
{
eCode = DevControl.DM_CheckPara(this._devHandle, channelNum, paraType, valueLen, valueBuf);
if (eCode == DevControl.tagErrorCode.DM_ERR_OK)
{
DevControl.paralist_addnode(setParaList, channelNum, paraType, valueLen, valueBuf);
}
else
{
break;
}
}
else
{
break;
}
}
}
}
return eCode;
}
/// <summary>
/// 将paralist里的数值填充入容器控件中的对应映射控件;
/// </summary>
/// <param name="paralist">IntPtr(paralist);</param>
/// <param name="ownerPage">容器控件;</param>
/// <param name="channelNum">参数所属通道号;</param>
/// <returns>是否成功</returns>
private bool SetParameterToProperty(IntPtr paralist, Control ownerPage, int channelNum)
{
//通过控件名与设备参数间的映射关系,将从设备获取的参数值填充至对应的配置界面控件;
if (paralist == IntPtr.Zero)
{
return false;
}
//遍历容器控件,通过控件名与设备参数间的映射关系,从参数列表中读取设备参数填充入对应控件;
foreach (Control control in ownerPage.Controls)
{
//控件为非叶节点控件,则使用递归继续向下遍历;
if (control.Controls.Count != 0)
{
SetParameterToProperty(paralist, control, channelNum);
}
//使用control.AccessibleName来标识参与映射的控件;
//可依据需求修改为其他标识;
if (control.AccessibleName != null)
{
bool isFind;
//查找控件映射的PARA_TYPES
DevControl.PARA_TYPES paraType = DevControl.PARA_TYPES.END_OF_PARA_TYPES;
isFind = this._propertyParaMap.FindParaByProperty(control.AccessibleName, ref paraType);
if (isFind == true)
{
byte[] valueBuf = new byte[100];
int valueLen = valueBuf.Length;
//使用PARA_TYPES从参数列表中读取设备参数填充入对应控件;
DevControl.tagErrorCode eCode = DevControl.paralist_getnode(paralist, channelNum, paraType, ref valueLen, valueBuf);
if (eCode == DevControl.tagErrorCode.DM_ERR_OK)
{
//当前设备不支持此参数;
if (valueLen == 0)
{
control.Enabled = false;
continue;
}
//将获取到的参数统一转换为字符串再按控件类型分别填充;
StringBuilder bufferString = new StringBuilder(100);
int stringLen = bufferString.Capacity;
eCode = DevControl.DM_Value2String(paraType, valueBuf, valueLen, bufferString, ref stringLen);
Debug.Assert(eCode == DevControl.tagErrorCode.DM_ERR_OK);
Type controlType = control.GetType();
if (controlType == typeof(System.Windows.Forms.TextBox))
{
((System.Windows.Forms.TextBox)control).Text = bufferString.ToString();
}
else if (controlType == typeof(System.Windows.Forms.CheckBox))
{
if (bufferString[0] == '1')
{
((System.Windows.Forms.CheckBox)control).Checked = true;
}
else if (bufferString[0] == '0')
{
((System.Windows.Forms.CheckBox)control).Checked = false;
}
else
{
Debug.Fail("out of range!");
}
}
else if (controlType == typeof(System.Windows.Forms.ComboBox))
{
if (valueBuf[0] < ((System.Windows.Forms.ComboBox)control).Items.Count)
{
((System.Windows.Forms.ComboBox)control).SelectedIndex = int.Parse(bufferString.ToString());
}
else
{
Debug.Fail("out of range!");
}
}
else if (controlType == typeof(System.Windows.Forms.NumericUpDown))
{
((System.Windows.Forms.NumericUpDown)(control)).Value = decimal.Parse(bufferString.ToString());
}
else
{
Debug.Fail("must support new controlType!");
continue;
}
}
}
}
}
return true;
}
/// <summary>
/// 依据容器控件中的对应映射控件,向远程设备请求对应的已配置参数;
/// </summary>
/// <param name="ownerPage">容器控件;</param>
/// <param name="channelNum">参数所属通道号;</param>
/// <param name="paraList">请求参数列表;</param>
public void GetQueryParaList(Control ownerPage, int channelNum,IntPtr paraList)
{
//遍历容器控件,通过控件名与设备参数间的映射关系,生成向远程设备请求配置参数值的参数列表;
foreach (Control control in ownerPage.Controls)
{
//控件不使能则不需要请求参数(包括其下所有子控件)
if ((control.Enabled == false))
{
continue;
}
//控件为非叶节点控件,则使用递归继续向下遍历;
if (control.Controls.Count != 0)
{
GetQueryParaList(control, channelNum, paraList);
}
//使用control.AccessibleName来标识参与映射的控件;
//可依据需求修改为其他标识;
if (control.AccessibleName != null)
{
bool isFind;
DevControl.PARA_TYPES paraType = DevControl.PARA_TYPES.END_OF_PARA_TYPES;
isFind = this._propertyParaMap.FindParaByProperty(control.AccessibleName, ref paraType);
if (isFind == true)
{
DevControl.paralist_addnode(paraList, channelNum, paraType);
}
}
}
}
/// <summary>
/// 依据容器控件中的对应映射控件,从设备获取参数并填充对应映射控件;
/// </summary>
/// <param name="ownerPage">容器控件;</param>
/// <param name="channelNum">参数所属通道号;</param>
/// <returns>tagErrorCode</returns>
public DevControl.tagErrorCode GetParameter(Control ownerPage, int channelNum)
{
IntPtr getParaList;
//从远程设备获取其当前配置参数;
getParaList = DevControl.paralistCreate(this._devHandle);
if (getParaList == IntPtr.Zero)
{
return DevControl.tagErrorCode.DM_ERR_MEM;
}
GetQueryParaList(ownerPage, channelNum, getParaList);
DevControl.tagErrorCode errCode = DevControl.DM_GetPara(this._devHandle, getParaList, 1000);
if (errCode == DevControl.tagErrorCode.DM_ERR_OK)
{
SetParameterToProperty(getParaList, ownerPage, channelNum);
}
DevControl.paralistDestroy(getParaList);
return errCode;
}
/// <summary>
/// 依据容器控件中的对应映射控件,将本地配置参数配置到远程设备;
/// </summary>
/// <param name="ownerPage">容器控件;</param>
/// <param name="channelNum">参数所属通道号;</param>
/// <returns>tagErrorCode</returns>
public DevControl.tagErrorCode SetParameter(Control ownerPage, int channelNum)
{
IntPtr setParaList;
DevControl.tagErrorCode eCode;
//将配置参数设置到对应远程设备;
setParaList = DevControl.paralistCreate(this._devHandle);
if (setParaList == IntPtr.Zero)
{
return DevControl.tagErrorCode.DM_ERR_MEM;
}
eCode = GetParameterFromProperty(ownerPage, channelNum, setParaList);
if (eCode == DevControl.tagErrorCode.DM_ERR_OK)
{
Debug.Assert(setParaList != IntPtr.Zero);
eCode = DevControl.DM_SetPara(this._devHandle, setParaList, 1000);
DevControl.paralistDestroy(setParaList);
}
return eCode;
}
}
/// <summary>
/// 控件标识与PARA_TYPES间映射关系管理对象;
/// 暂时只支持一对一映射;
/// </summary>
class PropertyParaMapClass
{
private string[] propertyParaMap;
public PropertyParaMapClass()
{
int maxPara = (int)DevControl.PARA_TYPES.END_OF_PARA_TYPES;
this.propertyParaMap = new string[maxPara];
}
/// <summary>
/// 添加控件与PARA_TYPES间的映射关系;
/// </summary>
/// <param name="propertyName">控件标识;</param>
/// <param name="paraType">DevControl.PARA_TYPES</param>
/// <returns>是否添加成功</returns>
public bool AddPropertyParaMap(string propertyName, DevControl.PARA_TYPES paraType)
{
int mapIndex = (int)paraType;
Debug.Assert(mapIndex < this.propertyParaMap.Length) ;
Debug.Assert(this.propertyParaMap[mapIndex] == null) ;
this.propertyParaMap[mapIndex] = propertyName;
return true;
}
/// <summary>
/// 使用控件标识查找其所对应的PARA_TYPES;
/// </summary>
/// <param name="propertyName">控件标识;</param>
/// <param name="paraType">DevControl.PARA_TYPES</param>
/// <returns>是否添加成功</returns>
public bool FindParaByProperty(string propertyName, ref DevControl.PARA_TYPES paraType)
{
int index = 0;
foreach (string name in this.propertyParaMap)
{
if (propertyName.Equals(name))
{
paraType = (DevControl.PARA_TYPES)index;
return true;
}
index++;
}
return false;
}
/// <summary>
/// 使用PARA_TYPES查找其所对应的控件标识;
/// </summary>
/// <param name="paraType">DevControl.PARA_TYPES</param>
/// <returns>对应的控件标识</returns>
public string FindPropertyByPara(DevControl.PARA_TYPES paraType)
{
return (string)this.propertyParaMap[(int)paraType];
}
}
}