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
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
 
namespace Shared.Phone.UserCenter.Device
{
    /// <summary>
    /// 设备管理的主界面
    /// </summary>
    public class DeviceManagementMainForm : UserCenterCommonForm
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 锁
        /// </summary>
        private object objLock = new object();
        /// <summary>
        /// 网关控件
        /// </summary>
        private GatewayViewRow gatewayViewRow = null;
        /// <summary>
        /// 列表控件
        /// </summary>
        private VerticalScrolViewLayout listView = null;
        /// <summary>
        /// 搜索控件
        /// </summary>
        //private SearchEditText txtSearchControl = null;
        /// <summary>
        /// 行控件的信息(Keys:Mac地址)
        /// </summary>
        private Dictionary<string, DeviceObjRowInfo> dicRowInfo = new Dictionary<string, DeviceObjRowInfo>();
        /// <summary>
        /// 重新获取设备的在线状态
        /// </summary>
        private bool reGetDeviceOnlineStatu = false;
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        public void ShowForm()
        {
            //设置标题信息
            base.SetTitleText(Language.StringByID(R.MyInternationalizationString.DeviceManagement));
 
            //右上添加按钮
            var btnAddDeviceIcon = new TopLayoutMostRightView();
            btnAddDeviceIcon.UnSelectedImagePath = "Item/Add.png";
            btnAddDeviceIcon.SelectedImagePath = "Item/AddSelected.png";
            topFrameLayout.AddChidren(btnAddDeviceIcon);
            btnAddDeviceIcon.MouseUpEventHandler += (sender, e) =>
            {
                ZbGateway realWay = null;
                if (GatewayResourse.NowSelectGateway == null || Common.LocalGateway.Current.GetRealGateway(ref realWay, GatewayResourse.NowSelectGateway) == false)
                {
                    //网关对象异常,请重新选择网关
                    string msg = Language.StringByID(R.MyInternationalizationString.uGatewayIsErrorAndReSelect);
                    this.ShowErrorMsg(msg, "OpenGatewayManagementForm");
                    return;
                }
                GatewayResourse.NowSelectGateway = realWay;
 
                var form = new Direction.AddDeviceTypeListForm();
                base.AddForm(form);
            };
 
            //初始化中部控件
            this.InitMiddleFrame();
        }
 
        /// <summary>
        /// 初始化中部控件(外部可以调用)
        /// </summary>
        /// <param name="i_reGetDeviceOnlineStatu">重新获取设备的在线状态</param>
        public void InitMiddleFrame(bool i_reGetDeviceOnlineStatu = true)
        {
            this.reGetDeviceOnlineStatu = i_reGetDeviceOnlineStatu;
            //移除全部
            bodyFrameLayout.RemoveAll();
            this.gatewayViewRow = null;
 
            //初始化网关行控件
            this.InitGatewayControl();
 
            //初始化搜索控件
            this.InitSearchControl();
 
            new System.Threading.Thread(() =>
            {
                //初始化设备列表控件
                this.InitDeviceListControl();
            })
            { IsBackground = true }.Start();
        }
 
        #endregion
 
        #region ■ 初始化网关行控件___________________
 
