Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Export.cs
1using System;
3using System.IO;
4using System.Text;
5using System.Threading.Tasks;
6using Waher.Content;
7using Waher.Events;
11
12namespace Waher.IoTGateway
13{
17 public static class Export
18 {
22 public static async Task<string> GetFullExportFolderAsync()
23 {
24 string Path = await GetExportFolderAsync();
25
26 if (string.IsNullOrEmpty(Path))
27 Path = System.IO.Path.Combine(Gateway.AppDataFolder, "Backup");
28
29 return Path;
30 }
31
35 public static async Task<string> GetFullKeyExportFolderAsync()
36 {
37 string Path = await GetExportKeyFolderAsync();
38
39 if (string.IsNullOrEmpty(Path))
40 Path = System.IO.Path.Combine(Gateway.AppDataFolder, "Backup");
41
42 return Path;
43 }
44
49 public static async Task<FileInformation[]> GetExportFilesAsync()
50 {
51 SortedDictionary<DateTime, FileInformation> Sorted = new SortedDictionary<DateTime, FileInformation>(new ReverseDateTimeOrder());
52 string Path = await GetFullExportFolderAsync();
53 if (Directory.Exists(Path))
54 GetFiles(Path, Sorted);
55
56 string Path2 = await GetFullKeyExportFolderAsync();
57 if (Path != Path2 && Directory.Exists(Path2))
58 GetFiles(Path2, Sorted);
59
60 FileInformation[] Result = new FileInformation[Sorted.Count];
61 Sorted.Values.CopyTo(Result, 0);
62
63 return Result;
64 }
65
66 private static void GetFiles(string Path, SortedDictionary<DateTime, FileInformation> Sorted)
67 {
68 string[] Files = Directory.GetFiles(Path, "*.*", SearchOption.TopDirectoryOnly);
69 int i, c = Files.Length;
70 DateTime Created;
71 long Size;
72 string SizeStr;
73 string s;
74
75 Path += System.IO.Path.DirectorySeparatorChar;
76
77 for (i = 0; i < c; i++)
78 {
79 s = Files[i];
80
81 try
82 {
83 using (FileStream fs = File.OpenRead(s))
84 {
85 Size = fs.Length;
86 SizeStr = FormatBytes(Size);
87 }
88 }
89 catch (Exception ex)
90 {
91 Log.Exception(ex);
92 Size = 0;
93 SizeStr = string.Empty;
94 }
95
96 Created = File.GetCreationTime(s);
97
98 if (s.StartsWith(Path))
99 s = s.Substring(Path.Length);
100
101 while (Sorted.ContainsKey(Created))
102 Created = Created.AddTicks(1);
103
104 Sorted[Created] = new FileInformation(s, Created, Size, SizeStr);
105 }
106 }
107
108 private class ReverseDateTimeOrder : IComparer<DateTime>
109 {
110 public ReverseDateTimeOrder()
111 {
112 }
113
114 public int Compare(DateTime x, DateTime y)
115 {
116 return y.CompareTo(x);
117 }
118 }
119
125 public static string FormatBytes(double Bytes)
126 {
127 int i = 0;
128
129 while (Bytes >= 1024 && i < 4)
130 {
131 Bytes /= 1024;
132 i++;
133 }
134
135 if (i == 0)
136 return Bytes.ToString("F0") + " " + ByteUnits[i];
137 else
138 return Bytes.ToString("F2") + " " + ByteUnits[i];
139 }
140
141 private static readonly string[] ByteUnits = { "B", "kB", "MB", "GB", "TB" };
142
146 public static async Task<string> GetExportFolderAsync()
147 {
148 if (exportFolderValue is null)
149 exportFolderValue = await RuntimeSettings.GetAsync("ExportFolder", string.Empty);
150
151 return exportFolderValue;
152 }
153
158 public static async Task SetExportFolderAsync(string Value)
159 {
160 if (exportFolderValue != Value)
161 {
162 exportFolderValue = Value;
163 await RuntimeSettings.SetAsync("ExportFolder", exportFolderValue);
164
165 if (!(BackupConfiguration.Instance is null))
166 await BackupConfiguration.Instance.UpdateExportFolder(await GetFullExportFolderAsync());
167
168 await OnExportFolderUpdated.Raise(BackupConfiguration.Instance, EventArgs.Empty);
169 }
170 }
171
172 private static string exportFolderValue = null;
173
178
182 public static async Task<string> GetExportKeyFolderAsync()
183 {
184 if (exportKeyFolderValue is null)
185 exportKeyFolderValue = await RuntimeSettings.GetAsync("ExportKeyFolder", string.Empty);
186
187 return exportKeyFolderValue;
188 }
189
194 public static async Task SetExportKeyFolderAsync(string Value)
195 {
196 if (exportKeyFolderValue != Value)
197 {
198 exportKeyFolderValue = Value;
199 await RuntimeSettings.SetAsync("ExportKeyFolder", exportKeyFolderValue);
200
201 BackupConfiguration.Instance?.UpdateExportKeyFolder(await GetFullKeyExportFolderAsync());
202
203 await OnExportKeyFolderUpdated.Raise(BackupConfiguration.Instance, EventArgs.Empty);
204 }
205 }
206
207 private static string exportKeyFolderValue = null;
208
213
217 public static bool ExportDatabase
218 {
219 get
220 {
221 if (!exportDatabase.HasValue)
222 exportDatabase = RuntimeSettings.Get("ExportDatabase", true);
223
224 return exportDatabase.Value;
225 }
226
227 set
228 {
229 if (exportDatabase != value)
230 {
231 exportDatabase = value;
232 RuntimeSettings.Set("ExportDatabase", exportDatabase.Value);
233 }
234 }
235 }
236
237 private static bool? exportDatabase = null;
238
242 public static bool ExportLedger
243 {
244 get
245 {
246 if (!exportLedger.HasValue)
247 exportLedger = RuntimeSettings.Get("ExportLedger", false);
248
249 return exportLedger.Value;
250 }
251
252 set
253 {
254 if (exportLedger != value)
255 {
256 exportLedger = value;
257 RuntimeSettings.Set("ExportLedger", exportLedger.Value);
258 }
259 }
260 }
261
262 private static bool? exportLedger = null;
263
267 public static bool ExportWebContent
268 {
269 get
270 {
271 if (!exportWebContent.HasValue)
272 exportWebContent = RuntimeSettings.Get("ExportWebContent", false);
273
274 return exportWebContent.Value;
275 }
276
277 set
278 {
279 if (exportWebContent != value)
280 {
281 exportWebContent = value;
282 RuntimeSettings.Set("ExportWebContent", exportWebContent.Value);
283 }
284 }
285 }
286
287 private static bool? exportWebContent = null;
288
294 public static async Task<bool> GetExportFolderAsync(string FolderName)
295 {
296 string Key = "Export." + FolderName;
297 bool Result;
298
299 lock (exportFolder)
300 {
301 if (exportFolder.TryGetValue(Key, out Result))
302 return Result;
303 }
304
305 Result = await RuntimeSettings.GetAsync(Key, true);
306
307 lock (exportFolder)
308 {
309 exportFolder[Key] = Result;
310 }
311
312 return Result;
313 }
314
320 public static async Task SetExportFolderAsync(string FolderName, bool Export)
321 {
322 string Key = "Export." + FolderName;
323
324 lock (exportFolder)
325 {
326 if (exportFolder.TryGetValue(Key, out bool b) && b == Export)
327 return;
328
329 exportFolder[Key] = Export;
330 }
331
332 await RuntimeSettings.SetAsync(Key, Export);
333 }
334
335 private static readonly Dictionary<string, bool> exportFolder = new Dictionary<string, bool>();
336
340 public static string ExportType
341 {
342 get
343 {
344 if (exportType is null)
345 exportType = RuntimeSettings.Get("ExportType", "Encrypted");
346
347 return exportType;
348 }
349
350 set
351 {
352 if (exportType != value)
353 {
354 exportType = value;
355 RuntimeSettings.Set("ExportType", exportType);
356 }
357 }
358 }
359
360 private static string exportType = null;
361
365 public static async Task SetAutomaticBackupsAsync(bool Value)
366 {
367 if (automaticBackups != Value)
368 {
369 automaticBackups = Value;
370 await RuntimeSettings.SetAsync("AutomaticBackups", automaticBackups.Value);
371 }
372 }
373
377 public static async Task<bool> GetAutomaticBackupsAsync()
378 {
379 if (!automaticBackups.HasValue)
380 automaticBackups = await RuntimeSettings.GetAsync("AutomaticBackups", true);
381
382 return automaticBackups.Value;
383 }
384
385 private static bool? automaticBackups = null;
386
391 public static async Task SetKeepDaysAsync(long Value)
392 {
393 if (backupKeepDays != Value)
394 {
395 backupKeepDays = Value;
396 await RuntimeSettings.SetAsync("BackupKeepDays", backupKeepDays.Value);
397 }
398 }
399
403 public static async Task<long> GetKeepDaysAsync()
404 {
405 if (!backupKeepDays.HasValue)
406 backupKeepDays = await RuntimeSettings.GetAsync("BackupKeepDays", 3);
407
408 return backupKeepDays.Value;
409 }
410
411 private static long? backupKeepDays = null;
412
417 public static async Task SetKeepMonthsAsync(long Value)
418 {
419 if (backupKeepMonths != Value)
420 {
421 backupKeepMonths = Value;
422 await RuntimeSettings.SetAsync("BackupKeepMonths", backupKeepMonths.Value);
423 }
424 }
425
429 public static async Task<long> GetKeepMonthsAsync()
430 {
431 if (!backupKeepMonths.HasValue)
432 backupKeepMonths = await RuntimeSettings.GetAsync("BackupKeepMonths", 3);
433
434 return backupKeepMonths.Value;
435 }
436
437 private static long? backupKeepMonths = null;
438
443 public static async Task SetKeepYearsAsync(long Value)
444 {
445 if (backupKeepYears != Value)
446 {
447 backupKeepYears = Value;
448 await RuntimeSettings.SetAsync("BackupKeepYears", backupKeepYears.Value);
449 }
450 }
451
455 public static async Task<long> GetKeepYearsAsync()
456 {
457 if (!backupKeepYears.HasValue)
458 backupKeepYears = await RuntimeSettings.GetAsync("BackupKeepYears", 3);
459
460 return backupKeepYears.Value;
461 }
462
463 private static long? backupKeepYears = null;
464
469 public static async Task SetBackupTimeAsync(TimeSpan Value)
470 {
471 if (backupTime != Value)
472 {
473 backupTime = Value;
474 await RuntimeSettings.SetAsync("BackupTime", backupTime.Value);
475 }
476 }
477
481 public static async Task<TimeSpan> GetBackupTimeAsync()
482 {
483 if (!backupTime.HasValue)
484 backupTime = await RuntimeSettings.GetAsync("BackupTime", TimeSpan.FromHours(5));
485
486 return backupTime.Value;
487 }
488
489 private static TimeSpan? backupTime = null;
490
494 public static async Task<DateTime> GetLastBackupAsync()
495 {
496 if (!lastBackup.HasValue)
497 lastBackup = await RuntimeSettings.GetAsync("LastBackup", DateTime.MinValue);
498
499 return lastBackup.Value;
500 }
501
505 public static async Task SetLastBackupAsync(DateTime Value)
506 {
507 if (lastBackup != Value)
508 {
509 lastBackup = Value;
510 await RuntimeSettings.SetAsync("LastBackup", Value);
511 }
512 }
513
514 private static DateTime? lastBackup = null;
515
522 public static void RegisterFolders(string CategoryId, string DisplayName, params string[] Folders)
523 {
524 lock (folders)
525 {
526 if (folders.ContainsKey(CategoryId))
527 throw new ArgumentException("Category ID already registered.", nameof(CategoryId));
528
529 folders[DisplayName] = new FolderCategory()
530 {
531 CategoryId = CategoryId,
532 DisplayName = DisplayName,
533 Folders = Folders
534 };
535 }
536 }
537
543 {
544 FolderCategory[] Result;
545
546 lock (folders)
547 {
548 Result = new FolderCategory[folders.Count];
549 folders.Values.CopyTo(Result, 0);
550 }
551
552 return Result;
553 }
554
558 public class FolderCategory
559 {
563 public string CategoryId;
564
568 public string DisplayName;
569
573 public string[] Folders;
574 }
575
576 private static readonly Dictionary<string, FolderCategory> folders = new Dictionary<string, FolderCategory>();
577
581 public static async Task<string[]> GetBackupHostsAsync()
582 {
583 if (backupHosts is null)
584 backupHosts = StringToArray(await RuntimeSettings.GetAsync("BackupHosts", string.Empty));
585
586 return backupHosts;
587 }
588
593 public static async Task SetBackupHostsAsync(string[] Value)
594 {
595 if (backupHosts != Value)
596 {
597 backupHosts = Value;
598 await RuntimeSettings.SetAsync("BackupHosts", ArrayToString(backupHosts));
599 }
600 }
601
602 private static string[] backupHosts = null;
603
607 public static async Task<string[]> GetKeyHostsAsync()
608 {
609 if (keyHosts is null)
610 keyHosts = StringToArray(await RuntimeSettings.GetAsync("KeyHosts", string.Empty));
611
612 return keyHosts;
613 }
614
619 public static async Task SetKeyHostsAsync(string[] Value)
620 {
621 if (keyHosts != Value)
622 {
623 keyHosts = Value;
624 await RuntimeSettings.SetAsync("KeyHosts", ArrayToString(keyHosts));
625 }
626 }
627
628 private static string[] keyHosts = null;
629
630 private static string[] StringToArray(string s)
631 {
632 return s.Split(CommonTypes.CRLF, StringSplitOptions.RemoveEmptyEntries);
633 }
634
635 private static string ArrayToString(string[] Items)
636 {
637 StringBuilder Result = new StringBuilder();
638 bool First = true;
639
640 foreach (string Item in Items)
641 {
642 if (First)
643 First = false;
644 else
645 Result.AppendLine();
646
647 Result.Append(Item);
648 }
649
650 return Result.ToString();
651 }
652 }
653}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:15
static readonly char[] CRLF
Contains the CR LF character sequence.
Definition: CommonTypes.cs:19
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1657
Information about an exportable folder category
Definition: Export.cs:559
string[] Folders
Set of folders
Definition: Export.cs:573
string DisplayName
Display name
Definition: Export.cs:568
Static class managing data export.
Definition: Export.cs:18
static async Task< string[]> GetKeyHostsAsync()
Secondary key hosts.
Definition: Export.cs:607
static async Task< string > GetExportFolderAsync()
Export folder.
Definition: Export.cs:146
static async Task< string > GetFullExportFolderAsync()
Full path to export folder.
Definition: Export.cs:22
static FolderCategory[] GetRegisteredFolders()
Gets registered exportable folders.
Definition: Export.cs:542
static async Task< string > GetFullKeyExportFolderAsync()
Full path to key folder.
Definition: Export.cs:35
static async Task< FileInformation[]> GetExportFilesAsync()
Gets information about exported files.
Definition: Export.cs:49
static async Task< string[]> GetBackupHostsAsync()
Secondary backup hosts.
Definition: Export.cs:581
static async Task SetLastBackupAsync(DateTime Value)
Set Timestamp of last backup.
Definition: Export.cs:505
static async Task SetKeepDaysAsync(long Value)
For how many days backups are kept.
Definition: Export.cs:391
static async Task< bool > GetExportFolderAsync(string FolderName)
Gets if a folder should be exported or not.
Definition: Export.cs:294
static async Task SetKeepMonthsAsync(long Value)
For how many months the monthly backups are kept.
Definition: Export.cs:417
static async Task SetAutomaticBackupsAsync(bool Value)
If automatic backups are activated
Definition: Export.cs:365
static async Task SetKeyHostsAsync(string[] Value)
Secondary key hosts.
Definition: Export.cs:619
static EventHandlerAsync OnExportKeyFolderUpdated
Event raised when the export key folder has been updated.
Definition: Export.cs:212
static bool ExportLedger
If the ledger should be exported.
Definition: Export.cs:243
static bool ExportDatabase
If the database should be exported.
Definition: Export.cs:218
static async Task< string > GetExportKeyFolderAsync()
Key folder
Definition: Export.cs:182
static async Task< long > GetKeepMonthsAsync()
For how many months the monthly backups are kept.
Definition: Export.cs:429
static async Task SetExportFolderAsync(string FolderName, bool Export)
Sets if a folder should be exported or not.
Definition: Export.cs:320
static void RegisterFolders(string CategoryId, string DisplayName, params string[] Folders)
Registers a set of exportable folders under one display name.
Definition: Export.cs:522
static async Task SetBackupTimeAsync(TimeSpan Value)
Time of day to start performing backups.
Definition: Export.cs:469
static string FormatBytes(double Bytes)
Formats a file size using appropriate unit.
Definition: Export.cs:125
static string ExportType
Export file type.
Definition: Export.cs:341
static bool ExportWebContent
If web content should be exported.
Definition: Export.cs:268
static async Task< long > GetKeepYearsAsync()
For how many years the yearly backups are kept.
Definition: Export.cs:455
static async Task SetBackupHostsAsync(string[] Value)
Secondary backup hosts.
Definition: Export.cs:593
static async Task< DateTime > GetLastBackupAsync()
Get Timestamp of last backup.
Definition: Export.cs:494
static EventHandlerAsync OnExportFolderUpdated
Event raised when the export folder has been updated.
Definition: Export.cs:177
static async Task SetKeepYearsAsync(long Value)
For how many years the yearly backups are kept.
Definition: Export.cs:443
static async Task< long > GetKeepDaysAsync()
For how many days backups are kept.
Definition: Export.cs:403
static async Task SetExportFolderAsync(string Value)
Export folder.
Definition: Export.cs:158
static async Task SetExportKeyFolderAsync(string Value)
Key folder
Definition: Export.cs:194
static async Task< bool > GetAutomaticBackupsAsync()
If automatic backups are activated
Definition: Export.cs:377
static async Task< TimeSpan > GetBackupTimeAsync()
Time of day to start performing backups.
Definition: Export.cs:481
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static string AppDataFolder
Application data folder.
Definition: Gateway.cs:3132
static BackupConfiguration Instance
Current instance of configuration.
Class containing information about a file.
Static class managing persistent settings.
static bool Set(string Key, string Value)
Sets a string-valued setting.
static string Get(string Key, string DefaultValue)
Gets a string-valued setting.
static async Task< string > GetAsync(string Key, string DefaultValue)
Gets a string-valued setting.
static async Task< bool > SetAsync(string Key, string Value)
Sets a string-valued setting.
delegate Task EventHandlerAsync(object Sender, EventArgs e)
Asynchronous version of EventArgs.