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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ZigBee.Device;
 
namespace Shared.Phone.UserCenter.Device
{
    /// <summary>
    /// 编辑要添加到网关的设备的信息(这里修改的是MAC名,这个画面会更改MAC的物理名字)
    /// </summary>
    public class DeviceMacInfoAddForm : UserCenterCommonForm
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 设备对象
        /// </summary>
        private List<CommonDevice> listNewDevice = null;
        /// <summary>
        /// 保存默认名字(主要针对没有修改名字的时候,必须要给一个默认名字给网关)
        /// </summary>
        private string saveDefultName = string.Empty;
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        /// <param name="listdevices">设备列表</param>
        public void ShowForm(List<CommonDevice> listdevices)
        {
            this.listNewDevice = listdevices;
 
            //设置标题信息
            base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uDeviceInfo));
 
            //初始化中部控件
            this.InitMiddleFrame();
 
            //设置全部回路的默认名字
            this.SetAllEpointName();
        }
 
        /// <summary>
        /// 初始化中部控件
        /// </summary>
        private void InitMiddleFrame()
        {
            this.bodyFrameLayout.RemoveAll();
 
            //进度条一样的东西,没什么逻辑
            var line = new ProgressLine();
            bodyFrameLayout.AddChidren(line);
            line.SetValue(80);
 
            //设备图片
            var btnpictrue = new PicViewControl(510, 320);
            btnpictrue.Gravity = Gravity.CenterHorizontal;
            btnpictrue.Y = Application.GetRealHeight(100);
            Common.LocalDevice.Current.SetRealDeviceIconToControl(btnpictrue, listNewDevice);
            bodyFrameLayout.AddChidren(btnpictrue);
 
            //下一步
            var btnNext = new BottomClickButton();
            btnNext.TextID = R.MyInternationalizationString.uNextway;
            bodyFrameLayout.AddChidren(btnNext);
 
            var listview = new VerticalScrolViewLayout();
            listview.Y = btnpictrue.Bottom + Application.GetRealHeight(100);
            listview.Height = btnNext.Y - btnpictrue.Bottom - Application.GetRealHeight(200);
            bodyFrameLayout.AddChidren(listview);
 
            //生产商名称
            var row = new RowLayout();
            row.Height = ControlCommonResourse.ListViewRowHeight;
            listview.AddChidren(row);
            var btnManuView = new RowTopGrayView(false);
            btnManuView.TextID = R.MyInternationalizationString.uManufacturerName;
            row.AddChidren(btnManuView);
            var btnManu = new RowBottomBlackView(false);
            btnManu.Text = listNewDevice[0].ManufacturerName;
            row.AddChidren(btnManu);
 
            //设备型号
            row = new RowLayout();
            row.Height = ControlCommonResourse.ListViewRowHeight;
            listview.AddChidren(row);
            var btnModelIdView = new RowTopGrayView(false);
            btnModelIdView.TextID = R.MyInternationalizationString.uModelIdentifier;
            row.AddChidren(btnModelIdView);
            var btnModelId = new RowBottomBlackView(false);
            btnModelId.Text = listNewDevice[0].ModelIdentifier;
            row.AddChidren(btnModelId);
 
            //序列号
            row = new RowLayout();
            row.Height = ControlCommonResourse.ListViewRowHeight;
            listview.AddChidren(row);
            var btnSerialView = new RowTopGrayView(false);
            btnSerialView.TextID = R.MyInternationalizationString.uSerialNumber;
            row.AddChidren(btnSerialView);
            var btnSerial = new RowBottomBlackView(false);
            btnSerial.Text = listNewDevice[0].SerialNumber;
            if (string.IsNullOrEmpty(btnSerial.Text) == true)
            {
                btnSerial.Text = listNewDevice[0].DeviceAddr;
            }
            row.AddChidren(btnSerial);
 
            //产品名称
            string caption = Language.StringByID(R.MyInternationalizationString.uProductName);
            string nameValue = Common.LocalDevice.Current.GetDeviceMacName(listNewDevice[0]);
            this.saveDefultName = nameValue;
 
            var btnDeviceView = new EditorNameValueRow(caption, nameValue);
            listview.AddChidren(btnDeviceView);
            btnDeviceView.InitControl();
            //请输入产品名称
            btnDeviceView.SetEmptyNameTip(Language.StringByID(R.MyInternationalizationString.uProductNameMastInput));
            //编辑产品名称
            btnDeviceView.SetDialogTitle(Language.StringByID(R.MyInternationalizationString.uEditorProductName));
            btnDeviceView.ActionNameChangedEvent += (deviceName) =>
            {
                //设备重命名
                this.DeviceReName(deviceName);
            };
 
            //只有是河东的设备的时候
            if (Common.LocalDevice.Current.IsHdlDevice(listNewDevice[0]) == true)
            {
                //添加【背光设置】行
                this.AddBackLightRow(listview);
            }
 
            //下一步
            btnNext.MouseUpEventHandler += (sender, e) =>
            {
                //网关成功添加设备的显示画面
                var form = new AddDeviceSuccessForm();
                base.AddFromAndRemoveNowForm(form, listNewDevice);
            };
        }
 
        #endregion
 
        #region ■ 背光设置___________________________
 
        /// <summary>
        /// 添加【背光设置】行
        /// </summary>
        /// <param name="listview"></param>
        private void AddBackLightRow(VerticalScrolViewLayout listview)
        {
            bool canAddRow = false;
            foreach (var device in listNewDevice)
            {
                if (device.Type == DeviceType.OnOffSwitch)
                {
                    canAddRow = true;
                    break;
                }
            }
            if (canAddRow == false)
            {
                //必须有一路回路是OnOffSwitch才行
                return;
            }
 
            //获取这一堆设备时属于什么类型的
            var myDeviceType = Common.LocalDevice.Current.GetMyDeviceEnumInfo(listNewDevice);
            //如果是控制面板
            if (myDeviceType.BeloneType == DeviceBeloneType.A按键面板)
            {
                //背光设置
                var statuRowLight = new StatuRowLayout(listview);
 
                var btnBackLight = new RowCenterView(false);
                btnBackLight.TextID = R.MyInternationalizationString.uBackLightSettion;
                statuRowLight.AddChidren(btnBackLight);
                //背光设置向右的图标
                statuRowLight.AddRightIconControl();
                statuRowLight.MouseUpEvent += (sender, e) =>
                {
                    var form = new DevicePanel.PanelBackLightSettionForm();
                    this.AddForm(form, listNewDevice);
                };
            }
        }
 
        #endregion
 
        #region ■ 修改名字___________________________
 
        /// <summary>
        /// 设备重命名然后打开新的画面
        /// </summary>
        /// <param name="i_deviceName">deviceName.</param>
        /// <param name="mode">是否显示错误</param>
        private async void DeviceReName(string i_deviceName, ShowErrorMode mode = ShowErrorMode.YES)
        {
            //修改MAC名
            string deviceName = i_deviceName.Trim();
            var result = await Common.LocalDevice.Current.ReMacName(this.listNewDevice, deviceName, mode);
            if (result == false)
            {
                return;
            }
            //更改过一次之后,则界面关闭后不再更改
            this.saveDefultName = string.Empty;
 
            //如果它的回路只有一个的话,则在设备上报的时候,修改MAC名字之后,连同端点名字也一起修改
            if (this.listNewDevice.Count == 1)
            {
                //修改端点名字
                result = await Common.LocalDevice.Current.ReName(this.listNewDevice[0], deviceName, mode);
                if (result == false)
                {
                    return;
                }
            }
        }
 
        /// <summary>
        /// 设置全部回路的默认名字
        /// </summary>
        private async void SetAllEpointName()
        {
            if (this.listNewDevice.Count <= 1)
            {
                return;
            }
            var tempValue = Common.LocalDevice.Current.GetDeviceObjectText(this.listNewDevice);
            var arry = tempValue.Split(new string[] { "(" }, StringSplitOptions.RemoveEmptyEntries);
            var objName = arry[0].Trim();
 
            foreach (var device in this.listNewDevice)
            {
                if (Common.LocalDevice.Current.GetSimpleEpointName(device) != string.Empty)
                {
                    continue;
                }
                //XXXXX(N回路)
                var epointName = objName + "(" + device.DeviceEpoint + Language.StringByID(R.MyInternationalizationString.uDeviceCircuit) + ")";
                await Common.LocalDevice.Current.ReName(device, epointName, ShowErrorMode.NO);
            }
        }
 
        #endregion
 
        #region ■ 画面关闭___________________________
 
        /// <summary>
        /// 画面关闭
        /// </summary>
        /// <param name="isCloseForm">是否关闭界面,false的时候,只会调用关闭函数里面的附加功能</param>
        public override void CloseForm(bool isCloseForm = true)
        {
            if (this.saveDefultName != string.Empty)
            {
                //必须给它一个默认名字才行
                this.DeviceReName(this.saveDefultName, ShowErrorMode.NO);
            }
            //搜到设备之后,不管之后怎么处理,先添加到主界面的列表及缓存中
            this.LoadFormMethodByName("DeviceManagementMainForm", "AddDeviceToFormTable", listNewDevice);
            base.CloseForm(isCloseForm);
        }
        #endregion
    }
}