        /// <summary>
        /// 初始化网关行控件
        /// </summary>
        private void InitGatewayControl()
        {
            //网关行
            GatewayResourse.NowSelectGateway = Common.LocalGateway.Current.GetLocalGateway(GatewayResourse.AppOldSelectGatewayId);
            this.gatewayViewRow = new GatewayViewRow(GatewayResourse.NowSelectGateway);
            bodyFrameLayout.AddChidren(this.gatewayViewRow);
            this.gatewayViewRow.InitControl();
            this.gatewayViewRow.RemoveBaseClickEvent();
 
            this.gatewayViewRow.MouseUpEvent += (sender, e) =>
            {
                //如果点击的是不在线的网关,则当什么事都没有发生
                if (this.gatewayViewRow.IsOnline == false)
                {
                    //指定的网关不在线
                    this.ShowNormalMsg(Language.StringByID(R.MyInternationalizationString.uTheGatewayIsNotOnline));
                    return;
                }
                var form = new Gateway.GatewayInfoEditorForm();
                this.AddForm(form, GatewayResourse.NowSelectGateway);
            };
 
            //三个点
            var btnMore = new MostRightEmptyView();
            btnMore.UseClickStatu = true;
            btnMore.UnSelectedImagePath = "Item/More.png";
            btnMore.SelectedImagePath = "Item/MoreSelected.png";
            this.gatewayViewRow.AddChidren(btnMore, ChidrenBindMode.NotBind);
            btnMore.MouseUpEventHandler += (sender, e) =>
            {
                //打开网关列表界面
                this.OpenGatewayManagementForm();
            };
 
            //设置网关接受在线状态推送
            this.AddGatewayOnlinePush();
        }
 
        #endregion
 
        #region ■ 初始化搜索控件_____________________
 
        /// <summary>
        /// 初始化搜索控件
        /// </summary>
        private void InitSearchControl()
        {
            ////搜索
            //txtSearchControl = new SearchEditText();
            //bodyFrameLayout.AddChidren(txtSearchControl);
            //txtSearchControl.Y += this.gatewayViewRow.Bottom;
            ////绑定回调函数
            //txtSearchControl.BindEvent(this.SetRowDataBySearchKeys);
 
            var btnTemp = new RowLayout();
            btnTemp.Y = this.gatewayViewRow.Bottom;
            btnTemp.Height = Application.GetRealHeight(50);
            btnTemp.BackgroundColor = UserCenterColor.Current.TopFrameLayout;
            bodyFrameLayout.AddChidren(btnTemp);
 
            //列表控件
            listView = new VerticalScrolViewLayout();
            //listView.Y = txtSearchControl.Bottom + Application.GetRealHeight(30);
            //listView.Height = bodyFrameLayout.Height - txtSearchControl.Bottom - Application.GetRealHeight(50);
            listView.Y = btnTemp.Bottom;
            listView.Height = bodyFrameLayout.Height - btnTemp.Bottom;
            bodyFrameLayout.AddChidren(listView);
        }
 
        #endregion
 
        #region ■ 初始化设备列表控件_________________
 
        /// <summary>
        /// 初始化设备列表控件
        /// </summary>
        private void InitDeviceListControl()
        {
            //显示进度条
            this.ShowProgressBar();
 
            //获取设备列表
            string gwID = Common.LocalGateway.Current.GetGatewayId(GatewayResourse.NowSelectGateway);
            var listDevice = Common.LocalDevice.Current.GetDeviceByGatewayID(gwID);
 
            this.dicRowInfo.Clear();
 
            //没有设备
            if (listDevice.Count == 0)
            {
                Application.RunOnMainThread(() =>
                {
                    //关闭进度条
                    this.CloseProgressBar();
                    //在界面中间显示添加设备的提示消息
                    this.ShowAddDeviceMsg();
                });
            }
            else
            {
                //根据MAC合并设备列表
                this.MargeAllDeviceByMac(listDevice);
 
                var listOta = new List<OTADevice>();
                //添加设备的菜单行
                int count = 0;
                foreach (var rowInfo in this.dicRowInfo.Values)
                {
                    //获取ota设备
                    var ota = Common.LocalDevice.Current.GetOTADevice(rowInfo.listDevice[0].DeviceAddr);
                    if (ota != null)
                    {
                        listOta.Add(ota);
                    }
                    Application.RunOnMainThread(() =>
                    {
                        count++;
                        //添加设备的菜单行
                        this.AddDeviceMenuRow(rowInfo.listDevice);
                        if (count == this.dicRowInfo.Count)
                        {
                            //开启网关在线监测的线程
                            this.StartGatewayOnlieCheckThread();
                            //检测设备新版本
                            this.CheckDeviceNewVersion(listOta);
                            //关闭进度条
                            this.CloseProgressBar();
                        }
                    });
                }
            }
        }
 
