黄学彪
2019-11-25 5727cf0b9b54da0a191dd1e23cb5abf21320fbff
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
using System;
using System.Collections.Generic;
using Shared.Common;
using Shared.Phone.Device.Category;
using ZigBee.Device;
namespace Shared.Phone.Device.Room
{
    public class AddRoomContent:FrameLayout
    {
        #region ◆ 变量____________________________
        /// <summary>
        /// The current room.
        /// </summary>
        public static Shared.Common.Room CurrentRoom;
        /// <summary>
        /// The instance.
        /// </summary>
        public static AddRoomContent instance;
        /// <summary>
        /// The function scene body view.
        /// </summary>
        public FrameLayout FunctionSceneBodyView;
        /// <summary>
        /// 功能
        /// </summary>
        public Button FunctionBtn;
        /// <summary>
        /// 场景
        /// </summary>
        public Button SceneBtn;
        /// <summary>
        /// The add function or scence button.
        /// </summary>
        public Button AddFunctionOrScenceBtn;
        /// <summary>
        /// 为空时添加
        /// </summary>
        public Button NoFunction;
        /// <summary>
        /// The no function tip.
        /// </summary>
        public Button NoFunctionTip;
        /// <summary>
        /// The no scence.
        /// </summary>
        public Button NoScence;
        /// <summary>
        /// The no scence tip.
        /// </summary>
        public Button NoScenceTip;
 
        #endregion
 
        #region ◆ 构造方法_________________________
        //添加或者修改房间设备、场景
        public AddRoomContent()
        {
            BackgroundColor = ZigbeeColor.Current.GXCBackgroundColor;
            instance = this;
        }
 
        public override void RemoveFromParent()
        {
            base.RemoveFromParent();
        }
        #endregion
 
