HDL Home App 第二版本 旧平台金堂用 正在使用
黄学彪
2020-12-14 e90209beae6a4e822cecb18e6889f8bda23f630e
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
using Newtonsoft.Json;
using Shared.Common;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
 
namespace Shared.Phone.UserCenter
{
    /// <summary>
    /// 自动备份的逻辑
    /// </summary>
    public class HdlAutoBackupLogic
    {
        #region ■ 变量声明___________________________
 
        #endregion
 
        #region ■ 上传备份___________________________
 
        /// <summary>
        /// 手动执行上传自动备份数据(0:没有可上传的自动备份数据 1:成功 -1:失败)
        /// </summary>
        /// <returns></returns>
        public static int DoUpLoadAutoBackupDataByHand()
        {
            //编辑文件
            List<string> listEditor = GetAutoBackupEditorFile();
            //删除文件
            List<string> listDelete = GetAutoBackupDeleteFile();
            //没有数据
            if (listEditor.Count == 0 && listDelete.Count == 0)
            {
                return 0;
            }
 
            //开启进度条 正在上传备份文件
            ProgressFormBar.Current.Start();
            ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uBackupFileUploading));
 
            //上传文件到云端
            bool result = UpLoadBackupFileToDB(listEditor);
            if (result == false)
            {
                ProgressFormBar.Current.Close();
                return -1;
            }
 
            //删除文件
            result = DoDeleteFileFromDB(listDelete);
            if (result == false)
            {
                ProgressFormBar.Current.Close();
                return -1;
            }
 
            ProgressFormBar.Current.Close();
 