        #endregion
 
        #region ■ 添加设备菜单行_____________________
 
        /// <summary>
        /// 添加设备的菜单行
        /// </summary>
        /// <param name="listDevice">设备对象</param>
        private void AddDeviceMenuRow(List<CommonDevice> listDevice)
        {
            var rowInfo = this.dicRowInfo[listDevice[0].DeviceAddr];
            if (rowInfo.frameLayout != null)
            {
                //直接沿用上一次的控件(按键值搜索专用)
                listView.AddChidren(rowInfo.frameLayout);
                return;
            }
 
            //创建一个可以展开和收缩的FrameLayout,相当于菜单栏
            var frame = new FrameLayout();
            frame.Height = ControlCommonResourse.ListViewRowHeight;
            listView.AddChidren(frame);
            rowInfo.frameLayout = frame;
 
            //控件
            var rowlayout = new DeviceObjectViewRow(listDevice);
            frame.AddChidren(rowlayout);
            rowlayout.InitControl();
            rowInfo.MenuRow = rowlayout;
 
            //向右的图标
            var btnRight = new MostRightEmptyView();
            btnRight.UnSelectedImagePath = "Item/Next.png";
            btnRight.SelectedImagePath = "Item/Down.png";
            rowlayout.AddChidren(btnRight, ChidrenBindMode.NotBind);
            //单击事件
            btnRight.MouseUpEventHandler += (sender, e) =>
            {
                btnRight.IsSelected = !btnRight.IsSelected;
                //展开或者隐藏列表
                this.ShowDetailList(listDevice, btnRight.IsSelected);
            };
 
            //提示新版本
            var btnNew = new InformationTipView(rowlayout.btnIcon);
            btnNew.Visible = false;
            rowlayout.AddChidren(btnNew, ChidrenBindMode.BindEventOnly);
            rowlayout.AddTag("btnNew", btnNew);
 
            //编辑
            var btnEditor = new RowEditorButton();
            rowlayout.AddRightView(btnEditor);
            btnEditor.MouseUpEventHandler += (sender, e) =>
            {
                //打开设备信息界面
                this.ShowDeviceMacInfoEditorForm(listDevice);
            };
 
            rowlayout.MouseUpEvent += (sender, e) =>
            {
                if (btnNew.Visible == true)
                {
                    //在有新版本的情况下,单击的是图标的话,跳转真实设备信息界面
                    if (sender is InformationTipView || sender is RowLeftIconView)
                    {
                        btnNew.Visible = false;
                        //打开设备信息界面
                        this.ShowDeviceMacInfoEditorForm(listDevice);
                        return;
                    }
                }
                //如果单击的是图标的话,跳转设备回路配置界面
                var form = new ConfigureDeviceListForm();
                this.AddForm(form, listDevice);
            };
        }
 
        #endregion
 
        #region ■ 添加设备明细行_____________________
 
