黄学彪
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
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
using Shared.Common;
using Shared.Phone.UserCenter;
using System;
using System.Collections.Generic;
using System.Globalization;
using ZigBee.Device;
 
namespace Shared.Phone.Category
{
    /// <summary>
    /// 添加或者编辑场景的界面
    /// </summary>
    public class AddOrEditorSceneForm : EditorCommonForm
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 场景变更事件(场景,房间ID)
        /// </summary>
        public Action<SceneUI, string> SceneChangedEvent = null;
        /// <summary>
        /// 修改的场景
        /// </summary>
        private SceneUI editorScene = null;
        /// <summary>
        /// 克隆的场景(临时编辑用)
        /// </summary>
        private SceneUI cloneScene = null;
        /// <summary>
        /// 场景当前的房间ID
        /// </summary>
        private string nowRoomId = string.Empty;
        /// <summary>
        /// 当前界面上显示的执行目标
        /// </summary>
        private List<Scene.DeviceListData> listAdjustTarget = null;
        /// <summary>
        /// 整个界面的上下滑动控件
        /// </summary>
        private VerticalFrameControl listBodyControl = null;
        /// <summary>
        /// 添加目标的的容器控件
        /// </summary>
        private FrameListControl frameTargetTableControl = null;
        /// <summary>
        /// 这个是加到【整个界面的上下滑动控件】中,促使界面能够滑动刚好超过【完成按钮】
        /// </summary>
        private FrameLayout frameBottomTemp = null;
        /// <summary>
        /// 场景图片显示控件(生成图片文件使用)
        /// </summary>
        private ImageView btnScenePic = null;
        /// <summary>
        /// 场景图片是否变更(针对编辑模式)
        /// </summary>
        private bool isScenePictrueChanged = false;
        /// <summary>
        /// 执行目标是否改变(针对编辑模式)
        /// </summary>
        private bool isAdjustTargetChanged = false;
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        /// <param name="i_editorScene">编辑的场景对象</param>
        public void ShowForm(SceneUI i_editorScene)
        {
            this.ScrollEnabled = false;
            this.editorScene = i_editorScene;
 
            //克隆一个临时编辑的变量
            this.cloneScene = new SceneUI();
            if (i_editorScene != null)
            {
                cloneScene.Id = i_editorScene.Id;
                cloneScene.Name = i_editorScene.Name;
                cloneScene.IconPath = i_editorScene.IconPath;
                cloneScene.IconPathType = i_editorScene.IconPathType;
                var room = HdlRoomLogic.Current.GetRoomBySceneId(i_editorScene.Id);
                if (room != null)
                {
                    //当前的房间
                    this.nowRoomId = room.Id;
                }
                //设置头部信息
                base.SetTitleText(Language.StringByID(R.MyInternationalizationString.EditorScene));
            }
            else
            {
                cloneScene.IconPath = "SceneIcon/0.png";
                listAdjustTarget = new List<Scene.DeviceListData>();
                //设置头部信息
                base.SetTitleText(Language.StringByID(R.MyInternationalizationString.AddScence));
            }
 
            //初始化中部信息
            this.InitMiddleFrame();
        }
 
        /// <summary>
        /// 初始化中部信息
        /// </summary>
        private void InitMiddleFrame()
        {
            //整个界面的上下滑动控件
            this.listBodyControl = new VerticalFrameControl();
            listBodyControl.Height = bodyFrameLayout.Height;
            bodyFrameLayout.AddChidren(listBodyControl);
 
            //初始化场景图片
            this.InitScenePictureControl();
 
            //初始化信息编辑控件
            this.InitInfoEditorControl();
 
            //初始化添加目标
            this.InitAddTargetControl();
 
            //完成
            var btnSave = new BottomClickButton();
            btnSave.TextID = R.MyInternationalizationString.uFinish;
            bodyFrameLayout.AddChidren(btnSave);
            btnSave.ButtonClickEvent += (sender, e) =>
            {
                //保存场景数据
                this.SaveSceneData();
            };
 
            //分享的场景不能保存
            if (this.editorScene != null && this.editorScene.IsSharedScene == true)
            {
                btnSave.CanClick = false;
            }
            else if (Config.Instance.Home.IsShowTemplate == true)
            {
                //模板场景不能保存
                btnSave.CanClick = false;
            }
            //如果是有模板,有设备的时候,在没有发送成功之前
            else if (Common.Config.Instance.Home.TemplateMode == 2
               && Common.Config.Instance.Home.SendTemplateSuccess == false)
            {
                btnSave.CanClick = false;
            }
        }
 
        #endregion
 
        #region ■ 初始化场景图片_____________________
 
