-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppConfig.cs
More file actions
96 lines (84 loc) · 3.33 KB
/
AppConfig.cs
File metadata and controls
96 lines (84 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#nullable enable
using Microsoft.Extensions.Configuration;
namespace InstDotNet;
/// <summary>
/// Application configuration model loaded from appsettings.json
/// </summary>
public class AppConfig
{
public MqttConfig MQTT { get; set; } = new();
public ApplicationConfig Application { get; set; } = new();
public AlgorithmConfig Algorithm { get; set; } = new();
public List<BeaconConfig> Beacons { get; set; } = new();
/// <summary>
/// Load configuration from appsettings.json and environment variables
/// </summary>
public static AppConfig Load()
{
var builder = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile("appsettings.Development.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
var configuration = builder.Build();
var config = new AppConfig();
configuration.Bind(config);
return config;
}
/// <summary>
/// Resolves placeholders in configuration strings (e.g., {$BoardID})
/// </summary>
public void ResolvePlaceholders(string boardId)
{
MQTT.ReceiveTopic = ResolvePlaceholder(MQTT.ReceiveTopic, boardId);
MQTT.SendTopic = ResolvePlaceholder(MQTT.SendTopic, boardId);
MQTT.ClientId = ResolvePlaceholder(MQTT.ClientId, boardId);
}
private static string ResolvePlaceholder(string value, string boardId)
{
if (string.IsNullOrEmpty(value))
return value;
return value.Replace("{$BoardID}", boardId, StringComparison.OrdinalIgnoreCase)
.Replace("{$BOARD_ID}", boardId, StringComparison.OrdinalIgnoreCase);
}
}
public class MqttConfig
{
public string ServerAddress { get; set; } = string.Empty;
public int Port { get; set; } = 1883;
public string ClientId { get; set; } = string.Empty;
public string Username { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
public string ReceiveTopic { get; set; } = string.Empty;
public string SendTopic { get; set; } = string.Empty;
public int TimeoutSeconds { get; set; } = 10;
public int RetryAttempts { get; set; } = 5;
public int RetryDelaySeconds { get; set; } = 2;
public double RetryBackoffMultiplier { get; set; } = 2.0;
public bool AutoReconnect { get; set; } = true;
public int ReconnectDelaySeconds { get; set; } = 5;
public int KeepAlivePeriodSeconds { get; set; } = 60;
public bool UseTls { get; set; } = false;
public bool AllowUntrustedCertificates { get; set; } = false;
public string? CertificatePath { get; set; }
public string? CertificatePassword { get; set; }
}
public class ApplicationConfig
{
public int UpdateIntervalMs { get; set; } = 10;
public string LogLevel { get; set; } = "Information";
public int HealthCheckPort { get; set; } = 8080;
}
public class AlgorithmConfig
{
public int MaxIterations { get; set; } = 10;
public float LearningRate { get; set; } = 0.1f;
public bool RefinementEnabled { get; set; } = true;
}
public class BeaconConfig
{
public string Id { get; set; } = string.Empty;
public double Latitude { get; set; }
public double Longitude { get; set; }
public double Altitude { get; set; }
}