        /// <summary>
        /// 添加设备的明细行
        /// </summary>
        /// <param name="frame">容器</param>
        /// <param name="device"></param>
        private void AddDeviceDetailRow(FrameLayout frame, CommonDevice device)
        {
            var rowInfo = this.dicRowInfo[device.DeviceAddr];
            if (rowInfo.dicDetailRow == null)
            {
                rowInfo.dicDetailRow = new Dictionary<string, DeviceRoomViewRow>();
            }
 
            //行控件
            var rowDevice = new DeviceRoomViewRow(device);
            rowDevice.Y = frame.ChildrenCount * ControlCommonResourse.ListViewRowHeight;
            frame.AddChidren(rowDevice);
            rowDevice.InitControl();
            //保存控件
            string maikey = Common.LocalDevice.Current.GetDeviceMainKeys(device);
            rowInfo.dicDetailRow[maikey] = rowDevice;
 
            //检测设备是否拥有测试的功能
            if (Common.LocalDevice.Current.DeviceIsCanTest(device) == true)
            {
                //测试
                var btnTest = new RowSecondRightIconView();
                btnTest.UnSelectedImagePath = "Item/Test.png";
                btnTest.SelectedImagePath = "Item/TestSelected.png";
                rowDevice.AddChidren(btnTest, ChidrenBindMode.NotBind);
                btnTest.MouseUpEventHandler += (sender, e) =>
                {
                    //测试
                    Common.LocalDevice.Current.SetTestCommand(device);
                };
            }
 
            //展开模式时,子控件行与顶部行的X的偏移量
            int Xvalue = ControlCommonResourse.ChidrenXvalue;
            rowDevice.btnIcon.X += Xvalue;
            rowDevice.btnDevie.X += Xvalue;
            rowDevice.btnRoom.X += Xvalue;
 
            rowDevice.MouseUpEvent += (sender, e) =>
            {
                var form = new DeviceEpointInfoForm();
                this.AddForm(form, device);
                form.ActionNameChangedEvent += (deviceName, listName) =>
                {
                    //变更房间
                    Common.Room.CurrentRoom.ChangedRoom(device, listName);
                    //刷新全部信息信息
                    rowDevice.listRoom = listName;
                    rowDevice.RefreshControlInfo(device);
                };
            };
        }
 
        #endregion
 
        #region ■ 打开设备信息界面___________________
 
        /// <summary>
        /// 打开设备信息界面
        /// </summary>
        /// <param name="listDevice"></param>
        private void ShowDeviceMacInfoEditorForm(List<CommonDevice> listDevice)
        {
            string macAddr = listDevice[0].DeviceAddr;
 
            //修改设备名字
            var form = new DeviceMacInfoEditorForm();
            form.AddForm(form, this.dicRowInfo[macAddr].listDevice);
            form.ActionFormClose += (deviceName) =>
            {
                if (string.IsNullOrEmpty(deviceName) == false)
                {
                    this.dicRowInfo[macAddr].MenuRow.btnDeviceName.Text = deviceName;
                }
            };
        }
 
        #endregion
 
        #region ■ 键值搜索___________________________
 
        /// <summary>
        /// 根据搜索键值,设定列表数据
        /// </summary>
        /// <param name="searchKey">Search key.</param>
        private void SetRowDataBySearchKeys(string searchKey)
        {
            lock (objLock)
            {
                Application.RunOnMainThread(() =>
                {
                    //首先先移除列表所有控件
                    this.listView.RemoveAll();
                });
 
                //如果搜索键值为空,则还原为原始状态:显示设备类型总览
                if (searchKey == string.Empty)
                {
                    foreach (var rowInfo in dicRowInfo.Values)
                    {
                        Application.RunOnMainThread(() =>
                        {
                            //添加设备的行
                            this.AddDeviceMenuRow(rowInfo.listDevice);
                        });
                    }
                }
                else
                {
                    //搜索名字里面包含键值的设备
                    foreach (var rowInfo in this.dicRowInfo.Values)
                    {
                        if ((rowInfo.MacName != null && rowInfo.MacName.Contains(searchKey) == true)
                            || rowInfo.DeviveTypeName.Contains(searchKey) == true)
                        {
                            Application.RunOnMainThread(() =>
                            {
                                //添加设备的行
                                this.AddDeviceMenuRow(rowInfo.listDevice);
                            });
                        }
                    }
                }
            }
        }
 
        #endregion
 
        #region ■ 展开折叠___________________________
 
