2using System.Collections.Generic;
5using System.Threading.Tasks;
27 if (
string.IsNullOrEmpty(Path))
40 if (
string.IsNullOrEmpty(Path))
52 SortedDictionary<DateTime, FileInformation> Sorted =
new SortedDictionary<DateTime, FileInformation>(
new ReverseDateTimeOrder());
54 if (Directory.Exists(Path))
55 GetFiles(Path, Sorted);
58 if (Path != Path2 && Directory.Exists(Path2))
59 GetFiles(Path2, Sorted);
62 Sorted.Values.CopyTo(Result, 0);
67 private static void GetFiles(
string Path, SortedDictionary<DateTime, FileInformation> Sorted)
69 string[] Files = Directory.GetFiles(Path,
"*.*", SearchOption.TopDirectoryOnly);
70 int i, c = Files.Length;
76 Path += System.IO.Path.DirectorySeparatorChar;
78 for (i = 0; i < c; i++)
84 using (FileStream fs = File.OpenRead(s))
94 SizeStr =
string.Empty;
97 Created = File.GetCreationTime(s);
99 if (s.StartsWith(Path))
100 s = s.Substring(Path.Length);
102 while (Sorted.ContainsKey(Created))
103 Created = Created.AddTicks(1);
109 private class ReverseDateTimeOrder : IComparer<DateTime>
111 public ReverseDateTimeOrder()
115 public int Compare(DateTime x, DateTime y)
117 return y.CompareTo(x);
130 while (Bytes >= 1024 && i < 4)
137 return Bytes.ToString(
"F0") +
" " + ByteUnits[i];
139 return Bytes.ToString(
"F2") +
" " + ByteUnits[i];
142 private static readonly
string[] ByteUnits = {
"B",
"kB",
"MB",
"GB",
"TB" };
149 if (exportFolderValue is
null)
152 return exportFolderValue;
161 if (exportFolderValue != Value)
163 exportFolderValue = Value;
173 private static string exportFolderValue =
null;
185 if (exportKeyFolderValue is
null)
188 return exportKeyFolderValue;
197 if (exportKeyFolderValue != Value)
199 exportKeyFolderValue = Value;
208 private static string exportKeyFolderValue =
null;
222 if (!exportDatabase.HasValue)
225 return exportDatabase.Value;
230 if (exportDatabase != value)
232 exportDatabase = value;
238 private static bool? exportDatabase =
null;
247 if (!exportLedger.HasValue)
250 return exportLedger.Value;
255 if (exportLedger != value)
257 exportLedger = value;
263 private static bool? exportLedger =
null;
272 if (!exportWebContent.HasValue)
275 return exportWebContent.Value;
280 if (exportWebContent != value)
282 exportWebContent = value;
288 private static bool? exportWebContent =
null;
297 string Key =
"Export." + FolderName;
302 if (exportFolder.TryGetValue(Key, out Result))
310 exportFolder[Key] = Result;
323 string Key =
"Export." + FolderName;
327 if (exportFolder.TryGetValue(Key, out
bool b) && b ==
Export)
330 exportFolder[Key] =
Export;
336 private static readonly Dictionary<string, bool> exportFolder =
new Dictionary<string, bool>();
345 if (exportType is
null)
353 if (exportType != value)
361 private static string exportType =
null;
368 if (automaticBackups != Value)
370 automaticBackups = Value;
380 if (!automaticBackups.HasValue)
383 return automaticBackups.Value;
386 private static bool? automaticBackups =
null;
394 if (backupKeepDays != Value)
396 backupKeepDays = Value;
406 if (!backupKeepDays.HasValue)
409 return backupKeepDays.Value;
412 private static long? backupKeepDays =
null;
420 if (backupKeepMonths != Value)
422 backupKeepMonths = Value;
432 if (!backupKeepMonths.HasValue)
435 return backupKeepMonths.Value;
438 private static long? backupKeepMonths =
null;
446 if (backupKeepYears != Value)
448 backupKeepYears = Value;
458 if (!backupKeepYears.HasValue)
461 return backupKeepYears.Value;
464 private static long? backupKeepYears =
null;
472 if (backupTime != Value)
484 if (!backupTime.HasValue)
487 return backupTime.Value;
490 private static TimeSpan? backupTime =
null;
497 if (!lastBackup.HasValue)
500 return lastBackup.Value;
508 if (lastBackup != Value)
515 private static DateTime? lastBackup =
null;
523 public static void RegisterFolders(
string CategoryId,
string DisplayName, params
string[] Folders)
527 if (folders.ContainsKey(CategoryId))
528 throw new ArgumentException(
"Category ID already registered.", nameof(CategoryId));
532 CategoryId = CategoryId,
533 DisplayName = DisplayName,
550 folders.Values.CopyTo(Result, 0);
577 private static readonly Dictionary<string, FolderCategory> folders =
new Dictionary<string, FolderCategory>();
584 if (backupHosts is
null)
596 if (backupHosts != Value)
603 private static string[] backupHosts =
null;
610 if (keyHosts is
null)
622 if (keyHosts != Value)
629 private static string[] keyHosts =
null;
631 private static string[] StringToArray(
string s)
633 return s.Split(
CommonTypes.
CRLF, StringSplitOptions.RemoveEmptyEntries);
636 private static string ArrayToString(
string[] Items)
638 StringBuilder Result =
new StringBuilder();
641 foreach (
string Item
in Items)
651 return Result.ToString();
Helps with parsing of commong data types.
static readonly char[] CRLF
Contains the CR LF character sequence.
Static class managing the application event log. Applications and services log events on this static ...
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.
Information about an exportable folder category
string[] Folders
Set of folders
string CategoryId
Category ID
string DisplayName
Display name
Static class managing data export.
static async Task< string[]> GetKeyHostsAsync()
Secondary key hosts.
static async Task< string > GetExportFolderAsync()
Export folder.
static async Task< string > GetFullExportFolderAsync()
Full path to export folder.
static FolderCategory[] GetRegisteredFolders()
Gets registered exportable folders.
static async Task< string > GetFullKeyExportFolderAsync()
Full path to key folder.
static async Task< FileInformation[]> GetExportFilesAsync()
Gets information about exported files.
static async Task< string[]> GetBackupHostsAsync()
Secondary backup hosts.
static async Task SetLastBackupAsync(DateTime Value)
Set Timestamp of last backup.
static async Task SetKeepDaysAsync(long Value)
For how many days backups are kept.
static async Task< bool > GetExportFolderAsync(string FolderName)
Gets if a folder should be exported or not.
static async Task SetKeepMonthsAsync(long Value)
For how many months the monthly backups are kept.
static async Task SetAutomaticBackupsAsync(bool Value)
If automatic backups are activated
static async Task SetKeyHostsAsync(string[] Value)
Secondary key hosts.
static EventHandlerAsync OnExportKeyFolderUpdated
Event raised when the export key folder has been updated.
static bool ExportLedger
If the ledger should be exported.
static bool ExportDatabase
If the database should be exported.
static async Task< string > GetExportKeyFolderAsync()
Key folder
static async Task< long > GetKeepMonthsAsync()
For how many months the monthly backups are kept.
static async Task SetExportFolderAsync(string FolderName, bool Export)
Sets if a folder should be exported or not.
static void RegisterFolders(string CategoryId, string DisplayName, params string[] Folders)
Registers a set of exportable folders under one display name.
static async Task SetBackupTimeAsync(TimeSpan Value)
Time of day to start performing backups.
static string FormatBytes(double Bytes)
Formats a file size using appropriate unit.
static string ExportType
Export file type.
static bool ExportWebContent
If web content should be exported.
static async Task< long > GetKeepYearsAsync()
For how many years the yearly backups are kept.
static async Task SetBackupHostsAsync(string[] Value)
Secondary backup hosts.
static async Task< DateTime > GetLastBackupAsync()
Get Timestamp of last backup.
static EventHandlerAsync OnExportFolderUpdated
Event raised when the export folder has been updated.
static async Task SetKeepYearsAsync(long Value)
For how many years the yearly backups are kept.
static async Task< long > GetKeepDaysAsync()
For how many days backups are kept.
static async Task SetExportFolderAsync(string Value)
Export folder.
static async Task SetExportKeyFolderAsync(string Value)
Key folder
static async Task< bool > GetAutomaticBackupsAsync()
If automatic backups are activated
static async Task< TimeSpan > GetBackupTimeAsync()
Time of day to start performing backups.
Static class managing the runtime environment of the IoT Gateway.
static string AppDataFolder
Application data folder.
static BackupConfiguration Instance
Current instance of configuration.
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.