JLChen
2022-01-12 56bbd33ccbc6ddc457bf96b7a6842f9a07aebbe7
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
using System;
using System.Collections.Generic;
using System.Threading;
 
namespace Shared.SimpleControl.Phone
{
    public class GuideSettingGateway : FrameLayout
    {
        static GuideSettingGateway curView;
        static GatewayBase gatewayDeicve;
        static VerticalScrolViewLayout bodyView;
        static Button btnCloseLoading;
        static Loading myLoading;
        bool isReBind = false;
        static List<Common> addedCommon;
        public GuideSettingGateway(GatewayBase gd)
        {
            gatewayDeicve = gd as GatewayBase;
            addedCommon = new List<Common> ();
            BackgroundColor = SkinStyle.Current.MainColor;
            curView = this;
            myLoading = new Loading ();
 
//#if __IOS__
            myLoading.LodingBackgroundColor = 0x00999999;
//#else
//            myLoading.LodingBackgroundColor = SkinStyle.Current.ViewColor;
//#endif
            btnCloseLoading = new Button ();
            btnCloseLoading.MouseUpEventHandler += (sender, e) => {
                Application.RunOnMainThread(()=>{
                    myLoading.Hide ();
                    btnCloseLoading.RemoveFromParent ();
                    WirelessConfig (new byte [] { 1 });//close
                    if (CommonPage.newDevice > 0) {
                        CommonPage.newDevice = 0;
                        SearchDeviceList ();
                    }
                });
            };
 
        }
 
        public override void RemoveFromParent ()
        {
            curView = null;
            addedCommon.Clear ();
            BackgroundColor = SkinStyle.Current.MainColor;
            base.RemoveFromParent ();
        }
 
        public void ShowPage ()
        {
            #region 标题
            var topView = new FrameLayout () {
                Y = Application.GetRealHeight (36),
                Height = Application.GetRealHeight (90),
                Width = Application.GetRealWidth (640),
            };
            AddChidren (topView);
 
            var title = new Button () {
                TextAlignment = TextAlignment.Center,
                Text = gatewayDeicve.Name,
                TextColor = SkinStyle.Current.TextColor1,
                TextSize = 19,
            };
            topView.AddChidren (title);
 
            var back = new Button () {
                Height = Application.GetRealHeight (90),
                Width = Application.GetRealWidth (85),
                UnSelectedImagePath = "Item/Back.png",
                SelectedImagePath = "Item/BackSelected.png",
            };
            topView.AddChidren (back);
 
            //Refresh.png
            back.MouseUpEventHandler += (sender, e) => {
                (Parent as PageLayout).PageIndex -= 1;
 
                if (gatewayDeicve.MAC.Replace (".", "") == UserConfig.Instance.GatewayMAC || MainPage.LoginUser.AccountString == "464027401@qq.com") {
                    var delFile = IO.FileUtils.ReadFiles ().FindAll ((w) => {
                        return (w.Split ('_') [0] == "Equipment") && (w.Split ('_') [2] == gatewayDeicve.SubnetID.ToString ());
                    });
                    for (int k = 0; k < delFile.Count; k++) {
                        IO.FileUtils.DeleteFile (delFile [k]);
                    }
                }
            };
 
            var btnSearch = new Button () {
                Width = Application.GetRealWidth (75),
                Height = Application.GetRealHeight (75),
                X = Application.GetRealWidth (520),
                Y = Application.GetRealHeight(10),
                UnSelectedImagePath = "Item/Refresh.png",
            };
            topView.AddChidren (btnSearch);
            btnSearch.MouseUpEventHandler += (sd, fs) => {
                bodyView.RemoveAll ();
                addedCommon.Clear ();
                SearchDeviceList ();
            };
#endregion
 
            bodyView = new VerticalScrolViewLayout () { 
                Y = topView.Bottom,
                Height = Application.GetRealHeight(1136- 126 - 95),
                BackgroundColor = SkinStyle.Current.ViewColor,
            };
            AddChidren (bodyView);
 
            var bottomView = new FrameLayout () { 
                BackgroundColor = SkinStyle.Current.Black50Transparent,
                Y = bodyView.Bottom,
                Height = Application.GetRealHeight(100),
            };
            AddChidren (bottomView);
 
            var btnAssignRoom = new Button () {
                Y = 1,
                BackgroundColor = SkinStyle.Current.ButtonColor,
                Text = "Assign Room",
                TextSize = 16,
                TextAlignment = TextAlignment.Center,
                TextColor = SkinStyle.Current.TextColor1
            };
            bottomView.AddChidren (btnAssignRoom);
            btnAssignRoom.MouseUpEventHandler += (sender, e) => {
                //bodyView.RemoveAll ();
                RemoteSetting ();
            };
            SearchDeviceList ();
        }
 
