Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

master to dev #24

Merged
merged 3 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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