        /// <summary>
        /// 展开或者隐藏列表
        /// </summary>
        /// <param name="listDevice">设备明细</param>
        /// <param name="isShow">是否展开</param>
        private void ShowDetailList(List<CommonDevice> listDevice, bool isShow)
        {
            var rowInfo = this.dicRowInfo[listDevice[0].DeviceAddr];
            //它原来的高度
            int oldHeight = rowInfo.frameLayout.Height;
            //变更的高度,默认为列表隐藏
            int heightValue = ControlCommonResourse.ListViewRowHeight;
 
            if (isShow == true)
            {
                //展开模式时,扩大依据为:它有几个子控件
                heightValue = (listDevice.Count + 1) * ControlCommonResourse.ListViewRowHeight;
                //标题自己就是一个子控件
                if (rowInfo.frameLayout.ChildrenCount == 1)
                {
                    new System.Threading.Thread(() =>
                    {
                        foreach (CommonDevice info in listDevice)
                        {
                            Application.RunOnMainThread(() =>
                            {
                                //加载它的列表
                                this.AddDeviceDetailRow(rowInfo.frameLayout, info);
                            });
                        }
                    })
                    { IsBackground = true }.Start();
                }
            }
            //自身高度变更
            rowInfo.frameLayout.Height = heightValue;
        }
 
        #endregion
 
        #region ■ 网关在线检测_______________________
 
        /// <summary>
        /// 开启网关在线监测的线程
        /// </summary>
        private void StartGatewayOnlieCheckThread()
        {
            if (GatewayResourse.NowSelectGateway == null)
            {
                return;
            }
 
            string selectGwId = Common.LocalGateway.Current.GetGatewayId(GatewayResourse.NowSelectGateway);
            new System.Threading.Thread(() =>
            {
                ZbGateway zbGateway = Common.LocalGateway.Current.GetLocalGateway(selectGwId);
                if (zbGateway == null)
                {
                    return;
                }
                //刷新网关在线状态
                Common.LocalGateway.Current.RefreshGatewayOnlineStatu(new List<ZbGateway>() { zbGateway });
 
                Application.RunOnMainThread(() =>
                {
                    if (this.gatewayViewRow != null && Common.LocalGateway.Current.GetGatewayId(this.gatewayViewRow.zbGateway) == selectGwId)
                    {
                        this.gatewayViewRow.zbGateway = zbGateway;
                        bool online = Common.LocalGateway.Current.CheckGatewayOnlineByFlag(zbGateway);
                        this.gatewayViewRow.RefreshControl(online);
                        //开启设备在线线程
                        this.StartDeviceListControlThread(online);
                    }
                });
            })
            { IsBackground = true }.Start();
        }
 
        /// <summary>
        /// 网关在线状态变更
        /// </summary>
        /// <param name="gateWay">网关对象</param>
        /// <param name="online">在线状态变更后的状态</param>
        public override void GatewayOnlinePush(ZbGateway gateWay, bool online)
        {
            if (Common.LocalGateway.Current.GetGatewayId(gateWay) == Common.LocalGateway.Current.GetGatewayId(this.gatewayViewRow.zbGateway))
            {
                Application.RunOnMainThread(() =>
                {
                    this.gatewayViewRow.zbGateway = gateWay;
                    this.gatewayViewRow.RefreshControl(online);
                });
            }
        }
 
        #endregion
 
        #region ■ 开启设备在线线程___________________
 
        /// <summary>
        /// 开启设备在线线程
        /// </summary>
        /// <param name="gatewayOnline">网关的在线状态</param>
        private void StartDeviceListControlThread(bool gatewayOnline)
        {
            if (gatewayOnline == false)
            {
                //设置全部设备离线
                this.SetAllDeviceOffLine();
                return;
            }
            //开启传感器报警监视
            this.StartCheckDeviceAlarm();
            //开启设备在线监测
            this.StartCheckDeviceOnline();
        }
 
        #endregion
 
        #region ■ 设备在线___________________________
 
