Skip to content

Commit

Permalink
Merge pull request #24 from jimmyeao/master
Browse files Browse the repository at this point in the history
master to dev
  • Loading branch information
jimmyeao authored Feb 13, 2024
2 parents 3c01db3 + 28aec7c commit 9043dec
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
32 changes: 26 additions & 6 deletions API/MqttClientWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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...
Expand Down
25 changes: 25 additions & 0 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions TEAMS2HA.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<TargetFramework>net7.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<AssemblyVersion>1.1.0.245</AssemblyVersion>
<FileVersion>1.1.0.245</FileVersion>
<AssemblyVersion>1.1.0.248</AssemblyVersion>
<FileVersion>1.1.0.248</FileVersion>
<ApplicationIcon>Assets\Square150x150Logo.scale-200.ico</ApplicationIcon>
<Title>Teams2HA</Title>
<PackageIcon>Square150x150Logo.scale-200.png</PackageIcon>
Expand Down

0 comments on commit 9043dec

Please sign in to comment.