            return 1;
        }
 
        /// <summary>
        /// 上传文件到云端
        /// </summary>
        /// <param name="listFile">编辑或者添加的文件(目前已经不是上传它了)</param>
        /// <returns></returns>
        private static bool UpLoadBackupFileToDB(List<string> listFile)
        {
            string localPath = Config.Instance.FullPath;
 
            //将模板数据保存到到指定的文件夹中
            var templateName = TemplateData.TemplateCommonLogic.Current.GetNewTemplateFileName(new DateTime(2000, 12, 31, 12, 59, 57));
            var templateFile = TemplateData.TemplateCommonLogic.Current.SaveTemplateDataToFile(templateName, "HomeTemplate");
 
            //将模板bin文件移动到备份文件夹中
            var localTemplateName = System.IO.Path.Combine(localPath, templateName);
            try { System.IO.File.Move(templateFile, localTemplateName); }
            catch (Exception ex) { HdlLogLogic.Current.WriteLog(ex, "移动模板失败"); }
 
            //获取本地文件
            var listAllFile = HdlFileLogic.Current.GetRootPathListFile();
            var listUpFile = new List<string>();
            foreach (string fileName in listAllFile)
            {
                //判断指定文件是否需要上传(根目录的才判断)
                if (HdlBackupLogic.Current.IsNotUpLoadFile(fileName) == true)
                {
                    continue;
                }
                //其他图片的资源文件,只有在变更了的时候,才上传
                if (fileName.EndsWith(".png") == true && listFile.Contains(fileName) == false)
                {
                    continue;
                }
                listUpFile.Add(fileName);
            }
 
            int listFileCount = listUpFile.Count;
            for (int i = 0; i < listUpFile.Count; i++)
            {
                string file = listUpFile[i];
                var datainfo = new FileInfoData();
                datainfo.BackupFileName = file;
                datainfo.BackupFileContent = HdlFileLogic.Current.ReadFileByteContent(System.IO.Path.Combine(localPath, file));
                if (datainfo.BackupFileContent == null)
                {
                    continue;
                }
 
                var upData = new UpLoadDataPra();
                upData.HomeId = Config.Instance.Home.Id;
                upData.UploadSubFileLists = new List<FileInfoData> { datainfo };
                //获取控制主人账号的Token
                upData.LoginAccessToken = UserCenterLogic.GetConnectMainToken();
 
                //执行是上传
                bool falge = UserCenterLogic.GetResultStatuByRequestHttps("App/HomeAppAutoDataBackup", true, upData, null, true);
                if (falge == false)
                {
                    return false;
                }
                //设置进度值
                ProgressFormBar.Current.SetValue(i + 1, listFileCount);
            }
 
            //删除掉这个模板文件
            HdlFileLogic.Current.DeleteFile(localTemplateName);
 
            //删除文件
            var backPath = DirNameResourse.AutoBackupDirectory;
            foreach (var file in listFile)
            {
                string fullName = System.IO.Path.Combine(backPath, file);
                HdlFileLogic.Current.DeleteFile(fullName);
            }
 
            return true;
        }
 
        /// <summary>
        /// 云端执行删除指定文件
        /// </summary>
        /// <param name="listData">删除的文件</param>
        /// <returns></returns>
        private static bool DoDeleteFileFromDB(List<string> listData)
        {
            if (listData.Count == 0)
            {
                return true;
            }
 
            //获取app的自动备份
            var data = HdlBackupLogic.Current.GetBackupListNameFromDB(1);
            if (data == null || data.Count == 0)
            {
                return true;
            }
            //自动备份只有一个
            var autoBackupId = data[0].Id;
 
            var upData = new DeleteFilePra();
            upData.BackupClassId = autoBackupId;
            upData.HomeId = Config.Instance.Home.Id;
            upData.DeleteFileNameLists = listData;
            //获取控制主人账号的Token
            upData.LoginAccessToken = UserCenterLogic.GetConnectMainToken();
 
            bool falge = UserCenterLogic.GetResultStatuByRequestHttps("App/DeleteAppBackupFile", true, upData);
            if (falge == false)
            {
                return false;
            }
 
            //删除文件
            var backPath = DirNameResourse.AutoBackupdeleteDirectory;
            foreach (var file in listData)
            {
                string fullName = System.IO.Path.Combine(backPath, file);
                HdlFileLogic.Current.DeleteFile(fullName);
            }
            return true;
        }
 
        #endregion
 
        #region ■ 获取文件___________________________
 
        /// <summary>
        /// 获取自动备份目录下的添加或者编辑的文件
        /// </summary>
        /// <returns></returns>
        public static List<string> GetAutoBackupEditorFile()
        {
            return HdlFileLogic.Current.GetFileFromDirectory(DirNameResourse.AutoBackupDirectory);
        }
 
        /// <summary>
        /// 获取自动备份目录下的删除的文件
        /// </summary>
        /// <returns></returns>
        public static List<string> GetAutoBackupDeleteFile()
        {
            return HdlFileLogic.Current.GetFileFromDirectory(DirNameResourse.AutoBackupdeleteDirectory);
        }
 
        #endregion
 
        #region ■ 设置文件状态_______________________
 
        /// <summary>
        /// 添加或者修改文件
        /// </summary>
        /// <param name="fileName">文件的名字,不含路径</param>
        public static void AddOrEditorFile(string fileName)
        {
            fileName = System.IO.Path.GetFileName(fileName);
            //自动备份目录
            string strBackPath = DirNameResourse.AutoBackupDirectory;
            if (System.IO.Directory.Exists(strBackPath) == false)
            {
                //预创建个人中心全部的文件夹
                HdlFileLogic.Current.CreatAllUserCenterDirectory();
            }
 
            //自动删除备份目录
            string strdelBackPath = DirNameResourse.AutoBackupdeleteDirectory;
            //如果删除列表里面有这个东西的话,移除掉
            string delFile = System.IO.Path.Combine(strdelBackPath, fileName);
            HdlFileLogic.Current.DeleteFile(delFile);
 
            string soureFile = System.IO.Path.Combine(Common.Config.Instance.FullPath, fileName);
            string newFile = System.IO.Path.Combine(strBackPath, fileName);
 
            //原原本本的复制文件到指定文件夹
            HdlFileLogic.Current.CopyFile(soureFile, newFile);
        }
 
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="fileName">文件的名字,不含路径</param>
        public static void DeleteFile(string fileName)
        {
            fileName = System.IO.Path.GetFileName(fileName);
            //自动删除备份目录
            string strBackPath = DirNameResourse.AutoBackupdeleteDirectory;
            string newFile = System.IO.Path.Combine(strBackPath, fileName);
 
            //创建一个空文件
            var file = System.IO.File.Create(newFile);
            file.Close();
 
            //自动备份目录
            strBackPath = DirNameResourse.AutoBackupDirectory;
            //如果备份列表里面有这个东西的话,移除掉
            string delFile = System.IO.Path.Combine(strBackPath, fileName);
 
            HdlFileLogic.Current.DeleteFile(delFile);
        }
 
        #endregion
 
        #region ■ 同步数据___________________________
 
        /// <summary>
        /// 同步云端数据(仅限APP启动之后) -1:异常   0:已经同步过,不需要同步  1:正常同步  2:没有自动备份数据
        /// </summary>
        /// <returns></returns>
        public static int SynchronizeDbAutoBackupData()
        {
            //暂时不支持成员
            if (UserCenterResourse.UserInfo.AuthorityNo == 3)
            {
                //同步服务器的分享内容
                HdlShardLogic.Current.SynchronizeDbSharedContent();
                return 1;
            }
            //判断是否能够同步数据
            string checkFile = DirNameResourse.AutoDownLoadBackupCheckFile;
            //如果本地已经拥有了这个文件,则说明不是新手机,不再自动还原
            if (System.IO.File.Exists(checkFile) == true)
            {
                return 0;
            }
 
            //获取app的自动备份
            var data = HdlBackupLogic.Current.GetBackupListNameFromDB(1);
            if (data == null)
            {
                return -1;
            }
            if (data.Count == 0)
            {
                //创建一个空文件(标识已经完成同步)
                var file = System.IO.File.Create(checkFile);
                file.Close();
                return 2;
            }
            //自动备份只有一个
            string backId = data[0].Id;
 
            //账号数据同步中
            ProgressFormBar.Current.Start();
            ProgressFormBar.Current.SetMsg(Language.StringByID(R.MyInternationalizationString.uAccountDataIsSynchronizing));
 
            //从云端获取备份的文件,然后存入本地指定的临时文件夹
            string tempDir = HdlBackupLogic.Current.GetBackFileFromDBAndSetToLocation(backId);
            if (tempDir == null)
            {
                //删除检测文件
                System.IO.File.Delete(checkFile);
                //关闭进度条
                ProgressFormBar.Current.Close();
                //同步失败
                return -1;
            }
            //如果读取到的文件完全没有问题,则清理本地的文件
            HdlFileLogic.Current.DeleteAllLocationFile(false);
 
            //没有错误的话,则移动到当前住宅文件夹下面
            HdlFileLogic.Current.MoveDirectoryFileToHomeDirectory(tempDir, true);
 
            //创建一个空文件(标识已经完成同步)
            var file2 = System.IO.File.Create(checkFile);
            file2.Close();
 
            //重新刷新住宅对象
            UserCenterLogic.RefreshHomeObject();
            //根据模板文件,恢复数据
            TemplateData.TemplateCommonLogic.Current.RecoverDataByTemplateBinFile();
            //强制生成设备和网关文件
            TemplateData.TemplateCommonLogic.Current.CreatDeviceAndGatewayFileFromMemoryByForce();
 
            //关闭进度条
            ProgressFormBar.Current.Close();
 
            return 1;
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 删除全部的自动备份的本地文件(此函数用于读取自动备份的时候使用)
        /// </summary>
        public static void DeleteAllAutoBackupFile()
        {
            //清空自动备份【文件夹】(编辑,追加)
            string dirPath = DirNameResourse.AutoBackupDirectory;
            HdlFileLogic.Current.DeleteDirectory(dirPath);
            HdlFileLogic.Current.CreateDirectory(dirPath, true);
 
            //清空自动备份【文件夹】(删除)
            dirPath = DirNameResourse.AutoBackupdeleteDirectory;
            HdlFileLogic.Current.DeleteDirectory(dirPath);
            HdlFileLogic.Current.CreateDirectory(dirPath, true);
        }
 
        #endregion
 
        #region ■ 备份提醒___________________________
 
        /// <summary>
        /// 保存备份提醒设置到本地
        /// </summary>
        /// <param name="notPrompted">不再提示</param>
        /// <param name="day"></param>
        public static void SaveBackupNotPrompted(bool notPrompted, int day = -1)
        {
            //文件全路径
            string fullName = DirNameResourse.AutoBackupNotPromptedFile;
            BackupNotPrompted info = null;
            if (System.IO.File.Exists(fullName) == true)
            {
                var data = HdlFileLogic.Current.ReadFileByteContent(fullName);
                info = JsonConvert.DeserializeObject<BackupNotPrompted>(System.Text.Encoding.UTF8.GetString(data));
            }
            if (info == null)
            {
                info = new BackupNotPrompted();
            }
 
            info.NotPrompted = notPrompted;
            if (day != -1)
            {
                info.OldDay = DateTime.Now.ToString("yyyy-MM-dd");
                info.Day = day;
            }
            //保存
            HdlFileLogic.Current.SaveFileContent(fullName, info);
        }
 
        /// <summary>
        /// 显示自动备份的界面
        /// </summary>
        public static void ShowAutoBackupPromptedForm()
        {
            if (UserCenterResourse.UserInfo.AuthorityNo == 3)
            {
                //暂不支持成员
                return;
            }
 
            //判断是否有文件变更了
            if (CheckAutoBackupFileIsChanged() == false)
            {
                return;
            }
            //判断能否显示自动备份的界面
            if (CheckCanShowAutoBackupForm() == true)
            {
                HdlThreadLogic.Current.RunMain(() =>
                {
                    var form = new HdlBackup.HdlAutoBackupForm();
                    form.AddForm();
                });
            }
        }
 
        /// <summary>
        /// 检测自动备份文件是否变更过
        /// </summary>
        /// <returns></returns>
        private static bool CheckAutoBackupFileIsChanged()
        {
            List<string> listFile1 = HdlFileLogic.Current.GetFileFromDirectory(DirNameResourse.AutoBackupDirectory);
            List<string> listFile2 = GetAutoBackupDeleteFile();
 
            if (listFile1.Count == 0 && listFile2.Count == 0)
            {
                //没有文件变更
                return false;
            }
            if (listFile2.Count > 0)
            {
                //有文件被删除
                return true;
            }
            foreach (var fileName in listFile1)
            {
                //住宅和收藏文件,不作为判断标准
                if (fileName.StartsWith("House_") == true
                  || fileName == "Room_Favorite.json")
                {
                    continue;
                }
                return true;
            }
            return false;
        }
 
        /// <summary>
        /// 检测能否显示自动备份的界面
        /// </summary>
        /// <returns></returns>
        private static bool CheckCanShowAutoBackupForm()
        {
            //文件全路径
            string fullName = DirNameResourse.AutoBackupNotPromptedFile;
            if (System.IO.File.Exists(fullName) == false)
            {
                //本地没有存在这个文件
                return true;
            }
            BackupNotPrompted info = null;
            var data = HdlFileLogic.Current.ReadFileByteContent(fullName);
            info = JsonConvert.DeserializeObject<BackupNotPrompted>(System.Text.Encoding.UTF8.GetString(data));
            if (info.NotPrompted == true)
            {
                //不再提示
                return false;
            }
            if (info.Day == 0)
            {
                return true;
            }
 
            DateTime oldTime = Convert.ToDateTime(info.OldDay);
            int intDay = (DateTime.Now - oldTime).Days;
            //时间已经超过
            if (intDay >= info.Day)
            {
                return true;
            }
            return false;
        }
 
        #endregion
 
        #region ■ 数据结构___________________________
 
        /// <summary>
        /// 自动备份不需要再次提醒的结构体
        /// </summary>
        private class BackupNotPrompted
        {
            /// <summary>
            /// 不再提示
            /// </summary>
            public bool NotPrompted = false;
            /// <summary>
            /// 前一次的日期:2019-01-01(格式)
            /// </summary>
            public string OldDay = string.Empty;
            /// <summary>
            /// 相隔日期天数
            /// </summary>
            public int Day = 0;
        }
 
        #endregion
    }
}