        /// <summary>
        /// 初始化场景图片
        /// </summary>
        private void InitScenePictureControl()
        {
            //场景图片这部分的白色背景
            var framePicBack = new FrameLayout();
            framePicBack.Height = Application.GetRealHeight(559);
            framePicBack.BackgroundColor = UserCenterColor.Current.White;
            listBodyControl.frameTable.AddChidren(framePicBack);
 
            //场景图片底部阴影
            var btnShadow = new PicViewControl(916, 487);
            btnShadow.Y = Application.GetRealHeight(46);
            btnShadow.Gravity = Gravity.CenterHorizontal;
            btnShadow.UnSelectedImagePath = "Room/Room_Rectangle.png";
            framePicBack.AddChidren(btnShadow);
            //场景图片
            this.btnScenePic = new ImageView();
            btnScenePic.Y = Application.GetRealHeight(46);
            btnScenePic.Width = this.GetPictrueRealSize(887);
            btnScenePic.Height = this.GetPictrueRealSize(444);
            btnScenePic.Radius = (uint)Application.GetRealHeight(17);
            btnScenePic.Gravity = Gravity.CenterHorizontal;
            if (this.editorScene == null)
            {
                btnScenePic.ImagePath = "SceneIcon/0.png";
            }
            else if (this.editorScene.IconPathType == 0)
            {
                btnScenePic.ImagePath = this.editorScene.IconPath;
            }
            else
            {
                btnScenePic.ImageBytes = Global.ReadFileByHomeId(this.editorScene.IconPath);
            }
            framePicBack.AddChidren(btnScenePic);
            //图片遮罩
            var btnZhezhao = new FrameLayout();
            btnZhezhao.Width = btnScenePic.Width;
            btnZhezhao.Height = btnScenePic.Height;
            btnZhezhao.Y = btnScenePic.Y;
            btnZhezhao.Gravity = Gravity.CenterHorizontal;
            btnZhezhao.Radius = (uint)Application.GetRealHeight(17);
            btnZhezhao.BackgroundColor = UserCenterColor.Current.PictrueZhezhaoColor;
            framePicBack.AddChidren(btnZhezhao);
 
            //分享的场景不能编辑
            if (this.editorScene == null || this.editorScene.IsSharedScene == false)
            {
                if (Config.Instance.Home.IsShowTemplate == true)
                {
                    //模板场景不允许编辑
                    return;
                }
                btnZhezhao.MouseUpEventHandler += (sender, e) =>
                {
                    //房间图片选择
                    this.ScenePictrueSelect(btnScenePic);
                };
            }
        }
 
        #endregion
 
        #region ■ 初始化信息编辑_____________________
 
        /// <summary>
        /// 初始化信息编辑控件
        /// </summary>
        private void InitInfoEditorControl()
        {
            //白色背景框
            var frameBack = new FrameLayout();
            frameBack.Y = Application.GetRealHeight(582);
            frameBack.Height = Application.GetRealHeight(429);
            frameBack.BackgroundColor = UserCenterColor.Current.White;
            this.listBodyControl.frameTable.AddChidren(frameBack);
 
            //信息编辑
            var btnTitle = new NormalViewControl(300, 60, true);
            btnTitle.X = HdlControlResourse.XXLeft;
            btnTitle.Y = Application.GetRealHeight(46);
            btnTitle.TextSize = 15;
            btnTitle.TextID = R.MyInternationalizationString.uInfoEditor;
            btnTitle.TextColor = UserCenterColor.Current.TextColor2;
            frameBack.AddChidren(btnTitle);
 
            //场景名称
            var rowScene = new FrameCaptionInputControl(Language.StringByID(R.MyInternationalizationString.SceneName), this.cloneScene.Name);
            rowScene.txtInput.PlaceholderText = Language.StringByID(R.MyInternationalizationString.PleaseInputSceneName);
            rowScene.txtInput.MaxByte = 32;//限制只能输入32个字节
            rowScene.Y = btnTitle.Bottom + Application.GetRealHeight(23);
            frameBack.AddChidren(rowScene);
            rowScene.InitControl();
            //底线
            rowScene.AddBottomLine();
            rowScene.txtInput.TextChangeEventHandler += (sender, value) =>
            {
                this.cloneScene.Name = value;
            };
 
            //所属区域
            var rowBelong = new BelongAreaControl();
            rowBelong.Y = rowScene.Bottom + Application.GetRealHeight(14);
            frameBack.AddChidren(rowBelong);
            rowBelong.InitControl(Language.StringByID(R.MyInternationalizationString.uBelongArea), this.nowRoomId);
            //分享的场景不能编辑
            if (this.editorScene == null || this.editorScene.IsSharedScene == false)
            {
                rowBelong.SelectRoomEvent += (selectId) =>
                {
                    this.nowRoomId = selectId;
                };
            }
            if (Config.Instance.Home.IsShowTemplate == true)
            {
                //模板场景不允许编辑
                rowBelong.CanClick = false;
            }
        }
 
        #endregion
 
        #region ■ 初始化添加目标_____________________
 
