1 |
6dab0250
|
silhavyj
|
using System.Runtime.InteropServices;
|
2 |
|
|
using LDClient.utils.loggers;
|
3 |
d0cf9476
|
Pultak
|
using Microsoft.Extensions.Configuration;
|
4 |
|
|
|
5 |
74bd1e40
|
Pultak
|
namespace LDClient.utils {
|
6 |
f281acac
|
silhavyj
|
|
7 |
74bd1e40
|
Pultak
|
internal class ConfigLoader {
|
8 |
f281acac
|
silhavyj
|
|
9 |
74bd1e40
|
Pultak
|
private const string LoggingSection = "Logging";
|
10 |
58cf6a6f
|
Pultak
|
private const string NetworkSection = "Network";
|
11 |
c2cc7813
|
Pultak
|
private const string CacheSection = "Cache";
|
12 |
f281acac
|
silhavyj
|
private const string DdSection = "DebuggerDetection";
|
13 |
d0cf9476
|
Pultak
|
|
14 |
c2cc7813
|
Pultak
|
#region Logger
|
15 |
f281acac
|
silhavyj
|
|
16 |
|
|
public int LogChunkSize { get; private set; }
|
17 |
|
|
public int LogChunkMaxCount { get; private set; }
|
18 |
|
|
public int LogArchiveMaxCount { get; private set; }
|
19 |
|
|
public int LogCleanupPeriod { get; private set; }
|
20 |
|
|
public LogVerbosity LogVerbosityType { get; private set; } = LogVerbosity.Full;
|
21 |
|
|
public LogFlow LogFlowType { get; private set; } = LogFlow.Console;
|
22 |
|
|
|
23 |
c2cc7813
|
Pultak
|
#endregion
|
24 |
d0cf9476
|
Pultak
|
|
25 |
c2cc7813
|
Pultak
|
#region Api
|
26 |
f281acac
|
silhavyj
|
|
27 |
|
|
public string ApiBaseAddress { get; private set; }
|
28 |
|
|
public string ApiUsbEndPoint { get; private set; }
|
29 |
|
|
public uint ApiPort { get; private set; }
|
30 |
58cf6a6f
|
Pultak
|
|
31 |
c2cc7813
|
Pultak
|
#endregion
|
32 |
|
|
|
33 |
|
|
#region Cache
|
34 |
f281acac
|
silhavyj
|
|
35 |
|
|
public string CacheFileName { get; private set; }
|
36 |
|
|
public uint MaxRetries { get; private set; }
|
37 |
|
|
public uint MaxEntries { get; private set; }
|
38 |
|
|
public uint RetryPeriod { get; private set; }
|
39 |
|
|
|
40 |
c2cc7813
|
Pultak
|
#endregion
|
41 |
|
|
|
42 |
|
|
#region Detection
|
43 |
f281acac
|
silhavyj
|
public string T32ProcessName { get; private set; }
|
44 |
|
|
public uint DetectionPeriod { get; private set; }
|
45 |
|
|
public string T32InfoLocation { get; private set; }
|
46 |
6dab0250
|
silhavyj
|
public string F32RemExecutable { get; private set; }
|
47 |
|
|
public string F32RemArguments { get; private set; }
|
48 |
|
|
public uint FetchInfoMaxAttempts { get; private set; }
|
49 |
|
|
public uint FetchInfoAttemptPeriod { get; private set; }
|
50 |
|
|
|
51 |
c2cc7813
|
Pultak
|
#endregion
|
52 |
58cf6a6f
|
Pultak
|
|
53 |
74bd1e40
|
Pultak
|
public ConfigLoader() {
|
54 |
d0cf9476
|
Pultak
|
var configuration = new ConfigurationBuilder()
|
55 |
|
|
.AddJsonFile("appsettings.json")
|
56 |
|
|
.Build();
|
57 |
|
|
ReadAllSettings(configuration);
|
58 |
|
|
}
|
59 |
|
|
|
60 |
f281acac
|
silhavyj
|
private void ReadAllSettings(IConfiguration configuration) {
|
61 |
d0cf9476
|
Pultak
|
|
62 |
|
|
try {
|
63 |
74bd1e40
|
Pultak
|
var logging = configuration.GetSection(LoggingSection);
|
64 |
d0cf9476
|
Pultak
|
//TODO: Exception handling
|
65 |
74bd1e40
|
Pultak
|
LogChunkSize = int.Parse(logging["LogChunkSize"]);
|
66 |
|
|
LogChunkMaxCount = int.Parse(logging["LogChunkMaxCount"]);
|
67 |
|
|
LogArchiveMaxCount = int.Parse(logging["LogArchiveMaxCount"]);
|
68 |
|
|
LogCleanupPeriod = int.Parse(logging["LogCleanupPeriod"]);
|
69 |
|
|
LogFlowType = (LogFlow)int.Parse(logging["LogFlowType"]);
|
70 |
|
|
LogVerbosityType = (LogVerbosity)int.Parse(logging["LogVerbosityType"]);
|
71 |
914776bd
|
Pultak
|
|
72 |
58cf6a6f
|
Pultak
|
var network = configuration.GetSection(NetworkSection);
|
73 |
|
|
ApiBaseAddress = network["ApiBaseAddress"];
|
74 |
|
|
ApiUsbEndPoint = network["ApiLDEndPoint"];
|
75 |
|
|
ApiPort = uint.Parse(network["ApiPort"]);
|
76 |
6dab0250
|
silhavyj
|
|
77 |
c2cc7813
|
Pultak
|
var cache = configuration.GetSection(CacheSection);
|
78 |
|
|
RetryPeriod = uint.Parse(cache["RetryPeriod"]);
|
79 |
|
|
MaxEntries = uint.Parse(cache["MaxEntries"]);
|
80 |
|
|
MaxRetries = uint.Parse(cache["MaxRetries"]);
|
81 |
|
|
CacheFileName = cache["CacheFileName"];
|
82 |
6dab0250
|
silhavyj
|
|
83 |
f281acac
|
silhavyj
|
var debugger = configuration.GetSection(DdSection);
|
84 |
914776bd
|
Pultak
|
T32ProcessName = debugger["T32ProcessName"];
|
85 |
|
|
T32InfoLocation = debugger["T32InfoLocation"];
|
86 |
|
|
DetectionPeriod = uint.Parse(debugger["DetectionPeriod"]);
|
87 |
6dab0250
|
silhavyj
|
F32RemExecutable = debugger["F32RemExecutable"];
|
88 |
|
|
F32RemArguments = debugger["F32RemArguments"];
|
89 |
|
|
FetchInfoMaxAttempts = uint.Parse(debugger["FetchInfoMaxAttempts"]);
|
90 |
|
|
FetchInfoAttemptPeriod = uint.Parse(debugger["FetchInfoAttemptPeriod"]);
|
91 |
d0cf9476
|
Pultak
|
|
92 |
|
|
Console.WriteLine("Configuration successfully loaded!");
|
93 |
|
|
} catch (FormatException e) {
|
94 |
|
|
//Console.WriteLine("Error reading app settings");
|
95 |
|
|
//TODO: remove stacktrace print in production
|
96 |
74bd1e40
|
Pultak
|
Console.WriteLine("Error during reading of configuration occurred!" + e);
|
97 |
d0cf9476
|
Pultak
|
throw new IOException("Reading of configuration file failed! " + e);
|
98 |
|
|
}
|
99 |
|
|
}
|
100 |
|
|
}
|
101 |
|
|
}
|