|
1 | 1 | using System; |
2 | 2 | using System.IO; |
3 | | -using System.Linq; |
4 | | -using System.Security.Cryptography.X509Certificates; |
5 | 3 | using System.Text; |
6 | 4 | using System.Threading; |
7 | 5 | using System.Threading.Tasks; |
8 | | -using HomeAutio.Mqtt.Core; |
| 6 | +using HomeAutio.Mqtt.Core.Options; |
| 7 | +using HomeAutio.Mqtt.Ecobee.Options; |
9 | 8 | using I8Beef.Ecobee; |
10 | 9 | using Microsoft.Extensions.Configuration; |
11 | 10 | using Microsoft.Extensions.DependencyInjection; |
12 | 11 | using Microsoft.Extensions.Hosting; |
13 | 12 | using Microsoft.Extensions.Logging; |
| 13 | +using Microsoft.Extensions.Options; |
14 | 14 | using Serilog; |
15 | 15 |
|
16 | 16 | namespace HomeAutio.Mqtt.Ecobee |
@@ -77,71 +77,21 @@ private static IHostBuilder CreateHostBuilder(IConfiguration config) |
77 | 77 | .ConfigureLogging((hostingContext, logging) => logging.AddSerilog()) |
78 | 78 | .ConfigureServices((hostContext, services) => |
79 | 79 | { |
| 80 | + services.AddOptions<EcobeeOptions>().BindConfiguration("ecobee"); |
| 81 | + services.AddOptions<MqttOptions>().BindConfiguration("mqtt"); |
| 82 | + |
80 | 83 | // Setup client |
81 | 84 | services.AddScoped(serviceProvider => new Client( |
82 | | - config.GetValue<string>("ecobee:ecobeeAppKey"), |
| 85 | + serviceProvider.GetRequiredService<IOptions<EcobeeOptions>>().Value.EcobeeAppKey, |
83 | 86 | ReadTokenFileAsync, |
84 | 87 | WriteTokenFileAsync)); |
85 | 88 |
|
86 | 89 | // Setup service instance |
87 | | - services.AddScoped<IHostedService, EcobeeMqttService>(serviceProvider => |
88 | | - { |
89 | | - // TLS settings |
90 | | - var brokerUseTls = config.GetValue("mqtt:brokerUseTls", false); |
91 | | - BrokerTlsSettings? brokerTlsSettings = null; |
92 | | - if (brokerUseTls) |
93 | | - { |
94 | | - var sslProtocol = config.GetValue("mqtt:brokerTlsSettings:protocol", "1.2") switch |
95 | | - { |
96 | | - "1.2" => System.Security.Authentication.SslProtocols.Tls12, |
97 | | - "1.3" => System.Security.Authentication.SslProtocols.Tls13, |
98 | | - _ => throw new NotSupportedException($"Only TLS 1.2 and 1.3 are supported") |
99 | | - }; |
100 | | - |
101 | | - var brokerTlsCertificatesSection = config.GetSection("mqtt:brokerTlsSettings:certificates"); |
102 | | - var brokerTlsCertificates = brokerTlsCertificatesSection.GetChildren() |
103 | | - .Select(x => |
104 | | - { |
105 | | - var file = x.GetValue<string>("file"); |
106 | | - var passPhrase = x.GetValue<string>("passPhrase"); |
107 | | - |
108 | | - if (!File.Exists(file)) |
109 | | - { |
110 | | - throw new FileNotFoundException($"Broker Certificate '{file}' is missing!"); |
111 | | - } |
112 | | - |
113 | | - return !string.IsNullOrEmpty(passPhrase) ? |
114 | | - new X509Certificate2(file, passPhrase) : |
115 | | - new X509Certificate2(file); |
116 | | - }).ToList(); |
117 | | - |
118 | | - brokerTlsSettings = new BrokerTlsSettings |
119 | | - { |
120 | | - AllowUntrustedCertificates = config.GetValue("mqtt:brokerTlsSettings:allowUntrustedCertificates", false), |
121 | | - IgnoreCertificateChainErrors = config.GetValue("mqtt:brokerTlsSettings:ignoreCertificateChainErrors", false), |
122 | | - IgnoreCertificateRevocationErrors = config.GetValue("mqtt:brokerTlsSettings:ignoreCertificateRevocationErrors", false), |
123 | | - SslProtocol = sslProtocol, |
124 | | - Certificates = brokerTlsCertificates |
125 | | - }; |
126 | | - } |
127 | | - |
128 | | - var brokerSettings = new BrokerSettings |
129 | | - { |
130 | | - BrokerIp = config.GetValue<string>("mqtt:brokerIp") ?? throw new InvalidOperationException("Configuration value mqtt:brokerIp not found"), |
131 | | - BrokerPort = config.GetValue("mqtt:brokerPort", 1883), |
132 | | - BrokerUsername = config.GetValue<string>("mqtt:brokerUsername"), |
133 | | - BrokerPassword = config.GetValue<string>("mqtt:brokerPassword"), |
134 | | - BrokerUseTls = brokerUseTls, |
135 | | - BrokerTlsSettings = brokerTlsSettings |
136 | | - }; |
137 | | - |
138 | | - return new EcobeeMqttService( |
| 90 | + services.AddScoped<IHostedService, EcobeeMqttService>(serviceProvider => new EcobeeMqttService( |
139 | 91 | serviceProvider.GetRequiredService<ILogger<EcobeeMqttService>>(), |
140 | 92 | serviceProvider.GetRequiredService<Client>(), |
141 | | - config.GetValue<string>("ecobee:ecobeeName") ?? "default", |
142 | | - config.GetValue<int>("ecobee:refreshInterval"), |
143 | | - brokerSettings); |
144 | | - }); |
| 93 | + serviceProvider.GetRequiredService<IOptions<EcobeeOptions>>(), |
| 94 | + serviceProvider.GetRequiredService<IOptions<MqttOptions>>())); |
145 | 95 | }); |
146 | 96 | } |
147 | 97 |
|
|
0 commit comments