CrabtreeOn,印度客户定制APP,迁移2.0平台版本
JLChen
2021-01-20 4c993fcb71b92f8e9837ca8f50f3b5e8f9c59cfe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
using System;
using System.Collections.Generic;
 
namespace Shared.SimpleControl.Phone
{
    public class AlexaDeviceListPage : FrameLayout
    {
        public AlexaDeviceListPage ()
        {
            BackgroundColor = SkinStyle.Current.MainColor;
        }
 
        public void ShowPage ()
        {
            #region 标题
            var topView = new FrameLayout () {
                Y = Application.GetRealHeight (36),
                Height = Application.GetRealHeight (90),
            };
            AddChidren (topView);
 
            var title = new Button () {
                TextAlignment = TextAlignment.Center,
                Text = "Alexa",
                TextSize = 19,
                TextColor = SkinStyle.Current.TextColor1
            };
            topView.AddChidren (title);
 
            Button ItemButton = new Button () {
                Width = Application.GetRealWidth (55),
                Height = Application.GetRealHeight (55),
                UnSelectedImagePath = "Item/+.png",
                SelectedImagePath = "Item/+.png",
                X = Application.GetRealWidth (640 - 80),
                Y = Application.GetRealHeight (17),
            };
            topView.AddChidren (ItemButton);
 
            var back = new Button () {
                Height = Application.GetRealHeight (90),
                Width = Application.GetRealWidth (85),
                UnSelectedImagePath = "Item/Back.png",
                SelectedImagePath = "Item/BackSelected.png",
            };
            topView.AddChidren (back);
            back.MouseUpEventHandler += (sender, e) => {
                (Parent as PageLayout).PageIndex -= 1;
            };
            #endregion
 
            var bodyView = new VerticalScrolViewLayout () {
                Y = topView.Bottom,
                Height = Application.GetRealHeight (1136 - 126),
                BackgroundColor = SkinStyle.Current.ViewColor,
            };
            AddChidren (bodyView);
            var alexaDeviceLista = new List<UpdateDeviceObj> ();
            var alexaSceneList = new List<SceneByRegion> ();
 
            System.Threading.Tasks.Task.Factory.StartNew (() => {
                Application.RunOnMainThread (() => {
                    MainPage.Loading.Start ("Please wait...");
                });
                try {
                    alexaDeviceLista = ReadDeviceList ();
                    alexaSceneList = ReadSceneList ();
 
                    if (alexaDeviceLista.Count > 0) {
                        Application.RunOnMainThread (() => {
                            InitDevcieRow (alexaDeviceLista, bodyView);
                        });
                    }
                    if (alexaSceneList.Count > 0) {
                        Application.RunOnMainThread (() => {
                            InitSceneRow (alexaSceneList, bodyView);
                        });
                    }
                } catch (Exception ex) {
                    Utlis.WriteLine (ex.Message);
                } finally {
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Hide ();
                    });
                }
            });
 