        /// <summary>
        /// 初始化添加目标
        /// </summary>
        private void InitAddTargetControl()
        {
            if (this.frameTargetTableControl == null || this.frameTargetTableControl.Parent == null)
            {
                //添加目标的的容器控件
                this.frameTargetTableControl = new FrameListControl();
                frameTargetTableControl.Y = Application.GetRealHeight(1034);
                frameTargetTableControl.BackgroundColor = UserCenterColor.Current.White;
                frameTargetTableControl.Height = Application.GetRealHeight(346);
                this.listBodyControl.frameTable.AddChidren(frameTargetTableControl);
            }
            else
            {
                this.frameTargetTableControl?.RemoveAll();
                frameTargetTableControl.Height = Application.GetRealHeight(346);
            }
 
            //添加目标
            var rowAddTarget = new FrameRowControl(frameTargetTableControl.rowSpace / 2);
            rowAddTarget.UseClickStatu = false;
            rowAddTarget.Y = Application.GetRealHeight(46 - 12);
            frameTargetTableControl.AddChidren(rowAddTarget);
            rowAddTarget.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.AddScentTargetAction), 600);
            //+号
            var btnAddIcon = rowAddTarget.AddMostRightEmptyIcon(61, 58);
            btnAddIcon.UnSelectedImagePath = "Item/Add.png";
            //底线
            rowAddTarget.AddBottomLine();
            rowAddTarget.ButtonClickEvent += (sender, e) =>
            {
                //显示添加目标菜单
                this.ShowAddTargetMenu();
            };
            if (Config.Instance.Home.IsShowTemplate == true)
            {
                //模板场景不允许编辑
                rowAddTarget.CanClick = false;
            }
            //如果是有模板,有设备的时候,在没有发送成功之前
            if (Common.Config.Instance.Home.TemplateMode == 2
               && Common.Config.Instance.Home.SendTemplateSuccess == false)
            {
                rowAddTarget.CanClick = false;
            }
 
            HdlThreadLogic.Current.RunThread(() =>
            {
                //从网关获取场景的执行目标
                var result = this.GetSceneTargetList();
                if (result == true)
                {
                    //添加【执行目标行】
                    this.AddTargetRow();
                }
            });
        }
 
        #endregion
 
        #region ■ 获取及添加【目标行】_______________
 
        /// <summary>
        /// 添加【执行目标行】
        /// </summary>
        private void AddTargetRow()
        {
            //检测网关上的数据是否和本地一致
            for (int i = 0; i < this.listAdjustTarget.Count; i++)
            {
                if (listAdjustTarget[i].Type == 0)
                {
                    //设备
                    var device = HdlDeviceCommonLogic.Current.GetDevice(listAdjustTarget[i].DeviceAddr, listAdjustTarget[i].Epoint);
                    if (device == null)
                    {
                        //不显示这个不存在本地的设备
                        listAdjustTarget.RemoveAt(i);
                        i--;
                    }
                    continue;
                }
                else if (listAdjustTarget[i].Type == 1)
                {
                    //场景
                    var scene = HdlSceneLogic.Current.GetSceneUIBySceneId(listAdjustTarget[i].ElseScenesId);
                    if (scene == null)
                    {
                        //不显示这个不存在本地的设备
                        listAdjustTarget.RemoveAt(i);
                        i--;
                    }
                    continue;
                }
            }
 
            HdlThreadLogic.Current.RunMain(() =>
            {
                for (int i = 0; i < listAdjustTarget.Count; i++)
                {
                    if (listAdjustTarget[i].Type == 0)
                    {
                        //设备
                        this.AddDeviceTargetRow(listAdjustTarget[i], i, i != listAdjustTarget.Count - 1);
                    }
                    else if (listAdjustTarget[i].Type == 1)
                    {
                        //场景
                        this.AddSceneTargetRow(listAdjustTarget[i], i, i != listAdjustTarget.Count - 1);
                    }
                    else if (listAdjustTarget[i].Type == 2)
                    {
                        //延时
                        this.AddDelayTimeTargetRow(listAdjustTarget[i], i, i != listAdjustTarget.Count - 1);
                    }
                }
                //初始化促使界面能够刚好滑动超过保存按钮的控件
                this.InitFrameBottomTempControl();
 
                //调整执行目标的桌布高度
                this.AdjustTargetTableHeight();
            });
        }
 
        #endregion
 
        #region ■ 添加设备执行目标行_________________
 
        /// <summary>
        /// 添加设备执行目标行
        /// </summary>
        private void AddDeviceTargetRow(Scene.DeviceListData data, int index, bool addLine)
        {
            //设备
            var device = HdlDeviceCommonLogic.Current.GetDevice(data.DeviceAddr, data.Epoint);
            var rowDevice = new DeviceRoomControl(device, frameTargetTableControl.rowSpace / 2);
            rowDevice.MainKeys = index.ToString();
            this.frameTargetTableControl.AddChidren(rowDevice);
            //控件向右偏移
            rowDevice.frameTable.LeftOffset = Application.GetRealWidth(104) - HdlControlResourse.XXLeft;
            rowDevice.InitControl();
            rowDevice.frameTable.UseClickStatu = false;
 
            //状态文本
            string statuText2 = HdlSafeguardLogic.Current.GetAdjustTargetStatuText(data.TaskList);
            var btnStatu = rowDevice.frameTable.AddMostRightView(statuText2, 400);
            if (addLine == true)
            {
                //底线
                rowDevice.frameTable.AddBottomLine();
            }
            //分享的场景不能编辑
            if (this.editorScene == null || this.editorScene.IsSharedScene == false)
            {
                if (Config.Instance.Home.IsShowTemplate == true)
                {
                    //模板场景不允许编辑
                    return;
                }
                //编辑
                var btnEditor = rowDevice.AddEditorControl();
                btnEditor.ButtonClickEvent += (sender, e) =>
                {
                    if (device.Type == DeviceType.DimmableLight//调光器
                      || device.Type == DeviceType.ColorDimmableLight)//彩灯
                    {
                        var form = new UserCenter.Safety.AlarmTargetStatuSelectLightForm();
                        form.AddForm(device, data.TaskList);
                        form.FinishSelectEvent += (statuText, listInfo) =>
                        {
                            //执行目标已经改变
                            this.isAdjustTargetChanged = true;
 
                            if (listInfo.Count == 0) { statuText = string.Empty; }
                            btnStatu.Text = statuText;
                            //将新的执行目标添加入缓存
                            data.TaskList.Clear();
                            data.TaskList.AddRange(listInfo);
                        };
                    }
                    else if (device.Type == DeviceType.Thermostat)//空调
                    {
                        var form = new UserCenter.Safety.AlarmTargetStatuSelectAcForm();
                        form.AddForm(device, data.TaskList);
                        form.FinishSelectEvent += (statuText, listInfo) =>
                        {
                            //执行目标已经改变
                            this.isAdjustTargetChanged = true;
 
                            if (listInfo.Count == 0) { statuText = string.Empty; }
                            btnStatu.Text = statuText;
                            //将新的执行目标添加入缓存
                            data.TaskList.Clear();
                            data.TaskList.AddRange(listInfo);
                        };
                    }
                    else if (device.Type == DeviceType.WindowCoveringDevice)//窗帘
                    {
                        var form = new UserCenter.Safety.AlarmTargetStatuSelectCurtainForm();
                        form.AddForm(device, data.TaskList);
                        form.FinishSelectEvent += (statuText, listInfo) =>
                        {
                            //执行目标已经改变
                            this.isAdjustTargetChanged = true;
 
                            if (listInfo.Count == 0) { statuText = string.Empty; }
                            btnStatu.Text = statuText;
                            //将新的执行目标添加入缓存
                            data.TaskList.Clear();
                            data.TaskList.AddRange(listInfo);
                        };
                    }
                    else
                    {
                        //其他直接归为开关类
                        var form = new UserCenter.Safety.AlarmTargetStatuSelectSwitchForm();
                        form.AddForm(device, data.TaskList);
                        form.FinishSelectEvent += (statuText, listInfo) =>
                        {
                            //执行目标已经改变
                            this.isAdjustTargetChanged = true;
 
                            if (listInfo.Count == 0) { statuText = string.Empty; }
                            btnStatu.Text = statuText;
                            //将新的执行目标添加入缓存
                            data.TaskList.Clear();
                            data.TaskList.AddRange(listInfo);
                        };
                    }
                };
 
                //删除
                var btnDelete = rowDevice.AddDeleteControl();
                btnDelete.ButtonClickEvent += (sender, e) =>
                {
                    //隐藏右滑菜单
                    rowDevice.HideMenu();
                    //确认是否要删除?
                    string msg = Language.StringByID(R.MyInternationalizationString.uShowDoDeleteMsg);
                    this.ShowMassage(ShowMsgType.Confirm, msg, () =>
                    {
                        //执行目标已经改变
                        this.isAdjustTargetChanged = true;
                        //调整执行目标里面的全部坐标(删除的时候使用)
                        this.AdjustTargetLocation(rowDevice.MainKeys);
                        //移除
                        rowDevice.RemoveFromParent();
                        this.listAdjustTarget.Remove(data);
                        //调整执行目标的桌布高度
                        this.AdjustTargetTableHeight();
                    });
                };
            }
        }
 
        #endregion
 
        #region ■ 添加场景执行目标行_________________
 
        /// <summary>
        /// 添加场景执行目标行
        /// </summary>
        private void AddSceneTargetRow(Scene.DeviceListData data, int index, bool addLine)
        {
            //场景
            var scene = HdlSceneLogic.Current.GetSceneUIBySceneId(data.ElseScenesId);
            var rowScene = new SceneRoomControl(scene.Id, scene.Name, frameTargetTableControl.rowSpace / 2);
            rowScene.MainKeys = index.ToString();
            frameTargetTableControl.AddChidren(rowScene);
            //控件向右偏移
            rowScene.frameTable.LeftOffset = Application.GetRealWidth(104) - HdlControlResourse.XXLeft;
            rowScene.InitControl();
            if (addLine == true)
            {
                //底线
                rowScene.frameTable.AddBottomLine();
            }
            rowScene.frameTable.UseClickStatu = false;
 
            //分享的场景不能编辑
            if (this.editorScene == null || this.editorScene.IsSharedScene == false)
            {
                if (Config.Instance.Home.IsShowTemplate == true)
                {
                    //模板场景不允许编辑
                    return;
                }
                //删除
                var btnDelete = rowScene.AddDeleteControl();
                btnDelete.ButtonClickEvent += (sender, e) =>
                {
                    //隐藏右滑菜单
                    rowScene.HideMenu();
 
                    //确认是否要删除?
                    string msg = Language.StringByID(R.MyInternationalizationString.uShowDoDeleteMsg);
                    this.ShowMassage(ShowMsgType.Confirm, msg, () =>
                    {
                        //执行目标已经改变
                        this.isAdjustTargetChanged = true;
                        //调整执行目标里面的全部坐标(删除的时候使用)
                        this.AdjustTargetLocation(rowScene.MainKeys);
                        //移除
                        rowScene.RemoveFromParent();
                        this.listAdjustTarget.Remove(data);
                        //调整执行目标的桌布高度
                        this.AdjustTargetTableHeight();
                    });
                };
            }
        }
 
        #endregion
 
        #region ■ 添加延时行_________________________
 
        /// <summary>
        /// 添加延时行
        /// </summary>
        private void AddDelayTimeTargetRow(Scene.DeviceListData data, int index, bool addLine)
        {
            string hourText = Language.StringByID(R.MyInternationalizationString.Hour);
            string minuText = Language.StringByID(R.MyInternationalizationString.Minute);
            string secondText = Language.StringByID(R.MyInternationalizationString.Second);
 
            //行控件
            var rowDelay = new RowLayoutControl(frameTargetTableControl.rowSpace / 2);
            rowDelay.MainKeys = index.ToString();
            frameTargetTableControl.AddChidren(rowDelay);
            //控件向右偏移
            rowDelay.frameTable.LeftOffset = Application.GetRealWidth(104) - HdlControlResourse.XXLeft;
            rowDelay.frameTable.UseClickStatu = false;
            //图标
            var btnIcon = rowDelay.frameTable.AddLeftIcon();
            btnIcon.UnSelectedImagePath = "Item/Timer.png";
            //延时
            var btnDelay = rowDelay.frameTable.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.Delay), 400);
            btnDelay.TextColor = UserCenterColor.Current.TextGrayColor3;
 
            //x小时x分钟x秒后
            string timeStr = this.GetTimeString(data.DelayTime, hourText, minuText, secondText);
            timeStr += Language.StringByID(R.MyInternationalizationString.Later);
            var btnStatu = rowDelay.frameTable.AddMostRightView(timeStr, 300);
 
            if (addLine == true)
            {
                //底线
                rowDelay.frameTable.AddBottomLine();
            }
 
            //分享的场景不能编辑
            if (this.editorScene == null || this.editorScene.IsSharedScene == false)
            {
                if (Common.Config.Instance.Home.IsShowTemplate == true)
                {
                    //模板场景不允许编辑
                    return;
                }
                //编辑
                var btnEditor = rowDelay.AddEditorControl();
                btnEditor.ButtonClickEvent += (sender, e) =>
                {
                    //延时时间选择
                    var form = new AdjustTargetAddDelayTimeForm();
                    form.AddForm(data.DelayTime);
                    form.FinishSelectEvent += (delayTime) =>
                    {
                        //执行目标已经改变
                        this.isAdjustTargetChanged = true;
                        data.DelayTime = delayTime;
                        //x小时x分钟x秒后
                        timeStr = this.GetTimeString(data.DelayTime, hourText, minuText, secondText);
                        btnStatu.Text = timeStr;
                    };
                };
                //删除
                var btnDelete = rowDelay.AddDeleteControl();
                btnDelete.ButtonClickEvent += (sender, e) =>
                {
                    //隐藏右滑菜单
                    rowDelay.HideMenu();
                    //确认是否要删除?
                    string msg = Language.StringByID(R.MyInternationalizationString.uShowDoDeleteMsg);
                    this.ShowMassage(ShowMsgType.Confirm, msg, () =>
                    {
                        //执行目标已经改变
                        this.isAdjustTargetChanged = true;
                        //调整执行目标里面的全部坐标(删除的时候使用)
                        this.AdjustTargetLocation(rowDelay.MainKeys);
                        //移除
                        rowDelay.RemoveFromParent();
                        this.listAdjustTarget.Remove(data);
                        //调整执行目标的桌布高度
                        this.AdjustTargetTableHeight();
                    });
                };
            }
        }
 
        /// <summary>
        /// 从网关获取场景的执行目标
        /// </summary>
        /// <returns></returns>
        private bool GetSceneTargetList()
        {
            if (this.editorScene == null || this.listAdjustTarget != null)
            {
                return true;
            }
 
            //打开进度条
            this.ShowProgressBar();
            //获取执行目标列表
            var listData = HdlSceneLogic.Current.GetAdjustTargetList(this.editorScene);
            if (listData == null)
            {
                this.CloseProgressBar(ShowReLoadMode.YES);
                return false;
            }
            this.listAdjustTarget = new List<Scene.DeviceListData>();
            listAdjustTarget.AddRange(listData);
 
            this.CloseProgressBar();
            return true;
        }
 
 
        #endregion
 
        #region ■ 初始化促使界面滑动控件_____________
 
        /// <summary>
        /// 初始化促使界面能够刚好滑动超过保存按钮的控件
        /// </summary>
        private void InitFrameBottomTempControl()
        {
            if (this.frameBottomTemp != null)
            {
                return;
            }
            //借用一下控件的Y轴
            var btnTemp = new BottomClickButton();
 
            //那个特殊的控件
            this.frameBottomTemp = new FrameLayout();
            frameBottomTemp.Height = bodyFrameLayout.Height - btnTemp.Yaxis + Application.GetRealHeight(23);
            this.listBodyControl.AddChidren(frameBottomTemp);
        }
 
        #endregion
 
        #region ■ 显示添加目标菜单___________________
 
        /// <summary>
        /// 显示添加目标菜单
        /// </summary>
        private void ShowAddTargetMenu()
        {
            var menuContr = new BottomMenuSelectForm();
            menuContr.AddForm(3);
            //功能
            menuContr.AddMenu(Language.StringByID(R.MyInternationalizationString.uFunction), () =>
            {
                var form = new AdjustTargetAddDeviceForm();
                form.AddForm(listAdjustTarget);
                form.FinishSelectEvent += (dicData) =>
                {
                    //执行目标已经改变
                    this.isAdjustTargetChanged = true;
                    foreach (var mainKey in dicData.Keys)
                    {
                        var device = HdlDeviceCommonLogic.Current.GetDevice(mainKey);
                        var data = new Scene.DeviceListData();
                        data.Type = 0;
                        data.DeviceAddr = device.DeviceAddr;
                        data.Epoint = device.DeviceEpoint;
                        data.TaskList = dicData[mainKey];
                        this.listAdjustTarget.Add(data);
                    }
                    //刷新列表控件
                    this.InitAddTargetControl();
                };
            });
            //场景
            menuContr.AddMenu(Language.StringByID(R.MyInternationalizationString.uScence), () =>
            {
                var form = new AdjustTargetAddSceneForm();
                form.AddForm(listAdjustTarget);
                form.FinishSelectEvent += (listSceneId) =>
                {
                    //执行目标已经改变
                    this.isAdjustTargetChanged = true;
                    foreach (int sceneId in listSceneId)
                    {
                        var data = new Scene.DeviceListData();
                        data.Type = 1;
                        data.ElseScenesId = sceneId;
                        this.listAdjustTarget.Add(data);
                    }
                    //刷新列表控件
                    this.InitAddTargetControl();
                };
            });
            //延时
            menuContr.AddMenu(Language.StringByID(R.MyInternationalizationString.Delay), () =>
            {
                //延时时间选择
                var form = new AdjustTargetAddDelayTimeForm();
                form.AddForm(0);
                form.FinishSelectEvent += (delayTime) =>
                {
                    //执行目标已经改变
                    this.isAdjustTargetChanged = true;
                    var data = new Scene.DeviceListData();
                    data.Type = 2;
                    data.DelayTime = delayTime;
                    this.listAdjustTarget.Add(data);
                    //刷新列表控件
                    this.InitAddTargetControl();
                };
            });
        }
 
        #endregion
 
        #region ■ 保存场景数据_______________________
 
        /// <summary>
        /// 保存场景数据
        /// </summary>
        private void SaveSceneData()
        {
            //检测能否保存场景
            if (this.CheckCanSaveScene() == false)
            {
                return;
            }
            HdlThreadLogic.Current.RunThread(() =>
            {
                //打开进度条
                this.ShowProgressBar();
                //新建场景
                if (this.editorScene == null)
                {
                    //保存新建的场景
                    this.SaveSceneDataByNewMode();
                }
                //编辑场景
                else
                {
                    //保存编辑的场景
                    this.SaveSceneDataByEditorMode();
                }
            });
        }
 
        /// <summary>
        /// 保存新建的场景
        /// </summary>
        private void SaveSceneDataByNewMode()
        {
            //添加场景
            this.editorScene = HdlSceneLogic.Current.AddNewSceneToGateway(this.cloneScene.Name, this.listAdjustTarget);
            //关闭进度条
            this.CloseProgressBar();
 
            if (editorScene == null)
            {
                return;
            }
            if (this.nowRoomId != string.Empty)
            {
                //添加房间
                var newRoom = HdlRoomLogic.Current.GetRoomById(this.nowRoomId);
                HdlSceneLogic.Current.AddSceneToRoom(newRoom, this.editorScene);
            }
            if (this.cloneScene.IconPathType != 0)
            {
                //自定义图片
                var tradeTime = DateTime.Now.ToString("yyyyMMddHHmmss");
                var fileName = $"SceneIcon_{tradeTime}.png";
                //生成文件图片
                IO.FileUtils.WriteFileByBytes(System.IO.Path.Combine(Config.Instance.FullPath, fileName), this.btnScenePic.ImageBytes);
                HdlBackupLogic.Current.AddOrEditorAutoBackFileStatu(fileName);
                this.cloneScene.IconPath = fileName;
            }
            //转移属性
            this.editorScene.Name = this.cloneScene.Name;
            this.editorScene.IconPathType = this.cloneScene.IconPathType;
            this.editorScene.IconPath = this.cloneScene.IconPath;
            this.editorScene.Save();
 
            //新建场景,主页需要重新刷新
            UserView.UserPage.Instance.RefreshAllForm = true;
 
            HdlThreadLogic.Current.RunMain(() =>
            {
                //调用回调函数
                this.SceneChangedEvent?.Invoke(this.editorScene, this.nowRoomId);
                this.CloseForm();
            });
        }
 
        /// <summary>
        /// 保存编辑的场景
        /// </summary>
        private async void SaveSceneDataByEditorMode()
        {
            if (this.editorScene.Name != this.cloneScene.Name)
            {
                //修改名称
                var result = await HdlSceneLogic.Current.EditorSceneNameFromGateway(this.editorScene, this.cloneScene.Name);
                if (result == false)
                {
                    //关闭进度条
                    this.CloseProgressBar();
                    return;
                }
            }
            if (isAdjustTargetChanged == true)
            {
                //修改场景
                var result = await HdlSceneLogic.Current.EditorSceneFromGateway(this.editorScene, this.listAdjustTarget);
                if (result == false)
                {
                    //关闭进度条
                    this.CloseProgressBar();
                    return;
                }
            }
 
            if (this.nowRoomId != string.Empty)
            {
                //变更房间
                HdlSceneLogic.Current.ChangedSceneRoom(this.editorScene, this.nowRoomId);
            }
            else
            {
                //移除出房间
                var room = HdlRoomLogic.Current.GetRoomBySceneId(this.editorScene.Id);
                if (room != null)
                {
                    HdlSceneLogic.Current.DeleteSceneFromRoom(room, this.editorScene);
                }
            }
 
            //图片改变了
            if (isScenePictrueChanged == true)
            {
                if (this.editorScene.IconPathType != 0)
                {
                    //删除自定义图片
                    HdlFileLogic.Current.DeleteFile(System.IO.Path.Combine(Config.Instance.FullPath, this.editorScene.IconPath));
                    HdlBackupLogic.Current.DeleteAutoBackFileStatu(this.editorScene.IconPath);
                }
                if (this.cloneScene.IconPathType != 0)
                {
                    //自定义图片
                    var tradeTime = DateTime.Now.ToString("yyyyMMddHHmmss");
                    var fileName = $"SceneIcon_{tradeTime}.png";
                    //生成文件图片
                    IO.FileUtils.WriteFileByBytes(System.IO.Path.Combine(Config.Instance.FullPath, fileName), this.btnScenePic.ImageBytes);
                    HdlBackupLogic.Current.AddOrEditorAutoBackFileStatu(fileName);
                    this.cloneScene.IconPath = fileName;
                }
            }
 
            //转移属性
            this.editorScene.Name = this.cloneScene.Name;
            this.editorScene.IconPathType = this.cloneScene.IconPathType;
            this.editorScene.IconPath = this.cloneScene.IconPath;
            this.editorScene.Save();
 
            //编辑场景,主页需要重新刷新
            UserView.UserPage.Instance.RefreshAllForm = true;
 
            //关闭进度条
            this.CloseProgressBar();
 
            HdlThreadLogic.Current.RunMain(() =>
            {
                //调用回调函数
                this.SceneChangedEvent?.Invoke(this.editorScene, this.nowRoomId);
                this.CloseForm();
            });
        }
 
        /// <summary>
        /// 检测能否保存场景
        /// </summary>
        /// <returns></returns>
        private bool CheckCanSaveScene()
        {
            if (this.cloneScene.Name == string.Empty)
            {
                //场景名不能为空
                this.ShowMassage(ShowMsgType.Remind, Language.StringByID(R.MyInternationalizationString.SceneNameCannotBeNull));
                return false;
            }
            bool hadTarget = false;
            foreach (var data in this.listAdjustTarget)
            {
                if (data.Type == 0 || data.Type == 1)
                {
                    //拥有执行目标
                    hadTarget = true;
                    break;
                }
            }
            if (hadTarget == false)
            {
                //请添加执行目标
                this.ShowMassage(ShowMsgType.Remind, Language.StringByID(R.MyInternationalizationString.uPleaseAddAdjustTarget));
                return false;
            }
            return true;
        }
 
        #endregion
 
        #region ■ 场景图片选择_______________________
 
        /// <summary>
        /// 场景图片选择
        /// </summary>
        /// <param name="imageContr"></param>
        private void ScenePictrueSelect(ImageView imageContr)
        {
            var menuContr = new BottomMenuSelectForm();
            menuContr.AddForm(3);
            //默认图库
            menuContr.AddMenu(Language.StringByID(R.MyInternationalizationString.LocalPicture), () =>
            {
                var form = new SelectLocalSceneImageForm();
                form.AddForm();
                form.FinishSelectEvent = (imgPath) =>
                {
                    if (string.IsNullOrEmpty(imgPath) == true)
                    {
                        return;
                    }
                    this.cloneScene.IconPathType = 0;
                    this.cloneScene.IconPath = imgPath;
                    imgPath = IO.FileUtils.GetImageFilePath(imgPath);
                    imageContr.ImageBytes = IO.FileUtils.ReadFile(imgPath);
                    //场景图片已经改变
                    this.isScenePictrueChanged = true;
                };
            });
            //拍照
            menuContr.AddMenu(Language.StringByID(R.MyInternationalizationString.Photograph), () =>
            {
                //通过相机拍照裁剪
                CropImage.TakePicture((imagePath) =>
                {
                    if (string.IsNullOrEmpty(imagePath) == true)
                    {
                        return;
                    }
                    this.cloneScene.IconPathType = 1;
                    imageContr.ImageBytes = Shared.IO.FileUtils.ReadFile(imagePath);
                    System.IO.File.Delete(imagePath);
                    //场景图片已经改变
                    this.isScenePictrueChanged = true;
 
                }, "HdlPic", 2, 1);
            });
            //我的相册
            menuContr.AddMenu(Language.StringByID(R.MyInternationalizationString.MyAblums), () =>
            {
                //从相册选择图片裁剪
                CropImage.SelectPicture((imagePath) =>
                {
                    if (string.IsNullOrEmpty(imagePath) == true)
                    {
                        return;
                    }
                    this.cloneScene.IconPathType = 2;
                    imageContr.ImageBytes = Shared.IO.FileUtils.ReadFile(imagePath);
                    System.IO.File.Delete(imagePath);
                    //场景图片已经改变
                    this.isScenePictrueChanged = true;
 
                }, "HdlPic", 2, 1);
            });
        }
 
        #endregion
 
        #region ■ 界面关闭___________________________
 
        /// <summary>
        /// 界面关闭
        /// </summary>
        public override void CloseFormBefore()
        {
            this.SceneChangedEvent = null;
 
            base.CloseFormBefore();
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 获取时间的翻译文本
        /// </summary>
        /// <param name="second"></param>
        /// <param name="hourText">小时的文本</param>
        /// <param name="minuText">分的文本</param>
        /// <param name="secondText">秒的文本</param>
        /// <returns></returns>
        private string GetTimeString(int second, string hourText, string minuText, string secondText)
        {
            string timeStr = string.Empty;
            int hour = second / 3600;
            int minu = second % 3600 / 60;
            int sec = second % 60;
            if (hour > 0)
            {
                timeStr += hour + hourText;
            }
            if (minu > 0)
            {
                timeStr += minu + minuText;
            }
            if (sec > 0)
            {
                timeStr += sec + secondText;
            }
            return timeStr;
        }
 
        /// <summary>
        /// 调整执行目标的桌布高度
        /// </summary>
        private void AdjustTargetTableHeight()
        {
            //调整桌布高度
            int minHeight = Application.GetRealHeight(346);
            int realHeight = frameTargetTableControl.GetChildren(frameTargetTableControl.ChildrenCount - 1).Bottom;
            realHeight += Application.GetRealHeight(23);
            if (realHeight > minHeight)
            {
                frameTargetTableControl.Height = realHeight;
            }
            else if (frameTargetTableControl.Height != minHeight)
            {
                frameTargetTableControl.Height = minHeight;
            }
            //这个特殊的东西必须放在执行目标容器的底部
            this.frameBottomTemp.Y = frameTargetTableControl.Bottom;
            //调整全局上下滑动控件的桌布高度
            this.listBodyControl.AdjustTableHeight();
        }
 
        /// <summary>
        /// 调整执行目标里面的全部坐标(删除的时候使用)
        /// </summary>
        /// <param name="mainKey">删除的主键</param>
        private void AdjustTargetLocation(string mainKey)
        {
            bool canChanged = false;
            int YY = 0;
            for (int i = 0; i < frameTargetTableControl.ChildrenCount; i++)
            {
                var myRow = frameTargetTableControl.GetChildren(i);
                if (canChanged == false && (myRow is RowLayoutControl))
                {
                    if (((RowLayoutControl)myRow).MainKeys == mainKey)
                    {
                        //已经到达要删除的索引
                        canChanged = true;
                        YY = frameTargetTableControl.GetChildren(i).Y;
                    }
                    continue;
                }
                if (canChanged == true)
                {
                    //它下面的行全部往上移
                    myRow.Y = YY;
                    YY = myRow.Bottom;
                }
            }
        }
 
        #endregion
    }
}