JLChen
2020-06-04 6d55af8792cf8fbef0055e677b900fc352dba9a2
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
using System;
using Shared;
using Shared.SimpleControl.R;
using Shared.SimpleControl;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
 
namespace SmartHome.UI.SimpleControl.Phone
{
    public class YiPanelSceneList:FrameLayout
    {
        YIPanel yipanel;
        public List<YiPanelScene> localSceneList = new List<YiPanelScene> ();
        public void Show (YIPanel yipanel, string name)
        {
            this.yipanel = yipanel;
            this.BackgroundColor = 0xFF1F1F1F;
            this.AddChidren (new Button {
                Height = Application.GetRealHeight (30),
            });
 
            var topFrameLayout = new FrameLayout {
                Height = Application.GetRealHeight (100),
                Y = Application.GetRealHeight (30),
            };
            AddChidren (topFrameLayout);
 
            var titleName = new Button {
                Text = name,
                //TextID = MyInternationalizationString.adddevice,
                TextSize = 17,
            };
            topFrameLayout.AddChidren (titleName);
 
            var add = new Button {
                Width = Application.GetRealWidth (72),
                Height = Application.GetRealHeight (58),
                UnSelectedImagePath = "MusicIcon/add.png",
                X = Application.GetRealWidth (525),
                Gravity = Gravity.CenterVertical,
            };
            topFrameLayout.AddChidren (add);
 
 
            var back = new Button {
                Width = Application.GetRealWidth (82),
                Height = Application.GetRealHeight (89),
                X = Application.GetRealWidth (10),
                Gravity = Gravity.CenterVertical,
                UnSelectedImagePath = "MusicIcon/HomepageBack.png",
            };
            topFrameLayout.AddChidren (back);
            back.MouseDownEventHandler += (sender, e) => {
                RemoveFromParent ();
            };
 
            var middle = new VerticalScrolViewLayout ();
            middle.Y = topFrameLayout.Bottom;
            middle.Height = Application.GetRealHeight (1136 - 130-80);
            middle.BackgroundColor = 0xff2F2F2F;
            this.AddChidren (middle);
 
 
            ///添加点击事件
            add.MouseUpEventHandler += (sender, e) => {
                localdata ();
                MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.load));
                System.Threading.Tasks.Task.Run (() => {
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Hide ();
                        var localSceneListPage = new LocalSceneListPage ();
                        MainPage.MainFrameLayout.AddChidren (localSceneListPage);
                        localSceneListPage.Show (() => { sceneview (middle); }, localSceneList,yipanel);
                    });
                });
            };
 
 
            if (yipanel.yIPanelSceneList.Count == 0) {
                refreshsecenelist (() => {
                    Application.RunOnMainThread (() => {
                        sceneview (middle);
                    });
                });
            } else {
                sceneview (middle);
            }
     
 
            var btnText = new Button {
                Y = middle.Bottom,
                Height = Application.GetRealHeight (80),
                TextID = MyInternationalizationString.synchronizepaneldata,
                TextSize = 16,
            };
            this.AddChidren (btnText);
            btnText.MouseUpEventHandler += (sender, e) => {
                var alert = new Alert (Language.StringByID (MyInternationalizationString.prompt), Language.StringByID (MyInternationalizationString.synchronizepaneldata),
                           Language.StringByID (MyInternationalizationString.cancel), Language.StringByID (MyInternationalizationString.confirm));
                alert.ResultEventHandler += (sender1, e1) => {
                    if (e1) {
                        syncscenedata ();
                    }
                };
                alert.Show ();
 
            };         
 
        }
        /// <summary>
        /// 更新本地场景最新数据
        /// </summary>
        void localdata(){
          
            localSceneList.Clear ();
            try {
                var filesList = Shared.IO.FileUtils.ReadFiles ().FindAll ((obj) => {
                    string [] str = obj.Split ('_');
                    return obj.StartsWith ("RoomScene_") || obj.StartsWith ("GlobalScene_");
                });
 
                foreach (var file in filesList) {
                    var scenestring = System.Text.Encoding.UTF8.GetString (Shared.IO.FileUtils.ReadFile (file));
                    if (scenestring == null) {
                        continue;
                    }
                    var jObject = JObject.Parse (scenestring);
                    if (jObject == null) {
                        continue;
                    }
 
                    YiPanelScene yiPanelScene = new YiPanelScene ();
                    yiPanelScene.Name = jObject ["Name"]?.ToString ();
                    var deviceFilePathList = JArray.Parse (jObject ["DeviceFilePathList"]?.ToString ());
                    foreach (var filepath in deviceFilePathList) {
                        var devicefilepath = filepath.ToString ();
                        var devicestring = System.Text.Encoding.UTF8.GetString (Shared.IO.FileUtils.ReadFile (devicefilepath));
                        if (devicestring == null) {
                            continue;
                        }
                        var jObject1 = Newtonsoft.Json.Linq.JObject.Parse (devicestring);
                        if (jObject1 == null) {
                            continue;
                        }
                       
                        switch ((DeviceType)jObject1.Value<int> ("Type")) {
 
                        case DeviceType.LightSwitch: {
 
                                YiPanelLightSwitch lightSwitch = new YiPanelLightSwitch ();
                                lightSwitch.large = 1;
                                lightSwitch.Name = jObject1 ["Name"]?.ToString ();
                                lightSwitch.SubnetID = jObject1.Value<byte> ("SubnetID");
                                lightSwitch.DeviceID = jObject1.Value<byte> ("DeviceID");
                                lightSwitch.LoopID = jObject1.Value<byte> ("LoopID");
                                lightSwitch.CurrentBrightness = jObject1.Value<byte> ("CurrentBrightness");
                                for (int i = 1; i <= 64; i++) {
                                    var devicenumber = yiPanelScene.scenedevicelist.Find ((c) => (c.large == 1) && (c.channelnumber == (byte)i));
                                    if (devicenumber == null) {
                                        lightSwitch.channelnumber = (byte)i;
                                        break;
                                    }
                                }
                                var dev = lightSwitch as YiPanelCommon;
                                yiPanelScene.scenedevicelist.Add (dev);
                            }
                            break;
                        case DeviceType.LightDimming: {
 
                                YiPanelLightDimming lightDimming = new YiPanelLightDimming ();
                                lightDimming.large = 1;
                                lightDimming.Name = jObject1 ["Name"]?.ToString ();
                                lightDimming.SubnetID = jObject1.Value<byte> ("SubnetID");
                                lightDimming.DeviceID = jObject1.Value<byte> ("DeviceID");
                                lightDimming.LoopID = jObject1.Value<byte> ("LoopID");
                                lightDimming.CurrentBrightness = jObject1.Value<byte> ("CurrentBrightness");
                                for (int i = 1; i <= 64; i++) {
                                    var devicenumber = yiPanelScene.scenedevicelist.Find ((c) => (c.large == 1) && (c.channelnumber == (byte)i));
                                    if (devicenumber == null) {
                                        lightDimming.channelnumber = (byte)i;
                                        break;
                                    }
                                }
                                var dev = lightDimming as YiPanelCommon;
                                yiPanelScene.scenedevicelist.Add (dev);
                            }
                            break;
                        case DeviceType.CurtainModel: {
 
                                YiPanelCurtainModel curtainModel = new YiPanelCurtainModel ();
                                curtainModel.large =2;
                                curtainModel.Name = jObject1 ["Name"]?.ToString ();
                                curtainModel.SubnetID = jObject1.Value<byte> ("SubnetID");
                                curtainModel.DeviceID = jObject1.Value<byte> ("DeviceID");
                                curtainModel.LoopID = jObject1.Value<byte> ("LoopID");
                                curtainModel.Status = (CurtainStatus)jObject1.Value<int> ("Status");
                                for (int i = 1; i <= 8; i++) {
                                    var devicenumber = yiPanelScene.scenedevicelist.Find ((c) => (c.large == 2) && (c.channelnumber == (byte)i));
                                    if (devicenumber == null) {
                                        curtainModel.channelnumber = (byte)i;
                                        break;
                                    }
                                }
                                var dev = curtainModel as YiPanelCommon;
                                yiPanelScene.scenedevicelist.Add (dev);
 
                            }
                            break;
                        case DeviceType.CurtainRoller: {
                                
                                YiPanelCurtainRoller curtainRoller = new YiPanelCurtainRoller ();
                                curtainRoller.large = 2;
                                curtainRoller.Name = jObject1 ["Name"]?.ToString ();
                                curtainRoller.SubnetID = jObject1.Value<byte> ("SubnetID");
                                curtainRoller.DeviceID = jObject1.Value<byte> ("DeviceID");
                                curtainRoller.LoopID = jObject1.Value<byte> ("LoopID");
                                curtainRoller.CurtainProress = jObject1.Value<byte> ("CurtainProress");
                                for (int i = 1; i <= 8; i++) {
                                    var devicenumber = yiPanelScene.scenedevicelist.Find ((c) => (c.large == 2) && (c.channelnumber == (byte)i));
                                    if (devicenumber == null) {
                                        curtainRoller.channelnumber = (byte)i;
                                        break;
                                    }
                                }
                                var dev = curtainRoller as YiPanelCommon;
                                yiPanelScene.scenedevicelist.Add (dev);
                            }
                            break;
                        case DeviceType.FreshAir: {
                                //device.typevalue = 4;
                                //b = 8;
                                //var freshAir = device as FreshAir;
                            }
                            break;
                        case DeviceType.HVAC: {
 
                                YiPanelHVAC hvac = new YiPanelHVAC ();
                                hvac.large = 7;
                                hvac.Name = jObject1 ["Name"]?.ToString ();
                                hvac.SubnetID = jObject1.Value<byte> ("SubnetID");
                                hvac.DeviceID = jObject1.Value<byte> ("DeviceID");
                                hvac.LoopID = jObject1.Value<byte> ("LoopID");
                                hvac.Power = jObject1.Value<byte> ("Power");
                                hvac.SetFanSpeed = jObject1.Value<byte> ("SetFanSpeed");
                                hvac.SetMode = jObject1.Value<byte> ("SetMode");
                                hvac.SetTemperature = jObject1.Value<byte> ("SetTemperature");
                                for (int i = 1; i <= 8; i++) {
                                    var devicenumber = yiPanelScene.scenedevicelist.Find ((c) => (c.large == 7) && (c.channelnumber == (byte)i));
                                    if (devicenumber == null) {
                                        hvac.channelnumber = (byte)i;
                                        break;
                                    }
                                }
                                var dev = hvac as YiPanelCommon;
                                yiPanelScene.scenedevicelist.Add (dev);
                            }
                            break;
                        case DeviceType.FoolHeat: {
 
                                YiPanelFoolHeat foolHeat = new YiPanelFoolHeat ();
                                foolHeat.large = 8;
                                foolHeat.Name = jObject1 ["Name"]?.ToString ();
                                foolHeat.SubnetID = jObject1.Value<byte> ("SubnetID");
                                foolHeat.DeviceID = jObject1.Value<byte> ("DeviceID");
                                foolHeat.LoopID = jObject1.Value<byte> ("LoopID");
                                foolHeat.PWDStatus = jObject1.Value<byte> ("PWDStatus");
                                foolHeat.TemperatureType = jObject1.Value<byte> ("TemperatureType");
                                foolHeat.TemperatureTypeValue = jObject1.Value<byte> ("TemperatureTypeValue");
 
                                for (int i = 1; i <= 8; i++) {
                                    var devicenumber = yiPanelScene.scenedevicelist.Find ((c) => (c.large == 8) && (c.channelnumber == (byte)i));
                                    if (devicenumber == null) {
                                        foolHeat.channelnumber = (byte)i;
                                        break;
                                    }
                                }
                                var dev = foolHeat as YiPanelCommon;
                                yiPanelScene.scenedevicelist.Add (dev);
                            }
                            break;
                        case DeviceType.MusicModel: {
 
                                YiPanelMusicModel musicModel = new YiPanelMusicModel ();
                                musicModel.large = 9;
                                musicModel.Name = jObject1 ["Name"]?.ToString ();
                                musicModel.SubnetID = jObject1.Value<byte> ("SubnetID");
                                musicModel.DeviceID = jObject1.Value<byte> ("DeviceID");
                                musicModel.LoopID = jObject1.Value<byte> ("LoopID");
                                musicModel.CurVol = jObject1 ["CurVol"]?.ToString ();
                                musicModel.playMode = jObject1.Value<byte> ("PlayMode");
                                musicModel.PlayStatus = (YiPanelMusicModel.Status)jObject1.Value<byte> ("PlayStatus");
 
                                for (int i = 1; i <= 9; i++) {
                                    var devicenumber = yiPanelScene.scenedevicelist.Find ((c) => (c.large == 8) && (c.channelnumber == (byte)i));
                                    if (devicenumber == null) {
                                        musicModel.channelnumber = (byte)i;
                                        break;
                                    }
                                }
                                var dev = musicModel as YiPanelCommon;
                                yiPanelScene.scenedevicelist.Add (dev);
                            }
                            break;
 
                        }
 
 
                    }
 
                    localSceneList.Add (yiPanelScene);
 
                }
 
                foreach (var localscene in localSceneList) {
                    var currscene = yipanel.yIPanelSceneList.Find ((c) => c.Name == localscene.Name);
                    if (currscene != null) {
                        foreach (var device in localscene.scenedevicelist) {
                            var dev = currscene.scenedevicelist.Find ((c) => (c.Type == device.Type) && (c.CommonLoopID == device.CommonLoopID));
                            if (dev == null) {
                                int b = 0;
                                switch (device.Type) {
                                case DeviceType.LightSwitch:
                                case DeviceType.LightDimming: {
                                        device.large = 1;
                                        b = 64;
                                    }
                                    break;
                                case DeviceType.CurtainModel:
                                case DeviceType.CurtainRoller: {
                                        device.large = 2;
                                        b = 8;
                                    }
                                    break;
                                case DeviceType.FreshAir: {
                                        //device.large = 4;
                                        //b = 8;
                                    }
                                    break;
                                case DeviceType.HVAC: {
                                        device.large = 7;
                                        b = 8;
                                    }
                                    break;
                                case DeviceType.FoolHeat: {
                                        device.large = 8;
                                        b = 8;
                                    }
                                    break;
                                case DeviceType.MusicModel: {
                                        device.large = 9;
                                        b = 8;
                                    }
                                    break;
 
                                }
 
                                for (int i = 1; i <= b; i++) {
                                    var devicenumber = currscene.scenedevicelist.Find ((c) => (c.large == device.large) && (c.channelnumber == (byte)i));
                                    if (devicenumber == null) {
                                        device.channelnumber = (byte)i;
                                        currscene.scenedevicelist.Add (device);
                                        break;
                                    }
                                }
 
                            } else {
                                device.channelnumber = dev.channelnumber;
                                currscene.scenedevicelist.Remove (dev);
                                currscene.scenedevicelist.Add (device);
                            }
                        }
                    }
                }
           
            } catch(Exception e) {
                var s = e.Message;
            }
        }
        /// <summary>
        /// 同步数据到面板
        /// </summary>
        void syncscenedata ()
        {
            var scenelist = new List<YiPanelScene> ();
            scenelist.Clear ();
            localdata ();
            ///只同步本地场景;
            foreach (var yIPanelscene in yipanel.yIPanelSceneList) {
                var currscene = localSceneList.Find ((c) => c.Name == yIPanelscene.Name);
                if (currscene != null) {
                    scenelist.Add (yIPanelscene);
                }
            }
 
            MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.insync)+"...");
            new System.Threading.Thread (() => {
                try {
 
                    for (int j = 0; j < scenelist.Count; j++) {
                        var sceneifon = scenelist [j];
                        var bytes = new byte [24];
                        bytes [0] = sceneifon.LargeClass;
                        bytes [1] = 1;
                        bytes [2] = sceneifon.Channelnumber;
                        bytes [3] = 0;
                        var pathBytes = System.Text.Encoding.UTF8.GetBytes (sceneifon.Name);
                        Array.Copy (pathBytes, 0, bytes, 4, pathBytes.Length > 20 ? 20 : pathBytes.Length);
                        var returnBytes = Control.ControlBytesSendHasReturn (Command.YIPanelDeviceModify, yipanel.SubnetID, yipanel.DeviceID, bytes);
                        if (returnBytes == null) {
                            return;
                        }
 
                        var bytes1 = new byte [5];
                        bytes1 [0] = sceneifon.LargeClass;
                        bytes1 [1] = 1;
                        bytes1 [2] = sceneifon.Channelnumber;
                        bytes1 [3] = 1;
                        bytes1 [4] = 1;
                        var data = Control.ControlBytesSendHasReturn (Command.YIPanelDeviceModify, yipanel.SubnetID, yipanel.DeviceID, bytes1);
 
                        var iconbytes= new byte [5];
                        iconbytes [0] = sceneifon.LargeClass;
                        iconbytes [1] = 1;
                        iconbytes [2] = sceneifon.Channelnumber;
                        iconbytes [3] = 8;
                        iconbytes [4] = sceneifon.IconIndex;
                        var iocndata = Control.ControlBytesSendHasReturn (Command.YIPanelDeviceModify, yipanel.SubnetID, yipanel.DeviceID, iconbytes);
                        
                        int b = 0;
                        for (int targetIndex = 0; targetIndex < sceneifon.scenedevicelist.Count; targetIndex++) {
 
                            var common = sceneifon.scenedevicelist [targetIndex];
                            var devicebytes = new byte [9];
                            devicebytes [0] = sceneifon.Channelnumber;
                            devicebytes [3] = common.SubnetID;
                            devicebytes [4] = common.DeviceID;
 
 
                            switch (common.Type) {
                            case DeviceType.CurtainModel: {
                                    var curtainModel = common as YiPanelCurtainModel;
                                    devicebytes [1] = (byte)++b;
                                    devicebytes [2] = 92;
 
                                    devicebytes [5] = common.LoopID;
                                    devicebytes [6] = (byte)curtainModel.Status;
                                    Control.ControlBytesSendHasReturn (Command.WriteWirelessPanelButtonKey, yipanel.SubnetID, yipanel.DeviceID, devicebytes);
                                }
                                break;
                            case DeviceType.CurtainRoller: {
                                    var curtainRoller = common as YiPanelCurtainRoller;
                                    devicebytes [1] = (byte)++b;
                                    devicebytes [2] = 92;
 
                                    devicebytes [5] = common.LoopID;
                                    devicebytes [6] = curtainRoller.CurtainProress;
                                    Control.ControlBytesSendHasReturn (Command.WriteWirelessPanelButtonKey, yipanel.SubnetID, yipanel.DeviceID, devicebytes);
                                }
                                break;
                          case DeviceType.LightSwitch: {
                                    var lightSwitch = common as YiPanelLightSwitch;
                                    devicebytes [1] = (byte)++b;
                                    devicebytes [2] = 89;
                                    devicebytes [5] = common.LoopID;
                                    devicebytes [6] = lightSwitch.CurrentBrightness;
                                    Control.ControlBytesSendHasReturn (Command.WriteWirelessPanelButtonKey, yipanel.SubnetID, yipanel.DeviceID, devicebytes);
                                }
                                break;
                            case DeviceType.LightDimming: {
                                    var lightDimming = common as YiPanelLightDimming;
                                    devicebytes [1] = (byte)++b;
                                    devicebytes [2] = 89;
                                    devicebytes [5] = common.LoopID;
                                    devicebytes [6] = lightDimming.CurrentBrightness;
                                    Control.ControlBytesSendHasReturn (Command.WriteWirelessPanelButtonKey, yipanel.SubnetID, yipanel.DeviceID, devicebytes);
                                }
                                break;
                            case DeviceType.HVAC: {
                                    var hVAC = common as YiPanelHVAC;
                                    devicebytes [1] = (byte)++b; 
                                    devicebytes [2] = 95;
                                    devicebytes [7] = hVAC.LoopID;
                                    devicebytes [5] = 3;
                                    devicebytes [6] = hVAC.Power;
                                    Control.ControlBytesSendHasReturn (Command.WriteWirelessPanelButtonKey, yipanel.SubnetID, yipanel.DeviceID, devicebytes);
                                    devicebytes [1] = (byte)++b;
                                    devicebytes [5] = 5;
                                    devicebytes [6] = hVAC.SetFanSpeed;
                                    Control.ControlBytesSendHasReturn (Command.WriteWirelessPanelButtonKey, yipanel.SubnetID, yipanel.DeviceID, devicebytes);
                                    devicebytes [1] = (byte)++b;
                                    devicebytes [5] = 6;
                                    devicebytes [6] = hVAC.SetMode;
                                    Control.ControlBytesSendHasReturn (Command.WriteWirelessPanelButtonKey, yipanel.SubnetID, yipanel.DeviceID, devicebytes);
                                    //0 制冷 1制热,2通风(没有温度值),3自动,4除湿
                                    switch (hVAC.SetMode) {
                                    case 0: {
                                            devicebytes [5] = 4;
                                        }
                                        break;
                                    case 1: {
                                            devicebytes [5] = 7;
                                        }
                                        break;
                                    case 3: {
                                            devicebytes [5] = 8;
                                        }
                                        break;
                                    case 4: {
                                            devicebytes [5] = 19;
                                        }
                                        break;
                                    }
                                    devicebytes [1] = (byte)++b;
                                    devicebytes [6] = hVAC.SetTemperature;
                                    Control.ControlBytesSendHasReturn (Command.WriteWirelessPanelButtonKey, yipanel.SubnetID, yipanel.DeviceID, devicebytes);
 
                                }
                                break;
                            case DeviceType.FoolHeat: {
                                    var foolHeat = common as YiPanelFoolHeat;
                                    devicebytes [1] = (byte)++b;
                                    devicebytes [2] = 95;
                                    devicebytes [7] = foolHeat.LoopID;
                                    devicebytes [5] = 20;
                                    devicebytes [6] = foolHeat.PWDStatus;
                                    Control.ControlBytesSendHasReturn (Command.WriteWirelessPanelButtonKey, yipanel.SubnetID, yipanel.DeviceID, devicebytes);
 
                                    devicebytes [1] = (byte)++b;
                                    devicebytes [5] = 21;
                                    devicebytes [6] = foolHeat.TemperatureType;
                                    Control.ControlBytesSendHasReturn (Command.WriteWirelessPanelButtonKey, yipanel.SubnetID, yipanel.DeviceID, devicebytes);
 
                                    switch (foolHeat.TemperatureType) {
                                    case 1: {
                                            devicebytes [5] = 25;
                                        }
                                        break;
                                    case 2: {
                                            devicebytes [5] = 26;
                                        }
                                        break;
                                    case 3: {
                                            devicebytes [5] = 27;
                                        }
                                        break;
                                    case 4: {
                                            devicebytes [5] = 28;
                                        }
                                        break;
                                    }
                                    devicebytes [1] = (byte)++b;
                                    devicebytes [6] = foolHeat.TemperatureTypeValue;
                                    Control.ControlBytesSendHasReturn (Command.WriteWirelessPanelButtonKey, yipanel.SubnetID, yipanel.DeviceID, devicebytes);
 
                                }
                                break;
                            case DeviceType.MusicModel: {
                                    var music = common as YiPanelMusicModel;
                                    devicebytes [1] = (byte)++b;
                                    devicebytes [2] = 103;
                                    devicebytes [7] = music.LoopID;
 
                                    devicebytes [5] = 2;
                                    devicebytes [6] = (byte)music.playMode;
                                    Control.ControlBytesSendHasReturn (Command.WriteWirelessPanelButtonKey, yipanel.SubnetID, yipanel.DeviceID, devicebytes);
 
                                    devicebytes [1] = (byte)++b;
                                    devicebytes [5] = 4;
                                    devicebytes [6] = (byte)music.PlayStatus;
                                    Control.ControlBytesSendHasReturn (Command.WriteWirelessPanelButtonKey, yipanel.SubnetID, yipanel.DeviceID, devicebytes);
 
                                    devicebytes [1] = (byte)++b;
                                    devicebytes [5] = 5;
                                    devicebytes [6] = Convert.ToByte (music.CurVol);
                                    Control.ControlBytesSendHasReturn (Command.WriteWirelessPanelButtonKey, yipanel.SubnetID, yipanel.DeviceID, devicebytes);
                                }
                                break;
 
                            }
 
 
                        }
 
                        //int dddd = 00;
                        ///
 
                    }
 
 
                } catch { } finally {
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Hide ();
                    });
                }
            }) { IsBackground = true }.Start ();
 
        }
        /// <summary>
        /// 读取面板场景
        /// </summary>
        /// <param name="action">Action.</param>
        void refreshsecenelist (Action action)
        {
            var type = yipanel.DeviceTypeList.Find ((obj) => { return obj.largeclass == 4; });
            if (type == null) {
                return;
            }
 
 
            yipanel.yIPanelSceneList.Clear ();
            List<int> intlist = new List<int> ();
            intlist.Clear();
            MainPage.Loading.Start ();
            new System.Threading.Thread (() => {
                try {
                    var validBytes = Shared.Control.ControlBytesSendHasReturn (Command.YIPanelDevice, yipanel.SubnetID, yipanel.DeviceID, new byte [2] { type.largeclass, type.subclass });
                    if (validBytes != null) {
                        string len = "";
                       
                        if (validBytes [1] == 1) {
                            for (int i = 3; i <= 6; i++) {
                                var bytes = validBytes [i];
                                var stringlen = System.Convert.ToString (bytes, 2);
                                var str = stringlen.Insert (0, new string ('0', 8 - stringlen.Length));
                                if (str.Length == 8) {
                                    for (int j = 7; j >= 0; j--) {
                                        len += str.Substring (j, 1);
                                    }
                                }
 
                            }
                            for (int j = 0; j < len.Length; j++) {
                                var strvalue = len.Substring (j, 1);
                                if (strvalue == "1") {
                                    intlist.Add (j + 1);
                                }
                            }
                        }
                    }
                    for (int i = 0; i < intlist.Count; i++) {
                        var value = (byte)intlist [i];
                        var scene = new YiPanelScene ();
                        scene.LargeClass = type.largeclass;
                        scene.sub = type.subclass;
                        scene.Channelnumber = value;
                        ///发送获取场景的名称指令
                        var nameBytes = Shared.Control.ControlBytesSendHasReturn (Command.YIPanelDeviceInof, yipanel.SubnetID, yipanel.DeviceID, new byte [4] { type.largeclass, type.subclass, value, 0 });
                        if (nameBytes != null) {
                            var bytes = new byte [20];
                            for (int j = 0; j < 20; j++) {
                                bytes [j] = nameBytes [j + 4];
                            }
                            scene.Name = System.Text.Encoding.UTF8.GetString (bytes).Replace ("\0", "");
 
                        }
                        ///发送获取场景的图标索引指令
                        var iconBytes = Shared.Control.ControlBytesSendHasReturn (Command.YIPanelDeviceInof, yipanel.SubnetID, yipanel.DeviceID, new byte [4] { type.largeclass, type.subclass, value, 8 });
                        if (iconBytes != null) {
                            scene.IconIndex = iconBytes [4];
 
                        }
                        yipanel.yIPanelSceneList.Add (scene);
                    }
                } catch { } finally {
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Hide ();
                        action ();
                    });
                }
            }){IsBackground=true}.Start();
 
 
        }
        /// <summary>
        ///加载场景列表界面
        /// </summary>
        void sceneview (VerticalScrolViewLayout middle)
        {
            middle.RemoveAll ();
 
            foreach (var scene in yipanel.yIPanelSceneList) {
                var sceneRowLayout = new RowLayout {
                    Height = Application.GetRealHeight (100),
                    BackgroundColor = 0xff323232,
                };
                middle.AddChidren (sceneRowLayout);
 
 
                var sceneicon = new Button {
                    Height = Application.GetRealHeight (80),
                    Width = Application.GetRealWidth (83),
                    UnSelectedImagePath = $"Yipanel/{scene.IconIndex}.png",
                    Gravity = Gravity.CenterVertical,
                    X = Application.GetRealWidth (10),
                };
                //sceneRowLayout.AddChidren (sceneicon);
 
                var scenetypename = new Button {
                    Width = Application.GetRealWidth (350),
                    Text = scene.Name,
                    TextAlignment = TextAlignment.CenterLeft,
                    X = Application.GetRealWidth (30),
                    //X = Application.GetRealWidth (10)+sceneicon.Width +Application.GetRealWidth (10),
                };
                sceneRowLayout.AddChidren (scenetypename);
 
                var editorscenename = new Button {
                    Height = Application.GetRealHeight (90),
                    Width = Application.GetRealWidth (70),
                    UnSelectedImagePath = "Item/Editor.png",
                    SelectedImagePath = "Item/EditorSelected.png",
                    Gravity = Gravity.CenterVertical,
                    X = Application.GetRealWidth (525),
                };
                sceneRowLayout.AddChidren (editorscenename);
                                         
 
                ///删除
                var del = new Button {
                    //TextID = MyInternationalizationString.Musicdel,
                    BackgroundColor = 0xFFFF0000,
                    Text = Language.StringByID (MyInternationalizationString.del),
                };
                sceneRowLayout.AddRightView (del);
 
                editorscenename.MouseUpEventHandler += (sender, e) => {
                    MainPage.Loading.Start (Language.StringByID (MyInternationalizationString.load));
                    System.Threading.Tasks.Task.Run (() => {
                        Application.RunOnMainThread (() => {
                            MainPage.Loading.Hide ();
                            var etid = new Etid ();
                            MainPage.MainFrameLayout.AddChidren (etid);
                            etid.Show (yipanel, scene, () => { scenetypename.Text = scene.Name; });
 
                        });
                    });
 
                };
 
                del.MouseUpEventHandler += (sender, e) => {
                    yipanel.yIPanelSceneList.RemoveAll ((c) => c.Channelnumber == scene.Channelnumber);
                    System.Threading.Tasks.Task.Run (() => {
                        try {
                            var bytes = new byte [10];
                            bytes [0] = scene.LargeClass;
                            bytes [1] = scene.sub;
                            bytes [2] = scene.Channelnumber;
                            bytes [3] = 1;
                            bytes [4] = 0;
                                                     
                            Shared.Control.ControlBytesSend (Command.YIPanelDeviceModify, yipanel.SubnetID, yipanel.DeviceID, bytes);
                        } catch { } finally {
                            Application.RunOnMainThread (() => {
                                sceneview (middle);
                            });
                        }
                    });
                };
 
            }
        }
    }
}