        /// <summary>
        /// 开启设备在线监测
        /// </summary>
        private void StartCheckDeviceOnline()
        {
            lock (objLock)
            {
                //添加接受网关自动推送的事件
                DeviceAttributeLogic.Current.AddReceiveDeviceOnlinePushEvent("DeviceListFormReceivePushOnline", this.ReceiveDeviceStatuPush);
            }
            //外部调用的话,不再重新获取设备状态
            if (GatewayResourse.NowSelectGateway == null || this.reGetDeviceOnlineStatu == false)
            {
                return;
            }
            string gwId = Common.LocalGateway.Current.GetGatewayId(GatewayResourse.NowSelectGateway);
            new System.Threading.Thread(async () =>
            {
                //这里主要只是获取在线状态
                var zbway = Common.LocalGateway.Current.GetLocalGateway(gwId);
                var result = await Common.LocalDevice.Current.GetDeviceListFromGateway(zbway, this.ReceiveDeviceStatuPush);
            })
            { IsBackground = true }.Start();
        }
 
        /// <summary>
        /// 接受设备在线推送(网关在线推送即在线)
        /// </summary>
        /// <param name="device"></param>
        private void ReceiveDeviceStatuPush(CommonDevice device)
        {
            lock (objLock)
            {
                if (device == null || this.Parent == null)
                {
                    return;
                }
                DeviceObjectViewRow row = null;
                if (this.dicRowInfo.ContainsKey(device.DeviceAddr) == true)
                {
                    row = this.dicRowInfo[device.DeviceAddr].MenuRow;
                }
                if (row == null)
                {
                    return;
                }
                //刷新设备的在线状态
                string mainkeys = Common.LocalDevice.Current.GetDeviceMainKeys(device);
                var localDevice = Common.LocalDevice.Current.GetDevice(mainkeys);
                if (localDevice != null)
                {
                    //在线状态一样的话,不需要刷新
                    if (localDevice.IsOnline == device.IsOnline)
                    {
                        return;
                    }
                    //保存状态
                    localDevice.IsOnline = device.IsOnline;
                    localDevice.ReSave();
                }
 
                Application.RunOnMainThread(() =>
                {
                    row.SetOnlineStatu(device.IsOnline == 1);
                });
            }
        }
 
        /// <summary>
        /// 设置全部设备离线
        /// </summary>
        private void SetAllDeviceOffLine()
        {
            lock (objLock)
            {
                foreach (var rowInfo in this.dicRowInfo.Values)
                {
                    Application.RunOnMainThread(() =>
                    {
                        rowInfo.MenuRow.SetOnlineStatu(false);
                    });
                }
            }
        }
 
        #endregion
 
        #region ■ 设备新版本_________________________
 
        /// <summary>
        /// 检测设备新版本
        /// </summary>
        /// <param name="list">ota设备</param>
        private void CheckDeviceNewVersion(List<OTADevice> list)
        {
            new System.Threading.Thread(async () =>
            {
                await System.Threading.Tasks.Task.Delay(2000);
                foreach (var ota in list)
                {
                    if (this.Parent == null)
                    {
                        return;
                    }
                    //添加升级固件信息(成不成功都无所谓)
                    var result = await FirmwareUpdateLogic.AddFirmwareVersionInfo(FirmwareLevelType.ZigbeeDevice, ota.HwVersion.ToString(), ota.ImgTypeId.ToString());
 
                    //获取设备最新版本
                    var deviceFirmware = FirmwareUpdateLogic.GetFirmwareMostVersionInfo(FirmwareLevelType.ZigbeeDevice,
                        ota.HwVersion.ToString(),
                        ota.ImgTypeId.ToString(),
                        ota.ImgVersion);
 
                    if (deviceFirmware == null)
                    {
                        continue;
                    }
 
                    //拥有新版本
                    Application.RunOnMainThread(() =>
                    {
                        if (this.dicRowInfo.ContainsKey(ota.DeviceAddr) == true)
                        {
                            var row = this.dicRowInfo[ota.DeviceAddr].MenuRow;
                            if (row != null)
                            {
                                var btnNew = (InformationTipView)row.GetTagByKey("btnNew");
                                btnNew.Visible = true;
                            }
                        }
                    });
                }
            })
            { IsBackground = true }.Start();
        }
 
        #endregion
 
        #region ■ 传感器报警_________________________
 