            ItemButton.MouseUpEventHandler += (sender, e) => {
                Action action = () => {
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Start ("Please wait...");
                    });
                    System.Threading.Tasks.Task.Run (() => {
                        try {
                            alexaDeviceLista = ReadDeviceList ();
                            alexaSceneList = ReadSceneList ();
                            Application.RunOnMainThread (() => {
                                InitDevcieRow (alexaDeviceLista, bodyView);
                            });
                        } catch (Exception ex) {
                            Utlis.WriteLine (ex.Message);
                        } finally {
                            Application.RunOnMainThread (() => {
                                MainPage.Loading.Hide ();
                            });
                        }
                    });
                };
 
                Action<List<string>, List<string>> saveAction = (roomSceneFilePathList, roomDeviceFilePathList) => {
                    System.Threading.Tasks.Task.Factory.StartNew (() => {
                        try {
                            Application.RunOnMainThread (() => {
                                MainPage.Loading.Start ("Please wait...");
                            });
                            foreach (var newDevicePath in roomDeviceFilePathList) {
                                var common = Newtonsoft.Json.JsonConvert.DeserializeObject<Common> (CommonPage.MyEncodingUTF8.GetString (IO.FileUtils.ReadFile (newDevicePath)));
                                //newDeviceList.Add (new UpdateDeviceObj () {
                                //    DeviceName = common.Name, DeviceID = common.DeviceID, Id = 1, DeviceType = common.Type.ToString (), LoopID = common.LoopID,
                                //    MAC = UserConfig.Instance.GatewayMAC, RegionID = UserConfig.Instance.CurrentRegion.RegionID, SubnetID = common.SubnetID
                                //});
                              
 
                                if (common != null) {
                                    var commonType = common.Type.ToString ();
                                    if (commonType == "CurtainModel") {
                                        commonType = "CurtainModule";
                                    } else if (commonType == "LightEnergySwitch" || commonType == "LightSwitchSocket" || commonType == "LightEnergySocket") {
                                        commonType = "LightSwitch";
                                    } else if (commonType == "ACInfrared") {
                                        commonType = "HVAC";
                                    }
                                    var oldDevice = alexaDeviceLista.Find ((obj) => obj.SubnetID == common.SubnetID && obj.DeviceID == common.DeviceID && obj.LoopID == common.LoopID && obj.DeviceType == commonType);
                                    if (oldDevice == null) {
                                        var isSuccess = AddDevice (common);
                                        if (!isSuccess) {
                                            //跳出循环
                                            break;
                                        }
                                    } else {
                                        //2020-07-21 如果已经存在 匹配设备备注是否需要更新
                                        if (oldDevice.DeviceName != common.Name) {
                                            //Utlis.WriteLine ("名字不匹配,提交更新:" + common.Name);
                                            var oldName = oldDevice.DeviceName;
                                            oldDevice.DeviceName = common.Name;
                                            var isSuccess = UpdateDevice (oldDevice);
                                            if (!isSuccess) {
                                                //还原
                                                oldDevice.DeviceName = oldName;
                                                //Utlis.WriteLine ("更新失败!!!!");
                                            } else {
                                                //Utlis.WriteLine ("更新成功!");
                                            }
                                        } else {
                                            //Utlis.WriteLine ("名字一样不用更新:"+ common.Name);
                                        }
 
                                    }
                                }
                            }
 
                            var roomDeviceFilePathListNew = new List<string> ();
                            foreach (var path in roomDeviceFilePathList) {
                                var deviceFilePath = path;
                                if (deviceFilePath.Contains ("LightEnergySocket")) {
                                    deviceFilePath = deviceFilePath.Replace ("LightEnergySocket", "LightSwitch");
                                } else if (deviceFilePath.Contains ("LightEnergySwitch")) {
                                    deviceFilePath = deviceFilePath.Replace ("LightEnergySwitch", "LightSwitch");
                                } else if (deviceFilePath.Contains ("LightSwitchSocket")) {
                                    deviceFilePath = deviceFilePath.Replace ("LightSwitchSocket", "LightSwitch");
                                } else if (deviceFilePath.Contains ("ACInfrared")) {
                                    deviceFilePath = deviceFilePath.Replace ("ACInfrared", "HVAC");
                                } else if (deviceFilePath.Contains ("CurtainModel")) {
                                    deviceFilePath = deviceFilePath.Replace ("CurtainModel", "CurtainModule");
                                }
                                roomDeviceFilePathListNew.Add (deviceFilePath);
                            }
 
                           foreach (var oldDevice in alexaDeviceLista) {
                                var oldDevicePath = "Equipment_" + oldDevice.DeviceType + "_" + oldDevice.SubnetID + "_" + oldDevice.DeviceID + "_" + (oldDevice.LoopID.ToString ().Length < 2 ? "0" + oldDevice.LoopID.ToString () : oldDevice.LoopID.ToString ());
                                if (!roomDeviceFilePathListNew.Contains (oldDevicePath)) {
                                    DelDevice (oldDevice.Id);
                                    Utlis.WriteLine ("delDevice " + oldDevice.DeviceName);
                                }
                            }
 
                            foreach (var newScenePath in roomSceneFilePathList) {
                                var tempScene = Scene.GetSceneByFilePath (newScenePath);
                                if (tempScene != null) {
                                    var oldScene = alexaSceneList.Find ((obj) => obj.SceneName == tempScene.Name);
                                    if (oldScene == null) {
                                        int sceneDeviceCount = tempScene.DeviceFilePathList.Count;
                                        var uploadScene = new SceneAndDeviceList () { MAC = UserConfig.Instance.GatewayMAC, RegionID = UserConfig.Instance.CurrentRegion.Id, SceneName = tempScene.Name };
 
                                        byte [] replyBytes = null;
                                        if (!tempScene.busScene) {
                                            foreach (var deviceFilePath in tempScene.DeviceFilePathList) {
                                                var common = Newtonsoft.Json.JsonConvert.DeserializeObject<Common> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                                if (common.Type == DeviceType.FanModule) {
                                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<FanModule> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                                    if (device != null) {
                                                        uploadScene.DeviceListObject.Add (new AddCloudSceneDeviceData () {
                                                            Command = (int)Command.SetSingleLight, SubnetId = device.SubnetID, DeviceId = device.DeviceID,
                                                            DeviceName = device.Name, DeviceType = "FanModule", LoopId = device.LoopID,
                                                            DataString = device.WindSpeed.ToString ()
                                                        });
                                                    }
                                                } else if (common.Type == DeviceType.LightDimming) {
                                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<LightDimming> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                                    if (device != null) {
                                                        uploadScene.DeviceListObject.Add (new AddCloudSceneDeviceData () {
                                                            Command = (int)Command.SetSingleLight, SubnetId = device.SubnetID, DeviceId = device.DeviceID,
                                                            DeviceName = device.Name, DeviceType = "LightDimming", LoopId = device.LoopID,
                                                            DataString = device.CurrentBrightness.ToString ()
                                                        });
                                                    }
                                                } else if (common.Type == DeviceType.LightDALI) {
                                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<LightDALI> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                                    if (device != null) {
                                                        uploadScene.DeviceListObject.Add (new AddCloudSceneDeviceData () {
                                                            Command = (int)Command.SetSingleLight, SubnetId = device.SubnetID, DeviceId = device.DeviceID,
                                                            DeviceName = device.Name, DeviceType = "LightDimming", LoopId = device.LoopID,
                                                            DataString = device.CurrentBrightness.ToString ()
                                                        });
                                                    }
                                                } else if (common.Type == DeviceType.LightRGB) {
                                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<LightLogic> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                                    if (device != null) {
                                                        uploadScene.DeviceListObject.Add (new AddCloudSceneDeviceData () {
                                                            Command = (int)Command.SetLogicLoopColor, SubnetId = device.SubnetID, DeviceId = device.DeviceID,
                                                            DeviceName = device.Name, DeviceType = "LightRGB", LoopId = device.LoopID,
                                                            DataString = device.CurrentBrightness + "," + device.RStatus + "," + device.GStatus + "," + device.BStatus
                                                        });
                                                    }
                                                } else if (common.Type == DeviceType.LightSwitch || common.Type == DeviceType.LightSwitchSocket) {
                                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<LightSwitch> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                                    if (device != null) {
                                                        uploadScene.DeviceListObject.Add (new AddCloudSceneDeviceData () {
                                                            Command = (int)Command.SetSingleLight, SubnetId = device.SubnetID, DeviceId = device.DeviceID,
                                                            DeviceName = device.Name, DeviceType = "LightSwitch", LoopId = device.LoopID,
                                                            DataString = device.CurrentBrightness.ToString ()
                                                        });
                                                    }
                                                } else if (common.Type == DeviceType.LightEnergySocket) {
                                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<LightSwitch> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                                    if (device != null) {
                                                        uploadScene.DeviceListObject.Add (new AddCloudSceneDeviceData () {
                                                            Command = (int)Command.SetSingleLight, SubnetId = device.SubnetID, DeviceId = device.DeviceID,
                                                            DeviceName = device.Name, DeviceType = "LightSwitch", LoopId = device.LoopID,
                                                            DataString = device.CurrentBrightness.ToString ()
                                                        });
                                                    }
                                                } else if (common.Type == DeviceType.LightEnergySwitch) {
                                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<LightSwitch> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                                    if (device != null) {
                                                        uploadScene.DeviceListObject.Add (new AddCloudSceneDeviceData () {
                                                            Command = (int)Command.SetSingleLight, SubnetId = device.SubnetID, DeviceId = device.DeviceID,
                                                            DeviceName = device.Name, DeviceType = "LightSwitch", LoopId = device.LoopID,
                                                            DataString = device.CurrentBrightness.ToString ()
                                                        });
                                                    }
                                                } else if (common.Type == DeviceType.CurtainModel) {
                                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<CurtainModel> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                                    if (device != null) {
                                                        uploadScene.DeviceListObject.Add (new AddCloudSceneDeviceData () {
                                                            Command = (int)Command.UpdataCurtainModelStutas, SubnetId = device.SubnetID, DeviceId = device.DeviceID,
                                                            DeviceName = device.Name, DeviceType = "CurtainModule", LoopId = device.LoopID,
                                                            DataString = ((byte)device.Status).ToString ()
                                                        });
                                                    }
                                                } else if (common.Type == DeviceType.CurtainRoller) {
                                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<CurtainRoller> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                                    if (device != null) {
                                                        uploadScene.DeviceListObject.Add (new AddCloudSceneDeviceData () {
                                                            Command = (int)Command.UpdataCurtainModelStutas, SubnetId = device.SubnetID, DeviceId = device.DeviceID,
                                                            DeviceName = device.Name, DeviceType = "CurtainRoller", LoopId = device.LoopID,
                                                            DataString = ((byte)device.CurtainProress + 3).ToString ()
                                                        });
                                                    }
                                                } else if (common.Type == DeviceType.CurtainTrietex) {
                                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<CurtainTrietex> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                                    if (device != null) {
                                                        uploadScene.DeviceListObject.Add (new AddCloudSceneDeviceData () {
                                                            Command = (int)Command.UpdataCurtainModelStutas, SubnetId = device.SubnetID, DeviceId = device.DeviceID,
                                                            DeviceName = device.Name, DeviceType = "CurtainRoller", LoopId = device.LoopID,
                                                            DataString = ((byte)device.CurtainProress + 3).ToString ()
                                                        });
                                                    }
                                                } else if (common.Type == DeviceType.HVAC || common.Type == DeviceType.ACInfrared) {
                                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<HVAC> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                                    if (device != null) {
                                                        uploadScene.DeviceListObject.Add (new AddCloudSceneDeviceData () {
                                                            Command = (int)Command.SetACMode, SubnetId = device.SubnetID, DeviceId = device.DeviceID,
                                                            DeviceName = device.Name, DeviceType = "HVAC", LoopId = device.LoopID,
                                                            DataString = device.Power + "," + device.TemperatureMode + "," + device.SetMode + "," + device.SetFanSpeed + "," + device.SetTemperature
                                                        });
                                                    }
                                                } else if (common.Type == DeviceType.FoolHeat) {
                                                    //var device = Newtonsoft.Json.JsonConvert.DeserializeObject<FoolHeat> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                                    //if (device == null) {
                                                    //    continue;
                                                    //}
                                                    //replyBytes = Control.ControlBytesSendHasReturn (Command.SetFoolHeat, device.SubnetID, device.DeviceID,
                                                    //new byte [] { device.LoopID, (byte)(device.Status + device.WorkingMode * 16), 0,device.WorkingMode,device.NormalTemperature, device.DayTemperature,
                                                    //device.NightTemperature, device.AwayTemperature , 0, 0 });
 
                                                } else if (common.Type == DeviceType.FanModule) {
                                                    //var device = Newtonsoft.Json.JsonConvert.DeserializeObject<FanModule> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                                    //if (device == null) {
                                                    //    continue;
                                                    //}
                                                    //replyBytes = Control.ControlBytesSendHasReturn (Command.SetSingleLight, device.SubnetID, device.DeviceID, new byte [] { 1, (byte)device.WindSpeed });
                                                } else if (common.Type == DeviceType.LogicModule) {
                                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<LogicModule> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                                    if (device != null) {
                                                        uploadScene.DeviceListObject.Add (new AddCloudSceneDeviceData () {
                                                            Command = (int)Command.SetScene, SubnetId = device.SubnetID, DeviceId = device.DeviceID,
                                                            DeviceName = device.Name, DeviceType = "SceneControl", LoopId = device.AreaID,
                                                            DataString = device.AreaSceneID.ToString ()
                                                        });
                                                    }
                                                } else if (common.Type == DeviceType.UniversalDevice) {//2020-09-02 新增通用开关
                                                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<UniversalDevice> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (deviceFilePath)));
                                                    if (device != null) {
                                                        uploadScene.DeviceListObject.Add (new AddCloudSceneDeviceData () {
                                                            Command = (int)Command.SetCommonSwitch, SubnetId = device.SubnetID, DeviceId = device.DeviceID,
                                                            DeviceName = device.Name, DeviceType = "UniversalDevice", LoopId = device.LoopID,
                                                            DataString = device.SendBytes[1].ToString(),
                                                        });
                                                    }
                                                }
 
                                            }
                                        } else {
                                            foreach (var common in tempScene.GlobalSceneDeviceList) {
                                                switch (common.obj1) {
                                                case 0://0   无效 Invalid
                                                    break;
                                                case 85://0x55  场景 Scene ||0x0002
                                                    uploadScene.DeviceListObject.Add (new AddCloudSceneDeviceData () {
                                                        Command = (int)Command.SetScene, SubnetId = common.SubnetID, DeviceId = common.DeviceID,
                                                        DeviceName = common.Name, DeviceType = "SceneControl", LoopId = common.obj2,
                                                        DataString = common.obj3.ToString ()
                                                    });
                                                    //replyBytes = Control.ControlBytesSendHasReturn (Command.SetScene, common.SubnetID, common.DeviceID, new byte [] { common.obj2, common.obj3, common.obj4, common.obj5 });
                                                    break;
                                                case 86://0x56  序列 Sequence ||0x001A
                                                    //replyBytes = Control.ControlBytesSendHasReturn (Command.SetSeries, common.SubnetID, common.DeviceID, new byte [] { common.obj2, common.obj3, common.obj4, common.obj5 });
                                                    break;
                                                case 88://0x58  通用开关 Universal Switch ||0xe01c
                                                    uploadScene.DeviceListObject.Add (new AddCloudSceneDeviceData () {
                                                        Command = (int)Command.SetCommonSwitch, SubnetId = common.SubnetID, DeviceId = common.DeviceID,
                                                        DeviceName = common.Name, DeviceType = "UVSwitch", LoopId = common.obj2,
                                                        DataString = common.obj3.ToString ()
                                                    });
                                                    //replyBytes = Control.ControlBytesSendHasReturn (Command.SetCommonSwitch, common.SubnetID, common.DeviceID, new byte [] { common.obj2, common.obj3, common.obj4, common.obj5 });
                                                    break;
                                                case 89: //0x59  单路调节 Single Channel Lighting Control  0031
                                                    uploadScene.DeviceListObject.Add (new AddCloudSceneDeviceData () {
                                                        Command = (int)Command.SetSingleLight, SubnetId = common.SubnetID, DeviceId = common.DeviceID,
                                                        DeviceName = common.Name, DeviceType = "LightDimming", LoopId = common.obj2,
                                                        DataString = common.obj3.ToString ()
                                                    });
                                                    //replyBytes = Control.ControlBytesSendHasReturn (Command.SetSingleLight, common.SubnetID, common.DeviceID, new byte [] { common.obj2, common.obj3, common.obj4, common.obj5 });
                                                    break;
                                                case 92://0x5c  窗帘开关 Curtain Switch e3e0
                                                    uploadScene.DeviceListObject.Add (new AddCloudSceneDeviceData () {
                                                        Command = (int)Command.UpdataCurtainModelStutas, SubnetId = common.SubnetID, DeviceId = common.DeviceID,
                                                        DeviceName = common.Name, DeviceType = "CurtainModel", LoopId = common.obj2,
                                                        DataString = common.obj3.ToString ()
                                                    });
                                                    //replyBytes = Control.ControlBytesSendHasReturn (Command.UpdataCurtainModelStutas, common.SubnetID, common.DeviceID, new byte [] { common.obj2, common.obj3, common.obj4, common.obj5 });
                                                    break;
                                                case 94://0x5e  GPRS控制 GPRS Control E3D4
                                                    replyBytes = Control.ControlBytesSendHasReturn (Command.GPRSControl, common.SubnetID, common.DeviceID, new byte [] { common.obj2, common.obj3, common.obj4, common.obj5 });
                                                    break;
                                                case 95://0x5f  面板控制 Panel Control e3d8
                                                    //replyBytes = Control.ControlBytesSendHasReturn (Command.InstructionPanelKey, common.SubnetID, common.DeviceID, new byte [] { common.obj2, common.obj3, common.obj4, common.obj5 });
                                                    break;
                                                case 100://0x64 广播场景 Broadcast Scene ||0x0002  
                                                    uploadScene.DeviceListObject.Add (new AddCloudSceneDeviceData () {
                                                        Command = (int)Command.SetScene, SubnetId = common.SubnetID, DeviceId = common.DeviceID,
                                                        DeviceName = common.Name, DeviceType = "SceneControl", LoopId = common.obj2,
                                                        DataString = common.obj3.ToString ()
                                                    });
                                                    //replyBytes = Control.ControlBytesSendHasReturn (Command.SetScene, common.SubnetID, common.DeviceID, new byte [] { common.obj2, common.obj3, common.obj4, common.obj5 });
                                                    break;
                                                case 101://0x65 广播回路 Broadcast Channel 0031
                                                    uploadScene.DeviceListObject.Add (new AddCloudSceneDeviceData () {
                                                        Command = (int)Command.SetSingleLight, SubnetId = common.SubnetID, DeviceId = common.DeviceID,
                                                        DeviceName = common.Name, DeviceType = "LightDimming", LoopId = common.obj2,
                                                        DataString = common.obj3.ToString ()
                                                    });
                                                    //replyBytes = Control.ControlBytesSendHasReturn (Command.SetSingleLight, common.SubnetID, common.DeviceID, new byte [] { common.obj2, common.obj3, common.obj4, common.obj5 });
                                                    break;
                                                    //case 102://0x66 消防模块 Security Module 011E
                                                    //    replyBytes = Control.ControlBytesSendHasReturn (Command.SecurityModule, common.SubnetID, common.DeviceID, new byte [] { common.obj2, common.obj3, common.obj4, common.obj5 });
                                                    //    break;
                                                    //case 103://0x67 音乐播放 Music Control 0218
                                                    //replyBytes = Control.ControlBytesSendHasReturn (Command.MusicControl, common.SubnetID, common.DeviceID, new byte [] { common.obj2, common.obj3, common.obj4, common.obj5 });
                                                    //break;
                                                    //104 通用控制 Universal Control
                                                    //105 连接页 Link Page
                                                    //107 RGB调节 RGB Control
                                                    //108 红外控制 Infrared Control
                                                    //case 109://109 逻辑灯调节 Logic Light Adjust ||  0xE45E
                                                    //    replyBytes = Control.ControlBytesSendHasReturn (Command.LogicLightAdjust, common.SubnetID, common.DeviceID, new byte [] { common.obj2, common.obj3, common.obj4, common.obj5 });
                                                    //    break;
                                                    //}
                                                    //110 逻辑场景 Logic Scene
                                                    //112 LCD & LED休眠 LCD & LED Sleep
                                                    //113 服务器控制   Server control
                                                }
                                            }
                                        }
 
                                        var isSuccess = AddScene (uploadScene);
                                        if (!isSuccess) {
                                            //跳出循环
                                            break;
                                        }
                                    }
                                }
 
                            }
 
 
                            alexaDeviceLista = ReadDeviceList ();
                            Application.RunOnMainThread (() => {
                                InitDevcieRow (alexaDeviceLista, bodyView);
                            });
                            alexaSceneList = ReadSceneList ();
                            Application.RunOnMainThread (() => {
                                InitSceneRow (alexaSceneList, bodyView);
                            });
 
                        } catch (Exception ex) {
                            Utlis.WriteLine (ex.Message);
                        } finally {
                            Application.RunOnMainThread (() => {
                                MainPage.Loading.Hide ();
                            });
                        }
                    });
                };
 
                System.Threading.Tasks.Task.Factory.StartNew (() => {
                    try {
                        Application.RunOnMainThread (() => {
                            MainPage.Loading.Start ("Please wait...");
                        });
                        var tempRoom = new Room ();
                        foreach (var device in alexaDeviceLista) {//特殊处理服务器与本地设备类型不一样的设备
                            var deviceTypeSaveString = device.DeviceType;
                            if (deviceTypeSaveString == "CurtainModel") {
                                deviceTypeSaveString = "CurtainModule";
                            } else if (deviceTypeSaveString == "LightEnergySwitch") {
                                deviceTypeSaveString = "LightSwitch";
                            } else if (deviceTypeSaveString == "LightEnergySocket") {
                                deviceTypeSaveString = "LightSwitch";
                            } else if (deviceTypeSaveString == "LightSwitchSocket") {
                                deviceTypeSaveString = "LightSwitch";
                            } else if (deviceTypeSaveString == "LightLogic") {
                                deviceTypeSaveString = "LightRGB";
                            }else if (deviceTypeSaveString == "ACInfrared") {
                                deviceTypeSaveString = "HVAC";
                            }
                            
                            var devicePath = "Equipment_" + deviceTypeSaveString + "_" + device.SubnetID + "_" + device.DeviceID + "_" + (device.LoopID.ToString ().Length < 2 ? "0" + device.LoopID.ToString () : device.LoopID.ToString ());
                            tempRoom.DeviceFilePathList.Add (devicePath);
                        }
                        foreach (var scene in alexaSceneList) {
                            tempRoom.SceneFilePathList.Add (scene.SceneName);
                        }
                        Application.RunOnMainThread (() => {
                            var userDeviceListPage = new AlexaSettingPage (tempRoom);
                            userDeviceListPage.Show ();
                            userDeviceListPage.ShowPage (action, saveAction);
                        });
                    } catch (Exception ex) {
                        Utlis.WriteLine (ex.Message);
                    } finally {
                        Application.RunOnMainThread (() => {
                            MainPage.Loading.Hide ();
                        });
                    }
                });
            };
        }
 
        #region 关于设备的方法
        /// <summary>
        /// 读取Alexa设备列表
        /// </summary>
        public List<UpdateDeviceObj> ReadDeviceList ()
        {
            var deviceList = new List<UpdateDeviceObj> ();
            var requestObj = new GetDeviceListObj () { RegionID = UserConfig.Instance.CurrentRegion.Id };
            var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj);
            var revertObj = MainPage.RequestHttps (API.GetDeviceList, requestJson);
            if (revertObj.StateCode.ToUpper () == StateCode.SUCCESS) {
                deviceList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<UpdateDeviceObj>> (revertObj.ResponseData.ToString ());
            } else {
                //2020-06-29 弹窗提示错误
                ShowErrorInfo (revertObj.StateCode);
 
            }
            return deviceList;
        }
 
        /// <summary>
        /// 添加设备
        /// </summary>
        public bool AddDevice (Common device)
        {
            var uploadDevice = new AlexaDeviceObj () {
                RegionID = UserConfig.Instance.CurrentRegion.Id, MAC = UserConfig.Instance.GatewayMAC.Replace (".", ""),
                DeviceName = device.Name, DeviceID = device.DeviceID, SubnetID = device.SubnetID, LoopID = device.LoopID, DeviceType = device.Type.ToString ()
            };
            if (device.Type.ToString () == "CurtainModel") {
                uploadDevice.DeviceType = "CurtainModule";
            } else if (device.Type.ToString () == "LightEnergySwitch" || device.Type.ToString () == "LightSwitchSocket" || device.Type.ToString () =="LightEnergySocket") {
                uploadDevice.DeviceType = "LightSwitch";
            } else if (device.Type.ToString () == "ACInfrared") {
                uploadDevice.DeviceType = "HVAC";
            }
            //var requestObj = new UploadDevicesObj () { Device = uploadDevice };
            var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (uploadDevice);
            var revertObj = MainPage.RequestHttps (API.UploadDevices, requestJson);
            if (revertObj.StateCode.ToUpper() == StateCode.SUCCESS) {
                return true;
            }else if (revertObj.StateCode == "Exist") {
                Application.RunOnMainThread (() => {
                    MainPage.AddTip (ErrorCode.AddFailed + " " + ErrorCode.NameExist + " ( " + device.Name + " )");
                });
                return true;
            } else {
                //2020-06-29 弹出提示错误
                ShowUploadDevicesInfo (revertObj.StateCode);
            }
            return false;
        }
 
        /// <summary>
        /// 上传设备 错误提示
        /// </summary>
        /// <param name="stateCodeStr"></param>
        void ShowUploadDevicesInfo (string stateCodeStr,bool bUpload = true)
        {
            string mes = "";
            if (stateCodeStr == "NoRecord") {
                //住宅未绑定网关!
                mes = ErrorCode.HomeNoRecord;
            } else if (stateCodeStr == "NoExist") {
                //住宅不存在!
                mes = ErrorCode.HomeNoExist;
            } else if (stateCodeStr == "Exist") {
                //设备名称已存在
                mes = ErrorCode.NameExist;
            } else if (stateCodeStr == ErrorCode.NetworkError) {
                mes = ErrorCode.NetworkError;
            } else {
                mes = ErrorCode.OperationFailed + ErrorCode.Reason + stateCodeStr;
 
            }
            if (!string.IsNullOrEmpty (mes)) {
                Application.RunOnMainThread (() => {
                    if (bUpload) {
                        MainPage.AddTip (ErrorCode.AddFailed + " " + mes);
                    } else {
                        MainPage.AddTip ("Update failed! " + mes);
                    }
 
                    //new Alert ("", ErrorCode.AddFailed + " " + mes, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                });
            }
 
        }
 
        /// <summary>
        /// 错误提醒
        /// </summary>
        /// <param name="stateCodeStr"></param>
        void ShowErrorInfo (string stateCodeStr)
        {
            string mes = "";
            if (stateCodeStr == ErrorCode.NetworkError) {
                mes = ErrorCode.NetworkError;
            } else {
                mes = ErrorCode.OperationFailed + ErrorCode.Reason + stateCodeStr;
 
            }
            if (!string.IsNullOrEmpty (mes)) {
                Application.RunOnMainThread (() => {
                    MainPage.AddTip (ErrorCode.OperationFailed + " " + mes);
                });
            }
 
        }
 
        /// <summary>
        /// 刷新设备名字
        /// </summary>
        public bool UpdateDevice (UpdateDeviceObj mUpdateDeviceObj)
        {
            var mUpdateAlexaDeviceObj = new UpdateAlexaDeviceObj () {
                Id = mUpdateDeviceObj.Id,
                DeviceType = mUpdateDeviceObj.DeviceType,
                SubnetID = mUpdateDeviceObj.SubnetID,
                DeviceID = mUpdateDeviceObj.DeviceID,
                LoopID = mUpdateDeviceObj.LoopID,
                DeviceName = mUpdateDeviceObj.DeviceName,
                MAC = mUpdateDeviceObj.MAC,
                RegionID = UserConfig.Instance.CurrentRegion.Id,
            };
 
            var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (mUpdateAlexaDeviceObj);
            var revertObj = MainPage.RequestHttps (API.UpdateDevice, requestJson);
            if (revertObj.StateCode.ToUpper () == StateCode.SUCCESS) {
                return true;
            } else if (revertObj.StateCode == "Exist") {
                Application.RunOnMainThread (() => {
                    MainPage.AddTip ("Update failed!" + " " + ErrorCode.NameExist + " ( " + mUpdateDeviceObj.DeviceName + " )");
                });
                return true;
            } else {
                //2020-06-29 弹出提示错误
                ShowUploadDevicesInfo (revertObj.StateCode, false);
            }
            return false;
        }
 
        /// <summary>
        /// 删除设备
        /// </summary>
        public bool DelDevice(string commonId)
        {
            var requestObj = new DelDeviceObj () { Id = commonId };
            var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj);
            var revertObj = MainPage.RequestHttps (API.DeleteDevice, requestJson);
            if (revertObj.StateCode.ToUpper() == StateCode.SUCCESS) {
                return true;
            } else {
                //2020-06-29 弹窗提示错误
                ShowErrorInfo (revertObj.StateCode);
            }
            return false;
        }
        #endregion
 
        #region 关于场景的方法
        /// <summary>
        /// 读取Alexa场景列表
        /// </summary>
        public List<SceneByRegion> ReadSceneList ()
        {
            var sceneList = new List<SceneByRegion> ();
            var requestObj = new SceneByRegionListObj () { RegionID = UserConfig.Instance.CurrentRegion.Id };
            var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj);
            var revertObj = MainPage.RequestHttps (API.GetSceneList, requestJson);
            if (revertObj.StateCode.ToUpper() == StateCode.SUCCESS) {
                sceneList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SceneByRegion>> (revertObj.ResponseData.ToString ());
            } else {
                //2020-06-29 弹窗提示错误
                ShowErrorInfo (revertObj.StateCode);
            }
            return sceneList;
        }
 
        /// <summary>
        /// 添加场景
        /// </summary>
        public bool AddScene (SceneAndDeviceList sceneMark)
        {
            var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (sceneMark);
            var revertObj = MainPage.RequestHttps (API.AddSceneAndDeviceList, requestJson);
            if (revertObj.StateCode.ToUpper() == StateCode.SUCCESS) {
                //var requestAddDeviceObj = new BindDeviceToSceneItemObj () { SceneId = sceneId, Command = command, DeviceID = 0, DataString = dataString };
                //var requestAddDeviceJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestAddDeviceObj);
                //var revertAddDeviceObj = MainPage.RequestHttps ("BindDeviceToSceneItem", requestAddDeviceJson);
                //if (revertAddDeviceObj.StateCode.ToUpper() == StateCode.SUCCESS) {
                    return true;
                //}
            } else if (revertObj.StateCode == "Exist") {
                Application.RunOnMainThread (() => {
                    MainPage.AddTip (ErrorCode.AddFailed + " " + ErrorCode.NameExist + " ( " + sceneMark.SceneName + " )");
                });
                return true;
            } else {
                //2020-06-29 弹出提示错误
                ShowUploadDevicesInfo (revertObj.StateCode);
            }
 
            return false;
        }
 
        /// <summary>
        /// 上传场景设备控制数据
        /// </summary>
        //public bool AddDeviceDateToScene (int sceneId,string command,string dataString)
        //{
        //    var requestAddDeviceObj = new BindDeviceToSceneItemObj () { SceneId = sceneId, Command = command, DeviceID = 0, DataString = dataString };
        //    var requestAddDeviceJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestAddDeviceObj);
        //    var revertAddDeviceObj = MainPage.RequestHttps ("BindDeviceToSceneItem", requestAddDeviceJson);
        //    if (revertAddDeviceObj.StateCode.ToUpper() == StateCode.SUCCESS) {
        //        return true;
        //    }
        //    return false;
        //}
 
        /// <summary>
        /// 删除场景
        /// </summary>
        public bool DelScene (string sceneId)
        {
            var requestObj = new DeleteSceneObj () { Id = sceneId };
            var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj);
            var revertObj = MainPage.RequestHttps (API.DeleteScene, requestJson);
            if (revertObj.StateCode.ToUpper() == StateCode.SUCCESS) {
                return true;
            } else {
                //2020-06-29 弹窗提示错误
                ShowErrorInfo (revertObj.StateCode);
            }
            return false;
        }
        #endregion
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="bodyView"></param>
        /// <param name="isScene"></param>
        void AddTopTitleView (VerticalScrolViewLayout bodyView, bool isScene = false) {
            var chooseTypeView = new RowLayout () {
                Height = Application.GetRealHeight (80),
                BackgroundColor = SkinStyle.Current.TitileView,
            };
            bodyView.AddChidren (chooseTypeView);
            Button btnEquipmentIcon = new Button () {
                Width = Application.GetRealHeight (65),
                Height = Application.GetRealHeight (65),
                UnSelectedImagePath = "Item/LogicModule.png",
                X = Application.GetRealWidth (30),
                Gravity = Gravity.CenterVertical,
            };
            chooseTypeView.AddChidren (btnEquipmentIcon);
         
            Button btnEquipmentType = new Button () {
                Width = Application.GetRealHeight (460),
                X = btnEquipmentIcon.Right,
                TextAlignment = TextAlignment.CenterLeft,
                Text = Language.StringByID (R.MyInternationalizationString.Device),
                TextColor = SkinStyle.Current.TextColor1,
                SelectedTextColor = SkinStyle.Current.DialogTextColor
            };
            chooseTypeView.AddChidren (btnEquipmentType);
 
            if (isScene) {
                btnEquipmentIcon.UnSelectedImagePath = "Scene/Scene.png";
                btnEquipmentType.Text = Language.StringByID (R.MyInternationalizationString.Scenes);
            }
        }
 
        /// <summary>
        /// 添加设备界面
        /// </summary>
        public void InitDevcieRow (List<UpdateDeviceObj> list, VerticalScrolViewLayout bodyView)
        {
            bodyView.RemoveAll ();
 
            if (list.Count > 0) {
                //添加标题
                AddTopTitleView (bodyView);
            }
 
            foreach (var device in list) {
                var rowView = new RowLayout () {
                    Height = Application.GetRealHeight (110),
                };
                bodyView.AddChidren (rowView);
                var btn = new Button () {
                    X = Application.GetRealWidth(60),
                    Width = Application.GetRealWidth (640 - 60),
                    Text = device.DeviceName,
                    TextAlignment = TextAlignment.CenterLeft,
                    TextColor = SkinStyle.Current.TextColor1,
                };
                rowView.AddChidren (btn);
 
                var btnDel = new Button () { 
                    Text = "Delete",
                    BackgroundColor = SkinStyle.Current.DelColor,
                };
                rowView.AddRightView (btnDel);
 
                btnDel.MouseUpEventHandler += (sender, e) => {
                    var alert = new Alert ("", "Do you want to delete device?", "No", "Yes");
                    alert.Show ();
                    alert.ResultEventHandler += (s, dd) => {
                        if (dd) {
                            //if (DelDevice (device.Id)) {
                            //    rowView.RemoveFromParent ();
                            //    list.Remove (device);
                            //}
 
                            System.Threading.Tasks.Task.Run (() => {
                                Application.RunOnMainThread (() => {
                                    MainPage.Loading.Start (Language.StringByID (R.MyInternationalizationString.load));
                                });
                                try {
                                    var result = DelDevice (device.Id);
                                    Application.RunOnMainThread (() => {
                                        if (result) {
                                            rowView.RemoveFromParent ();
                                            list.Remove (device);
                                        }
                                    });
 
                                } catch { } finally {
 
                                    Application.RunOnMainThread (() => {
                                        MainPage.Loading.Hide ();
                                    });
                                }
                            });
                        }
                    };
                };
            }
        }
 
        /// <summary>
        /// 添加场景界面
        /// </summary>
        public void InitSceneRow (List<SceneByRegion> list, VerticalScrolViewLayout bodyView)
        {
            if (list.Count > 0) {
                //添加标题
                AddTopTitleView (bodyView, true);
            }
 
            foreach (var scene in list) {
                var rowView = new RowLayout () {
                    Height = Application.GetRealHeight (110),
                };
                bodyView.AddChidren (rowView);
                var btn = new Button () {
                    X = Application.GetRealWidth (60),
                    Width = Application.GetRealWidth (640 - 60),
                    Text = scene.SceneName,
                    TextAlignment = TextAlignment.CenterLeft,
                    TextColor = SkinStyle.Current.TextColor1,
                };
                rowView.AddChidren (btn);
                btn.MouseUpEventHandler += (dd,ff) => {
                    //var alexaSceneSettingPage = new AlecaSceneEditPage ();
                    //UserMiddle.SettingPageView.AddChidren (alexaSceneSettingPage);
                    //alexaSceneSettingPage.ShowScene (scene);
                    //UserMiddle.SettingPageView.PageIndex = 2;
                };
 
                var btnDel = new Button () {
                    Text = "Delete",
                    BackgroundColor = SkinStyle.Current.DelColor,
                };
                rowView.AddRightView (btnDel);
 
                btnDel.MouseUpEventHandler += (sender, e) => {
                    var alert = new Alert ("", "Do you want to delete scene?", "No", "Yes");
                    alert.Show ();
                    alert.ResultEventHandler += (s, dd) => {
                        if (dd) {
                            //if (DelScene (scene.SceneId)) {
                            //    rowView.RemoveFromParent ();
                            //    list.Remove (scene);
                            //}
 
                            System.Threading.Tasks.Task.Run (() => {
                                Application.RunOnMainThread (() => {
                                    MainPage.Loading.Start (Language.StringByID (R.MyInternationalizationString.load));
                                });
                                try {
                                    var result = DelScene (scene.SceneId);
                                    Application.RunOnMainThread (() => {
                                        if (result) {
                                            rowView.RemoveFromParent ();
                                            list.Remove (scene);
                                        }
                                    });
 
                                } catch { } finally {
 
                                    Application.RunOnMainThread (() => {
                                        MainPage.Loading.Hide ();
                                    });
                                }
                            });
                        }
                    };
                };
            }
 
        }
    }
}