Skip to content
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
70 changes: 68 additions & 2 deletions MQTTControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Buffers;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using MQTTnet;
Expand Down Expand Up @@ -94,10 +95,12 @@ public static async Task Initialise(CancellationTokenSource cts, AppConfig? conf
return Task.CompletedTask;
};

client.ConnectedAsync += e =>
client.ConnectedAsync += async e =>
{
Console.WriteLine("MQTT: Connected.");
return Task.CompletedTask;
// Publish version information to version subtopic
await PublishVersionInfo().ConfigureAwait(false);
return;
};

client.DisconnectedAsync += e =>
Expand Down Expand Up @@ -204,6 +207,69 @@ public static async Task Publish(string message)
}
}

/// <summary>
/// Publishes version and build information as JSON to the version subtopic
/// </summary>
private static async Task PublishVersionInfo()
{
if (client == null || !client.IsConnected)
{
Console.WriteLine("MQTT: Version info publish skipped - client not connected.");
return;
}

try
{
// Create version info object
var versionInfo = new
{
version = VersionInfo.Version,
assemblyVersion = VersionInfo.AssemblyVersion,
fileVersion = VersionInfo.FileVersion,
informationalVersion = VersionInfo.InformationalVersion,
buildDate = VersionInfo.BuildDate,
gitCommitHash = VersionInfo.GitCommitHash,
fullVersion = VersionInfo.FullVersion,
clientId = _clientId,
timestamp = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
};

// Serialize to JSON
var jsonOptions = new JsonSerializerOptions
{
WriteIndented = false,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
var jsonPayload = JsonSerializer.Serialize(versionInfo, jsonOptions);

// Construct version topic as subtopic of SendTopic
var versionTopic = string.IsNullOrEmpty(_sendMessageTopic)
? "version"
: $"{_sendMessageTopic}/version";

var messageOut = new MqttApplicationMessageBuilder()
.WithTopic(versionTopic)
.WithPayload(jsonPayload)
.WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtLeastOnce)
.WithRetainFlag(true)
.Build();

if (_cts != null)
{
await client.PublishAsync(messageOut, _cts.Token).ConfigureAwait(false);
}
else
{
await client.PublishAsync(messageOut).ConfigureAwait(false);
}
Console.WriteLine($"MQTT: Published version info to {versionTopic}");
}
catch (Exception ex)
{
Console.WriteLine($"MQTT: Version info publish error: {ex.Message}");
}
}

public static void ReceiveMessage(string message)
{
Console.WriteLine($"Received message");
Expand Down
1 change: 1 addition & 0 deletions VersionInfo.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Linq;
using System.Reflection;

/// <summary>
Expand Down
Loading