wxr
2023-04-07 0041a49139ebb21f228de022b38462b137c2d71e
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
 
using System;
using System.Collections.Generic;
using System.Threading;
using HDL_ON.DAL.Server;
using HDL_ON.DriverLayer;
using HDL_ON.Entity;
using HDL_ON.UI.CSS;
using Shared;
 
namespace HDL_ON.UI
{
    public class AddGroupControlPage : FrameLayout
    {
        #region 控件列表
        /// <summary>
        /// 当前窗体
        /// </summary>
        FrameLayout bodyView;
        /// <summary>
        /// 楼层选择下拉图标
        /// </summary>
        Button btnFloorDownIcon;
        /// <summary>
        /// 楼层显示
        /// </summary>
        Button btnFloor;
        /// <summary>
        /// 全选按钮
        /// </summary>
        Button btnChooseAll;
        /// <summary>
        /// 功能列表集合显示区域
        /// </summary>
        VerticalScrolViewLayout functionListView;
        /// <summary>
        /// 完成按钮
        /// </summary>
        Button btnConfrim;
 
        #endregion
        /// <summary>
        /// 功能列表
        /// </summary>
        List<Function> groupControlLightList;
        /// <summary>
        /// 本地的灯光列表
        /// </summary>
        List<Function> lightList;
        /// <summary>
        /// 回调刷新
        /// </summary>
        Action backActon;
        /// <summary>
        /// 是否新增群控
        /// </summary>
        bool isAdd = true;
        /// <summary>
        /// 编辑的组控数据
        /// </summary>
        string editDataString = "";
 
        GroupControl groupControl;
 
        GroupControlType groupControlType = new GroupControlType();
 
        public AddGroupControlPage(GroupControl groupControl, Action action)
        {
            bodyView = this;
            if(groupControl == null)
            {
                this.groupControl = new GroupControl();
            }
            else
            {
                isAdd = false;
                this.groupControl = groupControl;
                editDataString = Newtonsoft.Json.JsonConvert.SerializeObject(this.groupControl);
            }
            groupControlLightList = new List<Function>();
            backActon = action;
            lightList = FunctionList.List.GetLightList();
        }
 
