CrabtreeOn,印度客户定制APP,迁移2.0平台版本
JLChen
2021-01-11 f8d640b74b6a2012338b77358b33f9d5a6f2457a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
using System;
using System.Collections.Generic;
 
namespace Shared.SimpleControl.Phone
{
    public class Databackup : FrameLayout
    {
        //WebServiceBackup service_backup = new WebServiceBackup ();
 
        VerticalScrolViewLayout VerticalScrolViewMiddle;
 
        /// <summary>
        /// 构造函数
        /// </summary>
        public Databackup ()
        {
            BackgroundColor = SkinStyle.Current.MainColor;
        }
 
        public void DatabackupShow ()
        {
            this.RemoveAll ();
            #region 标题
            var topView = new FrameLayout () {
                Y = Application.GetRealHeight (36),
                Height = Application.GetRealHeight (90),
                BackgroundColor = SkinStyle.Current.MainColor
            };
            AddChidren (topView);
 
            var title = new Button () {
                X = Application.GetRealWidth (90),
                Width = Application.GetRealWidth (640 - 180),
                TextAlignment = TextAlignment.Center,
                Text = UserConfig.Instance.CurrentRegion.Name + " " + Language.StringByID (R.MyInternationalizationString.Backup),
                TextSize = 19,
                TextColor = SkinStyle.Current.TextColor1,
            };
            topView.AddChidren (title);
            title.MouseUpEventHandler += (sdf, asf) => {
                Dialog dialog = new Dialog ();
 
                FrameLayout bodyView = new FrameLayout ();
                dialog.AddChidren (bodyView);
                bodyView.MouseUpEventHandler += (lkj, oiu) => {
                    dialog.Close ();
                };
 
                VerticalScrolViewLayout dialogBodyView = new VerticalScrolViewLayout () {
                    Y = Application.GetRealHeight (126),
                    Gravity = Gravity.CenterHorizontal,
                    Width = Application.GetRealWidth (500),
                    Height = Application.GetRealHeight (360),
                    BackgroundColor = SkinStyle.Current.MainColor,
                    Radius = 5,
                    BorderColor = SkinStyle.Current.Transparent,
                    BorderWidth = 0
                };
                bodyView.AddChidren (dialogBodyView);
 
                foreach (var homeTemp in UserConfig.Instance.HomeLists) {
                    var rowView = new RowLayout () {
                        Height = Application.GetRealHeight (110),
                    };
                    dialogBodyView.AddChidren (rowView);
 
                    Button btnRowLine = new Button () {
                        Y = Application.GetRealHeight (107),
                        Height = Application.GetRealHeight (3),
                        BackgroundColor = SkinStyle.Current.Black50Transparent
                    };
                    rowView.AddChidren (btnRowLine);
 
                    var btnPoint = new Button () {
                        Width = Application.GetRealWidth (10),
                        Height = Application.GetRealWidth (10),
                        X = Application.GetRealWidth (50),
                        Gravity = Gravity.CenterVertical,
                        UnSelectedImagePath = "Item/Point.png",
                        SelectedImagePath = "Item/Point.png",
                        Visible = homeTemp.Id != UserConfig.Instance.CurrentRegion.Id
                    };
                    rowView.AddChidren (btnPoint);
 
                    var btnName = new Button () {
                        Width = Application.GetRealWidth (550),
                        Text = homeTemp.Name,
                        TextAlignment = TextAlignment.CenterLeft,
                        X = Application.GetRealWidth (90),
                        TextColor = SkinStyle.Current.TextColor1,
                    };
                    rowView.AddChidren (btnName);
 
                    if (homeTemp.Id == UserConfig.Instance.CurrentRegion.Id) {
                        Button btnCheck = new Button () {
                            X = Application.GetRealWidth (20),
                            Y = Application.GetRealHeight (25),
                            Width = Application.GetRealWidth (72),
                            Height = Application.GetRealHeight (61),
                            UnSelectedImagePath = "Skin/Check.png",
                        };
                        rowView.AddChidren (btnCheck);
                    }
 
                    btnName.MouseUpEventHandler += (ss, ee) => {
                        if (UserConfig.Instance.CurrentRegion == homeTemp) {
                            dialog.Close ();
                            return;
                        }
                        MainPage.Loading.Start ("Please wait...");
                        System.Threading.Tasks.Task.Run (() => {
                            try {
                                var backuplist = IO.FileUtils.ReadFiles ();
                                int index = 0;
                                string oldRegionRootPath = IO.FileUtils.CreateRegionBackup (UserConfig.Instance.CurrentRegion.Id);
                                IO.FileUtils.DeleteRegionFiles (oldRegionRootPath);
                                foreach (var fileName in backuplist) {
                                    System.IO.FileInfo fileInfo = new System.IO.FileInfo (IO.FileUtils.RootPath + fileName);
                                    if (fileInfo.Exists) {
                                        fileInfo.MoveTo (oldRegionRootPath + fileName);
                                        Utlis.WriteLine ("move file : " + fileName);
                                    }
                                    index++;
                                    Application.RunOnMainThread (() => {
                                        int pro = (int)(index * 1.0 / backuplist.Count * 50);
                                        MainPage.Loading.Text = pro.ToString () + "%";
                                    });
                                }
                                IO.FileUtils.DeleteAllFile ();
                                string newRegionRootPath = IO.FileUtils.CreateRegionBackup (homeTemp.Id);
                                IO.FileUtils.RestoreRegionFiles (newRegionRootPath);
                                UserConfig.Instance.RefreshUserConfig ();
                                UserConfig.Instance.CurrentRegion = homeTemp;
                                UserConfig.Instance.SaveUserConfig ();
 
                                Room.InitAllRoom ();
                                //UserConfig.Instance.RefreshUserConfig ();
                                //MainPage.LoginUser.SaveUserInfo ();
                                Application.RunOnMainThread (() => {
                                    new Alert ("", Language.StringByID (R.MyInternationalizationString.SwitchRegionSuccessfully),
                                              Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                                    this.DatabackupShow ();
                                    EquipmentPublicClass.CheckLinkRemote (UserConfig.Instance.internetStatus);
                                });
                            } catch (Exception ex) {
                                Application.RunOnMainThread (() => {
                                    new Alert ("", Language.StringByID (R.MyInternationalizationString.CheckInternet),
                                               Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                                });
                                Utlis.WriteLine (ex.Message);
                            } finally {
                                Application.RunOnMainThread (() => {
                                    MainPage.Loading.Hide ();
                                    dialog.Close ();
                                });
                            }
                        });
                    };
                }
                dialog.Show ();
            };
 
            Button ItemButton = new Button () {
                Width = Application.GetRealWidth (55),
                Height = Application.GetRealHeight (55),
                UnSelectedImagePath = "Item/+.png",
                SelectedImagePath = "Item/+.png",
                Y = Application.GetRealHeight (15),
                X = Application.GetRealWidth (640 - 80),
            };
            topView.AddChidren (ItemButton);
 
            ItemButton.MouseUpEventHandler += (sender, e) => {
                Dialog dialog = new Dialog ();
 
                FrameLayout dialogBodyView = new FrameLayout () {
                    Gravity = Gravity.Center,
                    Width = Application.GetRealWidth (500),
                    Height = Application.GetRealHeight (500),
                    BackgroundColor = SkinStyle.Current.DialogColor,
                    Radius = 5,
                    BorderColor = SkinStyle.Current.Transparent,
                    BorderWidth = 0,
                };
                dialog.AddChidren (dialogBodyView);
 
                Button btnTitle = new Button () {
                    Height = Application.GetRealHeight (80),
                    BackgroundColor = SkinStyle.Current.DialogTitle,
                    TextAlignment = TextAlignment.Center,
                    TextID = R.MyInternationalizationString.Backup,
                    TextColor = SkinStyle.Current.DialogTextColor
                };
                dialogBodyView.AddChidren (btnTitle);
 
                Button btnackupRemark = new Button () {
                    Gravity = Gravity.CenterHorizontal,
                    Y = Application.GetRealHeight (100),
                    Width = Application.GetRealWidth (400),
                    Height = Application.GetRealHeight (80),
                    TextID = R.MyInternationalizationString.Remark,
                    TextAlignment = TextAlignment.CenterLeft,
                    TextColor = SkinStyle.Current.TextColor,
                };
                dialogBodyView.AddChidren (btnackupRemark);
 
                EditText etBackupRemark = new EditText () {
                    Gravity = Gravity.CenterHorizontal,
                    Y = btnackupRemark.Bottom,
                    Width = Application.GetRealWidth (400),
                    Height = Application.GetRealHeight (80),
                    TextAlignment = TextAlignment.Center,
                    Radius = 5,
                    BorderColor = SkinStyle.Current.BorderColor,
                    BorderWidth = 1,
                    TextColor = SkinStyle.Current.TextColor,
                };
                dialogBodyView.AddChidren (etBackupRemark);
                etBackupRemark.EditorEnterAction += (obj) => {
                    Application.HideSoftInput ();
                };
 
                FrameLayout bottomView = new FrameLayout () {
                    Y = Application.GetRealHeight (420),
                    Height = Application.GetRealHeight (85),
                    BackgroundColor = SkinStyle.Current.DialogTitle
                };
                dialogBodyView.AddChidren (bottomView);
 
                Button btnClose = new Button () {
                    Width = Application.GetRealWidth (249),
                    TextID = R.MyInternationalizationString.Close,
                    TextAlignment = TextAlignment.Center
                };
                bottomView.AddChidren (btnClose);
                btnClose.MouseUpEventHandler += (send2er, e2) => {
                    dialog.Close ();
                };
 
                Button btnBottomLine = new Button () {
                    X = btnClose.Right,
                    Width = 1,
                    BackgroundColor = SkinStyle.Current.Black50Transparent,
                };
                bottomView.AddChidren (btnBottomLine);
 
                Button btnSave = new Button () {
                    X = btnBottomLine.Right,
                    Width = Application.GetRealWidth (249),
                    TextID = R.MyInternationalizationString.SAVE,
                    TextAlignment = TextAlignment.Center
                };
                bottomView.AddChidren (btnSave);
 
                btnSave.MouseUpEventHandler += (sender2, e2) => {
                    if (etBackupRemark.Text.Trim () == "") {
                        new Alert ("", Language.StringByID (R.MyInternationalizationString.InputNewBakeUpFilesName),
                                       Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                        return;
                    }
                    if (MainPage.LoginUser == null) {
                        new Alert ("", Language.StringByID (R.MyInternationalizationString.PleaseLoginSystem),
                                      Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                        return;
                    }
                    userBakeupFile (etBackupRemark.Text.Trim ());
                    dialog.Close ();
                };
                dialog.Show ();
            };
 
            var back = new Button () {
                Height = Application.GetRealHeight (90),
                Width = Application.GetRealWidth (85),
                UnSelectedImagePath = "Item/Back.png",
                Gravity = Gravity.CenterVertical,
            };
            topView.AddChidren (back);
            back.MouseUpEventHandler += (sender, e) => {
                (Parent as PageLayout).PageIndex -= 1;
            };
            #endregion
 
            var FrameLayoutView = new FrameLayout () {
                Width = LayoutParams.MatchParent,
                Height = Application.GetRealHeight (1136 - 126),
                Y = topView.Bottom,
                BackgroundColor = SkinStyle.Current.ViewColor,
            };
            AddChidren (FrameLayoutView);
 
            VerticalScrolViewMiddle = new VerticalScrolViewLayout ();
            VerticalScrolViewMiddle.RemoveAll ();
            FrameLayoutView.AddChidren (VerticalScrolViewMiddle);
 
            System.Threading.Tasks.Task.Run (() => {
                try {
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Start ("Please wait...");
                    });
                    var requestObj = new GetHomeDataBackupPaggerObj () {
                        HomeId = UserConfig.Instance.CurrentRegion.Id,
                        LoginAccessToken = MainPage.LoginUser.LoginTokenString
                    };
                    string urlHead = MainPage.RequestHttpsHost;
                    if (requestObj.IsOtherAccountCtrl) {
                        urlHead = UserConfig.Instance.MasterAccountRequestBaseUrl;
                        requestObj.LoginAccessToken = UserConfig.Instance.MasterAccountToken;
                    }
 
                    //GetUserFolderObj requestObj = new GetUserFolderObj () { LevelID = UserConfig.Instance.CurrentRegion.Id };
                    var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj);
                    var revertObj = MainPage.RequestHttps (API.GetHomeDataBackupPagger, requestJson, urlHead);
                    if (revertObj.StateCode.ToUpper () == StateCode.SUCCESS) {
                        var data = Newtonsoft.Json.JsonConvert.DeserializeObject<GetBackupListResult> (revertObj.ResponseData.ToString ());
                        //var list = new List<BackupListNameInfo> ();
                        //foreach (var data2 in data.PageData) {
                        //    if (ZigbeeUniqueId != null && data2.GatewayUniqueId != ZigbeeUniqueId) {
                        //        continue;
                        //    }
                        //    if (getLogBackup == false && data2.BackupName == LogBackupName) {
                        //        //不获取Log备份
                        //        continue;
                        //    }
                        //    list.Add (data2);
                        //}
 
 
                        //var responseDataObj = Newtonsoft.Json.JsonConvert.DeserializeObject<List<FolderRes>> (revertObj.ResponseData.ToString ());
                        if (data == null) return;
 
                        Application.RunOnMainThread (() => {
                            foreach (var folder in data.PageData) {
                                addRow (folder.BackupName, folder.Id);
                            }
                        });
                    } else {
                        //2020-01-11 刷新一次MasterToken
                        if (requestObj.IsOtherAccountCtrl) {
                            //if (revertObj.StateCode == "ParameterOrEmpty" || revertObj.StateCode == "NoLogin" || revertObj.StateCode == "HomeIdAndTokenNoConsistent") {
                            if (revertObj.StateCode != ErrorCode.NetworkError) {
                                UserConfig.Instance.CheckIfNeedGetMasterAccountToken ();
                            }
                        }
                        MainPage.ShowAlertOnMainThread ("Failed to get backup data list!");
                    }
                    //var groupNames = service_backup.GetUserGroup (MainPage.LoginUser.MasterID, 0);
                    //Application.RunOnMainThread (() => {
                    //    foreach (UserBackup groupName in groupNames) {
                    //        addRow (groupName.UserGroup);
                    //    }
                    //});
                } catch {
                    Application.RunOnMainThread (() => {
                        new Alert ("", Language.StringByID (R.MyInternationalizationString.CheckInternet), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                    });
                } finally {
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Hide ();
                    });
                }
            });
        }
 