        /// <summary>
        /// 远程配置
        /// </summary>
        void RemoteSetting ()
        {
            byte [] gatewayBytes = null;
            byte [] serverIPBytes = null;
 
            string [] strServerIP = (MainPage.SeviceIP).Split ('.');
            string [] strServerIP1 = (MainPage.SeviceIP).Split ('.');
            MainPage.Loading.Start ("Configuring device...");
            System.Threading.Tasks.Task.Run (() => {
                try {
                    //2022-01-12 修复计算溢出问题
                    byte regionID1 = (byte)((UserConfig.Instance.CurrentRegion.RegionID >> (6 * 4)) & 0xFF);
                    byte regionID2 = (byte)((UserConfig.Instance.CurrentRegion.RegionID >> (4 * 4)) & 0xFF);
                    byte regionID3 = (byte)((UserConfig.Instance.CurrentRegion.RegionID >> (2 * 4)) & 0xFF);
                    byte regionID4 = (byte)((UserConfig.Instance.CurrentRegion.RegionID) & 0xFF);
                    var currentRegionIdBytes = new byte [] {regionID1, regionID2, regionID3, regionID4};
 
                    //var currentRegionIdBytes = new byte [] {
                    //    (byte)(UserConfig.Instance.CurrentRegion.RegionID / 256 / 256 / 256),
                    //    (byte)(UserConfig.Instance.CurrentRegion.RegionID / 256 / 256),
                    //    (byte)(UserConfig.Instance.CurrentRegion.RegionID / 256),
                    //    (byte)(UserConfig.Instance.CurrentRegion.RegionID%256),
                    //};
 
                    gatewayBytes = Control.ControlBytesSendHasReturn (Command.ReadGateWayModelInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, new byte [] { });
                    serverIPBytes = Control.ControlBytesSendHasReturn (Command.ReadGatewayServerIP, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, new byte [] { });
 
                    gatewayDeicve.Remote_GroupName = MainPage.LoginUser.AccountString;//UserConfig.Instance.CurrentRegion.RegionName;
                    gatewayDeicve.Remote_ProjectName = gatewayDeicve.MAC.Replace (".", "");//UserConfig.Instance.CurrentRegion.RegionName;
                    gatewayDeicve.Remote_UserName = "Admin";
                    gatewayDeicve.Remote_Password = "c" + MainPage.LoginUser.MasterID;
                    byte [] ggn = new byte [20];
                    byte [] b1 = CommonPage.MyEncodingGB2312.GetBytes (gatewayDeicve.Remote_GroupName);
                    gatewayDeicve.Remote_GroupName = CommonPage.MyEncodingGB2312.GetString (b1);
                    Array.Copy (b1, 0, ggn, 0, 20 < b1.Length ? 20 : b1.Length);
 
                    byte [] gpn = new byte [20];
                    byte [] b2 = CommonPage.MyEncodingGB2312.GetBytes (gatewayDeicve.Remote_ProjectName);
                    Array.Copy (b2, 0, gpn, 0, 20 < b2.Length ? 20 : b2.Length);
 
                    byte [] gun = new byte [8];
                    byte [] b3 = CommonPage.MyEncodingGB2312.GetBytes (gatewayDeicve.Remote_UserName);
                    Array.Copy (b3, 0, gun, 0, 8 < b3.Length ? 8 : b3.Length);
 
                    byte [] gpw = new byte [8];
                    byte [] b4 = CommonPage.MyEncodingGB2312.GetBytes (gatewayDeicve.Remote_Password);
                    Array.Copy (b4, 0, gpw, 0, 8 < b4.Length ? 8 : b4.Length);
 
 
                    byte [] macAddress = Control.ControlBytesSendHasReturn (Command.ReadDeviceMac, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, new byte [] { });
 
                    gatewayBytes [0] = 2;//远程标示
                    Array.Copy (ggn, 0, gatewayBytes, 1, 20 < ggn.Length ? 20 : ggn.Length);
                    Array.Copy (gpn, 0, gatewayBytes, 21, 20 < gpn.Length ? 20 : gpn.Length);
                    Array.Copy (gun, 0, gatewayBytes, 41, 8 < gun.Length ? 8 : gun.Length);
                    Array.Copy (gpw, 0, gatewayBytes, 49, 8 < gpw.Length ? 8 : gpw.Length);
 
                    Control.ControlBytesSend (Command.SetGateWayModelInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, gatewayBytes);
 
                    serverIPBytes [0] = Convert.ToByte (strServerIP [0]);
                    serverIPBytes [1] = Convert.ToByte (strServerIP [1]);
                    serverIPBytes [2] = Convert.ToByte (strServerIP [2]);
                    serverIPBytes [3] = Convert.ToByte (strServerIP [3]);
                    int point1 = 9999;
 
                    serverIPBytes [4] = Convert.ToByte (point1 / 256);
                    serverIPBytes [5] = Convert.ToByte (point1 % 256);
 
                    serverIPBytes [6] = Convert.ToByte (strServerIP1 [0]);
                    serverIPBytes [7] = Convert.ToByte (strServerIP1 [1]);
                    serverIPBytes [8] = Convert.ToByte (strServerIP1 [2]);
                    serverIPBytes [9] = Convert.ToByte (strServerIP1 [3]);
                    int point2 = 9999;
 
                    serverIPBytes [10] = Convert.ToByte (point2 / 256);
                    serverIPBytes [11] = Convert.ToByte (point2 % 256);
 
                    //远程ip地址的设置
                    var mobytes = Control.ControlBytesSendHasReturn (Command.SetGateWayModelInternetInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, serverIPBytes);
                    if (mobytes == null) {
                        Application.RunOnMainThread (() => {
                            new Alert ("", Language.StringByID (R.MyInternationalizationString.TipEquipmentNotOnline),
                                       Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                        });
                    } else if (mobytes [0] == 0xF5) {
                        Application.RunOnMainThread (() => {
                            new Alert ("", Language.StringByID (R.MyInternationalizationString.OperationFailed),
                                       Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                        });
                    } else {
                        if (gatewayDeicve.Type == DeviceType.OnePortWirelessFR) {
                            var bindReginIdStatus = Control.ControlBytesSendHasReturn (Command.Write_APP_Data_STORE_1D5E_CMD, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, currentRegionIdBytes);
                            if (bindReginIdStatus == null) {
                                Application.RunOnMainThread (() => {
                                    new Alert ("", Language.StringByID (R.MyInternationalizationString.TipEquipmentNotOnline), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                                });
                                return;
                            } else if (bindReginIdStatus [0] != 0xF8) {
                                //new Alert ("", "网关绑定住宅失败!", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                                Application.RunOnMainThread (() => {
                                    new Alert ("", "Gateway failed to bind residence!", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                                });
                                return;
                            }
                        }
                        Application.RunOnMainThread (() => {
                            MainPage.Loading.Start ("Gateway setting succeeded,data up.Please wait...");
                        });
                        IO.FileUtils.SaveEquipmentMessage (gatewayDeicve);
                        var gatewayMAC = gatewayDeicve.MAC.Replace (".", "");
                        var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (new EditMACByHomeId { RegionID = UserConfig.Instance.CurrentRegion.RegionID, MAC = gatewayMAC, IsReBind = true });
                        var respone = MainPage.RequestHttps ("EditMACByHomeId", requestJson);
                        if (respone.StateCode != "SUCCESS") {
                            Application.RunOnMainThread (() => {
                                new Alert ("", "Communication abnormality,gateway failed to bind residence!", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                                if (MainPage.LoginUser.AccountString == "464027401@qq.com") {
                                    new Alert (respone.StateCode, respone.ErrorInfo, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                                }
                            });
                            return;
                        }
                        var gatewayListObj = new GatewayListObj ();
                        var gatewayListJson = Newtonsoft.Json.JsonConvert.SerializeObject (gatewayListObj);
                        var gatewayListRevertObj = MainPage.RequestHttps ("GatewayList", gatewayListJson);
                        if (gatewayListRevertObj.StateCode == "SUCCESS") {
                            var responseGatewayDataObj = Newtonsoft.Json.JsonConvert.DeserializeObject<List<GatewayRes>> (gatewayListRevertObj.ResponseData.ToString ());
                            if (responseGatewayDataObj.Count > 0) {
                                UserConfig.Instance.GatewayList.Clear ();
                                UserConfig.Instance.GatewayList.AddRange (responseGatewayDataObj);
                                UserConfig.Instance.GatewayMAC = gatewayDeicve.MAC;
                                UserConfig.Instance.SaveUserConfig ();
                                MainPage.LoginUser.LastTime = DateTime.Now;
                                Application.RunOnMainThread (() => {
                                    UserMiddle.Init (true);
                                });
                            } else {
                                Application.RunOnMainThread (() => {
                                    new Alert ("", "Communication abnormality,gateway failed to bind residence.Please try again.", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                                    if (MainPage.LoginUser.AccountString == "464027401@qq.com") {
                                        new Alert ("22:" + respone.StateCode, respone.ErrorInfo, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                                    }
                                });
                            }
                        }
                    }
                } catch (Exception ex) {
                    Console.WriteLine (ex.Message);
                    Application.RunOnMainThread (() => {
                        new Alert ("", Language.StringByID (R.MyInternationalizationString.OperationFailed), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                    });
                    bool canRemove = false;
                } finally {
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Hide ();
                        //this.RemoveAll ();
                    });
                }
            });
            //Room.InitAllRoom ();
        }
 
        /// <summary>
        /// 开关无线配置
        /// </summary>
        void WirelessConfig (byte [] switchStatus)
        {
            Control.ControlBytesSend (Command.CheckConfigSuccess, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, switchStatus);
        }
 
        /// <summary>
        /// 搜索功能模块设备
        /// </summary>
        void SearchDeviceList ()
        {
            //var delFile = IO.FileUtils.ReadFiles ().FindAll ((w) => {
            //    return (w.Split ('_') [0] == "Equipment") && (w.Split ('_') [2] == gatewayDeicve.SubnetID.ToString ());
            //});
            //for (int k = 0; k < delFile.Count; k++) {
            //    IO.FileUtils.DeleteFile (delFile [k]);
            //}
            //MainPage.Loading.Start ("正在搜索设备列表。。。");
            MainPage.Loading.Start ("Searching for existing Devices...");
            CommonPage.DeviceList.Clear ();
            int countDevcie = 0;
            //全部重新更新设备下面所有的回路信息
            if (th != null) {
                th.Abort ();
                th = null;
            }
            ReadDevices (() => {
                MainPage.Loading.Hide ();
                for (int i = 0; i < CommonPage.DeviceList.Count; i++) {
                    Common common = CommonPage.DeviceList [i];
 
                    if (common.Type == DeviceType.MusicModel)
                        continue;
                    if (common.isMixBox) {
                        Console.WriteLine ("IsMixBox");
                        continue;
                    }
                    countDevcie++;
                }
 
                notLoopCommons.Clear ();
                notLoopCommons.AddRange (CommonPage.DeviceList);
 
                th = new Thread ((obj) => {
                    Waiting = false;
                    CommonPage.DeviceLoopList.Clear ();
                    var listCommonEquipment = new List<Common> ();
                    listCommonEquipment.AddRange (notLoopCommons);
                    int progress = 0;
                    for (int i = 0; i < listCommonEquipment.Count; i++) {
                        var common = listCommonEquipment [i];
                        if (common == null)
                            continue;
                        Application.RunOnMainThread (() => {
                            if(!common.isMixBox){
                                progress++;
                            }
                            MainPage.Loading.Start ("Loading:" + common.Name + " ,Progress " + progress.ToString () + "/" + CommonPage.searchTotal.ToString ());
                        });
                        byte bigClass = common.BigClass;
                        byte minClass = common.MinClass;
                        int count = common.LoopCount;
                        for (int k = 1, j = 0; k <= count; k++) {
                            while (Waiting) {
                                Thread.Sleep (100);
                            }
                            if (Control.ControlBytesSendHasReturn (Command.ReadDeviceLoopInfo, common.SubnetID, common.DeviceID, new byte [] { bigClass, minClass, (byte)k }) == null) {
                                j++;
                                //连续两次读取不到数据则跳出该循环
                                if (j == 2 && k == 2)
                                    break;
                            } else {
                                notLoopCommons.Remove (common);
                            }
                        }
                    }
                    th = null;
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Hide ();
                        if (gatewayDeicve.Type == DeviceType.OnePortWirelessFR && CommonPage.newDevice==0) {
                            CommonPage.newDevice = 0;
                            Control.ControlBytesSend (Command.GotoConfigMode, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, new byte [] { });
                            //new Alert ("", "网关已经进入配频模式,请手动新设备进入配频模式。", "Close").Show ();
                            new Alert ("", "Searching for new devices, Please make sure all new devices are in configuration mode.", "OK").Show ();
 
                            this.AddChidren (myLoading);
                            //myLoading.Start ("点击屏幕关闭配频模式");
                            myLoading.Start ("Searching, Please touch the here to exit the searching mode.");
                            this.AddChidren (btnCloseLoading);
                        }
                    });
                });
                th.Start ();
            });
        }
        static List<Common> notLoopCommons = new List<Common> ();
 
        public static void InitSearchDeviceList (Common common, bool config = false)
        {
            if (curView == null || addedCommon == null)
                return;
            lock (addedCommon) {
                if (addedCommon.Find ((obj)=>obj.Type == common.Type&& obj.CommonLoopID == common.CommonLoopID)!=null)
                    return;
                addedCommon.Add (common);
                if (common.SubnetID == gatewayDeicve.SubnetID) {
                    var deviceView = new RowLayout () {
                        Height = Application.GetRealHeight (110),
                    };
                    bodyView.AddChidren (deviceView);
                    #region Show Device
                    var btnIndex = new Button () {
                        Width = Application.GetRealWidth (120),
                        TextColor = SkinStyle.Current.TextColor1,
                        TextAlignment = TextAlignment.Center,
                        Text = bodyView.ChildrenCount.ToString ()
                    };
                    deviceView.AddChidren (btnIndex);
 
                    var btnDeviceName = new Button () {
                        X = btnIndex.Right,
                        Text = common.Name,
                        TextAlignment = TextAlignment.CenterLeft,
                        Width = Application.GetRealWidth (342),
                        TextColor = SkinStyle.Current.TextColor1,
                    };
                    deviceView.AddChidren (btnDeviceName);
 
                    //var btnDeviceOnline = new Button () { 
                    //    X    = Application.GetRealWidth(500),
                    //    Y = Application.GetRealHeight(70),
                    //    Width = Application.GetRealWidth(100),
                    //    Height = Application.GetRealHeight(40),
                    //    TextSize = 12,
                    //    Text = "Online",
                    //    TextColor = 0xFF5DBA46,
                    //    TextAlignment = TextAlignment.CenterLeft
                    //};
                    //deviceView.AddChidren (btnDeviceOnline);
                    //if (!common.Online) {
                    //    btnDeviceOnline.Text = "Offline";
                    //    btnDeviceOnline.TextColor = SkinStyle.Current.DelColor;
                    //}
 
                    EventHandler<MouseEventArgs> eHandler = (sender, e) => {
                        Dialog dialog = new Dialog ();
                        FrameLayout dialogBodyView = new FrameLayout () {
                            BackgroundColor = SkinStyle.Current.DialogColor,
                            Width = Application.GetRealWidth (494),
                            Height = Application.GetRealHeight (488),
                            Radius = 5,
                            BorderColor = SkinStyle.Current.Transparent,
                            BorderWidth = 0,
                            Gravity = Gravity.Center
                        };
                        dialog.AddChidren (dialogBodyView);
                        Button btnEquipmentTitle = new Button () {
                            Height = Application.GetRealHeight (70),
                            TextAlignment = TextAlignment.Center,
                            Text = "Device Setting",
                            BackgroundColor = SkinStyle.Current.DialogTitle,
                            TextColor = SkinStyle.Current.TextColor1,
                        };
                        dialogBodyView.AddChidren (btnEquipmentTitle);
                        Button lblChangeName = new Button () {
                            X = Application.GetRealWidth (40),
                            Y = btnEquipmentTitle.Bottom + Application.GetRealHeight (10),
                            Width = dialog.Width - 2 * btnEquipmentTitle.X,
                            Height = btnEquipmentTitle.Height,
                            TextID = R.MyInternationalizationString.EquipmentName,
                            TextAlignment = TextAlignment.CenterLeft,
                            TextColor = SkinStyle.Current.TextColor
                        };
                        dialogBodyView.AddChidren (lblChangeName);
                        EditText btnChangeName = new EditText () {
                            X = lblChangeName.X,
                            Y = lblChangeName.Bottom + Application.GetRealHeight (10),
                            Width = Application.GetRealWidth (425),
                            Height = Application.GetRealHeight (50),
                            TextAlignment = TextAlignment.CenterLeft,
                            Text = " " + common.Name,
                            BorderColor = SkinStyle.Current.BorderColor,
                            BorderWidth = 1,
                            Radius = 5,
                            TextColor = SkinStyle.Current.TextColor,
                        };
                        dialogBodyView.AddChidren (btnChangeName);
 
                        Button btnFindDevcie = new Button () {
                            Y = btnChangeName.Bottom + Application.GetRealHeight (50),
                            Gravity = Gravity.CenterHorizontal,
                            Width = Application.GetRealWidth (300),
                            Height = Application.GetRealHeight (70),
                            TextID = R.MyInternationalizationString.PositioningEquipment,
                            TextAlignment = TextAlignment.Center,
                            Radius = (uint)Application.GetRealHeight (5),
                            BackgroundColor = SkinStyle.Current.ButtonColor,
                            SelectedBackgroundColor = SkinStyle.Current.SelectedColor,
                            TextColor = SkinStyle.Current.TextColor1
                        };
                        dialogBodyView.AddChidren (btnFindDevcie);
                        btnFindDevcie.MouseDownEventHandler += (sender1, e1) => {
                            btnFindDevcie.IsSelected = true;
                        };
                        btnFindDevcie.MouseUpEventHandler += (sender1, e1) => {
                            btnFindDevcie.IsSelected = false;
                            //查找设备,发送命令让设备闪烁,面板设备与其他设备发送的数据不一样,多了一个byte,固定为:0 
                            Control.ControlBytesSend (Command.PositioningEquipment, common.SubnetID, common.DeviceID, new byte [] { 5 });
                        };
                        //---bottom--
                        Button btnBack = new Button () {
                            Width = Application.GetRealWidth (250),
                            Height = Application.GetRealHeight (85),
                            Y = Application.GetRealHeight (490 - 85),
                            BackgroundColor = SkinStyle.Current.DialogTitle,
                            TextID = R.MyInternationalizationString.Close,
                            TextColor = SkinStyle.Current.TextColor1
                        };
                        btnBack.MouseUpEventHandler += (s, e1) => {
                            dialog.Close ();
                        };
                        dialogBodyView.AddChidren (btnBack);
                        Button btnLineV = new Button () {
                            Width = 1,
                            Height = btnBack.Height,
                            BackgroundColor = SkinStyle.Current.White20Transparent,
                            X = btnBack.Right,
                            Y = btnBack.Y,
                        };
                        dialogBodyView.AddChidren (btnLineV);
                        Button btnSave = new Button () {
                            Width = Application.GetRealWidth (250),
                            Height = Application.GetRealHeight (85),
                            X = btnLineV.Right,
                            Y = btnBack.Y,
                            TextID = R.MyInternationalizationString.SAVE,
                            TextColor = SkinStyle.Current.TextColor1,
                            BackgroundColor = SkinStyle.Current.DialogTitle,
                        };
                        dialogBodyView.AddChidren (btnSave);
                        btnSave.MouseUpEventHandler += (sender2, e2) => {
                            //CommonPage.UpdateRemark (common.SubnetID, common.DeviceID, btnChangeName.Text.Trim ());
 
                            byte [] updateBytes = new byte [20];
                            byte [] remakeBytes = CommonPage.MyEncodingGB2312.GetBytes (btnChangeName.Text.Trim ());
                            Array.Copy (remakeBytes, 0, updateBytes, 0, remakeBytes.Length < 20 ? remakeBytes.Length : 20);
                            Control.ControlBytesSend (Command.Write_DeviceRamarkCMD, common.SubnetID, common.DeviceID, updateBytes);
 
 
                            btnDeviceName.Text = btnChangeName.Text;
                            dialog.Close ();
                            common.Name = btnChangeName.Text.Trim ();
                            IO.FileUtils.SaveEquipmentMessage (common);
                        };
                        dialog.Show ();
 
                    };
                    if (common.Type != DeviceType.InfraredMode) {
                        btnDeviceName.MouseUpEventHandler += eHandler;
                    }
 
                    var btnDelDevice = new Button () {
                        TextID = R.MyInternationalizationString.Del,
                        BackgroundColor = SkinStyle.Current.DelColor,
                    };
                    deviceView.AddRightView (btnDelDevice);
 
                    btnDelDevice.MouseUpEventHandler += (sender, e) => {
                        Alert alert = new Alert ("", Language.StringByID (R.MyInternationalizationString.TipDeleteEquipmentMessage)
                                        , Language.StringByID (R.MyInternationalizationString.Cancel), Language.StringByID (R.MyInternationalizationString.Confrim));
                        alert.Show ();
                        alert.ResultEventHandler += ( sender1,  e1) => {
                            if (e1) {
                                MainPage.Loading.Start ("Please wait...");
                                System.Threading.Tasks.Task.Run (() => {
                                    try {
                                        var delRev = Control.ControlBytesSendHasReturn (Command.DelDeviceCMD, common.SubnetID, 0, new byte [] { 1, 0, 0, 0, 0, 0, 0, 0, 0, common.SubnetID, common.DeviceID });
 
                                        string delFlag = "_" + common.SubnetID + "_" + common.DeviceID;
                                        var delFile = IO.FileUtils.ReadFiles ().FindAll ((w) => {
                                            return w.Contains (delFlag);
                                        });
                                        foreach (var roomToDelDevice in Room.Lists) {
                                            var dett = roomToDelDevice.DeviceFilePathList.FindAll ((xxs) => {
                                                return xxs.Contains (delFlag);
                                            });
                                            if (dett != null) {
                                                foreach (var delT in dett) {
                                                    roomToDelDevice.DeviceFilePathList.Remove (delT);
                                                }
                                                roomToDelDevice.Save (roomToDelDevice.RoomFilePath);
                                            }
                                        }
 
                                        for (int i = 0; i < delFile.Count; i++) {
                                            IO.FileUtils.DeleteFile (delFile [i]);
                                        }
                                        Application.RunOnMainThread (() => {
                                            btnDelDevice.Parent.RemoveFromParent ();
                                        });
                                    } catch (Exception ex) {
                                        Console.WriteLine (ex.Message);
                                    } finally {
                                        Application.RunOnMainThread (() => {
                                            MainPage.Loading.Hide ();
                                        });
                                    }
                                });
                            }
                        };
                    };
 
                    //if (common.Type == DeviceType.InfraredMode) {
                    //    notLoopCommons = new List<Common> ();
                    //    EventHandler<MouseEventArgs> mouseEvent = (sender, e) => {
                    //        //new SystemEquipmentBase ().EquipmentBaseViewShow (common);
                    //        var systemEquipmentBase = new GuideSettingInfraredModule ();
                    //        systemEquipmentBase.Show ();
                    //        systemEquipmentBase.EquipmentBaseViewShow (common);
                    //    };
                    //    deviceView.MouseUpEventHandler += mouseEvent;
                    //}
 
                    if (config) {
                        CommonPage.newDevice++;
                        //System.Threading.Tasks.Task.Factory.StartNew (() => {
                        //    try {
                        //        byte bigClass = common.BigClass;
                        //        byte minClass = common.MinClass;
                        //        int count = common.LoopCount;
                        //        for (int k = 1, j = 0; k <= count; k++) {
                        //            if (Control.ControlBytesSendHasReturn (Command.ReadDeviceLoopInfo, common.SubnetID, common.DeviceID, new byte [] { bigClass, minClass, (byte)k }) == null) {
                        //                j++;
                        //                //连续两次读取不到数据则跳出该循环
                        //                if (j == 2 && k == 2)
                        //                    break;
                        //            }
                        //        }
                        //    } catch (Exception ex) {
                        //        Console.WriteLine ("Cinfig Read Device Loop Info " + ex.Message);
                        //    }
                        //});
                    }
 
                    #endregion
                } else {
                    CommonPage.searchTotal--;
                }
            }
        }
        Thread th;
        //等待红外读取红外类型
        bool Waiting;
 
        void ReadDevices (Action action)
        {
            new System.Threading.Thread (() => {
                try {
                    //这里搜索设备,直到完成就退出
                    readDevice ();
                } catch (Exception ex) {
                    Console.WriteLine (ex.Message);
                }
                Application.RunOnMainThread (action);
            }) { IsBackground = true }.Start ();
        }
 
        /// <summary>
        /// 读取设备
        /// </summary>
        void readDevice ()
        {
            CommonPage.FindGateway = false;
            CommonPage.FindGatewayChilren = true;
            CommonPage.LocalPhoneFindDevice = true;
            CommonPage.RandomHigh = (byte)new Random ().Next (255);
            CommonPage.RandomLow = (byte)new Random ().Next (255);
            CommonPage.DeviceList.Clear ();
            CommonPage.searchTotal = 0;
            //如果两次都没有数据反馈,就不读取
            int readCount = 4;
            while (0 < readCount) {
                readCount--;
                var ms = new System.IO.MemoryStream ();
                ms.WriteByte (CommonPage.RandomHigh);
                ms.WriteByte (CommonPage.RandomLow);
 
                var list = CommonPage.DeviceList;
                int tempCount = list.Count;
                string s = ";";
                for (int i = 0; i < list.Count; i++) {
                    Common common = list [i];
                    if (common.SubnetID != gatewayDeicve.SubnetID)
                        continue;
                    if (s.Contains (";" + common.SubnetID.ToString () + ":" + common.DeviceID.ToString ()))
                        continue;
                    s += common.SubnetID.ToString () + ":" + common.DeviceID.ToString () + ";";
                    ms.WriteByte (common.SubnetID);
                    ms.WriteByte (common.DeviceID);
                }
                Console.WriteLine ("搜索非网关设备,网络地址是:" + CommonPage.EndPoint.ToString ());
                var control = new Control ();
                control.Send (new Target () {
                    IPEndPoint = CommonPage.EndPoint,
                    Command = Command.ReadDeviceModul,
                    SubnetID = gatewayDeicve.SubnetID,
                    DeviceID = 0xFF,
                    AddData = ms.ToArray ()
                }, SendCount.Zero, false);
 
                ms.Close ();
 
                System.Threading.Thread.Sleep (1000);
                //如果数量不相等,就重置次数
                if (tempCount != CommonPage.DeviceList.Count) {
                    readCount = 4;
                }
            }
            CommonPage.DeviceList = CommonPage.DeviceList.FindAll ((obj) => { return obj != null && obj.SubnetID == gatewayDeicve.SubnetID; });
            CommonPage.LocalPhoneFindDevice = false;
            CommonPage.FindGatewayChilren = false;
        }
 
    }
}