From a8d9b2b60faa0f369f57da7ba033d35d15da497f Mon Sep 17 00:00:00 2001 From: Michael Hallock Date: Sat, 24 Feb 2024 19:09:28 -0500 Subject: [PATCH] Updated all packages and rework security options --- .../EcobeeMqttService.cs | 20 +++--- .../HomeAutio.Mqtt.Ecobee.csproj | 14 ++-- .../Options/EcobeeOptions.cs | 23 ++++++ src/HomeAutio.Mqtt.Ecobee/Program.cs | 70 +++---------------- src/HomeAutio.Mqtt.Ecobee/appsettings.json | 2 +- 5 files changed, 51 insertions(+), 78 deletions(-) create mode 100644 src/HomeAutio.Mqtt.Ecobee/Options/EcobeeOptions.cs diff --git a/src/HomeAutio.Mqtt.Ecobee/EcobeeMqttService.cs b/src/HomeAutio.Mqtt.Ecobee/EcobeeMqttService.cs index e69d08e..b3adcd8 100644 --- a/src/HomeAutio.Mqtt.Ecobee/EcobeeMqttService.cs +++ b/src/HomeAutio.Mqtt.Ecobee/EcobeeMqttService.cs @@ -1,16 +1,18 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; using System.Threading; using System.Threading.Tasks; using HomeAutio.Mqtt.Core; +using HomeAutio.Mqtt.Core.Options; +using HomeAutio.Mqtt.Ecobee.Options; using I8Beef.Ecobee; using I8Beef.Ecobee.Protocol; using I8Beef.Ecobee.Protocol.Functions; using I8Beef.Ecobee.Protocol.Objects; using I8Beef.Ecobee.Protocol.Thermostat; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using MQTTnet; using MQTTnet.Client; using Newtonsoft.Json; @@ -38,19 +40,17 @@ public class EcobeeMqttService : ServiceBase /// /// Logging instance. /// The Ecobee client. - /// The target Ecobee name. - /// The refresh interval. - /// MQTT broker settings. + /// The Evobee options. + /// MQTT broker options. public EcobeeMqttService( ILogger logger, Client ecobeeClient, - string ecobeeName, - int refreshInterval, - BrokerSettings brokerSettings) - : base(logger, brokerSettings, "ecobee/" + ecobeeName) + IOptions ecobeeOptions, + IOptions mqttOptions) + : base(logger, mqttOptions, "ecobee/" + ecobeeOptions.Value.EcobeeName) { _log = logger; - _refreshInterval = refreshInterval * 1000; + _refreshInterval = ecobeeOptions.Value.RefreshInterval * 1000; SubscribedTopics.Add(TopicRoot + "/+/+/set"); _client = ecobeeClient; @@ -90,7 +90,7 @@ protected override Task StopServiceAsync(CancellationToken cancellationToken = d /// Event args. protected override async Task MqttMsgPublishReceived(MqttApplicationMessageReceivedEventArgs e) { - var message = Encoding.UTF8.GetString(e.ApplicationMessage.Payload); + var message = e.ApplicationMessage.ConvertPayloadToString(); _log.LogInformation("MQTT message received for topic " + e.ApplicationMessage.Topic + ": " + message); // Parse topic out diff --git a/src/HomeAutio.Mqtt.Ecobee/HomeAutio.Mqtt.Ecobee.csproj b/src/HomeAutio.Mqtt.Ecobee/HomeAutio.Mqtt.Ecobee.csproj index d0b6a56..0fd77fb 100644 --- a/src/HomeAutio.Mqtt.Ecobee/HomeAutio.Mqtt.Ecobee.csproj +++ b/src/HomeAutio.Mqtt.Ecobee/HomeAutio.Mqtt.Ecobee.csproj @@ -31,17 +31,17 @@ - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - + + + + + diff --git a/src/HomeAutio.Mqtt.Ecobee/Options/EcobeeOptions.cs b/src/HomeAutio.Mqtt.Ecobee/Options/EcobeeOptions.cs new file mode 100644 index 0000000..ca33fa7 --- /dev/null +++ b/src/HomeAutio.Mqtt.Ecobee/Options/EcobeeOptions.cs @@ -0,0 +1,23 @@ +namespace HomeAutio.Mqtt.Ecobee.Options +{ + /// + /// Ecobee options. + /// + public class EcobeeOptions + { + /// + /// Ecobee name. + /// + public string EcobeeName { get; set; } = "default"; + + /// + /// Ecobee app key. + /// + public string EcobeeAppKey { get; set; } = "blank"; + + /// + /// Refresh interval. + /// + public int RefreshInterval { get; set; } = 180; + } +} diff --git a/src/HomeAutio.Mqtt.Ecobee/Program.cs b/src/HomeAutio.Mqtt.Ecobee/Program.cs index e8df42b..863020d 100644 --- a/src/HomeAutio.Mqtt.Ecobee/Program.cs +++ b/src/HomeAutio.Mqtt.Ecobee/Program.cs @@ -1,16 +1,16 @@ using System; using System.IO; -using System.Linq; -using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading; using System.Threading.Tasks; -using HomeAutio.Mqtt.Core; +using HomeAutio.Mqtt.Core.Options; +using HomeAutio.Mqtt.Ecobee.Options; using I8Beef.Ecobee; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using Serilog; namespace HomeAutio.Mqtt.Ecobee @@ -77,71 +77,21 @@ private static IHostBuilder CreateHostBuilder(IConfiguration config) .ConfigureLogging((hostingContext, logging) => logging.AddSerilog()) .ConfigureServices((hostContext, services) => { + services.AddOptions().BindConfiguration("ecobee"); + services.AddOptions().BindConfiguration("mqtt"); + // Setup client services.AddScoped(serviceProvider => new Client( - config.GetValue("ecobee:ecobeeAppKey"), + serviceProvider.GetRequiredService>().Value.EcobeeAppKey, ReadTokenFileAsync, WriteTokenFileAsync)); // Setup service instance - services.AddScoped(serviceProvider => - { - // TLS settings - var brokerUseTls = config.GetValue("mqtt:brokerUseTls", false); - BrokerTlsSettings? brokerTlsSettings = null; - if (brokerUseTls) - { - var sslProtocol = config.GetValue("mqtt:brokerTlsSettings:protocol", "1.2") switch - { - "1.2" => System.Security.Authentication.SslProtocols.Tls12, - "1.3" => System.Security.Authentication.SslProtocols.Tls13, - _ => throw new NotSupportedException($"Only TLS 1.2 and 1.3 are supported") - }; - - var brokerTlsCertificatesSection = config.GetSection("mqtt:brokerTlsSettings:certificates"); - var brokerTlsCertificates = brokerTlsCertificatesSection.GetChildren() - .Select(x => - { - var file = x.GetValue("file"); - var passPhrase = x.GetValue("passPhrase"); - - if (!File.Exists(file)) - { - throw new FileNotFoundException($"Broker Certificate '{file}' is missing!"); - } - - return !string.IsNullOrEmpty(passPhrase) ? - new X509Certificate2(file, passPhrase) : - new X509Certificate2(file); - }).ToList(); - - brokerTlsSettings = new BrokerTlsSettings - { - AllowUntrustedCertificates = config.GetValue("mqtt:brokerTlsSettings:allowUntrustedCertificates", false), - IgnoreCertificateChainErrors = config.GetValue("mqtt:brokerTlsSettings:ignoreCertificateChainErrors", false), - IgnoreCertificateRevocationErrors = config.GetValue("mqtt:brokerTlsSettings:ignoreCertificateRevocationErrors", false), - SslProtocol = sslProtocol, - Certificates = brokerTlsCertificates - }; - } - - var brokerSettings = new BrokerSettings - { - BrokerIp = config.GetValue("mqtt:brokerIp") ?? throw new InvalidOperationException("Configuration value mqtt:brokerIp not found"), - BrokerPort = config.GetValue("mqtt:brokerPort", 1883), - BrokerUsername = config.GetValue("mqtt:brokerUsername"), - BrokerPassword = config.GetValue("mqtt:brokerPassword"), - BrokerUseTls = brokerUseTls, - BrokerTlsSettings = brokerTlsSettings - }; - - return new EcobeeMqttService( + services.AddScoped(serviceProvider => new EcobeeMqttService( serviceProvider.GetRequiredService>(), serviceProvider.GetRequiredService(), - config.GetValue("ecobee:ecobeeName") ?? "default", - config.GetValue("ecobee:refreshInterval"), - brokerSettings); - }); + serviceProvider.GetRequiredService>(), + serviceProvider.GetRequiredService>())); }); } diff --git a/src/HomeAutio.Mqtt.Ecobee/appsettings.json b/src/HomeAutio.Mqtt.Ecobee/appsettings.json index d3694b9..07dea80 100644 --- a/src/HomeAutio.Mqtt.Ecobee/appsettings.json +++ b/src/HomeAutio.Mqtt.Ecobee/appsettings.json @@ -1,4 +1,4 @@ -{ +{ "ecobee": { "ecobeeName": "default", "ecobeeAppKey": "blank",