diff --git a/API/MqttClientWrapper.cs b/API/MqttClientWrapper.cs index 3f54339..7fb7314 100644 --- a/API/MqttClientWrapper.cs +++ b/API/MqttClientWrapper.cs @@ -3,6 +3,7 @@ using MQTTnet.Protocol; using Serilog; using System; +using System.Security.Cryptography.X509Certificates; using System.Collections.Generic; using System.Diagnostics; using System.Threading.Tasks; @@ -27,24 +28,43 @@ public bool IsAttemptingConnection get { return _isAttemptingConnection; } private set { _isAttemptingConnection = value; } } - public MqttClientWrapper(string clientId, string mqttBroker, string mqttPort, string username, string password) + public MqttClientWrapper(string clientId, string mqttBroker, string mqttPort, string username, string password, bool useTls = false) { var factory = new MqttFactory(); _mqttClient = factory.CreateMqttClient() as MqttClient; int mqttportInt = System.Convert.ToInt32(mqttPort); - _mqttOptions = new MqttClientOptionsBuilder() + var mqttClientOptionsBuilder = new MqttClientOptionsBuilder() .WithClientId(clientId) - .WithTcpServer(mqttBroker, mqttportInt) .WithCredentials(username, password) - .WithCleanSession() - .Build(); + .WithCleanSession(); + + // If useTls is true or the port is 8883, configure the client to use TLS. + if (useTls || mqttportInt == 8883) + { + // Configure TLS options + mqttClientOptionsBuilder.WithTcpServer(mqttBroker, mqttportInt) + .WithTls(new MqttClientOptionsBuilderTlsParameters + { + UseTls = true, + AllowUntrustedCertificates = true, + IgnoreCertificateChainErrors = true, + IgnoreCertificateRevocationErrors = true + }); + Log.Information($"MQTT Client Created with TLS on port {mqttPort}."); + } + else + { + mqttClientOptionsBuilder.WithTcpServer(mqttBroker, mqttportInt); + Log.Information("MQTT Client Created with TCP."); + } + _mqttOptions = mqttClientOptionsBuilder.Build(); _mqttClient.ApplicationMessageReceivedAsync += OnMessageReceivedAsync; - Log.Information("MQTT Client Created"); } + public MqttClientWrapper(/* parameters */) { // Existing initialization code... diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 3419eb5..e974747 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -390,6 +390,26 @@ protected override void OnStateChanged(EventArgs e) #endregion Protected Methods #region Private Methods + private async Task ReconnectToMqttServer() + { + // Disconnect from the current MQTT server + if (mqttClientWrapper != null && mqttClientWrapper.IsConnected) + { + await mqttClientWrapper.DisconnectAsync(); + } + + // Create a new instance of MqttClientWrapper with new settings + mqttClientWrapper = new MqttClientWrapper( + "TEAMS2HA", + _settings.MqttAddress, + _settings.MqttPort, + _settings.MqttUsername, + _settings.MqttPassword + ); + + // Connect to the new MQTT server + await mqttClientWrapper.ConnectAsync(); + } private void SetWindowTitle() { var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; @@ -915,6 +935,11 @@ private bool SaveSettings() // Save the updated settings to file settings.SaveSettingsToFile(); + if (mqttSettingsChanged) + { + // Run the reconnection on a background thread to avoid UI freeze + Task.Run(async () => await ReconnectToMqttServer()).Wait(); + } return mqttSettingsChanged; } diff --git a/TEAMS2HA.csproj b/TEAMS2HA.csproj index b6e1c9f..37d73da 100644 --- a/TEAMS2HA.csproj +++ b/TEAMS2HA.csproj @@ -5,8 +5,8 @@ net7.0-windows enable true - 1.1.0.245 - 1.1.0.245 + 1.1.0.248 + 1.1.0.248 Assets\Square150x150Logo.scale-200.ico Teams2HA Square150x150Logo.scale-200.png