        void addRow (string groupName, string folderID)
        {
            var rowView = new RowLayout () {
                Height = Application.GetRealHeight (110),
            };
            VerticalScrolViewMiddle.AddChidren (rowView);
 
            Button btnRowLine = new Button () {
                Y = Application.GetRealHeight (107),
                Height = Application.GetRealHeight (3),
                BackgroundColor = SkinStyle.Current.Black50Transparent
            };
            rowView.AddChidren (btnRowLine);
 
            var btnDelFile = new Button () {
                TextID = R.MyInternationalizationString.Del,
                BackgroundColor = SkinStyle.Current.DelColor
            };
            btnDelFile.MouseUpEventHandler += (sender, e) => {
                Alert alert = new Alert ("", Language.StringByID (R.MyInternationalizationString.AreYouSureToDeleteFile), Language.StringByID (R.MyInternationalizationString.Cancel), Language.StringByID (R.MyInternationalizationString.Confrim));
                alert.ResultEventHandler += (sender2, e2) => {
                    if (e2) {
                        DeleteFolderData (folderID);
                    }
                };
                alert.Show ();
            };
 
            //if (MainPage.LoginUser.AccountType != 1) {
            //    rowView.AddRightView (btnDelFile);
            //}
 
            //2020-07-01 当前住宅属于自己的 允许删除备份
            if (!MainPage.IsAdministrator) {
                rowView.AddRightView (btnDelFile);
            }
 
            var btnPoint = new Button () {
                Width = Application.GetRealWidth (10),
                Height = Application.GetRealWidth (10),
                X = Application.GetRealWidth (25),
                Gravity = Gravity.CenterVertical,
                UnSelectedImagePath = "Item/Point.png",
                SelectedImagePath = "Item/Point.png",
            };
            rowView.AddChidren (btnPoint);
 
            var btnName = new Button () {
                Width = Application.GetRealWidth (342),
                Height = LayoutParams.MatchParent,
                Text = groupName.Trim (),
                TextAlignment = TextAlignment.CenterLeft,
                X = Application.GetRealWidth (50),
                TextColor = SkinStyle.Current.TextColor1,
            };
            rowView.AddChidren (btnName);
 
            btnName.MouseLongEventHandler += (sender, e) => {
                Alert alert = new Alert ("", Language.StringByID (R.MyInternationalizationString.AreYouSureToDeleteFile), Language.StringByID (R.MyInternationalizationString.Cancel), Language.StringByID (R.MyInternationalizationString.Confrim));
                alert.ResultEventHandler += (sender2, e2) => {
                    if (e2) {
                        DeleteFolderData (folderID);
                    }
                };
                alert.Show ();
            };
 
 
 
            var backupIcon = new Button () {
                Width = Application.GetRealWidth (72),
                Height = Application.GetRealHeight (57),
                X = Application.GetRealWidth (452),
            };
            rowView.AddChidren (backupIcon);
 
            var Backup_restores = new Button () {
                Width = Application.GetMinRealAverage (56),
                Height = Application.GetMinRealAverage (57),
                X = backupIcon.Right + Application.GetRealWidth (20),
                UnSelectedImagePath = "Register/Backup_ restores.png",
                SelectedImagePath = "Register/Backup_ restores_on.png",
                Gravity = Gravity.CenterVertical,
            };
            rowView.AddChidren (Backup_restores);
            Backup_restores.MouseDownEventHandler += (sender, e) => {
                Backup_restores.IsSelected = true;
            };
            Backup_restores.MouseUpEventHandler += (sender, e) => {
                Backup_restores.IsSelected = false;
                UserBakeupFileDetailedInformation (folderID);
            };
        }
 