        #region ◆ 显示视图_________________________
        /// <summary>
        /// 显示界面
        /// </summary>
        /// <param name="room">Room.</param>
        public void Show(Shared.Common.Room room)
        {
            CurrentRoom = room;
            #region topview
            var topBGView = new FrameLayout()
            {
                Height = Application.GetRealHeight(CommonPage.Navigation_Height),
                BackgroundColor = ZigbeeColor.Current.GXCTopViewBackgroundColor
            };
            AddChidren(topBGView);
            var topView = new FrameLayout()
            {
                Y = Application.GetRealHeight(CommonPage.NavigationTitle_Y),
                Height = Application.GetRealHeight(CommonPage.Navigation_Height - CommonPage.NavigationTitle_Y),
                BackgroundColor = ZigbeeColor.Current.GXCTopViewBackgroundColor,
            };
            AddChidren(topView);
 
            var title = new Button()
            {
                TextAlignment = TextAlignment.Center,
                Text = room.Name,
                TextSize = 20,
                TextColor = ZigbeeColor.Current.GXCTextBlackColor,
                Width = Application.GetRealWidth(CommonPage.AppRealWidth - 500),
                Gravity = Gravity.CenterHorizontal
            };
            topView.AddChidren(title);
 
            var back = new Device.CommonForm.BackButton() { };
            topView.AddChidren(back);
            back.MouseUpEventHandler += (sender, e) =>
            {
                this.RemoveFromParent();
            };
 
            AddFunctionOrScenceBtn = new Device.CommonForm.SelectedStatuButton()
            {
                Width = Application.GetMinRealAverage(100),
                Height = Application.GetMinRealAverage(100),
                UnSelectedImagePath = "Item/Add.png",
                SelectedImagePath = "Item/AddSelected.png",
                X = Application.GetRealWidth(CommonPage.AppRealWidth - 150),
                Gravity = Gravity.CenterVertical
            };
            topView.AddChidren(AddFunctionOrScenceBtn);
            #endregion
 
            #region midFL
            var midFL = new FrameLayout()
            {
                Height = Application.GetRealHeight(CommonPage.AppRealHeight - CommonPage.Navigation_Height),
                Y = topView.Bottom,
                BackgroundColor = ZigbeeColor.Current.GXCBackgroundColor,
            };
            this.AddChidren(midFL);
 
            int functionScene_Width = CommonPage.AppRealWidth / 2;
            int functionScene_Height = 150;
            //功能
            FunctionBtn = new Button()
            {
                Width = Application.GetRealWidth(functionScene_Width),
                Height = Application.GetRealHeight(functionScene_Height),
                BackgroundColor = ZigbeeColor.Current.GXCButtonhardWhiteColor,
                SelectedBackgroundColor = ZigbeeColor.Current.GXCButtonhardWhiteColor,
                TextColor = ZigbeeColor.Current.GXCTextBlackColor,
                SelectedTextColor= ZigbeeColor.Current.GXCTextBlueColor,
                TextAlignment = TextAlignment.Center,
                TextSize = 16,
                TextID = R.MyInternationalizationString.Function,
                IsSelected=true
            };
            midFL.AddChidren(FunctionBtn);
            //场景
            SceneBtn = new Button()
            {
                X=FunctionBtn.Right,
                Width = Application.GetRealWidth(functionScene_Width),
                Height = Application.GetRealHeight(functionScene_Height),
                BackgroundColor = ZigbeeColor.Current.GXCButtonhardWhiteColor,
                SelectedBackgroundColor= ZigbeeColor.Current.GXCButtonhardWhiteColor,
                TextColor = ZigbeeColor.Current.GXCTextBlackColor,
                SelectedTextColor = ZigbeeColor.Current.GXCTextBlueColor,
                TextAlignment = TextAlignment.Center,
                TextSize = 16,
                TextID = R.MyInternationalizationString.Scence,
                IsSelected=false
            };
            midFL.AddChidren(SceneBtn);
            var line = new Button()
            {
                Y=FunctionBtn.Bottom,
                Height= CommonPage.LineHeight,
                Width= Application.GetRealWidth(functionScene_Width),
                BackgroundColor=ZigbeeColor.Current.GXCButtonBlueColor
            };
            midFL.AddChidren(line);
            //功能和场景bodyView
            FunctionSceneBodyView = new FrameLayout()
            {
                Y=line.Bottom,
                Height=midFL.Height-line.Bottom,
                BackgroundColor=ZigbeeColor.Current.GXCBackgroundColor
            };
            midFL.AddChidren(FunctionSceneBodyView);
            #endregion
 
            #region event
 
            AddFunctionOrScenceBtn.MouseUpEventHandler += AddFunctionOrScenceHandler;
            //选择功能分栏
            FunctionBtn.MouseUpEventHandler += (sender, e) =>
            {
                SceneBtn.IsSelected = false;
                FunctionBtn.IsSelected = true;
                line.X = FunctionBtn.X;
                RefreshBodyView();
            };
            //选择场景分栏
            SceneBtn.MouseUpEventHandler += (sender, e) =>
            {
                FunctionBtn.IsSelected = false;
                SceneBtn.IsSelected = true;
                line.X = SceneBtn.X;
                RefreshBodyView();
            };
            RefreshBodyView();
            #endregion
        }
 
        #endregion
 
        #region ◆ 添加____________________________
        /// <summary>
        /// 添加功能或者场景
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">E.</param>
        private void AddFunctionOrScenceHandler(object sender, MouseEventArgs e)
        {
            if (CurrentRoom.IsSharedRoom)
            {
                RoomCommon.ShowTipRoomIsShared();
                return;
            }
 
            if (FunctionBtn.IsSelected)
            {
                //添加功能
                var addDeviceList = new AddDeviceList();
                UserView.HomePage.Instance.AddChidren(addDeviceList);
                UserView.HomePage.Instance.PageIndex += 1;
                addDeviceList.Show(CurrentRoom);
            }
            else
            {
                //添加场景
                var addScene = new AddRoomScene();
                AddRoomScene.SceneText = "";
                AddRoomScene.ImagePath = "Scene/Scene0.png";
                UserView.HomePage.Instance.AddChidren(addScene);
                UserView.HomePage.Instance.PageIndex += 1;
                AddRoomScene.sceneTargetDevicesList?.Clear();
                addScene.Show(CurrentRoom);
            }
        }
 
