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
using DataEntity.Device;
using DataEntity.Rack;
using DataEntity.Share;
using DataRWDAL.Device;
using DataRWDAL.Rack;
using HandyControl.Data;
using HxEnum;
using Microsoft.Win32;
using NPOI.HSSF.Record.Chart;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using XCommon;
using XCommon.Check.Data;
using XCommon.file;
using XCommon.Log;
using XCommon.Tip;
using XHandler.View.Dialog;
 
namespace XHandler.View.Device
{
    /// <summary>
    /// 设备编辑 页面
    /// </summary>
    public partial class DeviceAdd : System.Windows.Window
    {
        #region 参数 
        /// <summary>
        /// 操作类型(添加、修改)
        /// </summary>
        public string OperationType = "";
 
        /// <summary>
        /// 主键ID
        /// </summary>
        public string OperationId = "";
 
        /// <summary>
        /// 委托列表数据
        /// </summary>
        public delegate void DelegateForm();
 
        /// <summary>
        /// 委托方法名称
        /// </summary>
        public event DelegateForm LoadListEveForm;
 
        /// <summary>
        /// 加载控件老数据
        /// </summary>
        Dictionary<string, object> oldDic = new Dictionary<string, object>();
 
        /// <summary>
        /// 图片路径
        /// </summary>
        private string ImgSrc = string.Empty;
        #endregion
 
        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        public DeviceAdd()
        {
            InitializeComponent();
        }
        #endregion
 
        #region 初始化
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                #region 设备类型
                // 获取设备类型列表
                List<DeviceConfigTypeModel> deviceconfigTypeList = DeviceConfigTypeDB.GetAlllList(Shared.SoftwareInformation.software_information_id);
                cBoxDeviceType.ItemsSource = deviceconfigTypeList;
                cBoxDeviceType.SelectedIndex = 0;
                #endregion
 
                #region 通讯方式
                // 根据通讯方式枚举值,获取下拉值
                cBoxCommunicateType.ItemsSource = ComUtility.GetDropDownList<CommunicateTypeEnum>();
                cBoxCommunicateType.SelectedIndex = 0;
                #endregion
 
                if (OperationType == OperationTypeEnum.OperationType_Enum.EditEnum.ToString())
                {
                    var model = DeviceConfigDB.GetInfodById(OperationId);
                    if (model != null)
                    {
                        tBoxName.Text = model.Name;
                        cBoxDeviceType.SelectedValue = model.Type;
                        tBoxEquipmentId.Text = model.EquipmentId;
                        tBoxEquipmentModel.Text = model.EquipmentModel;
                        tBoxManufactor.Text = model.Manufactor;
                        // 禁用设备
                        cBoxDisableState.IsChecked = model.DisableState == EnumManagement.GetEnumValue(DisableStateEnum.Disable) ? true : false;
                        // 虚拟设备
                        cBoxVirtualConnectionState.IsChecked = model.VirtualConnectionState == EnumManagement.GetEnumValue(VirtualConnectionStateEnum.Virtually) ? true : false;
                        cBoxCommunicateType.SelectedValue = model.CommunicateType;
                        tBoxRemark.Text = model.Remark;
 
                        if (model.CommunicateType == EnumManagement.GetEnumValue(CommunicateTypeEnum.Socket))
                        {
                            tBoxIp.Text = model.Ip;
                            tBoxPort.Text = model.Port;
                        }
 
                        // imgBase64String显示图片
                        if (!string.IsNullOrWhiteSpace(model.EquipmentImg))
                        {
                            UpLogFileViewTxt(model.EquipmentImg);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LoggerHelper.ErrorLog("ERROR:", ex);
            }
        }
        #endregion
 
        #region 选择图片
        /// <summary>
        /// 选择图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddDevice_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = false;
            fileDialog.Title = "请选择文件";
            fileDialog.Filter = "JPG类型图片(*jpg*)|*.jpg|PNG类型图片(*png*)|*.png*"; //设置要选择的文件的类型
            if (fileDialog.ShowDialog() == true)
            {
                var tempFilePath = fileDialog.FileName;
 
                // 获取图片Base64字符串
                string pic = ImportFile.GetImageByString(tempFilePath);
                UpLogFileViewTxt(pic);
            }
        }
 
