Skip to content

Commit a8d9b2b

Browse files
committed
Updated all packages and rework security options
1 parent d7de3c6 commit a8d9b2b

File tree

5 files changed

+51
-78
lines changed

5 files changed

+51
-78
lines changed

src/HomeAutio.Mqtt.Ecobee/EcobeeMqttService.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Text;
54
using System.Threading;
65
using System.Threading.Tasks;
76
using HomeAutio.Mqtt.Core;
7+
using HomeAutio.Mqtt.Core.Options;
8+
using HomeAutio.Mqtt.Ecobee.Options;
89
using I8Beef.Ecobee;
910
using I8Beef.Ecobee.Protocol;
1011
using I8Beef.Ecobee.Protocol.Functions;
1112
using I8Beef.Ecobee.Protocol.Objects;
1213
using I8Beef.Ecobee.Protocol.Thermostat;
1314
using Microsoft.Extensions.Logging;
15+
using Microsoft.Extensions.Options;
1416
using MQTTnet;
1517
using MQTTnet.Client;
1618
using Newtonsoft.Json;
@@ -38,19 +40,17 @@ public class EcobeeMqttService : ServiceBase
3840
/// </summary>
3941
/// <param name="logger">Logging instance.</param>
4042
/// <param name="ecobeeClient">The Ecobee client.</param>
41-
/// <param name="ecobeeName">The target Ecobee name.</param>
42-
/// <param name="refreshInterval">The refresh interval.</param>
43-
/// <param name="brokerSettings">MQTT broker settings.</param>
43+
/// <param name="ecobeeOptions">The Evobee options.</param>
44+
/// <param name="mqttOptions">MQTT broker options.</param>
4445
public EcobeeMqttService(
4546
ILogger<EcobeeMqttService> logger,
4647
Client ecobeeClient,
47-
string ecobeeName,
48-
int refreshInterval,
49-
BrokerSettings brokerSettings)
50-
: base(logger, brokerSettings, "ecobee/" + ecobeeName)
48+
IOptions<EcobeeOptions> ecobeeOptions,
49+
IOptions<MqttOptions> mqttOptions)
50+
: base(logger, mqttOptions, "ecobee/" + ecobeeOptions.Value.EcobeeName)
5151
{
5252
_log = logger;
53-
_refreshInterval = refreshInterval * 1000;
53+
_refreshInterval = ecobeeOptions.Value.RefreshInterval * 1000;
5454
SubscribedTopics.Add(TopicRoot + "/+/+/set");
5555

5656
_client = ecobeeClient;
@@ -90,7 +90,7 @@ protected override Task StopServiceAsync(CancellationToken cancellationToken = d
9090
/// <param name="e">Event args.</param>
9191
protected override async Task MqttMsgPublishReceived(MqttApplicationMessageReceivedEventArgs e)
9292
{
93-
var message = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
93+
var message = e.ApplicationMessage.ConvertPayloadToString();
9494
_log.LogInformation("MQTT message received for topic " + e.ApplicationMessage.Topic + ": " + message);
9595

9696
// Parse topic out

src/HomeAutio.Mqtt.Ecobee/HomeAutio.Mqtt.Ecobee.csproj

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@
3131
</ItemGroup>
3232

3333
<ItemGroup>
34-
<PackageReference Include="HomeAutio.Mqtt.Core" Version="4.0.0.5" />
35-
<PackageReference Include="I8Beef.CodeAnalysis.EditorConfig" Version="1.0.4">
34+
<PackageReference Include="HomeAutio.Mqtt.Core" Version="5.0.0" />
35+
<PackageReference Include="I8Beef.CodeAnalysis.EditorConfig" Version="1.0.7">
3636
<PrivateAssets>all</PrivateAssets>
3737
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3838
</PackageReference>
3939
<PackageReference Include="I8Beef.Ecobee" Version="3.0.0.84" />
40-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
41-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
42-
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
43-
<PackageReference Include="Serilog.Settings.Configuration" Version="3.4.0" />
44-
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
40+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
41+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
42+
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
43+
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
44+
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
4545
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
4646
</ItemGroup>
4747

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace HomeAutio.Mqtt.Ecobee.Options
2+
{
3+
/// <summary>
4+
/// Ecobee options.
5+
/// </summary>
6+
public class EcobeeOptions
7+
{
8+
/// <summary>
9+
/// Ecobee name.
10+
/// </summary>
11+
public string EcobeeName { get; set; } = "default";
12+
13+
/// <summary>
14+
/// Ecobee app key.
15+
/// </summary>
16+
public string EcobeeAppKey { get; set; } = "blank";
17+
18+
/// <summary>
19+
/// Refresh interval.
20+
/// </summary>
21+
public int RefreshInterval { get; set; } = 180;
22+
}
23+
}

src/HomeAutio.Mqtt.Ecobee/Program.cs

Lines changed: 10 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
using System;
22
using System.IO;
3-
using System.Linq;
4-
using System.Security.Cryptography.X509Certificates;
53
using System.Text;
64
using System.Threading;
75
using System.Threading.Tasks;
8-
using HomeAutio.Mqtt.Core;
6+
using HomeAutio.Mqtt.Core.Options;
7+
using HomeAutio.Mqtt.Ecobee.Options;
98
using I8Beef.Ecobee;
109
using Microsoft.Extensions.Configuration;
1110
using Microsoft.Extensions.DependencyInjection;
1211
using Microsoft.Extensions.Hosting;
1312
using Microsoft.Extensions.Logging;
13+
using Microsoft.Extensions.Options;
1414
using Serilog;
1515

1616
namespace HomeAutio.Mqtt.Ecobee
@@ -77,71 +77,21 @@ private static IHostBuilder CreateHostBuilder(IConfiguration config)
7777
.ConfigureLogging((hostingContext, logging) => logging.AddSerilog())
7878
.ConfigureServices((hostContext, services) =>
7979
{
80+
services.AddOptions<EcobeeOptions>().BindConfiguration("ecobee");
81+
services.AddOptions<MqttOptions>().BindConfiguration("mqtt");
82+
8083
// Setup client
8184
services.AddScoped(serviceProvider => new Client(
82-
config.GetValue<string>("ecobee:ecobeeAppKey"),
85+
serviceProvider.GetRequiredService<IOptions<EcobeeOptions>>().Value.EcobeeAppKey,
8386
ReadTokenFileAsync,
8487
WriteTokenFileAsync));
8588

8689
// 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(
13991
serviceProvider.GetRequiredService<ILogger<EcobeeMqttService>>(),
14092
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>>()));
14595
});
14696
}
14797

src/HomeAutio.Mqtt.Ecobee/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{
1+
{
22
"ecobee": {
33
"ecobeeName": "default",
44
"ecobeeAppKey": "blank",

0 commit comments

Comments
 (0)