黄学彪
2020-12-16 0d9f64668fd7350d6a21fd157e32009a96d98134
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
using System;
using System.Collections.Generic;
using ZigBee.Device;
 
namespace Shared.Phone.UserCenter.Device
{
    /// <summary>
    /// 网关成功添加设备的显示画面
    /// </summary>
    public class DeviceAddSuccessForm : EditorCommonForm
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 新上报的设备
        /// </summary>
        private List<CommonDevice> listNewDevice = null;
        /// <summary>
        /// 列表控件
        /// </summary>
        private VerticalListControl listview = null;
        /// <summary>
        /// 设备备注控件
        /// </summary>
        private FrameCaptionInputControl btnNote = null;
        /// <summary>
        /// 保存默认名字(主要针对没有修改名字的时候,必须要给一个默认名字给网关)
        /// </summary>
        private string saveDefultName = string.Empty;
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        /// <param name="deviceMac">设备Mac地址</param>
        public void ShowForm(string deviceMac)
        {
            this.listNewDevice = HdlDeviceCommonLogic.Current.GetDevicesByMac(deviceMac);
            if (listNewDevice.Count == 0)
            {
                //对应单纯只有200回路的设备
                listNewDevice.Add(HdlDeviceCommonLogic.Current.GetOTADevice(deviceMac));
            }
 
            //添加新设备的话,主页需要重新刷新
            UserView.UserPage.Instance.RefreshAllForm = true;
 
            //设置标题信息
            base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uAddDevice));
 
            //初始化中部控件
            this.InitMiddleFrame();
 
            //设置全部回路的默认名字
            this.SetAllEpointName();
            // 设置网关所在的时区的时间给门锁时间
            this.SetDoorTime();
            //新入网的设备,清空掉它的房间
            this.ClearDeviceFromRoom();
            //添加设备的时候,关闭左划菜单的未分配界面
            MainPage.LeftListRoomViewFrom.Instance?.CloseUnallocatedRoomForm();
        }
 
        /// <summary>
        /// 初始化中部控件
        /// </summary>
        private void InitMiddleFrame()
        {
            //清空bodyFrame
            this.ClearBodyFrame();
 
            //初始化桌布
            this.InitFrameTable();
 
            var framePic = new FrameLayout();
            framePic.Width = Application.GetRealWidth(855);
            framePic.Height = Application.GetRealHeight(570);
            framePic.Gravity = Gravity.CenterHorizontal;
            framePic.Y = Application.GetRealHeight(58);
            bodyFrameLayout.AddChidren(framePic);
 
            var imageFile = HdlDeviceCommonLogic.Current.GetRealDeviceIcon(listNewDevice);
            string fullName = IO.FileUtils.GetImageFilePath(imageFile);
 
#if Android
            //设备图片
            var bim = Android.Graphics.BitmapFactory.DecodeFile(fullName);
            var btnpictrue = new PicViewControl(bim.Width, bim.Height);
#endif
#if iOS
            //设备图片
            var bim = UIKit.UIImage.FromFile(fullName);
            var btnpictrue = new PicViewControl((int)bim.Size.Width, (int)bim.Size.Height);
#endif
            bim.Dispose();
            bim = null;
 
            btnpictrue.Gravity = Gravity.Center;
            btnpictrue.UnSelectedImagePath = imageFile;
            framePic.AddChidren(btnpictrue);
 
            string nameValue = HdlDeviceCommonLogic.Current.GetDeviceMacName(listNewDevice[0]);
            this.saveDefultName = nameValue;
 
            //设备备注
            string caption = Language.StringByID(R.MyInternationalizationString.uDeviceNote);
            string deviceName = HdlDeviceCommonLogic.Current.GetDeviceMacName(listNewDevice[0]);
            this.btnNote = new FrameCaptionInputControl(caption, deviceName, listview.rowSpace / 2);
            this.btnNote.txtInput.MaxByte = 48;//限制只能输入48个字节
            listview.AddChidren(btnNote);
            btnNote.InitControl();
            //划线
            btnNote.AddBottomLine();
            btnNote.txtInput.FinishInputEvent += () =>
            {
                if (btnNote.Text.Trim() == string.Empty)
                {
                    btnNote.Text = HdlDeviceCommonLogic.Current.GetDeviceMacName(listNewDevice[0]);
                    return;
                }
                //修改名字
                this.DeviceReName(btnNote.Text.Trim());
            };
 
            //设备类型
            caption = Language.StringByID(R.MyInternationalizationString.uDeviceType);
            deviceName = HdlDeviceCommonLogic.Current.GetDeviceObjectText(listNewDevice);
            var btnType = new FrameCaptionViewControl(caption, deviceName, listview.rowSpace / 2);
            btnType.UseClickStatu = false;
            listview.AddChidren(btnType);
            btnType.InitControl();
            //划线
            btnType.AddBottomLine();
 
            //安装位置
            var rowBeloneArea = new BelongAreaControl(listview.rowSpace / 2);
            listview.AddChidren(rowBeloneArea);
            rowBeloneArea.InitControl(Language.StringByID(R.MyInternationalizationString.uInstallationLocation), this.listNewDevice);
            //底线
            rowBeloneArea.AddBottomLine();
 
            var listCheck = new List<string>();
            rowBeloneArea.SelectRoomEvent += (roomKeys) =>
            {
                //选择未分配时,清空
                if (roomKeys == string.Empty) { listCheck = new List<string>(); }
                foreach (var device in this.listNewDevice)
                {
                    if (device is OTADevice)
                    {
                        continue;
                    }
                    if (roomKeys == string.Empty)
                    {
                        //如果选择的是未分配,则它的全部回路无条件全部清空房间
                        HdlRoomLogic.Current.ChangedRoom(device, roomKeys);
                        continue;
                    }
 
                    var room = HdlRoomLogic.Current.GetRoomByDevice(device);
                    string mainKeys = HdlDeviceCommonLogic.Current.GetDeviceMainKeys(device);
                    if (room == null)
                    {
                        //这里有点特殊,如果回路没有设置有区域的时候,才设置
                        listCheck.Add(mainKeys);
                        HdlRoomLogic.Current.ChangedRoom(device, roomKeys);
                    }
                    else if (listCheck.Contains(mainKeys) == true)
                    {
                        //如果这个回路之前都还没有区域,在本界面还没有关闭之前,可以无条件随便变更
                        HdlRoomLogic.Current.ChangedRoom(device, roomKeys);
                    }
                }
                //保存设备房间索引
                HdlRoomLogic.Current.SaveRealDeviceRoomId(this.listNewDevice, roomKeys);
            };
 
            //更多配置
            var btnHelp = new NormalViewControl(bodyFrameLayout.Width, Application.GetRealHeight(49), false);
            btnHelp.Y = Application.GetRealHeight(1388);
            btnHelp.TextSize = 12;
            btnHelp.TextAlignment = TextAlignment.Center;
            btnHelp.TextColor = UserCenterColor.Current.TextOrangeColor;
            btnHelp.Text = Language.StringByID(R.MyInternationalizationString.uMoreSettion);
            btnHelp.ButtonClickEvent += (sender, e) =>
            {
                //更改过一次之后,则界面关闭后不再更改
                this.saveDefultName = string.Empty;
                //改名字
                btnNote.txtInput.FinishInputEvent();
 
                this.CloseForm();
 
                var form = new DeviceMacInfoEditorForm();
                form.AddForm(this.listNewDevice[0].DeviceAddr);
            };
            bodyFrameLayout.AddChidren(btnHelp);
            //底线
            int lineWidth = btnHelp.GetRealWidthByText();
            var btnLine = new NormalViewControl(lineWidth, HdlControlResourse.BottomLineHeight, false);
            btnLine.BackgroundColor = UserCenterColor.Current.TextOrangeColor;
            btnLine.Gravity = Gravity.CenterHorizontal;
            btnLine.Y = btnHelp.Bottom - Application.GetRealHeight(8);
            bodyFrameLayout.AddChidren(btnLine);
        }
 
        /// <summary>
        /// 初始化桌布
        /// </summary>
        private void InitFrameTable()
        {
            //弧度的圆的一半的高度(固定)
            int halfRoundHeigth = Application.GetRealHeight(116) / 2;
            //弧度的圆
            var btnRound = new NormalViewControl(bodyFrameLayout.Width, halfRoundHeigth * 2, false);
            btnRound.Y = Application.GetRealHeight(708);
            btnRound.BackgroundColor = UserCenterColor.Current.White;
            btnRound.Radius = (uint)halfRoundHeigth;
            bodyFrameLayout.AddChidren(btnRound);
            //明细列表的桌布,白色背景(覆盖弧度的圆的半边)
            var detailBackFrame = new FrameLayout();
            detailBackFrame.Y = btnRound.Bottom - btnRound.Height / 2;
            detailBackFrame.Height = Application.GetRealHeight(1200);//高度就是要它超过
            detailBackFrame.BackgroundColor = UserCenterColor.Current.White;
            bodyFrameLayout.AddChidren(detailBackFrame);
 
            //设备编辑
            var btnTile = new NormalViewControl(800, 60, true);
            btnTile.X = HdlControlResourse.XXLeft;
            btnTile.TextSize = 15;
            btnTile.TextColor = UserCenterColor.Current.TextColor2;
            btnTile.TextID = R.MyInternationalizationString.uDeviceEditor;
            detailBackFrame.AddChidren(btnTile);
 
            this.listview = new VerticalListControl(12);
            listview.Y = btnTile.Bottom + Application.GetRealHeight(17);
            listview.Height = Application.GetRealHeight(522);
            detailBackFrame.AddChidren(listview);
 
            //完成
            var btnFinish = new BottomClickButton();
            btnFinish.Y = Application.GetRealHeight(736);
            btnFinish.TextID = R.MyInternationalizationString.uFinish;
            detailBackFrame.AddChidren(btnFinish);
            btnFinish.MouseUpEventHandler += (sender, e) =>
            {
                //更改过一次之后,则界面关闭后不再更改
                this.saveDefultName = string.Empty;
                //改名字
                btnNote.txtInput.FinishInputEvent();
                //关闭自身
                this.CloseForm();
            };
        }
 
        #endregion
 
        #region ■ 修改名字___________________________
 
        /// <summary>
        /// 设备重命名然后打开新的画面
        /// </summary>
        /// <param name="i_deviceName">deviceName.</param>
        /// <param name="mode">是否显示错误</param>
        private void DeviceReName(string i_deviceName, ShowErrorMode mode = ShowErrorMode.YES)
        {
            //修改MAC名
            string deviceName = i_deviceName.Trim();
            var result = HdlDeviceCommonLogic.Current.ReMacName(this.listNewDevice, deviceName, mode);
            if (result == false)
            {
                return;
            }
            //更改过一次之后,则界面关闭后不再更改
            this.saveDefultName = string.Empty;
        }
 
        /// <summary>
        /// 设置全部回路的默认名字
        /// </summary>
        private void SetAllEpointName()
        {
            foreach (var device in this.listNewDevice)
            {
                if ((device is OTADevice) || HdlDeviceCommonLogic.Current.GetSimpleEpointName(device) != string.Empty)
                {
                    //有名字不处理
                    continue;
                }
                //获取端点名字
                var epointName = HdlDeviceCommonLogic.Current.GetDeviceEpointName(device);
                HdlDeviceCommonLogic.Current.ReName(device, epointName, ShowErrorMode.NO);
            }
        }
 
        /// <summary>
        /// 设置网关所在的时区的时间给门锁时间
        /// </summary>
        private async void SetDoorTime()
        {
            //如果是门锁
            if (this.listNewDevice[0].Type == DeviceType.DoorLock)
            {
                var doorLock = this.listNewDevice[0] as ZigBee.Device.DoorLock;
                //1、入网后先获取门锁在网关中的时区时间
                var curDateTime = DoorLock.DoorLockCommonInfo.GetDoorLockZoneTime();
                //2、将时间设置到网关中
                var dateTime = new DateTime(curDateTime.Year, curDateTime.Month, curDateTime.Day, curDateTime.Hour, curDateTime.Minute, curDateTime.Second);
                var setTimestamp = DoorLock.DoorLockCommonInfo.GetUnixTimeStamp(dateTime);
                await doorLock.RectifyDoorLockTimeAsync(int.Parse(setTimestamp));
            }
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 新入网的设备,清空掉它的房间
        /// </summary>
        private void ClearDeviceFromRoom()
        {
            foreach (var device in this.listNewDevice)
            {
                HdlRoomLogic.Current.DeleteDevice(device);
                HdlRoomLogic.Current.DeleteLoveDevice(device);
            }
            HdlRoomLogic.Current.DeleteRealDeviceFromRoom(this.listNewDevice[0]);
        }
 
        #endregion
 
        #region ■ 画面关闭___________________________
 
        /// <summary>
        /// 画面关闭
        /// </summary>
        public override void CloseFormBefore()
        {
            if (this.saveDefultName != string.Empty)
            {
                //必须给它一个默认名字才行
                this.DeviceReName(this.saveDefultName, ShowErrorMode.NO);
            }
            //添加设备到列表
            this.LoadFormMethodByName("DeviceListMainForm", "AddDeviceToFormTable", listNewDevice[0].DeviceAddr);
 
            base.CloseFormBefore();
        }
        #endregion
    }
}