        /// <summary>
        /// imgBase64String显示图片
        /// </summary>
        /// <param name="imgBase64String"></param>
        private void UpLogFileViewTxt(string imgBase64String)
        {
            srcImage.Source = ImportFile.GetImage(imgBase64String);
            ImgSrc = imgBase64String;
        }
        #endregion
 
        #region 确定
        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            if (!CheckData())
            {
                return;
            }
 
            DeviceConfigModel deviceConfigModel = new DeviceConfigModel();
            if (OperationType == OperationTypeEnum.OperationType_Enum.EditEnum.ToString())
            {
                deviceConfigModel = DeviceConfigDB.GetInfodById(OperationId);
            }
            deviceConfigModel.Name = tBoxName.Text;
            deviceConfigModel.Type = cBoxDeviceType.SelectedValue.ToString();
            deviceConfigModel.EquipmentId = tBoxEquipmentId.Text;
            deviceConfigModel.EquipmentImg = ImgSrc;
            // 禁用设备(1禁用,0不禁用)
            deviceConfigModel.DisableState = cBoxDisableState.IsChecked == true ?
                EnumManagement.GetEnumValue(DisableStateEnum.Disable) : EnumManagement.GetEnumValue(DisableStateEnum.Enable);
            // 虚拟链接状态(1:虚拟,0:真实)
            deviceConfigModel.VirtualConnectionState = cBoxVirtualConnectionState.IsChecked == true ?
                EnumManagement.GetEnumValue(VirtualConnectionStateEnum.Virtually) : EnumManagement.GetEnumValue(VirtualConnectionStateEnum.Real);
 
            deviceConfigModel.CommunicateType = Convert.ToInt32(cBoxCommunicateType.SelectedValue);
            deviceConfigModel.Remark = tBoxRemark.Text;
 
            deviceConfigModel.EquipmentModel = string.Empty;
            if (!string.IsNullOrWhiteSpace(tBoxEquipmentModel.Text))
            {
                deviceConfigModel.EquipmentModel = tBoxEquipmentModel.Text;
            }
 
            deviceConfigModel.Manufactor = string.Empty;
            if (!string.IsNullOrWhiteSpace(tBoxManufactor.Text))
            {
                deviceConfigModel.Manufactor = tBoxManufactor.Text;
            }
 
            deviceConfigModel.Ip = string.Empty;
            deviceConfigModel.PlcHeartbeatAddress = null;
            deviceConfigModel.PlcErrorAddress = null;
            deviceConfigModel.Port = string.Empty;
            deviceConfigModel.PortName = string.Empty;
            deviceConfigModel.StopBits = null;
            deviceConfigModel.BaudRate = null;
            deviceConfigModel.DataBits = null;
            deviceConfigModel.Parity = null;
            if (deviceConfigModel.CommunicateType == EnumManagement.GetEnumValue(CommunicateTypeEnum.Socket))
            {
                deviceConfigModel.Ip = tBoxIp.Text;
                deviceConfigModel.Port = tBoxPort.Text;
            }
 
            int updateCnt = 0;
            if (OperationType == OperationTypeEnum.OperationType_Enum.EditEnum.ToString())
            {
                deviceConfigModel.Id = OperationId;
                deviceConfigModel.ModifyTime = DateTime.Now;
                deviceConfigModel.ModifyName = Shared.User.username;
                updateCnt = DeviceConfigDB.Update(deviceConfigModel);
            }
            else
            {
                deviceConfigModel.ProjectId = Shared.SoftwareInformation.software_information_id;
                deviceConfigModel.CreateTime = DateTime.Now;
                deviceConfigModel.CreatName = Shared.User.username;
                deviceConfigModel.Id = Guid.NewGuid().ToString();
                updateCnt = DeviceConfigDB.Add(deviceConfigModel);
            }
 
            #region 点位
            PositionModel positionModel = PositionDB.GetInfoByRelationId(deviceConfigModel.Id);
            if (positionModel == null)
            {
                positionModel = new PositionModel();
                positionModel.id = Guid.NewGuid().ToString();
                positionModel.type = EnumManagement.GetEnumValue(PositonTypeEnum.Device);
                positionModel.name = deviceConfigModel.Name;
                positionModel.relation_Id = deviceConfigModel.Id;
                positionModel.software_information_id = Shared.SoftwareInformation.software_information_id;
                positionModel.creat_name = Shared.User.username;
                positionModel.create_time = DateTime.Now;
                PositionDB.Add(positionModel);
            }
            else
            {
                positionModel.name = deviceConfigModel.Name;
            }
            #endregion
 
