schangxiang@126.com
2025-11-04 f5ed29dc26c7cd952d56ec5721a2efc43cd25992
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
using DataEntity.Device;
using DataEntity.Page;
using DataEntity.Share;
using DataRWDAL.Device;
using HandyControl.Data;
using HxEnum;
using HxSocketImplement.Sockets;
using HxSocketImplement.Sockets.HxService;
using System;
using System.Collections.Generic;
using System.Windows;
using XCommon;
using XCommon.Log;
using XCommon.Tip;
using XHandler.View.MethodProperty;
using XHandler.View.Page;
using XImagingXhandler.XDAL;
using static HxEnum.StateEnum;
using Button = System.Windows.Controls.Button;
using Pagination = DataEntity.Page.Pagination;
using UserControl = System.Windows.Controls.UserControl;
 
namespace XHandler.View.Device
{
    /// <summary>
    /// 设备管理页面
    /// </summary>
    public partial class DeveiceManagement : UserControl
    {
        #region 变量
        /// <summary>
        /// 分页数据
        /// </summary>
        private Pagination m_pagination = new Pagination();
        private Pagination m_paginationDeviceMethod = new Pagination();
 
        /// <summary>
        /// 左侧DataGrid选中的DeviceId
        /// </summary>
        private string m_deviceconfigId = string.Empty;
        #endregion
 
        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        public DeveiceManagement()
        {
            InitializeComponent();
        }
        #endregion
 
