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