            if (updateCnt > 0)
            {
                LoadListEveForm();
                //new MessageDialog("保存成功").ShowDialog();
                ShowTip.ShowNotice("保存成功", InfoType.Success);
                this.Close();
            }
        }
 
        /// <summary>
        /// 检查输入数据
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            try
            {
                if (string.IsNullOrWhiteSpace(cBoxDeviceType.SelectedValue.ToString()))
                {
                    //new MessageDialog("请选择设备类型").ShowDialog();
                    ShowTip.ShowNotice("请选择设备类型", InfoType.Warning);
                    return false;
                }
                if (string.IsNullOrWhiteSpace(tBoxEquipmentId.Text))
                {
                    //new MessageDialog("请填写设备ID").ShowDialog();
                    ShowTip.ShowNotice("请填写设备ID", InfoType.Warning);
                    tBoxEquipmentId.Focus();
                    return false;
                }
                if (string.IsNullOrWhiteSpace(tBoxName.Text))
                {
                    //new MessageDialog("请填写名称").ShowDialog();
                    ShowTip.ShowNotice("请填写名称", InfoType.Warning);
                    tBoxName.Focus();
                    return false;
                }
                if (cBoxCommunicateType.SelectedValue == null)
                {
                    //new MessageDialog("请选通讯方式").ShowDialog();
                    ShowTip.ShowNotice("请选通讯方式", InfoType.Warning);
                    tBoxName.Focus();
                    cBoxCommunicateType.Focus();
                    return false;
                }
 
                if (string.IsNullOrWhiteSpace(tBoxIp.Text))
                {
                    //new MessageDialog("请填写IP").ShowDialog();
                    ShowTip.ShowNotice("请填写IP", InfoType.Warning);
                    tBoxIp.Focus();
                    return false;
                }
 
                if (!ValidateData.IsIp(tBoxIp.Text))
                {
                    //new MessageDialog("请填写正确的IP").ShowDialog();
                    ShowTip.ShowNotice("请填写正确的IP", InfoType.Warning);
                    tBoxIp.Focus();
                    return false;
                }
 
                if (string.IsNullOrWhiteSpace(tBoxPort.Text))
                {
                    //new MessageDialog("请填写端口").ShowDialog();
                    ShowTip.ShowNotice("请填写端口", InfoType.Warning);
                    tBoxPort.Focus();
                    return false;
                }
 
                if (!ValidateData.IsNumber(tBoxPort.Text))
                {
                    //new MessageDialog("请填写正确的端口").ShowDialog();
                    ShowTip.ShowNotice("请填写正确的端口", InfoType.Warning);
                    tBoxPort.Focus();
                    return false;
                }
 
                if (string.IsNullOrWhiteSpace(ImgSrc))
                {
                    //new MessageDialog("请选择图片").ShowDialog();
                    ShowTip.ShowNotice("请选择图片", InfoType.Warning);
                    return false;
                }
 
 
                //if (groupBox_serialPort.Visible)
                //{
                //    if (string.IsNullOrWhiteSpace(uiTextBox_PortName.Text))
                //    {
                //        ShowWarningTip("请填写端口名称");
                //        uiTextBox_PortName.Focus();
                //        return false;
                //    }
 
                //    if (string.IsNullOrWhiteSpace(uiTextBox_BaudRate.Text))
                //    {
                //        ShowWarningTip("请填写波特率");
                //        uiTextBox_BaudRate.Focus();
                //        return false;
                //    }
 
                //    if (string.IsNullOrWhiteSpace(uiTextBox_DataBits.Text))
                //    {
                //        ShowWarningTip("请填写数据位长度");
                //        uiTextBox_DataBits.Focus();
                //        return false;
                //    }
                //}
            }
            catch(Exception ex)
            {
                LoggerHelper.ErrorLog("CheckData error", ex);
            }
 
            return true;
        }
        #endregion
 
        #region 拖动窗体
        /// <summary>
        /// 拖动窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                this.DragMove();
            }
        }
        #endregion
 
        #region 关闭页面
        /// <summary>
        /// 关闭页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
 
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        #endregion
 
        #region ESC关闭画面
        /// <summary>
        /// ESC关闭画面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                this.Close();
            }
        }
        #endregion
    }
}