|
2 | 2 | using System; |
3 | 3 | using System.Buffers; |
4 | 4 | using System.Text; |
| 5 | +using System.Text.Json; |
5 | 6 | using System.Threading; |
6 | 7 | using System.Threading.Tasks; |
7 | 8 | using MQTTnet; |
@@ -94,10 +95,12 @@ public static async Task Initialise(CancellationTokenSource cts, AppConfig? conf |
94 | 95 | return Task.CompletedTask; |
95 | 96 | }; |
96 | 97 |
|
97 | | - client.ConnectedAsync += e => |
| 98 | + client.ConnectedAsync += async e => |
98 | 99 | { |
99 | 100 | Console.WriteLine("MQTT: Connected."); |
100 | | - return Task.CompletedTask; |
| 101 | + // Publish version information to version subtopic |
| 102 | + await PublishVersionInfo().ConfigureAwait(false); |
| 103 | + return; |
101 | 104 | }; |
102 | 105 |
|
103 | 106 | client.DisconnectedAsync += e => |
@@ -204,6 +207,69 @@ public static async Task Publish(string message) |
204 | 207 | } |
205 | 208 | } |
206 | 209 |
|
| 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 | + |
207 | 273 | public static void ReceiveMessage(string message) |
208 | 274 | { |
209 | 275 | Console.WriteLine($"Received message"); |
|
0 commit comments