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
using System;
using System.Collections.Generic;
using Shared.Common;
using Shared.Phone.UserView;
using ZigBee.Device;
namespace Shared.Phone.UserCenter.Device
{
    /// <summary>
    /// 卷帘设置反向与限位
    /// </summary>
    public class RollerShadeDirectionLimitSettingForm: UserCenterCommonForm,ZigBee.Common.IStatus
    {
        #region ◆ 设备接口__________________________
        /// <summary>
        /// Changed the specified common.
        /// </summary>
        /// <param name="common">Common.</param>
        public void Changed(CommonDevice common)
        {
            //throw new NotImplementedException();
        }
        /// <summary>
        /// 设备状态更新接口
        /// <para>type:如果为 DeviceInComingRespon:设备新上报</para>
        /// <para>type:如果为 IASInfoReport:RemoveDeviceRespon</para>
        /// <para>type:如果为 DeviceStatusReport:设备上报</para>
        /// <para>type:如果为 IASInfoReport:IAS安防信息上报</para>
        /// <para>type:如果为 OnlineStatusChange: 设备在线状态更新</para>
        /// </summary>
        /// <param name="common">Common.</param>
        /// <param name="typeTag">Type tag.</param>
        public void DeviceInfoChange(CommonDevice common, string typeTag)
        {
            if (typeTag == "DeviceStatusReport")
            {
                Application.RunOnMainThread(() =>
                {
                    try
                    {
                        var device = listRollershade[0];
 
                        //是否为当前设备
                        if (device.DeviceEpoint != common.DeviceEpoint || device.DeviceAddr != common.DeviceAddr)
                        {
                            return;
                        }
                        if ((common as Rollershade).DeviceStatusReport == null || (common as Rollershade).DeviceStatusReport.CluterID != 258)
                        {
                            return;
                        }
                        var attriButeList = (common as Rollershade).DeviceStatusReport.AttriBute;
                        if(attriButeList==null ||  attriButeList.Count==0)
                        {
                            return;
                        }
                        switch (attriButeList[0].AttributeId)
                        {
                            case 0:
                                //类型
                                rollershade.DeviceStatusReport = (common as Rollershade).DeviceStatusReport;
                                rollershade.WcdType = attriButeList[0].AttriButeData;
                                rollershade.LastDateTime = DateTime.Now;
                                break;
                            case 1:
                                //总长
                                rollershade.DeviceStatusReport = (common as Rollershade).DeviceStatusReport;
                                rollershade.WcdCurrentLength = attriButeList[0].AttriButeData;
                                windowCurrentLength = rollershade.WcdCurrentLength;
                                rollershade.LastDateTime = DateTime.Now;
                                break;
                            case 23:
                                //方向
                                rollershade.DeviceStatusReport = (common as Rollershade).DeviceStatusReport;
                                if (0 < (attriButeList[0].AttriButeData & 0x01))
                                {
                                    //电机反向
                                    rollershade.WcdCurrentDirection = true;
                                    directionBtn.TextID = R.MyInternationalizationString.ReverseDirection;
                                }
                                else
                                {
                                    //电机正向
                                    rollershade.WcdCurrentDirection = false;
                                    directionBtn.TextID = R.MyInternationalizationString.ForwardDirection;
                                }
                                rollershade.LastDateTime = DateTime.Now;
                                break;
                            case 16:
                                //上限位
                                rollershade.DeviceStatusReport = (common as Rollershade).DeviceStatusReport;
                                if (windowCurrentLength != 0)
                                {
                                    rollershade.WcdCurrentWcdInstalOpenLimit = attriButeList[0].AttriButeData;
                                    startSeekBar.Progress = (int)(rollershade.WcdCurrentWcdInstalOpenLimit / (windowCurrentLength * 1.0) * 100);
                                    startBtn.Text = $"{Language.StringByID(R.MyInternationalizationString.SetStartLimit)} : {startSeekBar.Progress}%";
                                    rollershade.LastDateTime = DateTime.Now;
                                }
                                break;
                            case 17:
                                //下限位
                                rollershade.DeviceStatusReport = (common as Rollershade).DeviceStatusReport;
                                if (windowCurrentLength != 0)
                                {
                                    rollershade.WcdCurrentInstalCloseLimit = attriButeList[0].AttriButeData;
                                    endSeekBar.Progress = (int)(rollershade.WcdCurrentInstalCloseLimit / (windowCurrentLength * 1.0) * 100);
                                    endBtn.Text = $"{Language.StringByID(R.MyInternationalizationString.SetEndLimit)} : {endSeekBar.Progress}%";
                                    rollershade.LastDateTime = DateTime.Now;
                                }
                                break;
                            case 8:
                                //窗帘百分比
                                //发送设置上限位
                                //发送设置下限位
                                rollershade.DeviceStatusReport = (common as Rollershade).DeviceStatusReport;
                                rollershade.LastDateTime = DateTime.Now;
                                rollershade.WcdCurrentPositionLiftPercentage= attriButeList[0].AttriButeData;
                                System.Console.WriteLine($"收到了回复:{rollershade.WcdCurrentPositionLiftPercentage}  {startSeekBar.Progress} {endSeekBar.Progress}");
                                if (canSetStartLimit && System.Math.Abs(rollershade.WcdCurrentPositionLiftPercentage - startSeekBar.Progress) < 5)
                                {
                                    hadReceivedStartEndLimitCode = true;
                                    CommonPage.Loading.Hide();
                                    var alert = new Alert(Language.StringByID(R.MyInternationalizationString.TIP), Language.StringByID(R.MyInternationalizationString.SettingStartLimit), Language.StringByID(R.MyInternationalizationString.Cancel), Language.StringByID(R.MyInternationalizationString.Confrim));
                                    alert.Show();
                                    alert.ResultEventHandler += async (sender2, e2) =>
                                    {
                                        if (e2)
                                        {
                                            try
                                            {
                                                CommonPage.Loading.Start();
                                                var status = await SetLimitAsync(Rollershade.CurtainPrivateInstalledLimi.UpLimit, rollershade);
                                                if (status == 0)
                                                {
                                                    System.Console.WriteLine("上行程设置成功");
                                                    var tip = new TipViewControl(Language.StringByID(R.MyInternationalizationString.Success));
                                                    tip.ShowView();
                                                }
                                                else if (status == -1)
                                                {
                                                    CommonPage.Instance.ShowErrorInfoAlert(R.MyInternationalizationString.ConnectGWFail);
                                                }
                                                else
                                                {
                                                    CommonPage.Instance.ShowErrorInfoAlert(R.MyInternationalizationString.FAIL);
                                                }
                                            }
                                            catch (Exception ex)
                                            {
                                                System.Console.WriteLine($"设置上限位失败{ex.Message}");
                                            }
                                            finally
                                            {
                                                CommonPage.Loading.Hide();
                                            }
                                        }
                                    };
                                }
                                if (canSetEndLimit && System.Math.Abs(rollershade.WcdCurrentPositionLiftPercentage - endSeekBar.Progress) < 5)
                                {
                                    hadReceivedStartEndLimitCode = true;
                                    CommonPage.Loading.Hide();
                                    var alert = new Alert(Language.StringByID(R.MyInternationalizationString.TIP), Language.StringByID(R.MyInternationalizationString.SettingEndLimit), Language.StringByID(R.MyInternationalizationString.Cancel), Language.StringByID(R.MyInternationalizationString.Confrim));
                                    alert.Show();
                                    alert.ResultEventHandler += async (sender2, e2) =>
                                    {
                                        if (e2)
                                        {
                                            try
                                            {
                                                CommonPage.Loading.Start();
                                                var status = await SetLimitAsync(Rollershade.CurtainPrivateInstalledLimi.DownLimit, rollershade);
                                                if (status == 0)
                                                {
                                                    System.Console.WriteLine("下行程设置成功");
                                                    var tip = new TipViewControl(Language.StringByID(R.MyInternationalizationString.Success));
                                                    tip.ShowView();
                                                }
                                                else if (status == -1)
                                                {
                                                    CommonPage.Instance.ShowErrorInfoAlert(R.MyInternationalizationString.ConnectGWFail);
                                                }
                                                else
                                                {
                                                    CommonPage.Instance.ShowErrorInfoAlert(R.MyInternationalizationString.FAIL);
                                                }
                                            }
                                            catch (Exception ex)
                                            {
                                                System.Console.WriteLine($"设置下限位失败{ex.Message}");
                                            }
                                            finally
                                            {
                                                CommonPage.Loading.Hide();
                                            }
                                        }
                                    };
                                }
                                break;
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine($"Error:{ex.Message}");
                    }
                });
            }
        }
        /// <summary>
        /// Changeds the IL ogic status.
        /// </summary>
        /// <param name="logic">Logic.</param>
        public void ChangedILogicStatus(ZigBee.Device.Logic logic)
        {
            //throw new NotImplementedException();
        }
        /// <summary>
        /// Changeds the IS cene status.
        /// </summary>
        /// <param name="scene">Scene.</param>
        public void ChangedISceneStatus(Scene scene)
        {
            //throw new NotImplementedException();
        }
        #endregion
 
        #region ◆ 变量_____________________________
        /// <summary>
        /// 设置方向与限位的设备--窗帘
        /// </summary>
        private List<CommonDevice> listRollershade = null;
        /// <summary>
        /// The rollershade.
        /// </summary>
        private Rollershade rollershade = null;
        /// <summary>
        /// 起始方向btn
        /// </summary>
        private RowSecondRightTextView directionBtn = null;
        /// <summary>
        /// 设置开始的限位的滑条
        /// </summary>
        private HorizontalSeekBar startSeekBar = null;
        /// <summary>
        /// 设置限位的开始百分比按钮
        /// </summary>
        private Button startBtn = null;
        /// <summary>
        /// 设置结束限位的滑条
        /// </summary>
        private HorizontalSeekBar endSeekBar = null;
        /// <summary>
        /// 设置限位的结束百分比按钮
        /// </summary>
        private Button endBtn = null;
        //延时 300毫秒
        private readonly double sleepSpan = 0.3;
        /// <summary>
        /// 是否可以发送设置上限位命令了
        /// </summary>
        private bool canSetStartLimit = false;
        /// <summary>
        /// 接口中是否收到了设置开始\结束限位的导轨移动的百分比
        /// </summary>
        private bool hadReceivedStartEndLimitCode = false;
        /// <summary>
        /// 是否可以发送设置结束限位命令了
        /// </summary>
        ///
        private bool canSetEndLimit = false;
        /// <summary>
        /// 窗帘总长
        /// </summary>
        private int windowCurrentLength = 0;
        /// <summary>
        /// 设置限位的线程列表
        /// </summary>
        static List<System.Threading.Thread> threads = new List<System.Threading.Thread> { };
        #endregion
 
        #region ◆ 构造方法__________________________
        /// <summary>
        /// 构造方法
        /// </summary>
        public RollerShadeDirectionLimitSettingForm()
        {
            UserView.HomePage.Instance.ScrollEnabled = false;
            ZigBee.Device.ZbGateway.StatusList.Add(this);
        }
 
        #endregion
 
        #region ◆ 关闭界面__________________________
        /// <summary>
        /// 画面关闭
        /// </summary>
        /// <param name="isCloseForm">是否关闭界面,false的时候,只会调用关闭函数里面的附加功能</param>
        public override void CloseForm(bool isCloseForm = true)
        {
            UserView.HomePage.Instance.ScrollEnabled = true;
            ZigBee.Device.ZbGateway.StatusList.Remove(this);
            base.CloseForm(isCloseForm);
        }
 
        #endregion
 
        #region ◆ 显示界面__________________________
 
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        /// <param name="i_listdevice">设备</param>
        public void ShowForm(List<CommonDevice> i_listdevice)
        {
            if(i_listdevice==null || i_listdevice.Count==0)
            {
                return;
            }
            this.listRollershade = i_listdevice;
            this.rollershade = listRollershade[0] as Rollershade;
 
            //设置标题信息
            base.SetTitleText(Language.StringByID(R.MyInternationalizationString.DirectionLimit));
 
            //初始化中部控件
            this.InitMiddleFrame();
        }
 
        #endregion
 
        #region ◆ 初始化中部控件_____________________
        /// <summary>
        /// 初始化中部控件
        /// </summary>
        private void InitMiddleFrame()
        {
            bodyFrameLayout.RemoveAll();
 
            var listview = new VerticalScrolViewLayout();
            listview.ScrollEnabled = false;
            bodyFrameLayout.AddChidren(listview);
            //读取状态
            InitReadStatu();
            //起始方向
            AddinitialDirectionRow(listview);
            //限位
            AddDirectionLimitRow(listview);
            //恢复行程设置
            AddRestoreTravelSettingsRow(listview);
        }
        #endregion
 
        #region ◆ 读取状态__________________________
        /// <summary>
        /// 读取状态
        /// </summary>
        private void InitReadStatu()
        {
            //不上非远程
            if (rollershade.Gateway == null)
            {
                return;
            }
            UserHomeView.ReadStatus(rollershade, () =>
            {
                rollershade.ReadWcdType();
                rollershade.ReadWcdLength();
                rollershade.ReadCurtainDirectionAndMode();
                rollershade.ReadWcdInstalOpenLimit();
                rollershade.ReadWcdInstalCloseLimit();
            }, 1);
        }
 
        #endregion
 
        #region ◆ 添加 【起始方向】行_________________
 
        /// <summary>
        /// 添加 【起始方向】行
        /// </summary>
        /// <param name="listview">Lisrview.</param>
        private void AddinitialDirectionRow(VerticalScrolViewLayout listview)
        {
            var initialDirectionRow = new StatuRowLayout(listview);
 
            var initialDirectionBtn = new RowCenterView(false);
            initialDirectionBtn.TextID = R.MyInternationalizationString.InitialDirection;
            initialDirectionRow.AddChidren(initialDirectionBtn);
 
            directionBtn = new RowSecondRightTextView();
            directionBtn.TextID = rollershade.WcdCurrentDirection == false ? R.MyInternationalizationString.ForwardDirection : R.MyInternationalizationString.ReverseDirection;
            directionBtn.TextColor = UserCenterColor.Current.TextGrayColor;
            initialDirectionRow.AddChidren(directionBtn);
 
            var directionRightBtn = new RowRightIconView();
            initialDirectionRow.AddChidren(directionRightBtn);
            initialDirectionRow.MouseUpEvent +=  (sender, e) =>
            {
                try
                {
                    CommonPage.Loading.Start();
                    System.Console.WriteLine("方向");
                    var list = new List<string> { };
                    list.Add(Language.StringByID(R.MyInternationalizationString.ForwardDirection));
                    list.Add(Language.StringByID(R.MyInternationalizationString.ReverseDirection));
                    PickerView.Show(list, async (obj) =>
                    {
                        directionBtn.Text = obj;
                        bool direction = directionBtn.Text.Trim() == Language.StringByID(R.MyInternationalizationString.ReverseDirection);
                        var setWritableValueResponAllData = await rollershade.SetCurtainDirectionAsync(direction);
                        if (setWritableValueResponAllData == null || setWritableValueResponAllData.setWritableValueResponData == null)
                        {
                            CommonPage.Loading.Hide();
                            Shared.Common.CommonPage.Instance.ShowErrorInfoAlert(R.MyInternationalizationString.ConnectGWFail);
                            return;
                        }
                        var status = setWritableValueResponAllData.setWritableValueResponData.Status;
                        //以后补充提示
                        //0:配置成功(若配置成功,下面的AttributeId字段不存在)
                        //34:不支持该属性
                        //135:无效的属性值
                        //141:无效的数据类型
                        if (status == 0)
                        {
                            System.Console.WriteLine("设置方向成功");
                            rollershade.WcdCurrentDirection = direction;
                        }
                        else
                        {
                            CommonPage.Loading.Hide();
                            Shared.Common.CommonPage.Instance.ShowErrorInfoAlert(R.MyInternationalizationString.FAIL);
                        }
                    }, Language.StringByID(R.MyInternationalizationString.Confrim));
                }
                catch(Exception ex)
                {
                    System.Console.WriteLine($"设置方向行错误:{ex.Message}");
                }
                finally
                {
                    CommonPage.Loading.Hide();
                }
 
            };
 
        }
 
        #endregion
 
        #region ◆ 添加 【限位】行____________________
        /// <summary>
        /// 添加 【限位】行
        /// </summary>
        /// <param name="listview">Lisrview.</param>
        private void AddDirectionLimitRow(VerticalScrolViewLayout listview)
        {
            if(rollershade.WcdType==0)
            {
                //AddDirectionLimitRowJL(listview);
            }
            else if(rollershade.WcdType==4)
            {
                AddDirectionLimitRowKHL(listview);
            }
 
        }
 
        /// <summary>
        /// 添加 卷帘【限位】行
        /// </summary>
        /// <param name="listview">Lisrview.</param>
        private void AddDirectionLimitRowJL(VerticalScrolViewLayout listview)
        {
            var directionLimitRow = new RowLayout();
            directionLimitRow.Height = ControlCommonResourse.ListViewRowHeight;
            directionLimitRow.LineColor = ZigbeeColor.Current.GXCLineColor;
            listview.AddChidren(directionLimitRow);
 
            var directionLimitBtn = new RowCenterView(false);
            directionLimitBtn.TextID = R.MyInternationalizationString.Limit;
            directionLimitRow.AddChidren(directionLimitBtn);
            //方向与限位设置向右的图标
            //向右的图标
            var btnDirectionRight = new Button()
            {
                X = directionLimitRow.Width - Application.GetRealWidth(150),
                Height = Application.GetMinRealAverage(110),
                Width = Application.GetMinRealAverage(110),
                UnSelectedImagePath = "Item/Next.png",
                SelectedImagePath = "Item/DownSelected.png",
                Gravity = Gravity.CenterVertical
            };
            //btnDirectionRight.UnSelectedImagePath = "Item/Next.png";
            //btnDirectionRight.SelectedImagePath = "Item/Down.png";
            directionLimitRow.AddChidren(btnDirectionRight);
        }
 
        /// <summary>
        /// 添加 开合帘【限位】行
        /// </summary>
        /// <param name="listview">Lisrview.</param>
        private void AddDirectionLimitRowKHL(VerticalScrolViewLayout listview)
        {
            var directionLimitRow = new RowLayout();
            directionLimitRow.Height = ControlCommonResourse.ListViewRowHeight;
            directionLimitRow.LineColor = ZigbeeColor.Current.GXCLineColor;
            listview.AddChidren(directionLimitRow);
 
            var directionLimitBtn = new RowCenterView(false);
            directionLimitBtn.TextID = R.MyInternationalizationString.Limit;
            directionLimitRow.AddChidren(directionLimitBtn);
            //方向与限位设置向右的图标
            //向右的图标
            var btnDirectionRight = new Button()
            {
                X = directionLimitRow.Width - Application.GetRealWidth(150),
                Height = Application.GetMinRealAverage(110),
                Width = Application.GetMinRealAverage(110),
                UnSelectedImagePath = "Item/Next.png",
                SelectedImagePath = "Item/DownSelected.png",
                Gravity = Gravity.CenterVertical
            };
            //btnDirectionRight.UnSelectedImagePath = "Item/Next.png";
            //btnDirectionRight.SelectedImagePath = "Item/Down.png";
            directionLimitRow.AddChidren(btnDirectionRight);
 
            var startLimitRowView = new FrameLayout()
            {
                Y = ControlCommonResourse.ListViewRowHeight,
                Height = ControlCommonResourse.ListViewRowHeight * 2,
                BackgroundColor = ZigbeeColor.Current.GXCBackgroundColor,
                Tag = "startEndLimitRowView"
            };
            directionLimitRow.AddChidren(startLimitRowView);
 
            var startLine = new Button()
            {
                Height = 1,
                BackgroundColor = ZigbeeColor.Current.GXCLineColor
            };
            startLimitRowView.AddChidren(startLine);
 
            startSeekBar = new HorizontalSeekBar()
            {
                Height = Application.GetRealHeight(80),
                Width = Application.GetRealWidth(750),
                Gravity = Gravity.Center,
                ThumbColor = ZigbeeColor.Current.GXCButtonBlueColor,
                BackgroundColor = ZigbeeColor.Current.GXCSliderUnSelectedColor,
                ProgressColor = ZigbeeColor.Current.GXCButtonBlueColor,
                Max = 100,
                Progress = 0
            };
            startLimitRowView.AddChidren(startSeekBar);
 
            startBtn = new Button()
            {
                Y = Application.GetRealHeight(30),
                Height = Application.GetRealHeight(80),
                X = Application.GetRealWidth(CommonPage.XLeft),
                Width = Application.GetRealWidth(500),
                TextColor = ZigbeeColor.Current.GXCTextBlackColor,
                TextAlignment = TextAlignment.CenterLeft,
                Text = $"{Language.StringByID(R.MyInternationalizationString.SetStartLimit)} : {startSeekBar.Progress}%"
            };
            startLimitRowView.AddChidren(startBtn);
 
            var endLimitRowView = new FrameLayout()
            {
                Y = startLimitRowView.Bottom,
                Height = ControlCommonResourse.ListViewRowHeight * 2,
                BackgroundColor = ZigbeeColor.Current.GXCBackgroundColor,
                Tag = "startEndLimitRowView"
            };
            directionLimitRow.AddChidren(endLimitRowView);
 
            var endLine = new Button()
            {
                Height = 1,
                BackgroundColor = ZigbeeColor.Current.GXCLineColor
            };
            endLimitRowView.AddChidren(endLine);
 
            endSeekBar = new HorizontalSeekBar()
            {
                Y = endLine.Bottom,
                Height = Application.GetRealHeight(80),
                BackgroundColor = ZigbeeColor.Current.GXCSliderUnSelectedColor,
                Width = Application.GetRealWidth(750),
                Gravity = Gravity.Center,
                ThumbColor = ZigbeeColor.Current.GXCButtonBlueColor,
                ProgressColor = ZigbeeColor.Current.GXCButtonBlueColor,
                Max = 100,
                Progress = 100
            };
            endLimitRowView.AddChidren(endSeekBar);
            endBtn = new Button()
            {
                Y = Application.GetRealHeight(30),
                Height = Application.GetRealHeight(80),
                X = Application.GetRealWidth(CommonPage.XLeft),
                Width = Application.GetRealWidth(500),
                TextColor = ZigbeeColor.Current.GXCTextBlackColor,
                TextAlignment = TextAlignment.CenterLeft,
                Text = $"{Language.StringByID(R.MyInternationalizationString.SetEndLimit)} : {endSeekBar.Progress}%"
            };
            endLimitRowView.AddChidren(endBtn);
 
            bool isStartSendDelayFrist = false;
            bool isEndSendDelayFrist = false;
 
            DateTime lastStartSendDelayTime = DateTime.Now;
            DateTime lastEndSendDelayTime = DateTime.Now;
 
            startSeekBar.ProgressChanged += async (sender1, e1) =>
            {
                canSetStartLimit = false;
                startBtn.Text = $"{Language.StringByID(R.MyInternationalizationString.SetStartLimit)} : {startSeekBar.Progress}%";
                if (isStartSendDelayFrist == false)
                {
                    isStartSendDelayFrist = true;
                    //先发送删除限位点命令
                    var t = await DeleteUpLimitAsync(rollershade);
                    endBtn.Text = $"{Language.StringByID(R.MyInternationalizationString.SetEndLimit)} : {endSeekBar.Progress}%";
                    rollershade.WcdGoToTiltValue(startSeekBar.Progress);
                    lastStartSendDelayTime = DateTime.Now;
                }
                else
                {
                    if ((DateTime.Now - lastStartSendDelayTime).TotalSeconds > sleepSpan)
                    {
                        rollershade.WcdGoToTiltValue(startSeekBar.Progress);
                        lastStartSendDelayTime = DateTime.Now;
                    }
                }
            };
            startSeekBar.MouseUpEventHandler += (sender1, e1) =>
            {
                isStartSendDelayFrist = false;
                canSetStartLimit = true;
                hadReceivedStartEndLimitCode = false;
                rollershade.WcdGoToTiltValue(startSeekBar.Progress);
                lastStartSendDelayTime = DateTime.Now;
                CommonPage.Loading.Start(Language.StringByID(R.MyInternationalizationString.PleaseWaitWhileTheGuidewayIsMoving));
                DeviceDelayAction(rollershade, (hadReceived) =>
                {
                    Application.RunOnMainThread(() =>
                    {
                        if (hadReceived == false)
                        {
                            var tip = new TipViewControl(Language.StringByID(R.MyInternationalizationString.FailedPleaseTryAgain));
                            tip.ShowView();
                        }
                        else
                        {
                            //var tip = new TipViewControl(Language.StringByID(R.MyInternationalizationString.Success));
                            //tip.ShowView();
                        }
                        CommonPage.Loading.Hide();
                    });
                }, 5);
            };
 
            endSeekBar.ProgressChanged += async (sender1, e1) =>
            {
                canSetEndLimit = false;
                endBtn.Text = $"{Language.StringByID(R.MyInternationalizationString.SetEndLimit)} : {endSeekBar.Progress}%";
                if (isEndSendDelayFrist == false)
                {
                    isEndSendDelayFrist = true;
                    //先发送删除限位点命令
                    await DeleteDownLimintAsync(rollershade);
                    rollershade.WcdGoToTiltValue(endSeekBar.Progress);
                    lastEndSendDelayTime = DateTime.Now;
                }
                else
                {
                    if ((DateTime.Now - lastEndSendDelayTime).TotalSeconds > sleepSpan)
                    {
                        rollershade.WcdGoToTiltValue(endSeekBar.Progress);
                        lastEndSendDelayTime = DateTime.Now;
                    }
                }
            };
            endSeekBar.MouseUpEventHandler += (sender1, e1) =>
            {
                isEndSendDelayFrist = false;
                hadReceivedStartEndLimitCode = false;
                canSetEndLimit = true;
                rollershade.WcdGoToTiltValue(endSeekBar.Progress);
                lastEndSendDelayTime = DateTime.Now;
                CommonPage.Loading.Start(Language.StringByID(R.MyInternationalizationString.PleaseWaitWhileTheGuidewayIsMoving));
                DeviceDelayAction(rollershade, (hadReceived) =>
                {
                    Application.RunOnMainThread(() =>
                    {
                        if (hadReceived == false)
                        {
                            var tip = new TipViewControl(Language.StringByID(R.MyInternationalizationString.FailedPleaseTryAgain));
                            tip.ShowView();
                        }
                        else
                        {
                            //var tip = new TipViewControl(Language.StringByID(R.MyInternationalizationString.Success));
                            //tip.ShowView();
                        }
                        CommonPage.Loading.Hide();
                    });
                }, 5);
 
            };
 
            void limit_MouseUpEvnet(object sender, MouseEventArgs mouseEventArgs)
            {
                btnDirectionRight.IsSelected = !btnDirectionRight.IsSelected;
                if (btnDirectionRight.IsSelected)
                {
                    directionLimitRow.Height = ControlCommonResourse.ListViewRowHeight * 5;
                }
                else
                {
                    directionLimitRow.Height = ControlCommonResourse.ListViewRowHeight;
                }
            }
            btnDirectionRight.MouseUpEventHandler += limit_MouseUpEvnet;
            directionLimitBtn.MouseUpEventHandler += limit_MouseUpEvnet;
            directionLimitRow.MouseUpEventHandler += limit_MouseUpEvnet;
        }
 
        #endregion
 
        #region ◆ 添加 【恢复行程设置】行______________
        /// <summary>
        /// 添加 【恢复行程设置】行
        /// </summary>
        /// <param name="listview">Lisrview.</param>
        private void AddRestoreTravelSettingsRow(VerticalScrolViewLayout listview)
        {
            var RestoreTravelSettingsRow = new StatuRowLayout(listview);
 
            var RestoreTravelSettingsRowBtn = new RowCenterView(false);
            RestoreTravelSettingsRowBtn.TextID = R.MyInternationalizationString.RestoreTravelSettings;
            RestoreTravelSettingsRow.AddChidren(RestoreTravelSettingsRowBtn);
            RestoreTravelSettingsRow.MouseUpEvent += (sender, e) =>
            {
                try
                {
                    CommonPage.Loading.Start();
                    System.Console.WriteLine("恢复限位");
                    var alert = new Alert(Language.StringByID(R.MyInternationalizationString.TIP), Language.StringByID(R.MyInternationalizationString.IsCancelRestoreTravelSettings), Language.StringByID(R.MyInternationalizationString.Cancel), Language.StringByID(R.MyInternationalizationString.Confrim));
                    alert.Show();
                    alert.ResultEventHandler += async (alertSender, alertE) =>
                    {
                        if (alertE)
                        {
                            var setWritableValueResponAllData = await rollershade.RestoreCurtainLimitAsync();
                            if(setWritableValueResponAllData==null || setWritableValueResponAllData.setWritableValueResponData==null)
                            {
                                CommonPage.Loading.Hide();
                                CommonPage.Instance.ShowErrorInfoAlert(R.MyInternationalizationString.FAIL);
                                return;
                            }
                            var status = setWritableValueResponAllData.setWritableValueResponData.Status;
                            //以后补充提示
                            //0:配置成功(若配置成功,下面的AttributeId字段不存在)
                            //34:不支持该属性
                            //135:无效的属性值
                            //141:无效的数据类型
                            if (status == 0)
                            {
                                //恢复行程成功后手动去读取状态
                                InitReadStatu();
                                System.Console.WriteLine("恢复行程设置成功");
                                var tip = new TipViewControl(Language.StringByID(R.MyInternationalizationString.Success));
                                tip.ShowView();
 
                            }
                            else
                            {
                                CommonPage.Loading.Hide();
                                CommonPage.Instance.ShowErrorInfoAlert(R.MyInternationalizationString.FAIL);
                            }
                        }
                    };
                }
                catch(Exception ex)
                {
                    System.Console.WriteLine($"恢复行程设置失败{ex.Message}");
                }
                finally
                {
                    CommonPage.Loading.Hide();
                }
            };
        }
 
        #endregion
 
        #region ◆ 设备延时回调_______________________
        /// <summary>
        /// 设备延时回调
        /// </summary>
        /// <param name="commonDevice">Common device.</param>
        /// <param name="action">Action.</param>
        /// <param name="delayTime">延时执行时间,默认10秒</param>
        public void DeviceDelayAction(CommonDevice commonDevice,Action<bool> action,int delayTime=10)
        {
            var threadName = commonDevice.GetHashCode().ToString();
            var tt = threads.Find((obj) => obj.Name == threadName);
            if (threads.Find((obj) => obj.Name==threadName)==null)
            {
                var thread = new System.Threading.Thread(() =>
                  {
                      var dateTime = DateTime.Now;
                      while ((DateTime.Now - dateTime).TotalSeconds < delayTime)
                      {
                          System.Threading.Thread.Sleep(100);
                      }
                      lock (threads)
                      {
                          threads.RemoveAll((obj) => obj.Name == threadName);
                      }
                      action?.Invoke(hadReceivedStartEndLimitCode);
                  })
                { IsBackground = true, Name = threadName };
                lock (threads)
                {
                    threads.Add(thread);
                }
                thread.Start();
            }
            else
            {
                System.Console.WriteLine("dddd");
            }
        }
 
        #endregion
 
        #region ◆ 设置上、下限位______________________
        /// <summary>
        /// 设置上、下限位
        /// <para>0 :成功</para>
        /// <para>-1 :失败(网关不在线)</para>
        /// <para>34:不支持该属性</para>
        /// <para>135:无效的属性值</para>
        /// <para>141:无效的数据类型</para>
        /// </summary>
        /// <returns>The limit async.</returns>
        /// <param name="upOrDownLimit">上或者下限位</param>
        /// <param name="r">窗帘.</param>
        private async System.Threading.Tasks.Task<int> SetLimitAsync(Rollershade.CurtainPrivateInstalledLimi upOrDownLimit, Rollershade r)
        {
            var setWritableValueResponAllData = await r.SetCurtainInstalledLimitAsync(upOrDownLimit);
            if (setWritableValueResponAllData == null || setWritableValueResponAllData.setWritableValueResponData == null)
            {
                return -1;
            }
            return setWritableValueResponAllData.setWritableValueResponData.Status;
        }
 
        #endregion
 
        #region ◆ 删除限位点_________________________
        /// <summary>
        /// 删除上限位点
        /// </summary>
        /// <returns>The up limits async.</returns>
        /// <param name="r">The red component.</param>
        private async System.Threading.Tasks.Task<CommonDevice.SetWritableValueResponAllData> DeleteUpLimitAsync(Rollershade r)
        {
            return await r.DeleteCurtainLimitsAsync(Rollershade.LimiType.UpLimit);
        }
 
        /// <summary>
        /// 删除下限位点
        /// </summary>
        /// <returns>The down limint async.</returns>
        /// <param name="r">The red component.</param>
        private async System.Threading.Tasks.Task<CommonDevice.SetWritableValueResponAllData> DeleteDownLimintAsync(Rollershade r)
        {
            return await r.DeleteCurtainLimitsAsync(Rollershade.LimiType.DownLimit);
        }
 
        #endregion
 
 
    }
}