Skip to content

Commit

Permalink
Enhanced MQTT logging and client ID generation
Browse files Browse the repository at this point in the history
- Enhanced logging for MQTT disconnections in MqttService.cs to include disconnection reasons and exception messages, with explicit error logging for exceptions.
- Implemented dynamic MQTT client ID generation using the machine name (TEAMS2HA_{Environment.MachineName}) and increased the keep-alive period from 30 to 60 seconds for improved connection stability.
- Updated the project file version in TEAMS2HA.csproj from 1.1.0.773 to 1.1.0.776, indicating a new release.
  • Loading branch information
jimmyeao committed Jul 16, 2024
1 parent 1769c28 commit 1a21db1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
12 changes: 8 additions & 4 deletions API/MqttService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ private MqttService()

_mqttClient.DisconnectedAsync += async e =>
{
Log.Information("Disconnected from MQTT broker.");
Log.Information($"Disconnected from MQTT broker. Reason: {e.Reason} | Exception: {e.Exception?.Message}");
if (e.Exception != null)
{
Log.Error($"Exception during disconnect: {e.Exception}");
}
_mqttClient.ApplicationMessageReceivedAsync -= OnMessageReceivedAsync;
await Task.CompletedTask;
Expand Down Expand Up @@ -246,10 +250,10 @@ public async Task ConnectAsync(AppSettings settings)
await _mqttClient.StopAsync();
Log.Information("Existing MQTT client stopped successfully.");
}

string uniqueClientId = $"TEAMS2HA_{Environment.MachineName}";
var mqttClientOptionsBuilder = new MqttClientOptionsBuilder()
.WithClientId("TEAMS2HA")
.WithKeepAlivePeriod(TimeSpan.FromSeconds(30))
.WithClientId(uniqueClientId)
.WithKeepAlivePeriod(TimeSpan.FromSeconds(60))
.WithCleanSession(true)
.WithCredentials(settings.MqttUsername, settings.MqttPassword);
if (settings.UseWebsockets && !settings.UseTLS)
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>net8.0-windows7.0</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<AssemblyVersion>1.1.0.773</AssemblyVersion>
<FileVersion>1.1.0.773</FileVersion>
<AssemblyVersion>1.1.0.776</AssemblyVersion>
<FileVersion>1.1.0.776</FileVersion>
<ApplicationIcon>Assets\Square150x150Logo.scale-200.ico</ApplicationIcon>
<Title>Teams2HA</Title>
<PackageIcon>Square150x150Logo.scale-200.png</PackageIcon>
Expand Down

0 comments on commit 1a21db1

Please sign in to comment.