        /// <summary>
        /// 删除某个备份
        /// </summary>
        /// <param name="folderID"></param>
        void DeleteFolderData (string folderID)
        {
            MainPage.Loading.Start ("Please wait...");
            System.Threading.Tasks.Task.Run (() => {
                try {
                    var requestObj = new DelHomeAppGatewayNameObj () {
                        BackupClassId = folderID,
                        HomeId = UserConfig.Instance.CurrentRegion.Id,
                        LoginAccessToken = MainPage.LoginUser.LoginTokenString
                    };
                    string urlHead = MainPage.RequestHttpsHost;
                    if (requestObj.IsOtherAccountCtrl) {
                        urlHead = UserConfig.Instance.MasterAccountRequestBaseUrl;
                        requestObj.LoginAccessToken = UserConfig.Instance.MasterAccountToken;
                    }
 
                    //DeleteFolderDataObj requestObj = new DeleteFolderDataObj () { Id = folderID };
                    var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj);
                    var revertObj = MainPage.RequestHttps (API.DelHomeAppGatewayName, requestJson, urlHead);
                    if (revertObj.StateCode.ToUpper () == StateCode.SUCCESS) {
                        Application.RunOnMainThread (() => {
                            DatabackupShow ();
                        });
                    } else {
                        ShowDelErrorInfo (revertObj.StateCode);
                    }
                } catch { } finally {
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Hide ();
                    });
                }
            });
 
        }
 
        //2020-01-11
        /// <summary>
        /// 删除某个备份
        /// </summary>
        /// <param name="folderID"></param>
        void DeleteFolderDataAfterUploadFailed (string folderID)
        {
            Application.RunOnMainThread (() => {
                MainPage.Loading.Start ("Please wait...");
            });
 
 
            try {
                var requestObj = new DelHomeAppGatewayNameObj () {
                    BackupClassId = folderID,
                    HomeId = UserConfig.Instance.CurrentRegion.Id,
                    LoginAccessToken = MainPage.LoginUser.LoginTokenString
                };
                string urlHead = MainPage.RequestHttpsHost;
                if (requestObj.IsOtherAccountCtrl) {
                    urlHead = UserConfig.Instance.MasterAccountRequestBaseUrl;
                    requestObj.LoginAccessToken = UserConfig.Instance.MasterAccountToken;
                }
 
                //DeleteFolderDataObj requestObj = new DeleteFolderDataObj () { Id = folderID };
                var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj);
                var revertObj = MainPage.RequestHttps (API.DelHomeAppGatewayName, requestJson, urlHead);
                if (revertObj.StateCode.ToUpper () == StateCode.SUCCESS) {
                    //删除成功
                } else {
                    //删除失败
                }
            } catch { } finally {
                Application.RunOnMainThread (() => {
                    MainPage.Loading.Hide ();
                });
            }
 
 
        }
 
        /// <summary>
        /// (2)ParameterOrEmpty,则响应字段中[ErrorInfo]为错误信息,ResponseData则为null
        //3)NoLogin,则响应字段中 [ErrorInfo] 为错误信息为 [无效登录Token!]
        //(5)NoRecord,则响应字段中 [ErrorInfo] 为错误信息为 [确保您所提交 [项目 (住宅)Id不存在!]
        //(6)BackupClassIdNoIsYou, 则响应字段中 [ErrorInfo]为错误信息为 [您所备注名主键 = XXXX并不属于您当前住宅:XXXX]
        //(7)NoPermission, 则响应字段中 [ErrorInfo]为错误信息为 [您并不具有此分享者此住宅的权限!]
        //(8)InsufficientAuthority, 则响应字段中 [ErrorInfo]为错误信息为 [您并不具有此分享者此住宅的完全控制权限!]
        //(9)HomeIdAndTokenNoConsistent, 则响应字段中 [ErrorInfo]为错误信息为 [您当前Token与你当前控制的HomeId不一致!]
        //(10)DeliveryBackNoUpdateDel, 则响应字段中 [ErrorInfo]为错误信息为 [此前提交文件夹Id是属于交付备份, 不能进行更新删除!]
        /// </summary>
        /// <param name="stateCodeStr"></param>
        void ShowDelErrorInfo (string stateCodeStr)
        {
            string mes = "";
            if (stateCodeStr == "HomeIdAndTokenNoConsistent") {
                //Token 验证失败
                mes = ErrorCode.HomeIdAndTokenNoConsistent;
            } else if (stateCodeStr == "DeliveryBackNoUpdateDel") {
                //交付备份, 不能删除!
                mes = ErrorCode.DeliveryBackNoUpdateDel;
            } else if (stateCodeStr == ErrorCode.NetworkError) {
                mes = ErrorCode.NetworkError;
            } else {
                mes = ErrorCode.OperationFailed + ErrorCode.Reason + stateCodeStr;
 
            }
            if (!string.IsNullOrEmpty (mes)) {
                Application.RunOnMainThread (() => {
                    new Alert ("", mes, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                });
            }
 
 
        }
 
 
        /// <summary>
        /// 用户备份文件
        /// </summary>
        void userBakeupFile (string groupName)
        {
            //确定要备份该文件的数据
            //Alert alert = new Alert ("", Language.StringByID (R.MyInternationalizationString.SureToBackupYourthisFile), Language.StringByID (R.MyInternationalizationString.Cancel), Language.StringByID (R.MyInternationalizationString.Confrim));
 
            //alert.ResultEventHandler += (object sender2, bool e2) => {
            //if (e2) {
            MainPage.Loading.Start ("Upload...");
 
            System.Threading.Tasks.Task.Run (() => {
                try {
 
                    //if (string.IsNullOrEmpty (UserConfig.Instance.GatewayMAC)) return;
                    //2020-01-11 没绑定MAC不允许上传备份
                    if (!UserConfig.Instance.CheckHomeGatewaysNotEmpty()) {
                        MainPage.ShowAlertOnMainThread (ErrorCode.BindGatewayFirst);
                        return;
                    }
 
                    var requestObj = new AddHomeAppGatewayNameObj () {
                        HomeId = UserConfig.Instance.CurrentRegion.Id,
                        BackupName = groupName,
                        LoginAccessToken = MainPage.LoginUser.LoginTokenString,
                        GatewayUniqueId = UserConfig.Instance.GatewayMAC
                    };
                    string urlHead = MainPage.RequestHttpsHost;
                    if (requestObj.IsOtherAccountCtrl) {
                        urlHead = UserConfig.Instance.MasterAccountRequestBaseUrl;
                        requestObj.LoginAccessToken = UserConfig.Instance.MasterAccountToken;
                    }
 
                    //AddFolderObj requestObj = new AddFolderObj () { LevelID = UserConfig.Instance.CurrentRegion.Id ,Name = groupName };
                    var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj);
                    var revertObj = MainPage.RequestHttps (API.AddHomeAppGatewayName, requestJson, urlHead);
                    if (revertObj.StateCode.ToUpper () == StateCode.SUCCESS) {
                        var DATA = Newtonsoft.Json.JsonConvert.DeserializeObject<AddBackupNameResult> (revertObj.ResponseData.ToString ());
                        var addFolderID = DATA.BackupClassId;
                        //UpLoadBackupFileToDB (addFolderID);
                        UpLoadBackupFileToDBNew (addFolderID);
 
 
                    } else {
                        ShowAddHomeAppGatewayNameErrorInfo (revertObj.StateCode);
 
                    }
 
                } catch (Exception ex) {
                    Shared.Application.RunOnMainThread (() => {
                        new Alert ("", Language.StringByID (R.MyInternationalizationString.CheckInternet),
                                   Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                        Utlis.WriteLine (ex.Message);
                    });
                } finally {
                    Shared.Application.RunOnMainThread (() => {
                        MainPage.Loading.Hide ();
                        DatabackupShow ();
                    });
                }
            });
            //    }
            //};
            //alert.Show ();
        }
 
        /// <summary>
        ///(1)Success 则[调用此接口操作成功],ResponseData则为null
        //(2)ParameterOrEmpty,则响应字段中 [ErrorInfo] 为错误信息, ResponseData则为null
        //(3)NoLogin,则响应字段中 [ErrorInfo] 为错误信息为 [无效登录Token!]
        //(4)NoRecord,则响应字段中 [ErrorInfo] 为错误信息为 [确保您所提交 [项目 (住宅)Id不存在!]
        //(5)Exist, 则响应字段中 [ErrorInfo]为错误信息为 [您在住宅: XXX, 当前备份名: XXXX已存在, 请换个备份再提交!]
        //(6)NoBind, 则响应字段中 [ErrorInfo]为错误信息为 [网关的唯一Id = XXX,与住宅: XXXX并不存在绑定关系!]
        //(7)NoPermission, 则响应字段中 [ErrorInfo]为错误信息为 [您并不具有此分享者此住宅的权限!]
 
        //(8)InsufficientAuthority, 则响应字段中 [ErrorInfo]为错误信息为 [您并不具有此分享者此住宅的完全控制权限!]
        //(9)HomeIdAndTokenNoConsistent, 则响应字段中 [ErrorInfo]为错误信息为 [您当前Token与你当前控制的HomeId不一致!]
        /// </summary>
        /// <param name="stateCodeStr"></param>
        void ShowAddHomeAppGatewayNameErrorInfo (string stateCodeStr)
        {
            string mes = "";
            if (stateCodeStr == "Exist") {
                //备份名已存在
                mes = ErrorCode.NameExist;
            } else if (stateCodeStr == "HomeIdAndTokenNoConsistent") {
                //Token 验证失败
                mes = ErrorCode.HomeIdAndTokenNoConsistent;
            } else if (stateCodeStr == ErrorCode.NetworkError) {
                mes = ErrorCode.NetworkError;
            } else {
                mes = ErrorCode.OperationFailed + ErrorCode.Reason + stateCodeStr;
 
            }
            if (!string.IsNullOrEmpty (mes)) {
                Application.RunOnMainThread (() => {
                    new Alert ("", mes, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                });
            }
 
 
        }
 
        ///// <summary>
        ///// 上传备份文件
        ///// </summary>
        ///// <param name="mBackupClassId"></param>
        //void UpLoadBackupFileToDB (string mBackupClassId)
        //{
 
        //    var backuplist = IO.FileUtils.ReadFiles ();
        //    int index = 0;
        //    foreach (var fileName in backuplist) {
        //        index++;
        //        /// <summary>
        //        /// 如果是特殊的注册登陆文件,则不需要备份到服务器
        //        /// </summary>
        //        if (fileName == UserInfo.GlobalRegisterFile) {
        //            continue;
        //        }
        //        //整理数据
        //        var datainfo = new FileInfoData ();
        //        datainfo.BackupFileName = fileName;
        //        datainfo.BackupFileContent = Shared.IO.FileUtils.ReadFile (fileName);
 
        //        var list = new List<FileInfoData> ();
        //        list.Add (datainfo);
 
        //        var upData = new UploadHomeAppGatewaySubFilesObj ();
        //        upData.HomeId = UserConfig.Instance.CurrentRegion.Id;
        //        upData.BackupClassId = mBackupClassId;
        //        upData.UploadSubFileLists = list;
        //        //获取控制主人账号的Token
        //        upData.LoginAccessToken = MainPage.LoginUser.LoginTokenString;
        //        string urlHead = MainPage.RequestHttpsHost;
        //        if (upData.IsOtherAccountCtrl) {
        //            urlHead = UserConfig.Instance.MasterAccountRequestBaseUrl;
        //            upData.LoginAccessToken = UserConfig.Instance.MasterAccountToken;
        //        }
 
        //        //AddUserBackupObj requestObj2 = new AddUserBackupObj () { LevelID = Convert.ToInt32 (revertObj.ResponseData), Name = fileName, DetailByte = IO.FileUtils.ReadFile (fileName) };
        //        var requestJson2 = Newtonsoft.Json.JsonConvert.SerializeObject (upData);
        //        var revertObj2 = MainPage.RequestHttps (API.UploadHomeAppGatewaySubFiles, requestJson2, urlHead);
        //        if (revertObj2.StateCode.ToUpper () == StateCode.SUCCESS) {
        //            //index++;
        //            Application.RunOnMainThread (() => {
        //                int pro = (int)(index * 1.0 / backuplist.Count * 100);
        //                MainPage.Loading.Text = pro.ToString () + "%";
        //            });
        //        } else {
        //            //提示错误
        //            break;
        //        }
        //    }
        //    if (index != backuplist.Count) {
        //        //2020-01-11
        //        DeleteFolderDataAfterUploadFailed (mBackupClassId);
        //        Shared.Application.RunOnMainThread (() => {
        //            new Alert ("", "Upload failed!", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
        //            MainPage.Loading.Hide ();
        //        });
 
        //    }
 
        //    //Shared.Application.RunOnMainThread (() => {
        //    //    if (index == 0) {
        //    //        new Alert ("", "Upload failed!", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
        //    //    }
 
        //    //    MainPage.Loading.Hide ();
        //    //});
        //}
 
 
 
 
        /// <summary>
        /// 用户恢复文件
        /// </summary>
        public void UserBakeupFileDetailedInformation (string BackupClassId)
        {
            //是否确定恢复该文件数据信息
            Alert alert = new Alert ("", Language.StringByID (R.MyInternationalizationString.SureToRestoreFileInformation), Language.StringByID (R.MyInternationalizationString.Cancel), Language.StringByID (R.MyInternationalizationString.Confrim));
            alert.ResultEventHandler += (sender2, e2) => {
                if (e2) {
                    MainPage.Loading.Start ("Download...");
                    System.Threading.Tasks.Task.Run (() => {
                        try {
                            var fileNames = IO.FileUtils.ReadFiles ();
                            foreach (var fileName in fileNames) {
                                if (fileName == UserInfo.GlobalRegisterFile) {
                                    continue;
                                }
                                IO.FileUtils.DeleteFile (fileName);
                            }
 
                            var requestObj2 = new GetHomeDataBackupUploadListPaggerObj ();
                            requestObj2.HomeId = UserConfig.Instance.CurrentRegion.Id;
                            requestObj2.BackupClassId = BackupClassId;
                            requestObj2.PageSetting.PageSize = 999999;
                            requestObj2.PageSetting.Page = 1;
                            //获取控制主人账号的Token
                            requestObj2.LoginAccessToken = MainPage.LoginUser.LoginTokenString;
                            string urlHead = MainPage.RequestHttpsHost;
                            if (requestObj2.IsOtherAccountCtrl) {
                                urlHead = UserConfig.Instance.MasterAccountRequestBaseUrl;
                                requestObj2.LoginAccessToken = UserConfig.Instance.MasterAccountToken;
                            }
 
                            //获取所有文件组名
                            //var requestObj2 = new UserBackupListObj () { LevelID = folderID };
                            var requestJson2 = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj2);
                            var revertObj2 = MainPage.RequestHttps (API.GetHomeDataBackupUploadListPagger, requestJson2, urlHead);
                            if (revertObj2.StateCode.ToUpper () == StateCode.SUCCESS) {
                                var fileListData = Newtonsoft.Json.JsonConvert.DeserializeObject<LoadBackInfoResult> (revertObj2.ResponseData.ToString ());
                                var responseDataObj = fileListData.PageData;
                                ////获取文件名字
                                //List<string> listFileName = new List<string> ();
                                //foreach (LoadBackupFileNameInfo file in fileListData.PageData) {
                                //    listFileName.Add (file.BackupFileName);
                                //}
                                //var responseDataObj = Newtonsoft.Json.JsonConvert.DeserializeObject<List<BackupInfoRes>> (revertObj2.ResponseData.ToString ());
 
                                var mDownLoad = new DownloadSomeDataBackupObj ();
                                mDownLoad.HomeId = UserConfig.Instance.CurrentRegion.Id;
                                mDownLoad.BackupClassId = BackupClassId;
                                //获取控制主人账号的Token
                                mDownLoad.LoginAccessToken = MainPage.LoginUser.LoginTokenString;
                                string mUrlHead = MainPage.RequestHttpsHost;
                                if (mDownLoad.IsOtherAccountCtrl) {
                                    urlHead = UserConfig.Instance.MasterAccountRequestBaseUrl;
                                    mDownLoad.LoginAccessToken = UserConfig.Instance.MasterAccountToken;
                                }
 
                                MultiThreadDownload (responseDataObj, mDownLoad, mUrlHead);
 
 
                                //int index = 0;
                               
                                //foreach (var file in responseDataObj) {
                                //    index++;
                                //    if (file.BackupFileName == "null" || "UserConfig" == file.BackupFileName) {
                                //        continue;
                                //    }
                                //    mDownLoad.BackupFileName = file.BackupFileName;
                                //    //var requestObj3 = new BackupDetailObj () { Id = file.Id };
                                //    var requestJson3 = Newtonsoft.Json.JsonConvert.SerializeObject (mDownLoad);
                                //    var revertObj3 = MainPage.RequestHttpsReturnByte (API.DownloadSomeDataBackup, requestJson3, false, mUrlHead);
                                //    if (revertObj3 != null) {
 
                                //        IO.FileUtils.WriteFileByBytes (file.BackupFileName, revertObj3);
                                //        Application.RunOnMainThread (() => {
                                //            int pro = (int)(index * 1.0 / responseDataObj.Count * 100);
                                //            MainPage.Loading.Text = pro.ToString () + "%";
                                //        });
                                //    } else {
                                //        break;
                                //    }
 
                                //}
 
 
                                //if (index != responseDataObj.Count) {
                                //    //删除已经下载的文件个数错误
                                //    var fileNames2 = IO.FileUtils.ReadFiles ();
                                //    foreach (var fileName in fileNames2) {
                                //        if (fileName == UserInfo.GlobalRegisterFile) {
                                //            continue;
                                //        }
                                //        IO.FileUtils.DeleteFile (fileName);
                                //    }
 
                                //    MainPage.ShowAlertOnMainThread ("Download failed!");
 
                                //    return;
                                //}
 
                                //MainPage.LoginUser.SaveUserInfo ();
                                //Room.InitAllRoom ();
                                //Application.RunOnMainThread (() => {
                                //    this.RemoveFromParent ();
                                //    CommonPage.IsRemote = false;
                                //    UserMiddle.Init ();
                                //    EquipmentPublicClass.CheckLinkRemote (UserConfig.Instance.internetStatus);
                                //    new Alert ("", Language.StringByID (R.MyInternationalizationString.RestoreFileIsSuccessfull),
                                //   Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                                //});
                                //MainPage.LoginUser = Newtonsoft.Json.JsonConvert.DeserializeObject<UserInfo> (System.Text.Encoding.UTF8.GetString (Shared.IO.FileUtils.ReadFile (UserInfo.GlobalRegisterFile)));
                                //if (MainPage.LoginUser != null) {
                                //    MainPage.LoginUser.LastTime = System.DateTime.Now;
                                //    MainPage.LoginUser.SaveUserInfo ();
                                //}
                            }
                        } catch (Exception ex) {
                            Shared.Application.RunOnMainThread (() => {
                                new Alert ("", Language.StringByID (R.MyInternationalizationString.CheckInternet),
                                           Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                            });
                            Utlis.WriteLine (ex.ToString ());
                        } finally {
                            Shared.Application.RunOnMainThread (() => {
                                MainPage.Loading.Hide ();
                            });
                        }
                    });
                }
            };
            alert.Show ();
        }
 
 
        bool DownloadSomeDataBackup (DownloadSomeDataBackupObj mmDownLoad, string mUrlHead, LoadBackupFileNameInfo file)
        {
            DownloadSomeDataBackupObj mDownLoad = mmDownLoad;
            mDownLoad.BackupFileName = file.BackupFileName;
            //var requestObj3 = new BackupDetailObj () { Id = file.Id };
            var requestJson3 = Newtonsoft.Json.JsonConvert.SerializeObject (mDownLoad);
            var revertObj3 = MainPage.RequestHttpsReturnByteNew (API.DownloadSomeDataBackup, requestJson3, mUrlHead);
            if (revertObj3 != null) {
 
                IO.FileUtils.WriteFileByBytes (file.BackupFileName, revertObj3);
                return true;
            } else {
                return false;
            }
        }
 
        /// <summary>
        /// 删除本地文件
        /// </summary>
        void ReadFilesAndDelete ()
        {
 
            var fileNames = IO.FileUtils.ReadFiles ();
            foreach (var fileName in fileNames) {
                if (fileName == UserInfo.GlobalRegisterFile || fileName == UserConfig.configFile) {
                    continue;
                }
                IO.FileUtils.DeleteFile (fileName);
            }
 
        }
 
        private readonly object SendLocker = new object ();
        private readonly object DownloadLocker = new object ();
 
        /// <summary>
        /// 多线程下载
        /// </summary>
        void MultiThreadDownload (List<LoadBackupFileNameInfo> dataList, DownloadSomeDataBackupObj mDownLoad, string mUrlHead, int maxThreads = 5)
        {
 
            int index = 0;
            int indexCount = 0;
 
            var mFileList = dataList.FindAll ((obj) => obj.BackupFileName != "null" && obj.BackupFileName != UserConfig.configFile && obj.BackupFileName != UserInfo.GlobalRegisterFile && obj.BackupFileName != "AccountListDB" && obj.BackupFileName != CommonConfig.ConfigFile);
            if (maxThreads > mFileList.Count) {
                maxThreads = mFileList.Count;
            }
 
            if (mFileList.Count <= 0) return;
 
 
            int resultCount = mFileList.Count / maxThreads;
 
            //int residue = mFileList.Count % maxThreads;
 
            /*开启若干线程,分别下载对应的资源*/
            for (int i = 1; i <= maxThreads; i++) {
                int startIndex = ((i - 1) * resultCount + 0);
                int endIndex = resultCount * i - 1;
                if (i == maxThreads) {
                    /*
                     * 最后一个线程
                     * */
                    endIndex = mFileList.Count - 1;
                }
                var id = i;
                var startIndex1 = startIndex;
                var endIndex1 = endIndex;
                //Utlis.WriteLine ($"下载线程:{id} startIndex1:{startIndex1}  endIndex1:{endIndex1}");
                new System.Threading.Thread (() => {
 
                    for (var j = startIndex1; j <= endIndex1; j++) {
                        try {
                            //如果失败重新下载,重复3次
                            int errorCount = 0;
                            while (errorCount < 3) {
                                var result = DownloadSomeDataBackup (mDownLoad, mUrlHead, mFileList [j]);
                                if (result) {
                                    lock (DownloadLocker) {
                                        index++;
                                    }
                                    Application.RunOnMainThread (() => {
                                        int pro = (int)(index * 1.0 / mFileList.Count * 100);
                                        MainPage.Loading.Text = pro.ToString () + "%";
                                    });
                                    errorCount = 3;
                                } else {
                                    errorCount++;
                                    Utlis.WriteLine ($"下载失败: {mFileList [j].BackupFileName} 次数:{errorCount}");
                                }
                            }
                        } catch { } finally {
                            lock (SendLocker) {
                                indexCount++;
                            }
                        }
 
                        //var t1 = DateTime.Now.Second;
                        //var result = DownloadSomeDataBackup (mDownLoad, mUrlHead, mFileList [j]);
                        //if (result) {
                        //    index++;
                        //    Application.RunOnMainThread (() => {
                        //        int pro = (int)(index * 1.0 / mFileList.Count * 100);
                        //        MainPage.Loading.Text = pro.ToString () + "%";
                        //    });
 
                        //} else {
                        //    Utlis.WriteLine ($"下载失败: {mFileList [j].BackupFileName} 时间:{DateTime.Now.Second - t1}S");
                        //}
                        //indexCount++;
                        //Utlis.WriteLine ($"下载线程:{id}  完成index:{j}  indexCount: {indexCount}");
                    }
 
                }) { IsBackground = true }.Start ();
 
                System.Threading.Thread.Sleep (100);
 
            }
 
            while (indexCount < mFileList.Count) {
                System.Threading.Thread.Sleep (200);
            }
            Utlis.WriteLine ($"下载完成");
 
            if (index != mFileList.Count) {
                //删除已经下载的文件个数错误
                ReadFilesAndDelete ();
 
                Application.RunOnMainThread (() => {
                    new Alert (Language.StringByID (R.MyInternationalizationString.Tip), Language.StringByID (R.MyInternationalizationString.DownloadFailed),
                               Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                });
 
 
                return;
            }
 
 
            MainPage.LoginUser.SaveUserInfo ();
            Room.InitAllRoom ();
            Application.RunOnMainThread (() => {
                this.RemoveFromParent ();
                //CommonPage.IsRemote = false;
                UserMiddle.Init ();
                EquipmentPublicClass.CheckLinkRemote (UserConfig.Instance.internetStatus);
                new Alert ("", Language.StringByID (R.MyInternationalizationString.RestoreFileIsSuccessfull),
               Language.StringByID (R.MyInternationalizationString.Close)).Show ();
            });
            MainPage.LoginUser = Newtonsoft.Json.JsonConvert.DeserializeObject<UserInfo> (System.Text.Encoding.UTF8.GetString (Shared.IO.FileUtils.ReadFile (UserInfo.GlobalRegisterFile)));
            if (MainPage.LoginUser != null) {
                MainPage.LoginUser.LastTime = System.DateTime.Now;
                MainPage.LoginUser.SaveUserInfo ();
            }
 
 
 
        }
 
        bool UploadDataBackup (UploadHomeAppGatewaySubFilesObj upData, string mUrlHead, List<string> fileList, int startIndex1, int endIndex1)
        {
            try {
 
                var list = new List<FileInfoData> ();
                for (var j = startIndex1; j <= endIndex1; j++) {
                    var datainfo = new FileInfoData ();
                    datainfo.BackupFileName = fileList [j];
                    datainfo.BackupFileContent = Shared.IO.FileUtils.ReadFile (fileList [j]);
                    list.Add (datainfo);
                }
 
                //整理数据
                UploadHomeAppGatewaySubFilesObj mUpData = upData;
                mUpData.UploadSubFileLists = list;
 
                var requestJson2 = Newtonsoft.Json.JsonConvert.SerializeObject (mUpData);
                var revertObj2 = MainPage.RequestHttps (API.UploadHomeAppGatewaySubFiles, requestJson2, mUrlHead);
                if (revertObj2.StateCode.ToUpper () == StateCode.SUCCESS) {
                    return true;
                } else {
                    //提示错误
                    return false;
                }
            } catch {
                return false;
            }
        }
 
 
 
 
        /// <summary>
        /// 上传备份文件
        /// 多次上传多个文件
        /// </summary>
        /// <param name="mBackupClassId"></param>
        void UpLoadBackupFileToDBNew (string mBackupClassId, int maxThreads = 20)
        {
            var backuplist = IO.FileUtils.ReadFiles ();
            var mFileList = backuplist.FindAll ((obj) => obj != "null" && obj != UserConfig.configFile && obj != UserInfo.GlobalRegisterFile && obj != "AccountListDB" && obj != CommonConfig.ConfigFile);
            if (mFileList.Count <= 0) return;
 
            if (maxThreads > mFileList.Count) {
                maxThreads = mFileList.Count;
            }
 
            int index = 0;
            int indexCount = 0;
            int resultCount = mFileList.Count / maxThreads;
 
            var upData = new UploadHomeAppGatewaySubFilesObj ();
            upData.HomeId = UserConfig.Instance.CurrentRegion.Id;
            upData.BackupClassId = mBackupClassId;
            //获取控制主人账号的Token
            upData.LoginAccessToken = MainPage.LoginUser.LoginTokenString;
            string urlHead = MainPage.RequestHttpsHost;
            if (upData.IsOtherAccountCtrl) {
                urlHead = UserConfig.Instance.MasterAccountRequestBaseUrl;
                upData.LoginAccessToken = UserConfig.Instance.MasterAccountToken;
            }
 
 
            for (int i = 1; i <= resultCount; i++) {
                int startIndex = ((i - 1) * maxThreads + 0);
                int endIndex = maxThreads * i - 1;
                if (i == resultCount) {
                    /*最后一个线程* */
                    endIndex = mFileList.Count - 1;
                }
                var id = i;
                var startIndex1 = startIndex;
                var endIndex1 = endIndex;
                //2020-06-10 修改大图片大文件处理
                var result = UploadDataBackupNew (upData, urlHead, mFileList, startIndex1, endIndex1);
                if (result) {
                    index++;
                    Application.RunOnMainThread (() => {
                        int pro = (int)(index * 1.0 / resultCount * 100);
                        MainPage.Loading.Text = pro.ToString () + "%";
                    });
 
                }
                indexCount++;
 
            }
 
 
            Utlis.WriteLine ($"上传完成");
 
            if (index != resultCount) {
                //2020-01-11 备份失败
                DeleteFolderDataAfterUploadFailed (mBackupClassId);
                Shared.Application.RunOnMainThread (() => {
                    new Alert ("", Language.StringByID (R.MyInternationalizationString.FailedToBackupFile), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                    MainPage.Loading.Hide ();
                });
                return;
            }
 
            Application.RunOnMainThread (() => {
                //MainPage.Loading.Text = "100%";
                new Alert ("", Language.StringByID (R.MyInternationalizationString.BackupFileIsSuccessful), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
            });
 
        }
 
 
        /// <summary>
        /// 2020-06-05 需要等待云端增加接口
        /// 处理大文件和图片上传
        /// 
        /// </summary>
        /// <param name="upData"></param>
        /// <param name="mUrlHead"></param>
        /// <param name="fileList"></param>
        /// <param name="startIndex1"></param>
        /// <param name="endIndex1"></param>
        /// <returns></returns>
        bool UploadDataBackupNew (UploadHomeAppGatewaySubFilesObj upData, string mUrlHead, List<string> fileList, int startIndex1, int endIndex1)
        {
            try {
 
                var list = new List<FileInfoData> ();
                for (var j = startIndex1; j <= endIndex1; j++) {
                    var datainfo = new FileInfoData ();
                    datainfo.BackupFileName = fileList [j];
                    datainfo.BackupFileContent = Shared.IO.FileUtils.ReadFile (fileList [j]);
 
                    if (datainfo.BackupFileContent.Length > 1024 * 10) {
                        var upDataBig = new UploadHomeAppGatewaySubFilesBigObj ();
                        upDataBig.RequestVersion = upData.RequestVersion;
                        upDataBig.HomeId = upData.HomeId;
                        upDataBig.BackupClassId = upData.BackupClassId;
                        upDataBig.FileName = fileList [j];
                        upDataBig.IsOtherAccountCtrl = upData.IsOtherAccountCtrl;
 
                        var revertObj5 = MainPage.RequestHttpsNewBig (API.UploadHomeAppGatewaySingleSubFile, datainfo.BackupFileContent, upDataBig, mUrlHead);
 
                    } else {
                        list.Add (datainfo);
                    }
                }
 
                //整理数据
                UploadHomeAppGatewaySubFilesObj mUpData = upData;
                mUpData.UploadSubFileLists = list;
 
                //var requestJson2 = Newtonsoft.Json.JsonConvert.SerializeObject (mUpData);
                //var revertObj2 = MainPage.RequestHttps (API.UploadHomeAppGatewaySubFiles, requestJson2, mUrlHead);
                //if (revertObj2.StateCode.ToUpper () == StateCode.SUCCESS) {
                //    return true;
                //} else {
                //    //提示错误
                //    return false;
                //}
 
                //2020-11-16 如果失败重新上传,重复3次
                int errorCount = 0;
                var result = false;
                while (errorCount < 3) {
                    var requestJson2 = Newtonsoft.Json.JsonConvert.SerializeObject (mUpData);
                    var revertObj2 = MainPage.RequestHttps (API.UploadHomeAppGatewaySubFiles, requestJson2, mUrlHead, MainPage.TIME_OUT_20);
                    if (revertObj2.StateCode.ToUpper () == StateCode.SUCCESS) {
                        result = true;
                        errorCount = 3;
                    } else {
                        errorCount++;
                        //Utlis.WriteLine ($"上传失败({startIndex1}-{endIndex1}),次数:{errorCount}");
                    }
                }
                return result;
            } catch {
                return false;
            }
        }
 
 
    }
}