From 6f98a3fd096074cd1083f781d55510805568e73a Mon Sep 17 00:00:00 2001 From: Jonas Hansen Date: Thu, 9 Jun 2022 16:36:24 +0200 Subject: [PATCH] Add OnConnect/Disconnect to MqttConService --- src/ToMqttNet/MqttConnectionService.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ToMqttNet/MqttConnectionService.cs b/src/ToMqttNet/MqttConnectionService.cs index cac6c42..aa29618 100644 --- a/src/ToMqttNet/MqttConnectionService.cs +++ b/src/ToMqttNet/MqttConnectionService.cs @@ -17,6 +17,8 @@ public class MqttConnectionService : BackgroundService, IMqttConnectionService private IManagedMqttClient? _mqttClient; public event EventHandler OnApplicationMessageReceived = null!; + public event EventHandler? OnConnect; + public event EventHandler? OnDisconnect; public MqttConnectionService( ILogger logger, @@ -57,11 +59,14 @@ await _mqttClient.PublishAsync( .WithTopic($"{MqttOptions.NodeId}/connected") .WithRetainFlag() .Build()); + + OnConnect?.Invoke(this, new EventArgs()); }); _mqttClient.UseDisconnectedHandler((evnt) => { _logger.LogInformation(evnt.Exception, "Disconnected from mqtt: {reason}", evnt.Reason); + OnDisconnect?.Invoke(this, new EventArgs()); }); _mqttClient.UseApplicationMessageReceivedHandler((evnt) =>