Projekt

Obecné

Profil

« Předchozí | Další » 

Revize b10bb263

Přidáno uživatelem Jakub Šilhavý před více než 2 roky(ů)

re #9568 Broke config loader down into separate methods.

Zobrazit rozdíly:

ld_client/LDClient/Program.cs
61 61
        while (true) {
62 62
            Thread.Sleep(MainLoopDelayMs);
63 63
        }
64
        
65
        return 0;
66 64
    }
67 65
}
ld_client/LDClient/network/data/ConnectionStatus.cs
1 1
using System.Runtime.Serialization;
2 2

  
3 3
namespace LDClient.network.data {
4
    
4 5
    public enum ConnectionStatus {
6
        
5 7
        [EnumMember(Value = "connected")]
6 8
        Connected,
7 9
        [EnumMember(Value = "disconnected")]
8 10
        Disconnected
9

  
10 11
    }
11 12
}
ld_client/LDClient/network/data/DebuggerInfo.cs
4 4
    public class DebuggerInfo {
5 5
        
6 6
        [JsonPropertyName("serial_number")]
7
        public string SerialNumber { get; set; }
7
        public string? SerialNumber { get; set; }
8 8
    }
9 9
}
ld_client/LDClient/network/data/Payload.cs
4 4
using JsonSerializer = System.Text.Json.JsonSerializer;
5 5

  
6 6
namespace LDClient.network.data {
7
    
7 8
    [JsonObject(MemberSerialization.OptIn)]
8 9
    public class Payload {
9 10

  
......
14 15
        public string? HostName { get; set; }
15 16

  
16 17
        [JsonPropertyName("timestamp")]
17
        //[Newtonsoft.Json.JsonConverter(typeof(DateFormatConverter), "yyyy-MM-dd HH:mm:ss.ffffff")]
18 18
        public string? TimeStamp { get; set; }
19 19

  
20 20
        [JsonPropertyName("head_device")]
......
25 25
        public DebuggerInfo?  BodyDevice { get; set; }
26 26
        
27 27
        [JsonPropertyName("status")]
28
        //[Newtonsoft.Json.JsonConverter(typeof(StringEnumConverter))]
29 28
        public ConnectionStatus Status { get; set; }
30 29

  
31 30

  
ld_client/LDClient/utils/ConfigLoader.cs
1
using System.Runtime.InteropServices;
2
using LDClient.utils.loggers;
1
using LDClient.utils.loggers;
3 2
using Microsoft.Extensions.Configuration;
4 3

  
5 4
namespace LDClient.utils {
6 5
    
7 6
    internal class ConfigLoader {
7

  
8
        private const int ErrorExitCode = 1;
9
        private const string ConfigFile = "appsettings.json";
8 10
        
9 11
        private const string LoggingSection = "Logging";
10 12
        private const string NetworkSection = "Network";
......
24 26

  
25 27
        #region Api
26 28
        
27
        public string ApiBaseAddress { get; private set; }
28
        public string ApiUsbEndPoint { get; private set; }
29
        public string ApiBaseAddress { get; private set; } = null!;
30
        public string ApiUsbEndPoint { get; private set; } = null!;
29 31
        public uint ApiPort { get; private set; }
30 32

  
31 33
        #endregion
32 34

  
33 35
        #region Cache
34 36
        
35
        public string CacheFileName { get; private set; }
37
        public string CacheFileName { get; private set; } = null!;
36 38
        public uint MaxRetries { get; private set; }
37 39
        public uint MaxEntries { get; private set; }
38 40
        public uint RetryPeriod { get; private set; }
......
40 42
        #endregion
41 43

  
42 44
        #region Detection
43
        public string T32ProcessName { get; private set; }
45
        public string T32ProcessName { get; private set; } = null!;
44 46
        public uint DetectionPeriod { get; private set; }
45
        public string T32InfoLocation { get; private set; }
46
        public string F32RemExecutable { get; private set; }
47
        public string T32InfoLocation { get; private set; } = null!;
48
        public string F32RemExecutable { get; private set; } = null!;
47 49
        public uint FetchInfoMaxAttempts { get; private set;  }
48 50
        public uint FetchInfoAttemptPeriod { get; private set; }
49
        public string[] F32RemArguments { get; private set; }
51
        public string[] F32RemArguments { get; private set; } = null!;
50 52
        public int T32RemSuccessExitCode { get; private set; }
51 53
        public int T32RemWaitTimeoutMs { get; private set; }
52 54

  
......
54 56

  
55 57
        public ConfigLoader() {
56 58
            var configuration = new ConfigurationBuilder()
57
                .AddJsonFile("appsettings.json")
59
                .AddJsonFile(ConfigFile)
58 60
                .Build();
59
            ReadAllSettings(configuration);
60
        }
61 61

  
62
        private void ReadAllSettings(IConfiguration configuration) {
62
            ReadLoggerSection(configuration);
63
            ReadApiSection(configuration);
64
            ReadCacheSection(configuration);
65
            ReadDebuggerSection(configuration);
66
            
67
            Console.WriteLine("Configuration successfully loaded!");
68
        }
63 69

  
70
        private void ReadLoggerSection(IConfiguration configuration) {
64 71
            try {
65 72
                var logging = configuration.GetSection(LoggingSection);
66
                //TODO: Exception handling
67 73
                LogChunkSize = int.Parse(logging["LogChunkSize"]);
68 74
                LogChunkMaxCount = int.Parse(logging["LogChunkMaxCount"]);
69 75
                LogArchiveMaxCount = int.Parse(logging["LogArchiveMaxCount"]);
70 76
                LogCleanupPeriod = int.Parse(logging["LogCleanupPeriod"]);
71 77
                LogFlowType = (LogFlow)int.Parse(logging["LogFlowType"]);
72 78
                LogVerbosityType = (LogVerbosity)int.Parse(logging["LogVerbosityType"]);
79
            } catch (Exception e) {
80
                Console.WriteLine(e.Message);
81
                Environment.Exit(ErrorExitCode);
82
            }
83
        }
73 84

  
85
        private void ReadApiSection(IConfiguration configuration) {
86
            try {
74 87
                var network = configuration.GetSection(NetworkSection);
75 88
                ApiBaseAddress = network["ApiBaseAddress"];
76 89
                ApiUsbEndPoint = network["ApiLDEndPoint"];
77 90
                ApiPort = uint.Parse(network["ApiPort"]);
78
                
91
            } catch (Exception e) {
92
                Console.WriteLine(e.Message);
93
                Environment.Exit(ErrorExitCode);
94
            }
95
        }
96

  
97
        private void ReadCacheSection(IConfiguration configuration) {
98
            try {
79 99
                var cache = configuration.GetSection(CacheSection);
80 100
                RetryPeriod = uint.Parse(cache["RetryPeriod"]);
81 101
                MaxEntries = uint.Parse(cache["MaxEntries"]);
82 102
                MaxRetries = uint.Parse(cache["MaxRetries"]);
83 103
                CacheFileName = cache["CacheFileName"];
84
                
104
            } catch (Exception e) {
105
                Console.WriteLine(e.Message);
106
                Environment.Exit(ErrorExitCode);
107
            }
108
        }
109

  
110
        private void ReadDebuggerSection(IConfiguration configuration) {
111
            try {
85 112
                var debugger = configuration.GetSection(DdSection);
86 113
                T32ProcessName = debugger["T32ProcessName"];
87 114
                T32InfoLocation = debugger["T32InfoLocation"];
......
92 119
                T32RemSuccessExitCode = int.Parse(debugger["T32RemSuccessExitCode"]);
93 120
                T32RemWaitTimeoutMs = int.Parse(debugger["T32RemWaitTimeoutMs"]);
94 121
                F32RemArguments = configuration.GetSection($"{DdSection}:F32RemCommands").GetChildren().Select(key => key.Value).ToArray();
95

  
96
                Console.WriteLine("Configuration successfully loaded!");
97
            } catch (FormatException e) {
98
                //Console.WriteLine("Error reading app settings");
99
                //TODO: remove stacktrace print in production
100
                Console.WriteLine("Error during reading of configuration occurred!" + e);
101
                throw new IOException("Reading of configuration file failed! " + e);
122
            } catch (Exception e) {
123
                Console.WriteLine(e);
124
                Environment.Exit(ErrorExitCode);
102 125
            }
103 126
        }
104 127
    }

Také k dispozici: Unified diff