        /// <summary>
        /// 开启传感器报警监视(仅供外部调用,网关在线状态确认后执行)
        /// </summary>
        private void StartCheckDeviceAlarm()
        {
            DeviceAttributeLogic.Current.AddSafetyAlarmEvent("DeviceListFormSensor", this.SetAlarmInfoByInterfaceResult);
        }
 
        /// <summary>
        /// 根据接口推送,设置报警信息
        /// </summary>
        /// <param name="common"></param>
        /// <param name="safetyDevice">是否是安防设备报警</param>
        private void SetAlarmInfoByInterfaceResult(CommonDevice common, bool safetyDevice)
        {
            lock (objLock)
            {
                if (this.dicRowInfo.ContainsKey(common.DeviceAddr) == false)
                {
                    return;
                }
                var row = this.dicRowInfo[common.DeviceAddr].MenuRow;
                if (row == null)
                {
                    return;
                }
 
                //获取传感器报警信息的翻译文本
                var msgInfo = Common.LocalSafeguard.Current.GetSensorAlarmInfo(common);
                if (msgInfo == null)
                {
                    return;
                }
                Application.RunOnMainThread(() =>
                {
                    row.StartSelectStatuThread(3000);
                });
            }
        }
 
        #endregion
 
        #region ■ 实现外部调用_______________________
 
        /// <summary>
        /// 添加新的设备到界面桌布中
        /// </summary>
        /// <param name="listDevice">List device.</param>
        public void AddDeviceToFormTable(List<CommonDevice> listDevice)
        {
            if (listDevice.Count == 0)
            {
                return;
            }
            //if (listView.ChildrenCount == 0)
            //{
            //    this.txtSearchControl.Visible = true;
            //}
 
            string macKey = listDevice[0].DeviceAddr;
            //新建一个对象
            if (this.dicRowInfo.ContainsKey(macKey) == false)
            {
                var rowNewInfo = new DeviceObjRowInfo();
                rowNewInfo.listDevice = listDevice;
                rowNewInfo.MacName = Common.LocalDevice.Current.GetDeviceMacName(listDevice[0]);
                rowNewInfo.DeviveTypeName = Common.LocalDevice.Current.GetDeviceObjectText(listDevice);
                this.dicRowInfo[macKey] = rowNewInfo;
 
                //创建新的行
                this.AddDeviceMenuRow(listDevice);
 
                return;
            }
            //如果存在的话
            var rowInfo = this.dicRowInfo[macKey];
            if (rowInfo.dicDetailRow == null)
            {
                //这个东西还没有展开过
                return;
            }
 
            foreach (var device in listDevice)
            {
                string mainkey = Common.LocalDevice.Current.GetDeviceMainKeys(device);
                if (rowInfo.dicDetailRow.ContainsKey(mainkey) == false)
                {
                    //这种情况应该不会出现
                    continue;
                }
                var row = rowInfo.dicDetailRow[mainkey];
                //刷新控件信息
                row.RefreshControlInfo(device);
            }
        }
 
        /// <summary>
        /// 变更网关名字(仅供外部调用)
        /// </summary>
        /// <param name="zbGateway"></param>
        public void ChangedGatewayName(ZbGateway zbGateway)
        {
            if (Common.LocalGateway.Current.GetGatewayId(zbGateway) == Common.LocalGateway.Current.GetGatewayId(this.gatewayViewRow.zbGateway))
            {
                this.gatewayViewRow.btnName.Text = Common.LocalGateway.Current.GetGatewayName(zbGateway);
                this.gatewayViewRow.zbGateway = zbGateway;
            }
        }
 