        #endregion
 
        #region ◆ 刷新____________________________
 
        /// <summary>
        /// 刷新
        /// </summary>
        public void RefreshBodyView()
        {
            FunctionSceneBodyView.RemoveAll();
            if (FunctionBtn.IsSelected)
            {
                //选择功能
                var filePathList = CurrentRoom.DeviceUIFilePathList;
                if (filePathList.Count == 0)
                {
                    NoFunction = new Button()
                    {
                        Width = Application.GetMinRealAverage(220),
                        Height = Application.GetMinRealAverage(220),
                        UnSelectedImagePath = "Room/AddScenceFunction.png",
                        Gravity = Gravity.Center
                    };
                    FunctionSceneBodyView.AddChidren(NoFunction);
                    NoFunctionTip = new Button()
                    {
                        Y = NoFunction.Bottom,
                        Height = Application.GetMinRealAverage(200),
                        TextID = R.MyInternationalizationString.AddFunction,
                        TextColor = ZigbeeColor.Current.GXCTextBlackColor,
                        TextSize = 15,
                        TextAlignment = TextAlignment.Center
                    };
                    FunctionSceneBodyView.AddChidren(NoFunctionTip);
                    //添加功能或者场景
                    NoFunction.MouseUpEventHandler += AddFunctionOrScenceHandler;
                    NoFunctionTip.MouseUpEventHandler += AddFunctionOrScenceHandler;
                }
                else
                {
                    var dlistScrowView = new VerticalScrolViewLayout()
                    {
                    };
                    FunctionSceneBodyView.AddChidren(dlistScrowView);
 
                    foreach (var filePath in filePathList)
                    {
                        var device = Newtonsoft.Json.JsonConvert.DeserializeObject<DeviceUI>(System.Text.Encoding.UTF8.GetString(Global.ReadFileByHomeId(filePath)));
                        if (device == null || device.CommonDevice == null)
                        {
                            continue;
                        }
                        var deviceItemFL = new RowLayout()
                        {
                            Height = Application.GetRealHeight(170)
                        };
                        dlistScrowView.AddChidren(deviceItemFL);
                        var deviceIMG = new Button()
                        {
                            X = Application.GetRealWidth(50),
                            Width = Application.GetMinRealAverage(110),
                            Height = Application.GetMinRealAverage(110),
                            UnSelectedImagePath = device.IconPath,
                            SelectedImagePath = device.OnlineIconPath,
                            IsSelected = device.CommonDevice.IsOnline == 1,
                            Gravity = Gravity.CenterVertical
                        };
                        deviceItemFL.AddChidren(deviceIMG);
                        var deviceName = new Button()
                        {
                            X = deviceIMG.Right + Application.GetRealWidth(50),
                            Width = Application.GetRealWidth(700),
                            Height = Application.GetRealHeight(110),
                            TextColor = ZigbeeColor.Current.GXCTextBlackColor,
                            SelectedTextColor = ZigbeeColor.Current.GXCButtonBlueColor,
                            TextAlignment = TextAlignment.CenterLeft,
                            TextSize = 16,
                            Text = device.CommonDevice.DeviceEpointName,
                            Gravity = Gravity.CenterVertical
                        };
                        deviceItemFL.AddChidren(deviceName);
                        var deviceRight = new Button()
                        {
                            X = Application.GetRealWidth(1080 - 150),
                            Width = Application.GetMinRealAverage(110),
                            Height = Application.GetMinRealAverage(110),
                            UnSelectedImagePath = "Item/Next.png",
                            Gravity = Gravity.CenterVertical
                        };
                        //deviceItemFL.AddChidren(deviceRight);
                        var deviceLine = new Button()
                        {
                            Y = Application.GetRealHeight(170) - 1,
                            Height = 1,
                            BackgroundColor = ZigbeeColor.Current.GXCLineColor,
                            Tag = device
                        };
                        deviceItemFL.AddChidren(deviceLine);
 
                        var delBtn = new Button()
                        {
                            BackgroundColor = ZigbeeColor.Current.GXCRedColor,
                            TextID = R.MyInternationalizationString.Delete,
                            TextColor = ZigbeeColor.Current.GXCTextWhiteColor
                        };
                        if (!CurrentRoom.IsSharedRoom)
                        {
                            deviceItemFL.AddRightView(delBtn);
                        }
 
                        delBtn.MouseUpEventHandler += (send, ee) =>
                        {
                            var alert = new Alert(Language.StringByID(R.MyInternationalizationString.TIP), Language.StringByID(R.MyInternationalizationString.ConfirmDelete), Language.StringByID(R.MyInternationalizationString.Cancel), Language.StringByID(R.MyInternationalizationString.Confrim));
                            alert.Show();
                            alert.ResultEventHandler += (send1, e1) =>
                            {
                                if (e1)
                                {
                                    CurrentRoom.DeleteDevice(device.FileName);
                                    Show(CurrentRoom);
                                }
                            };
                        };
                    }
                }
            }
            else
            {
                //选择场景
                var sceneList = CurrentRoom.SceneUIList;
                if (sceneList.Count == 0)
                {
                    NoScence = new Button()
                    {
                        Width = Application.GetMinRealAverage(220),
                        Height = Application.GetMinRealAverage(220),
                        UnSelectedImagePath = "Room/AddScenceFunction.png",
                        Gravity = Gravity.Center
                    };
                    FunctionSceneBodyView.AddChidren(NoScence);
                    NoScenceTip = new Button()
                    {
                        Y = NoScence.Bottom,
                        Height = Application.GetMinRealAverage(200),
                        TextID = R.MyInternationalizationString.AddScence,
                        TextColor = ZigbeeColor.Current.GXCTextBlackColor,
                        TextSize = 15,
                        TextAlignment = TextAlignment.Center
                    };
                    FunctionSceneBodyView.AddChidren(NoScenceTip);
                    //添加功能或者场景
                    NoScence.MouseUpEventHandler += AddFunctionOrScenceHandler;
                    NoScenceTip.MouseUpEventHandler += AddFunctionOrScenceHandler;
                }
                else
                {
                    var sceneScrolView = new VerticalScrolViewLayout { };
                    FunctionSceneBodyView.AddChidren(sceneScrolView);
                    foreach (var scene in sceneList)
                    {
                        if (scene == null)
                        {
                            continue;
                        }
                        var sceneFL = new FrameLayout()
                        {
                            Width = Application.GetRealWidth(1080 - 100),
                            Height = Application.GetRealHeight(400),
                            Tag = scene,
                            Gravity = Gravity.CenterHorizontal
                        };
                        sceneScrolView.AddChidren(sceneFL);
                        var sceneRowLayout = new RowLayout()
                        {
                            Y = Application.GetRealHeight(30),
                            Height = Application.GetRealHeight(400 - 10 - 30),
                            LineColor = ZigbeeColor.Current.GXCBackgroundColor,
                            Radius = CommonPage.BigFormRadius
                        };
                        sceneFL.AddChidren(sceneRowLayout);
                        var sceneItemFL = new FrameLayout()
                        {
                            BackgroundColor = ZigbeeColor.Current.GXCBackgroundColor,
                            Radius = CommonPage.BigFormRadius
                        };
                        sceneRowLayout.AddChidren(sceneItemFL);
                        var sceneImg = new Button()
                        {
                            UnSelectedImagePath = scene.IconPath
                        };
                        sceneItemFL.AddChidren(sceneImg);
                        var hardBlackBG = new Button()
                        {
                            BackgroundColor = ZigbeeColor.Current.GXCBlack70Color,
                            SelectedBackgroundColor = ZigbeeColor.Current.GXCBlack80Color
                        };
                        sceneItemFL.AddChidren(hardBlackBG);
                        var sceneNameBtn = new Button()
                        {
                            Text = scene.Name,
                            TextColor = ZigbeeColor.Current.GXCTextWhiteColor
                        };
                        sceneItemFL.AddChidren(sceneNameBtn);
                        //删除
                        var deleteBtn = new CommonForm.RowLayoutDeleteButton
                        {
                            Tag = scene
                        };
                        if (!CurrentRoom.IsSharedRoom)
                        {
                            sceneRowLayout.AddRightView(deleteBtn);
                        }
 
                        EventHandler<MouseEventArgs> sceneDownHandler = (sender, e) =>
                        {
                            hardBlackBG.IsSelected = true;
                        };
                        sceneNameBtn.MouseDownEventHandler += sceneDownHandler;
                        hardBlackBG.MouseDownEventHandler += sceneDownHandler;
                        //调用场景
                        EventHandler<MouseEventArgs> sceneHandler = async (sender, e) =>
                        {
                            hardBlackBG.IsSelected = false;
                            //1成功 0失败
                            //后面补上相关提示
                            var sceneOpenAllData = await ZigBee.Device.Scene.ControlSceneAsync(scene.Id, 0);
                            //主网关不在线
                            if (sceneOpenAllData == null)
                            {
                                //CommonPage.Instance.ShowErrorInfoAlert(R.MyInternationalizationString.TheMainGatewayIsNotOnLine);
                                CommonPage.Instance.FailureToServer();
                                return;
                            }
                            if (sceneOpenAllData.sceneOpenData == null)
                            {
                                CommonPage.Instance.ShowErrorInfoAlert(R.MyInternationalizationString.FailedPleaseTryAgain);
                                return;
                            }
                            //成功
                            if (sceneOpenAllData.sceneOpenData.Result == 1)
                            {
 
                            }
                            //失败
                            else
                            {
                                CommonPage.Instance.ShowErrorInfoAlert(R.MyInternationalizationString.FailedPleaseTryAgain);
                                return;
                            }
                        };
                        sceneNameBtn.MouseUpEventHandler += sceneHandler;
                        hardBlackBG.MouseUpEventHandler += sceneHandler;
 
                        //删除场景
                        EventHandler<MouseEventArgs> delEvent = (sender, e) =>
                        {
                            var alert = new Alert(Language.StringByID(R.MyInternationalizationString.TIP), Language.StringByID(R.MyInternationalizationString.ConfirmDelete), Language.StringByID(R.MyInternationalizationString.Cancel), Language.StringByID(R.MyInternationalizationString.Confrim));
                            alert.Show();
                            alert.ResultEventHandler += async (send1, e1) =>
                            {
                                if (e1)
                                {
                                    //0 移除失败 1 移除成功 2 没有该场景
                                    var removeSceneAllData = await ZigBee.Device.Scene.DeleteSceneAsync(scene.Id);
                                    if (removeSceneAllData == null || removeSceneAllData.removeSceneData == null)
                                    {
                                        return;
                                    }
                                    if (removeSceneAllData.removeSceneData.Result == 1)
                                    {
                                        //1成功
                                        CurrentRoom.RemoveScene(scene);
                                        //RefreshBodyView();
                                        sceneScrolView.RemoveViewByTag((sender as Button).Tag);
                                    }
                                    else if (removeSceneAllData.removeSceneData.Result == 0)
                                    {
                                        //0 移除失败
                                        CommonPage.Instance.ShowErrorInfoAlert(R.MyInternationalizationString.FailedPleaseTryAgain);
                                        return;
                                    }
                                    else if (removeSceneAllData.removeSceneData.Result == 2)
                                    {
                                        //2 没有该场景
                                        CurrentRoom.RemoveScene(scene);
                                        //RefreshBodyView();
                                        sceneScrolView.RemoveViewByTag((sender as Button).Tag);
                                        CommonPage.Instance.ShowErrorInfoAlert(R.MyInternationalizationString.TheSceneIsNull);
                                    }
                                }
                            };
                        };
                        deleteBtn.MouseUpEventHandler += delEvent;
                    }
                }
            }
        }
 
        #endregion
    }
}