        #region 初始化
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                // 设备
                GetData();
            }
            catch (Exception ex)
            {
                LoggerHelper.ErrorLog("ERROR:", ex);
            }
        }
 
        /// <summary>
        /// 获取设备信息
        /// </summary>
        private void GetData()
        {
            DataBinding(m_pagination);
        }
 
        /// <summary>
        /// 获取设备方法信息
        /// </summary>
        private void GetDataDeviceMethod()
        {
            DataBindingDeviceMethod(m_paginationDeviceMethod);
        }
        #endregion
 
        #region 设备相关
        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            m_deviceconfigId = string.Empty;
            dgDeviceMethodData.ItemsSource = null;
            tBoxDeviceName.Text = string.Empty;
 
            pagerToolsControl_paging.Current = PaginationDefaultParameter.Current;
        }
 
        /// <summary>
        /// 添加设备
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddDevice_Click(object sender, RoutedEventArgs e)
        {
            DeviceAdd deviceAdd = new DeviceAdd();
            deviceAdd.Owner = (Window)Shared.Main;
            deviceAdd.OperationType = OperationTypeEnum.OperationType_Enum.AddEnum.ToString();
            deviceAdd.LoadListEveForm -= GetData;
            deviceAdd.LoadListEveForm += GetData;
            deviceAdd.ShowDialog();
        }
 
        /// <summary>
        /// 分页绑定
        /// </summary>
        /// <param name="pagination"></param> 
        private void DataBinding(Pagination pagination)
        {
            var result = DeviceConfigDB.GetPageData(pagination, GetSearch(),Shared.SoftwareInformation.software_information_id);
 
            if (result != null)
            {
                for (int i = 0; i < result.Item1.Count; i++)
                {
                    result.Item1[i].SId = i + 1;
                    result.Item1[i].CommunicateTypeName =
                        EnumManagement.GetEnumDescription(EnumManagement.GetField<CommunicateTypeEnum>(result.Item1[i].CommunicateType));
                }
 
                pagerToolsControl_paging.IsTrigger = false;
                pagerToolsControl_paging.DataCount = result.Item2;   // 当前查到数量
                pagerToolsControl_paging.Current = pagination.Current;
                pagerToolsControl_paging.IsTrigger = true;
                dgDeviceData.ItemsSource = result.Item1;
            }
        }
 
        /// <summary>
        /// 获取搜索参数
        /// </summary>
        /// <returns></returns>
        private DeviceConfigModel GetSearch()
        {
            DeviceConfigModel searchModel = new DeviceConfigModel();
            searchModel.Name = searchDeviceName.Text;
            return searchModel;
        }
 
        #region 操作
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Button btn = (Button)sender;
                if (btn != null)
                {
                    string deviceId = (string)btn.Tag;
                    var result = DeviceConfigDB.GetInfodById(deviceId);
                    if (result != null)
                    {
                        DeviceAdd deviceAdd = new DeviceAdd();
                        deviceAdd.Owner = (Window)Shared.Main;
                        deviceAdd.OperationType = OperationTypeEnum.OperationType_Enum.EditEnum.ToString();
                        deviceAdd.OperationId = deviceId;
                        deviceAdd.LoadListEveForm -= GetData;
                        deviceAdd.LoadListEveForm += GetData;
                        deviceAdd.ShowDialog();
                    }
                }
            }
            catch (Exception ex)
            {
                LoggerHelper.ErrorLog("ERROR:", ex);
            }
        }
 
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Button btn = (Button)sender;
                if (btn != null)
                {
                    // 获取所有设备信息
                    string deviceId = (string)btn.Tag;
                    var deviceConfigList = DeviceConfigMethodDB.GetListByDeviceconfigId(deviceId);
                    if (deviceConfigList != null && deviceConfigList.Count > 0)
                    {
                        //new MessageDialog("此设备下有方法,不能删除").ShowDialog();
                        ShowTip.ShowNotice("此设备下有方法,不能删除", InfoType.Warning);
                        return;
                    }
 
                    var result = DeviceConfigDB.GetInfodById(deviceId);
                    if (result != null)
                    {
                        PlsToolTipWin plsToolTipWin = new PlsToolTipWin($"您确定要删除【{result.Name}】吗?");
                        plsToolTipWin.ShowDialog();
                        if (plsToolTipWin.DialogResult == true)
                        {
 
                            DeviceConfigDB.DelById(new DeviceConfigModel { Id = deviceId });
                            GetData();
                            //new MessageDialog("删除成功").ShowDialog();
                            ShowTip.ShowNotice("删除成功", InfoType.Success);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LoggerHelper.ErrorLog("ERROR:", ex);
            }
        }
 
        /// <summary>
        /// 方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDeviceMethod_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Button btn = (Button)sender;
                if (btn != null)
                {
                    // 获取所有设备信息
                    m_deviceconfigId = (string)btn.Tag;
                    DeviceConfigModel deviceConfig = DeviceConfigDB.GetInfodById(m_deviceconfigId);
                    tBoxDeviceName.Text = deviceConfig.Name;
 
                    // 设备方法
                    GetDataDeviceMethod();
                }
            }
            catch (Exception ex)
            {
                LoggerHelper.ErrorLog("ERROR:", ex);
            }
        }
        #endregion
 
        #region 分页事件
        /// <summary>
        /// 当前页改变时发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pagerToolsControl_paging_PageCntChanged(object sender, C_EventArgsClass e)
        {
            m_pagination.Current = e.CurrentPageNo;
            DataBinding(m_pagination);
        }
 
        /// <summary>
        /// 每页行数改变时发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pagerToolsControl_paging_OnePageRowCntChanged(object sender, C_EventArgsClass e)
        {
            m_pagination.Current = e.CurrentPageNo;
            m_pagination.PageSize = e.PagerNum;
            DataBinding(m_pagination);
        }
        #endregion
 
        #endregion
 
        #region 设备方法相关
        /// <summary>
        /// 搜索设备方法名
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearchMethodName_Click(object sender, RoutedEventArgs e)
        {
            pagerToolsDeviceMethod.Current = PaginationDefaultParameter.Current;
        }
 
        /// <summary>
        /// 添加方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddDeviceMethod_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(m_deviceconfigId))
            {
                DeviceMehtodAdd deviceMehtodAdd = new DeviceMehtodAdd();
                deviceMehtodAdd.Owner = (Window)Shared.Main;
                deviceMehtodAdd.OperationType = OperationTypeEnum.OperationType_Enum.AddEnum.ToString();
                deviceMehtodAdd.LoadListEveForm -= GetDataDeviceMethod;
                deviceMehtodAdd.LoadListEveForm += GetDataDeviceMethod;
                deviceMehtodAdd.DeviceconfigId = m_deviceconfigId;
                deviceMehtodAdd.ShowDialog();
            }
            else
            {
                ShowTip.ShowNotice("请选择数据再添加方法", InfoType.Warning);
            }
        }
 
        /// <summary>
        /// 分页绑定
        /// </summary>
        /// <param name="pagination"></param> 
        private void DataBindingDeviceMethod(Pagination pagination)
        {
            var result = DeviceConfigMethodDB.GetPageData(pagination, GetSearchDeviceMethod());
 
            if (result != null)
            {
                for (int i = 0; i < result.Item1.Count; i++)
                {
                    result.Item1[i].SId = i + 1;
                }
 
                pagerToolsDeviceMethod.IsTrigger = false;
                pagerToolsDeviceMethod.DataCount = result.Item2;   // 当前查到数量
                pagerToolsDeviceMethod.Current = pagination.Current;
                pagerToolsDeviceMethod.IsTrigger = true;
                dgDeviceMethodData.ItemsSource = result.Item1;
            }
        }
 
        /// <summary>
        /// 获取设备方法搜索参数
        /// </summary>
        /// <returns></returns>
        private DeviceConfigMethodModel GetSearchDeviceMethod()
        {
            DeviceConfigMethodModel searchModel = new DeviceConfigMethodModel();
            searchModel.DeviceconfigId = m_deviceconfigId;
            searchModel.ParameterName = searchDeviceMethodName.Text;
            return searchModel;
        }
 
        #region 操作
        /// <summary>
        /// 修改方法信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMethodEdit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Button btn = (Button)sender;
                if (btn != null)
                {
                    string deviceMethodId = (string)btn.Tag;
                    var result = DeviceConfigMethodDB.GetInfodById(deviceMethodId);
                    if (result != null)
                    {
                        DeviceMehtodAdd deviceMehtodAdd = new DeviceMehtodAdd();
                        deviceMehtodAdd.Owner = (Window)Shared.Main;
                        deviceMehtodAdd.OperationType = OperationTypeEnum.OperationType_Enum.EditEnum.ToString();
                        deviceMehtodAdd.LoadListEveForm -= GetDataDeviceMethod;
                        deviceMehtodAdd.LoadListEveForm += GetDataDeviceMethod;
                        deviceMehtodAdd.DeviceMethodId = deviceMethodId;
                        deviceMehtodAdd.DeviceconfigId = m_deviceconfigId;
                        deviceMehtodAdd.ShowDialog();
                    }
                }
            }
            catch (Exception ex)
            {
                LoggerHelper.ErrorLog("ERROR:", ex);
            }
        }
 
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMethodDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Button btn = (Button)sender;
                if (btn != null)
                {
                    string deviceMethodId = (string)btn.Tag;
 
                    // 获取设备方法参数
                    var getInfoByDeviceconfigMethodList = DeviceConfigMethodParametersDB.GetInfoByDeviceconfigMethodId(deviceMethodId);
                    if (getInfoByDeviceconfigMethodList != null && getInfoByDeviceconfigMethodList.Count > 0)
                    {
                        ShowTip.ShowNotice("此方法下有参数,不能删除", InfoType.Warning);
                        return;
                    }
 
                    var result = DeviceConfigMethodDB.GetInfodById(deviceMethodId);
                    if (result != null)
                    {
                        PlsToolTipWin plsToolTipWin = new PlsToolTipWin($"您确定要删除【{result.Name}】吗?");
                        plsToolTipWin.ShowDialog();
                        if (plsToolTipWin.DialogResult == true)
                        {
                            DeviceConfigMethodDB.DelById(new DeviceConfigMethodModel { Id = deviceMethodId });
                            GetDataDeviceMethod();
                            //new MessageDialog("删除成功").ShowDialog();
                            ShowTip.ShowNotice("删除成功", InfoType.Success);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LoggerHelper.ErrorLog("ERROR:", ex);
            }
        }
 
        /// <summary>
        /// 调试
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMethodDebug_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(m_deviceconfigId))
                {
                    Button btn = (Button)sender;
                    string deviceMethodId = (string)btn.Tag;
 
                    var deviceMethod = DeviceConfigMethodDB.GetInfodById(deviceMethodId);
                    
                    PlsToolTipWin plsToolTipWin = new PlsToolTipWin($"您确定要调试【{deviceMethod.ParameterName + "->" + deviceMethod.Name}】吗?");
                    plsToolTipWin.ShowDialog();
                    if (plsToolTipWin.DialogResult == true)
                    {
                        ShowTip.ShowNotice("执行中,请等待...", InfoType.Info);
 
                        var getDeviceConfigModel = DeviceConfigDB.GetInfodById(m_deviceconfigId);   //获取设备
                        if (getDeviceConfigModel != null)
                        {
                            // 获取发送参数
                            Dictionary<string, object> dictionaryList = new Dictionary<string, object>();
                            var getInfoByDeviceconfigMethodList = DeviceConfigMethodParametersDB.GetInfoByDeviceconfigMethodId(deviceMethodId, ParameterTypeEnum.Send);
                            
                            foreach (var item in getInfoByDeviceconfigMethodList)
                            {
                                dictionaryList.Add(item.ParameterName, ComUtility.ToTypeConvert(item.Value, item.DataType.ToString()));
                            }
 
                            InstructCommonMethodSendModel sendModel = HxSocketParameterService.GetInstructCommonMethodSendModel(new InstructCommonMethodSendModel()
                            {
                                EquipmentType = getDeviceConfigModel.Type.Trim(),
                                Ip = getDeviceConfigModel.Ip.Trim(),
                                Port = Convert.ToInt32(getDeviceConfigModel.Port.Trim()),
                                EquipmentId = getDeviceConfigModel.EquipmentId.Trim(),
                                VirtualConnectionState = getDeviceConfigModel.VirtualConnectionState,
                                CommunicateType = getDeviceConfigModel.CommunicateType,
                                SendData = new HxSendBase { experiment_id = HxSockServiceExecute.InstrunctionId(), parameters = dictionaryList, method = deviceMethod.ParameterName },
                            });
 
                            var resultModel = ResultHandle.InstructCommonMethod(sendModel, false);
 
 
                            if (resultModel != null && resultModel.status == StateEnum_Equipment.Completed)
                            {
                                ShowTip.ShowNotice("方法:" + deviceMethod.ParameterName + "-》" + resultModel.data.json, InfoType.Success, 5);
                            }
                            else
                            {
                                ShowTip.ShowNotice("调试失败,方法:" + deviceMethod.ParameterName + "-》" + resultModel?.ErrorMsg?.Error_Text, InfoType.Warning);
                            }
                        }
                        else
                        {
                            ShowTip.ShowNotice("设备数据未找到", InfoType.Error);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LoggerHelper.ErrorLog("ERROR:", ex);
            }
 
 
 
 
        }
 
        /// <summary>
        /// 参数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMethodParamter_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(m_deviceconfigId))
                {
                    Button btn = (Button)sender;
                    string deviceMethodId = (string)btn.Tag;
 
                    // 根据设备Id,获取设备信息
                    DeviceConfigModel deviceConfig = DeviceConfigDB.GetInfodById(m_deviceconfigId);
                    if (deviceConfig.CommunicateType == EnumManagement.GetEnumValue(CommunicateTypeEnum.Socket))        // Socket-TCP标准协议
                    {
                        DeviceMethodParameters frm = new DeviceMethodParameters();
                        frm.Owner = (Window)Shared.Main;
                        frm.DeviceconfigId = m_deviceconfigId;
                        frm.DeviceconfigMethodId = deviceMethodId;
                        frm.ShowDialog();
                    }
                }
            }
            catch (Exception ex)
            {
                LoggerHelper.ErrorLog("ERROR:", ex);
            }
        }
        #endregion
 
        #region 分页事件
        /// <summary>
        /// 当前页改变时发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pagerToolsDeviceMethod_PageCntChanged(object sender, C_EventArgsClass e)
        {
            m_paginationDeviceMethod.Current = e.CurrentPageNo;
            DataBindingDeviceMethod(m_paginationDeviceMethod);
        }
 
        /// <summary>
        /// 每页行数改变时发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pagerToolsDeviceMethod_OnePageRowCntChanged(object sender, C_EventArgsClass e)
        {
            m_paginationDeviceMethod.Current = e.CurrentPageNo;
            m_paginationDeviceMethod.PageSize = e.PagerNum;
            DataBindingDeviceMethod(m_paginationDeviceMethod);
        }
        #endregion
 
        #endregion
    }
}