Projekt

Obecné

Profil

Stáhnout (5.29 KB) Statistiky
| Větev: | Tag: | Revize:
1 b10bb263 silhavyj
using LDClient.utils.loggers;
2 d0cf9476 Pultak
using Microsoft.Extensions.Configuration;
3
4 74bd1e40 Pultak
namespace LDClient.utils {
5 f281acac silhavyj
    
6 74bd1e40 Pultak
    internal class ConfigLoader {
7 b10bb263 silhavyj
8
        private const int ErrorExitCode = 1;
9
        private const string ConfigFile = "appsettings.json";
10 74bd1e40 Pultak
        private const string LoggingSection = "Logging";
11 58cf6a6f Pultak
        private const string NetworkSection = "Network";
12 c2cc7813 Pultak
        private const string CacheSection = "Cache";
13 f281acac silhavyj
        private const string DdSection = "DebuggerDetection";
14 d0cf9476 Pultak
15 c2cc7813 Pultak
        #region Logger
16 f281acac silhavyj
        
17
        public int LogChunkSize { get; private set; }
18
        public int LogChunkMaxCount { get; private set; }
19
        public int LogArchiveMaxCount { get; private set; }
20
        public int LogCleanupPeriod { get; private set; }
21
        public LogVerbosity LogVerbosityType { get; private set; } = LogVerbosity.Full;
22
        public LogFlow LogFlowType { get; private set; } = LogFlow.Console;
23
        
24 c2cc7813 Pultak
        #endregion
25 d0cf9476 Pultak
26 c2cc7813 Pultak
        #region Api
27 f281acac silhavyj
        
28 b10bb263 silhavyj
        public string ApiBaseAddress { get; private set; } = null!;
29
        public string ApiUsbEndPoint { get; private set; } = null!;
30 f281acac silhavyj
        public uint ApiPort { get; private set; }
31 58cf6a6f Pultak
32 c2cc7813 Pultak
        #endregion
33
34
        #region Cache
35 f281acac silhavyj
        
36 b10bb263 silhavyj
        public string CacheFileName { get; private set; } = null!;
37 f281acac silhavyj
        public uint MaxRetries { get; private set; }
38
        public uint MaxEntries { get; private set; }
39
        public uint RetryPeriod { get; private set; }
40
        
41 c2cc7813 Pultak
        #endregion
42
43
        #region Detection
44 b10bb263 silhavyj
        public string T32ProcessName { get; private set; } = null!;
45 f281acac silhavyj
        public uint DetectionPeriod { get; private set; }
46 b10bb263 silhavyj
        public string T32InfoLocation { get; private set; } = null!;
47
        public string F32RemExecutable { get; private set; } = null!;
48 6dab0250 silhavyj
        public uint FetchInfoMaxAttempts { get; private set;  }
49
        public uint FetchInfoAttemptPeriod { get; private set; }
50 b10bb263 silhavyj
        public string[] F32RemArguments { get; private set; } = null!;
51 4dcc6c07 silhavyj
        public int T32RemSuccessExitCode { get; private set; }
52
        public int T32RemWaitTimeoutMs { get; private set; }
53
54 c2cc7813 Pultak
        #endregion
55 58cf6a6f Pultak
56 74bd1e40 Pultak
        public ConfigLoader() {
57 d0cf9476 Pultak
            var configuration = new ConfigurationBuilder()
58 b10bb263 silhavyj
                .AddJsonFile(ConfigFile)
59 d0cf9476 Pultak
                .Build();
60
61 b10bb263 silhavyj
            ReadLoggerSection(configuration);
62
            ReadApiSection(configuration);
63
            ReadCacheSection(configuration);
64
            ReadDebuggerSection(configuration);
65
            
66
            Console.WriteLine("Configuration successfully loaded!");
67
        }
68 d0cf9476 Pultak
69 b10bb263 silhavyj
        private void ReadLoggerSection(IConfiguration configuration) {
70 d0cf9476 Pultak
            try {
71 74bd1e40 Pultak
                var logging = configuration.GetSection(LoggingSection);
72
                LogChunkSize = int.Parse(logging["LogChunkSize"]);
73
                LogChunkMaxCount = int.Parse(logging["LogChunkMaxCount"]);
74
                LogArchiveMaxCount = int.Parse(logging["LogArchiveMaxCount"]);
75
                LogCleanupPeriod = int.Parse(logging["LogCleanupPeriod"]);
76
                LogFlowType = (LogFlow)int.Parse(logging["LogFlowType"]);
77
                LogVerbosityType = (LogVerbosity)int.Parse(logging["LogVerbosityType"]);
78 b10bb263 silhavyj
            } catch (Exception e) {
79
                Console.WriteLine(e.Message);
80
                Environment.Exit(ErrorExitCode);
81
            }
82
        }
83 914776bd Pultak
84 b10bb263 silhavyj
        private void ReadApiSection(IConfiguration configuration) {
85
            try {
86 58cf6a6f Pultak
                var network = configuration.GetSection(NetworkSection);
87
                ApiBaseAddress = network["ApiBaseAddress"];
88
                ApiUsbEndPoint = network["ApiLDEndPoint"];
89
                ApiPort = uint.Parse(network["ApiPort"]);
90 b10bb263 silhavyj
            } catch (Exception e) {
91
                Console.WriteLine(e.Message);
92
                Environment.Exit(ErrorExitCode);
93
            }
94
        }
95
96
        private void ReadCacheSection(IConfiguration configuration) {
97
            try {
98 c2cc7813 Pultak
                var cache = configuration.GetSection(CacheSection);
99
                RetryPeriod = uint.Parse(cache["RetryPeriod"]);
100
                MaxEntries = uint.Parse(cache["MaxEntries"]);
101
                MaxRetries = uint.Parse(cache["MaxRetries"]);
102
                CacheFileName = cache["CacheFileName"];
103 b10bb263 silhavyj
            } catch (Exception e) {
104
                Console.WriteLine(e.Message);
105
                Environment.Exit(ErrorExitCode);
106
            }
107
        }
108
109
        private void ReadDebuggerSection(IConfiguration configuration) {
110
            try {
111 f281acac silhavyj
                var debugger = configuration.GetSection(DdSection);
112 914776bd Pultak
                T32ProcessName = debugger["T32ProcessName"];
113
                T32InfoLocation = debugger["T32InfoLocation"];
114
                DetectionPeriod = uint.Parse(debugger["DetectionPeriod"]);
115 6dab0250 silhavyj
                F32RemExecutable = debugger["F32RemExecutable"];
116
                FetchInfoMaxAttempts = uint.Parse(debugger["FetchInfoMaxAttempts"]);
117
                FetchInfoAttemptPeriod = uint.Parse(debugger["FetchInfoAttemptPeriod"]);
118 4dcc6c07 silhavyj
                T32RemSuccessExitCode = int.Parse(debugger["T32RemSuccessExitCode"]);
119
                T32RemWaitTimeoutMs = int.Parse(debugger["T32RemWaitTimeoutMs"]);
120 19c2a5c4 Pultak
                F32RemArguments = configuration.GetSection($"{DdSection}:F32RemArguments").GetChildren().Select(key => key.Value).ToArray();
121 b10bb263 silhavyj
            } catch (Exception e) {
122
                Console.WriteLine(e);
123
                Environment.Exit(ErrorExitCode);
124 d0cf9476 Pultak
            }
125
        }
126
    }
127
}