Skip to content

Commit cdabbfc

Browse files
authored
Merge pull request #5 from DynamicDevices/feature/mqtt-version-info-publish
feat: Publish version and build info to MQTT on connect
2 parents 71c1000 + 5bc3ebf commit cdabbfc

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

MQTTControl.cs

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Buffers;
44
using System.Text;
5+
using System.Text.Json;
56
using System.Threading;
67
using System.Threading.Tasks;
78
using MQTTnet;
@@ -94,10 +95,12 @@ public static async Task Initialise(CancellationTokenSource cts, AppConfig? conf
9495
return Task.CompletedTask;
9596
};
9697

97-
client.ConnectedAsync += e =>
98+
client.ConnectedAsync += async e =>
9899
{
99100
Console.WriteLine("MQTT: Connected.");
100-
return Task.CompletedTask;
101+
// Publish version information to version subtopic
102+
await PublishVersionInfo().ConfigureAwait(false);
103+
return;
101104
};
102105

103106
client.DisconnectedAsync += e =>
@@ -204,6 +207,69 @@ public static async Task Publish(string message)
204207
}
205208
}
206209

210+
/// <summary>
211+
/// Publishes version and build information as JSON to the version subtopic
212+
/// </summary>
213+
private static async Task PublishVersionInfo()
214+
{
215+
if (client == null || !client.IsConnected)
216+
{
217+
Console.WriteLine("MQTT: Version info publish skipped - client not connected.");
218+
return;
219+
}
220+
221+
try
222+
{
223+
// Create version info object
224+
var versionInfo = new
225+
{
226+
version = VersionInfo.Version,
227+
assemblyVersion = VersionInfo.AssemblyVersion,
228+
fileVersion = VersionInfo.FileVersion,
229+
informationalVersion = VersionInfo.InformationalVersion,
230+
buildDate = VersionInfo.BuildDate,
231+
gitCommitHash = VersionInfo.GitCommitHash,
232+
fullVersion = VersionInfo.FullVersion,
233+
clientId = _clientId,
234+
timestamp = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
235+
};
236+
237+
// Serialize to JSON
238+
var jsonOptions = new JsonSerializerOptions
239+
{
240+
WriteIndented = false,
241+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
242+
};
243+
var jsonPayload = JsonSerializer.Serialize(versionInfo, jsonOptions);
244+
245+
// Construct version topic as subtopic of SendTopic
246+
var versionTopic = string.IsNullOrEmpty(_sendMessageTopic)
247+
? "version"
248+
: $"{_sendMessageTopic}/version";
249+
250+
var messageOut = new MqttApplicationMessageBuilder()
251+
.WithTopic(versionTopic)
252+
.WithPayload(jsonPayload)
253+
.WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtLeastOnce)
254+
.WithRetainFlag(true)
255+
.Build();
256+
257+
if (_cts != null)
258+
{
259+
await client.PublishAsync(messageOut, _cts.Token).ConfigureAwait(false);
260+
}
261+
else
262+
{
263+
await client.PublishAsync(messageOut).ConfigureAwait(false);
264+
}
265+
Console.WriteLine($"MQTT: Published version info to {versionTopic}");
266+
}
267+
catch (Exception ex)
268+
{
269+
Console.WriteLine($"MQTT: Version info publish error: {ex.Message}");
270+
}
271+
}
272+
207273
public static void ReceiveMessage(string message)
208274
{
209275
Console.WriteLine($"Received message");

VersionInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Linq;
12
using System.Reflection;
23

34
/// <summary>

0 commit comments

Comments
 (0)