        /// <summary>
        /// 移除指定设备(仅供外部调用)
        /// </summary>
        /// <param name="listdevice">Listdevice.</param>
        public void RemoveDeviceFromMemory(List<CommonDevice> listdevice)
        {
            //缓存中删除设备
            string macAddr = listdevice[0].DeviceAddr;
            DeviceObjRowInfo rowInfo = null;
            if (macAddr == null || this.dicRowInfo.ContainsKey(macAddr) == false)
            {
                return;
            }
            rowInfo = this.dicRowInfo[macAddr];
 
            Application.RunOnMainThread(() =>
            {
                //移除
                this.dicRowInfo.Remove(macAddr);
                rowInfo.frameLayout.RemoveFromParent();
 
                if (listView.ChildrenCount == 0)
                {
                    //在界面中间显示添加设备的提示消息
                    this.ShowAddDeviceMsg();
                }
            });
        }
 
        #endregion
 
        #region ■ 合并数据___________________________
 
        /// <summary>
        /// 根据MAC合并设备列表
        /// </summary>
        /// <param name="listDevice"></param>
        private void MargeAllDeviceByMac(List<CommonDevice> listDevice)
        {
            //设备排序
            List<CommonDevice> listSort = Common.LocalDevice.Current.SortDeviceList(listDevice);
            foreach (CommonDevice device in listSort)
            {
                if (device == null || device.DeviceAddr == null)
                {
                    continue;
                }
                if (this.dicRowInfo.ContainsKey(device.DeviceAddr) == false)
                {
                    this.dicRowInfo[device.DeviceAddr] = new DeviceObjRowInfo();
                }
                this.dicRowInfo[device.DeviceAddr].listDevice.Add(device);
            }
            //收集检索用的信息
            foreach (var rowInfo in this.dicRowInfo.Values)
            {
                rowInfo.MacName = Common.LocalDevice.Current.GetDeviceMacName(rowInfo.listDevice[0]);
                rowInfo.DeviveTypeName = Common.LocalDevice.Current.GetDeviceObjectText(rowInfo.listDevice);
            }
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 在界面中间显示添加设备的提示消息
        /// </summary>
        private void ShowAddDeviceMsg()
        {
            //如果没有设备,则显示一个白板
            //this.txtSearchControl.Visible = false;
            var btnmsg = new MsgViewControl(1000, true);
            btnmsg.TextID = R.MyInternationalizationString.uClickTopRightToAdd;
            btnmsg.Gravity = Gravity.Center;
            bodyFrameLayout.AddChidren(btnmsg);
        }
 
        /// <summary>
        /// 打开网关列表界面
        /// </summary>
        public void OpenGatewayManagementForm()
        {
            var form = new Gateway.GatewayManagementForm();
            this.AddForm(form);
        }
 
        #endregion
 
        #region ■ 关闭界面___________________________
 
        /// <summary>
        /// 画面关闭
        /// </summary>
        /// <param name="isCloseForm">是否关闭界面,false的时候,只会调用关闭函数里面的附加功能</param>
        public override void CloseForm(bool isCloseForm = true)
        {
            DeviceAttributeLogic.Current.RemoveEvent("DeviceListFormSensor");
            DeviceAttributeLogic.Current.RemoveEvent("DeviceListFormReceivePushOnline");
 
            GatewayResourse.NowSelectGateway = null;
 
            base.CloseForm(isCloseForm);
        }
 
        #endregion
 
        #region ■ 结构体类___________________________
 
        /// <summary>
        /// 设备行信息
        /// </summary>
        private class DeviceObjRowInfo
        {
            /// <summary>
            /// 设备的MAC名字
            /// </summary>
            public string MacName = string.Empty;
            /// <summary>
            /// 设备类型的文本信息
            /// </summary>
            public string DeviveTypeName = string.Empty;
            /// <summary>
            /// 桌布FrameLayout
            /// </summary>
            public FrameLayout frameLayout = null;
            /// <summary>
            /// 菜单行
            /// </summary>
            public DeviceObjectViewRow MenuRow = null;
            /// <summary>
            /// 明细行
            /// </summary>
            public Dictionary<string, DeviceRoomViewRow> dicDetailRow = null;
            /// <summary>
            /// 设备列表
            /// </summary>
            public List<CommonDevice> listDevice = new List<CommonDevice>();
        }
 
        #endregion
    }
}