5using System.Threading.Tasks;
26 if (
string.IsNullOrEmpty(Path))
39 if (
string.IsNullOrEmpty(Path))
51 SortedDictionary<DateTime, FileInformation> Sorted =
new SortedDictionary<DateTime, FileInformation>(
new ReverseDateTimeOrder());
53 if (Directory.Exists(Path))
54 GetFiles(Path, Sorted);
57 if (Path != Path2 && Directory.Exists(Path2))
58 GetFiles(Path2, Sorted);
61 Sorted.Values.CopyTo(Result, 0);
66 private static void GetFiles(
string Path, SortedDictionary<DateTime, FileInformation> Sorted)
68 string[] Files = Directory.GetFiles(Path,
"*.*", SearchOption.TopDirectoryOnly);
69 int i, c = Files.Length;
75 Path +=
System.IO.Path.DirectorySeparatorChar;
77 for (i = 0; i < c; i++)
83 using (FileStream fs = File.OpenRead(s))
93 SizeStr =
string.Empty;
96 Created = File.GetCreationTime(s);
98 if (s.StartsWith(Path))
99 s = s.Substring(Path.Length);
101 while (Sorted.ContainsKey(Created))
102 Created = Created.AddTicks(1);
108 private class ReverseDateTimeOrder : IComparer<DateTime>
110 public ReverseDateTimeOrder()
114 public int Compare(DateTime x, DateTime y)
116 return y.CompareTo(x);
129 while (Bytes >= 1024 && i < 4)
136 return Bytes.ToString(
"F0") +
" " + ByteUnits[i];
138 return Bytes.ToString(
"F2") +
" " + ByteUnits[i];
141 private static readonly
string[] ByteUnits = {
"B",
"kB",
"MB",
"GB",
"TB" };
148 if (exportFolderValue is
null)
151 return exportFolderValue;
160 if (exportFolderValue != Value)
162 exportFolderValue = Value;
172 private static string exportFolderValue =
null;
184 if (exportKeyFolderValue is
null)
187 return exportKeyFolderValue;
196 if (exportKeyFolderValue != Value)
198 exportKeyFolderValue = Value;
207 private static string exportKeyFolderValue =
null;
221 if (!exportDatabase.HasValue)
224 return exportDatabase.Value;
229 if (exportDatabase != value)
231 exportDatabase = value;
237 private static bool? exportDatabase =
null;
246 if (!exportLedger.HasValue)
249 return exportLedger.Value;
254 if (exportLedger != value)
256 exportLedger = value;
262 private static bool? exportLedger =
null;
271 if (!exportWebContent.HasValue)
274 return exportWebContent.Value;
279 if (exportWebContent != value)
281 exportWebContent = value;
287 private static bool? exportWebContent =
null;
296 string Key =
"Export." + FolderName;
301 if (exportFolder.TryGetValue(Key, out Result))
309 exportFolder[Key] = Result;
322 string Key =
"Export." + FolderName;
326 if (exportFolder.TryGetValue(Key, out
bool b) && b ==
Export)
329 exportFolder[Key] =
Export;
335 private static readonly Dictionary<string, bool> exportFolder =
new Dictionary<string, bool>();
344 if (exportType is
null)
352 if (exportType != value)
360 private static string exportType =
null;
367 if (automaticBackups != Value)
369 automaticBackups = Value;
379 if (!automaticBackups.HasValue)
382 return automaticBackups.Value;
385 private static bool? automaticBackups =
null;
393 if (backupKeepDays != Value)
395 backupKeepDays = Value;
405 if (!backupKeepDays.HasValue)
408 return backupKeepDays.Value;
411 private static long? backupKeepDays =
null;
419 if (backupKeepMonths != Value)
421 backupKeepMonths = Value;
431 if (!backupKeepMonths.HasValue)
434 return backupKeepMonths.Value;
437 private static long? backupKeepMonths =
null;
445 if (backupKeepYears != Value)
447 backupKeepYears = Value;
457 if (!backupKeepYears.HasValue)
460 return backupKeepYears.Value;
463 private static long? backupKeepYears =
null;
471 if (backupTime != Value)
483 if (!backupTime.HasValue)
486 return backupTime.Value;
489 private static TimeSpan? backupTime =
null;
496 if (!lastBackup.HasValue)
499 return lastBackup.Value;
507 if (lastBackup != Value)
514 private static DateTime? lastBackup =
null;
522 public static void RegisterFolders(
string CategoryId,
string DisplayName, params
string[] Folders)
526 if (folders.ContainsKey(CategoryId))
527 throw new ArgumentException(
"Category ID already registered.", nameof(CategoryId));
531 CategoryId = CategoryId,
532 DisplayName = DisplayName,
549 folders.Values.CopyTo(Result, 0);
576 private static readonly Dictionary<string, FolderCategory> folders =
new Dictionary<string, FolderCategory>();
583 if (backupHosts is
null)
595 if (backupHosts != Value)
602 private static string[] backupHosts =
null;
609 if (keyHosts is
null)
621 if (keyHosts != Value)
628 private static string[] keyHosts =
null;
630 private static string[] StringToArray(
string s)
632 return s.Split(
CommonTypes.
CRLF, StringSplitOptions.RemoveEmptyEntries);
635 private static string ArrayToString(
string[] Items)
637 StringBuilder Result =
new StringBuilder();
640 foreach (
string Item
in Items)
650 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.