        public void LoadPage()
        {
            bodyView.BackgroundColor = CSS_Color.BackgroundColor;
            new TopViewDiv(bodyView, Language.StringByID(StringId.CombinedDimming)).LoadTopView();
 
            var contentView = new FrameLayout()
            {
                Y = Application.GetRealHeight(64),
                Height = Application.GetRealHeight(550),
            };
            bodyView.AddChidren(contentView);
 
            #region 组名称
            FrameLayout groupNameView;
            groupNameView = new FrameLayout()
            {
                Height = Application.GetRealHeight(50),
                BackgroundColor = CSS_Color.MainBackgroundColor,
            };
            contentView.AddChidren(groupNameView);
            Button btnGroupNameTitle = new Button()
            {
                X = Application.GetRealWidth(16),
                Width = Application.GetRealWidth(78),
                TextID = StringId.GroupNmae,
                TextAlignment = TextAlignment.CenterLeft,
                TextSize = CSS_FontSize.SubheadingFontSize,
                TextColor = CSS_Color.FirstLevelTitleColor,
            };
            groupNameView.AddChidren(btnGroupNameTitle);
 
            var etGroupName = new EditText()
            {
                X = Application.GetRealWidth(155),
                Width = Application.GetRealWidth(200),
                PlaceholderText = Language.StringByID(StringId.PlsEntry),
                TextSize = CSS_FontSize.TextFontSize,
                TextAlignment = TextAlignment.CenterRight,
                TextColor = CSS_Color.TextualColor,
                PlaceholderTextColor = CSS_Color.PromptingColor1,
            };
            groupNameView.AddChidren(etGroupName);
 
            groupNameView.AddChidren(
                new Button()
                {
                    Gravity = Gravity.CenterHorizontal,
                    Y = Application.GetRealHeight(49),
                    Height = Application.GetRealHeight(1),
                    Width = Application.GetRealWidth(343),
                    BackgroundColor = CSS_Color.DividingLineColor,
                });
            #endregion
            #region 位置管理
            var locationMagtView = new FrameLayout()
            {
                Y = Application.GetRealHeight(50),
                Height = Application.GetRealHeight(50),
                BackgroundColor = CSS_Color.MainBackgroundColor,
            };
            contentView.AddChidren(locationMagtView);
 
            var btnLocationMagtTitle = new Button()
            {
                X = Application.GetRealWidth(16),
                Width = Application.GetRealWidth(160),
                TextAlignment = TextAlignment.CenterLeft,
                TextColor = CSS_Color.FirstLevelTitleColor,
                TextSize = CSS_FontSize.SubheadingFontSize,
                TextID = StringId.LocationManagement,
            };
            locationMagtView.AddChidren(btnLocationMagtTitle);
 
            var btnLocationValues = new Button()
            {
                X = Application.GetRealWidth(86),
                Width = Application.GetRealWidth(237),
                TextAlignment = TextAlignment.CenterRight,
                TextColor = CSS_Color.PromptingColor1,
                TextSize = CSS_FontSize.TextFontSize,
                Text = groupControl.GetRoomListName(),
            };
            locationMagtView.AddChidren(btnLocationValues);
 
            var btnLocationInfoRight = new Button()
            {
                X = Application.GetRealWidth(339),
                Gravity = Gravity.CenterVertical,
                Width = Application.GetMinRealAverage(16),
                Height = Application.GetMinRealAverage(16),
                UnSelectedImagePath = "Public/RightIcon.png",
            };
            locationMagtView.AddChidren(btnLocationInfoRight);
            EventHandler<MouseEventArgs> eventHandler = (sender, e) =>
            {
                Application.HideSoftInput();
                //修改功能所属房间之后的回调事件
                Action chooseRoomBackAction = () => {
                    try
                    {
                        btnLocationValues.Text = groupControl.GetRoomListName();
                    }
                    catch { }
                };
                var view = new GroupChooseRoomPage(groupControl, chooseRoomBackAction);
                MainPage.BasePageView.AddChidren(view);
                view.LoadPage();
                MainPage.BasePageView.PageIndex = MainPage.BasePageView.ChildrenCount - 1;
            };
            btnLocationInfoRight.MouseUpEventHandler = eventHandler;
            btnLocationValues.MouseUpEventHandler = eventHandler;
            
 
            //locationMagtView.AddChidren(
            //    new Button()
            //    {
            //        Y = Application.GetRealHeight(49),
            //        Gravity = Gravity.CenterHorizontal,
            //        BackgroundColor = CSS_Color.DividingLineColor,
            //        Width = Application.GetRealWidth(343),
            //        Height = Application.GetRealHeight(1)
            //    });
            #endregion
 
            //contentView.AddChidren(new Button()
            //{
            //    Y = Application.GetRealHeight(164),
            //    BackgroundColor = CSS_Color.BackgroundColor,
            //});
 
            /// <summary>
            /// 房间内容显示区域
            /// </summary>
            var roomFloorChangeView = new FrameLayout()
            {
                Y = Application.GetRealHeight(172),
                Height = Application.GetRealHeight(52),
                BackgroundColor = CSS_Color.BackgroundColor,
            };
            bodyView.AddChidren(roomFloorChangeView);
            #region 房间顶部切换显示区域
            btnFloorDownIcon = new Button()
            {
                Width = Application.GetMinRealAverage(16),
                Height = Application.GetMinRealAverage(16),
                X = Application.GetRealWidth(16),
                Y = Application.GetRealHeight(18),
                UnSelectedImagePath = "Public/DownIcon.png",
            };
            roomFloorChangeView.AddChidren(btnFloorDownIcon);
 
            btnFloor = new Button()
            {
                X = btnFloorDownIcon.Right,
                Width = Application.GetRealWidth(200),
                TextColor = CSS_Color.FirstLevelTitleColor,
                TextSize = CSS_FontSize.PromptFontSize_FirstLevel,
                TextAlignment = TextAlignment.CenterLeft,
                Text = DB_ResidenceData.Instance.CurFloor.roomName,
            };
            roomFloorChangeView.AddChidren(btnFloor);
 
            btnChooseAll = new Button()
            {
                X = Application.GetRealWidth(245),
                Width = Application.GetRealWidth(109),
                TextAlignment = TextAlignment.CenterRight,
                TextSize = CSS_FontSize.TextFontSize,
                TextColor = CSS_Color.FirstLevelTitleColor,
                TextID = StringId.SelectedAll,
            };
            if (groupControlLightList.Count > 0)
            {
                btnChooseAll.TextID = StringId.Cancel;
            }
            roomFloorChangeView.AddChidren(btnChooseAll);
            btnChooseAll.MouseUpEventHandler = (sender, e) => {
                if (groupControlLightList.Count > 0)
                {
                    groupControlLightList.Clear();
                    btnChooseAll.TextID = StringId.SelectAll;
                    btnConfrim.IsSelected = false;
                }
                else
                {
                    groupControlLightList.AddRange(lightList);
                    btnChooseAll.TextID = StringId.Cancel;
                    btnConfrim.IsSelected = true;
                }
                LoadLightRow(lightList);
            };
 
            #endregion
 
            functionListView = new VerticalScrolViewLayout()
            {
                Y = Application.GetRealHeight(220),
                Height = Application.GetRealHeight(640 - 12 - 52 - 100 -100),
            };
            bodyView.AddChidren(functionListView);
 
 
            functionListView.AddChidren(new Button()
            {
                Height = Application.GetRealHeight(10),
            });
 
            #region 底部view
            var bottomView = new FrameLayout()
            {
                Y = Application.GetRealHeight(591),
                Height = Application.GetRealHeight(100),
                BackgroundColor = CSS_Color.MainBackgroundColor,
                Radius = (uint)Application.GetRealWidth(12),
            };
            this.AddChidren(bottomView);
 
            btnConfrim = new Button()
            {
                Y = Application.GetRealHeight(12),
                Gravity = Gravity.CenterHorizontal,
                Width = Application.GetRealWidth(220),
                Height = Application.GetRealHeight(44),
                Radius = (uint)Application.GetRealHeight(22),
                TextAlignment = TextAlignment.Center,
                TextColor = CSS_Color.MainBackgroundColor,
                BackgroundColor = CSS_Color.BackgroundColor,
                TextID = StringId.Confirm,
                TextSize = CSS_FontSize.SubheadingFontSize,
                SelectedTextColor = CSS_Color.MainBackgroundColor,
                SelectedBackgroundColor = CSS_Color.MainColor,
                IsSelected = !isAdd
            };
            bottomView.AddChidren(btnConfrim);
            btnConfrim.MouseUpEventHandler = (sender, e) => {
                if (btnConfrim.IsSelected)
                {
                    //保存群控数据
                    var name = etGroupName.Text.Trim();
                    if (string.IsNullOrEmpty(name))
                    {
                        new PublicAssmebly().TipMsg(StringId.Tip, StringId.PlsEntryGroupName);
                        return;
                    }
                    if (groupControlLightList.Count < 2)
                    {
                        new PublicAssmebly().TipMsg(StringId.Tip, StringId.PlsSelectMoreData);
                        return;
                    }
                    if (isAdd)
                    {
                        groupControl.name = name;
                        groupControl.sids.Clear();
                        foreach (var light in groupControlLightList)
                        {
                            try
                            {
                                var gc = new GroupControlFunction();
                                gc.sid = light.sid;
                                gc.spk = light.spk;
                                groupControl.sids.Add(gc);
                            }
                            catch (Exception ex)
                            {
                                MainPage.Log($"新增群控转换数据异常:{ex.Message}");
                            }
                        }
                        try
                        {
                            groupControl.type = groupControlType.type;
                            groupControl.sid = groupControl.NewGroupControlSid();
                            var pack = Common.ApiUtlis.Ins.HttpRequest.AddGroupControl(new List<GroupControl>() { groupControl });
                            if (pack != null)
                            {
                                if (pack.Code == StateCode.SUCCESS)
                                {
                                    backActon?.Invoke();
                                    this.RemoveFromParent();
                                }
                                else
                                {
                                    IMessageCommon.Current.ShowErrorInfoAlter(pack.Code);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            MainPage.Log($"新增群控异常:{ex.Message}");
                        }
                    }
                    else
                    {
                        try
                        {
                            var newEditDataString = Newtonsoft.Json.JsonConvert.SerializeObject(this.groupControl);
                            //没有更新数据则直接退出
                            if (newEditDataString == editDataString)
                            {
                                this.RemoveFromParent();
                                return;
                            }
                            groupControl.sids.Clear();
                            foreach (var light in groupControlLightList)
                            {
                                try
                                {
                                    var gc = new GroupControlFunction();
                                    gc.sid = light.sid;
                                    gc.spk = light.spk;
                                    groupControl.sids.Add(gc);
                                }
                                catch (Exception ex)
                                {
                                    MainPage.Log($"新增群控转换数据异常:{ex.Message}");
                                }
                            }
 
                            var pack = Common.ApiUtlis.Ins.HttpRequest.EditGroupControl(new List<GroupControl>() { groupControl });
                            if (pack != null)
                            {
                                if (pack.Code == StateCode.SUCCESS)
                                {
                                    backActon?.Invoke();
                                    this.RemoveFromParent();
                                }
                                else
                                {
                                    IMessageCommon.Current.ShowErrorInfoAlter(pack.Code);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            MainPage.Log($"编辑群控异常:{ex.Message}");
                        }
 
                    }
                }
            };
 
            #endregion
 
 
            LoadDialog_ChangeFloor();
 
 
            new Thread(() => {
                if (isAdd)
                {
                    var pack = Common.ApiUtlis.Ins.HttpRequest.GetGroupControlTypes("light.switch");
                    if (pack != null)
                    {
                        if (pack.Code == StateCode.SUCCESS)
                        {
                            try
                            {
 
                                var groupControlTypes = Newtonsoft.Json.JsonConvert.DeserializeObject<List<GroupControlType>>(pack.Data.ToString());
                                if (groupControlTypes != null && groupControlTypes.Count > 0)
                                {
                                    groupControlType = groupControlTypes[0];
                                    Application.RunOnMainThread(() => {
                                        LoadLightRow(lightList);
                                    });
                                }
                            }
                            catch (Exception ex)
                            {
                                MainPage.Log($"读取组控类型失败:{ex.Message}");
                            }
                        }
                        else
                        {
                            IMessageCommon.Current.ShowErrorInfoAlter(pack.Code);
                        }
                    }
                }
                else
                {
                    var pack = Common.ApiUtlis.Ins.HttpRequest.GetGroupControInfo(groupControl.userDeviceGroupControlId);
                    if (pack != null)
                    {
                        if (pack.Code == StateCode.SUCCESS)
                        {
                            try
                            {
 
                                var groupControlTemps = Newtonsoft.Json.JsonConvert.DeserializeObject<List<GroupControl>>(pack.Data.ToString());
                                if (groupControlTemps != null && groupControlTemps.Count > 0)
                                {
                                    groupControl = groupControlTemps[0];
                                    Application.RunOnMainThread(() =>
                                    {
                                        LoadLightRow(lightList);
                                    });
                                }
                            }
                            catch (Exception ex)
                            {
                                MainPage.Log($"读取组控信息失败:{ex.Message}");
                            }
                        }
                        else
                        {
                            IMessageCommon.Current.ShowErrorInfoAlter(pack.Code);
                        }
                    }
                }
            }) { IsBackground = true }.Start();
 
            if (!isAdd)
            {
                etGroupName.Text = groupControl.name;
                btnLocationValues.Text = groupControl.GetRoomListName();
            }
        }
 
        /// <summary>
        /// 显示的设备的总数
        /// </summary>
        int showCount = 0;
 
        /// <summary>
        /// 加载功能row
        /// </summary>
        /// <param name="lightList"></param>
        void LoadLightRow(List<Function> functions, bool isAppend = false)
        {
            var waitPage = new Loading();
            bodyView.AddChidren(waitPage);
            waitPage.Start("");
 
            new Thread(() =>
            {
                try
                {
                    Application.RunOnMainThread(() =>
                    {
                        if (!isAppend)
                        {
                            showCount = 0;
                            functionListView.RemoveAll();
                        }
                        int i = 0;
                        foreach (var function in functions)
                        {
                            i++;
                            if (i > 100)
                            {
                                break;
                            }
                            showCount++;
                            var functionDiv = new LightRow(function)
                            {
                                Gravity = Gravity.CenterHorizontal,
                                Height = Application.GetRealHeight(62),
                                BorderColor = 0x00FFFFFF,
                                BorderWidth = 1,
                                BackgroundColor = CSS_Color.MainBackgroundColor,
                                Tag = function.spk + function.sid
                            };
                            functionDiv.AddChidren(new Button()
                            {
                                Y = Application.GetRealHeight(61),
                                Gravity = Gravity.CenterHorizontal,
                                Width = Application.GetRealWidth(343),
                                Height = Application.GetRealWidth(1),
                                BackgroundColor = CSS_Color.DividingLineColor,
                            });
 
                            Action setAction = () =>
                            {
                                if (groupControlLightList.Count > 0)
                                {
                                    btnConfrim.IsSelected = true;
                                }
                                else
                                {
                                    btnConfrim.IsSelected = false;
                                }
                            };
                            if (groupControl.sids.Find((obj) => obj.sid == function.sid) != null)
                            {
                                groupControlLightList.Add(function);
                            }
 
                            functionDiv.LoadDiv(groupControlLightList, setAction);
                            functionListView.AddChidren(functionDiv);
                        }
 
                        if (functions.Count > showCount)
                        {
                            var btnAppend = new Button()
                            {
                                Height = Application.GetRealHeight(60),
                                TextAlignment = TextAlignment.Center,
                                TextSize = CSS_FontSize.SubheadingFontSize,
                                TextColor = CSS_Color.FirstLevelTitleColor,
                                TextID = StringId.LoadMore,
                            };
                            functionListView.AddChidren(btnAppend);
                            btnAppend.MouseUpEventHandler = (sender, e) =>
                            {
                                btnAppend.RemoveFromParent();
                                LoadLightRow(functions, true);
                            };
                        }
                    });
                }
                catch (Exception ex)
                {
                    MainPage.Log("ShowFunctionRowError : " + ex.Message);
                }
                finally
                {
                    Application.RunOnMainThread(() =>
                    {
                        if (waitPage != null)
                        {
                            waitPage.RemoveFromParent();
                            waitPage = null;
                        }
                    });
                }
            })
            { IsBackground = true }.Start();
 
        }
 
 
        /// <summary>
        /// 住宅列表点击事件
        /// </summary>
        void LoadDialog_ChangeFloor()
        {
            string nowSelectId = null;
            btnFloor.MouseUpEventHandler += (sender, e) =>
            {
                Application.HideSoftInput();
                //显示下拉列表
                var form = new FloorRoomSelectPopupView();
                form.ShowDeviceFunctionView(btnFloor, this.lightList, (selectId, listFunc) =>
                {
                    btnChooseAll.MouseUpEventHandler = (sender2, e2) => {
                        if (groupControlLightList.Count > 0)
                        {
                            groupControlLightList.Clear();
                            btnChooseAll.TextID = StringId.SelectAll;
                            btnConfrim.IsSelected = false;
                        }
                        else
                        {
                            groupControlLightList.AddRange(listFunc);
                            btnChooseAll.TextID = StringId.Cancel;
                            btnConfrim.IsSelected = true;
                        }
                        LoadLightRow(listFunc);
                    };
                    nowSelectId = selectId;
                    //重新加载界面
                    LoadLightRow(listFunc);
                }, nowSelectId,100);
            };
        }
 
    }
 
 
}