From fd701b269c6fc63b0c5cf55accfee52db1f5a6ab Mon Sep 17 00:00:00 2001 From: Ryan Wagoner Date: Thu, 25 Oct 2018 21:27:20 -0400 Subject: [PATCH] - Publish config when reconnecting to MQTT --- OmniLinkBridge/Modules/MQTTModule.cs | 17 ++++++++++++++--- OmniLinkBridge/Notifications/Notification.cs | 16 ++++++++++++++-- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/OmniLinkBridge/Modules/MQTTModule.cs b/OmniLinkBridge/Modules/MQTTModule.cs index e27bd65..810bb9f 100644 --- a/OmniLinkBridge/Modules/MQTTModule.cs +++ b/OmniLinkBridge/Modules/MQTTModule.cs @@ -22,6 +22,7 @@ namespace OmniLinkBridge.Modules private OmniLinkII OmniLink { get; set; } private IManagedMqttClient MqttClient { get; set; } + private bool ControllerConnected { get; set; } private Regex regexTopic = new Regex(Global.mqtt_prefix + "/([A-Za-z]+)([0-9]+)/(.*)", RegexOptions.Compiled); @@ -52,8 +53,16 @@ namespace OmniLinkBridge.Modules .Build(); MqttClient = new MqttFactory().CreateManagedMqttClient(); - MqttClient.Connected += (sender, e) => { log.Debug("Connected"); }; - MqttClient.ConnectingFailed += (sender, e) => { log.Debug("Error " + e.Exception.Message); }; + MqttClient.Connected += (sender, e) => + { + log.Debug("Connected"); + + // For the initial connection wait for the controller connected event to publish config + // For subsequent connections publish config immediately + if(ControllerConnected) + PublishConfig(); + }; + MqttClient.ConnectingFailed += (sender, e) => { log.Debug("Error connecting " + e.Exception.Message); }; MqttClient.StartAsync(manoptions); @@ -256,7 +265,7 @@ namespace OmniLinkBridge.Modules { PublishConfig(); - MqttClient.PublishAsync($"{Global.mqtt_prefix}/status", "online", MqttQualityOfServiceLevel.AtMostOnce, true); + ControllerConnected = true; } private void PublishConfig() @@ -266,6 +275,8 @@ namespace OmniLinkBridge.Modules PublishUnits(); PublishThermostats(); PublishButtons(); + + MqttClient.PublishAsync($"{Global.mqtt_prefix}/status", "online", MqttQualityOfServiceLevel.AtMostOnce, true); } private void PublishAreas() diff --git a/OmniLinkBridge/Notifications/Notification.cs b/OmniLinkBridge/Notifications/Notification.cs index 2ae1b23..db14f28 100644 --- a/OmniLinkBridge/Notifications/Notification.cs +++ b/OmniLinkBridge/Notifications/Notification.cs @@ -1,10 +1,15 @@ -using System.Collections.Generic; +using log4net; +using System; +using System.Collections.Generic; +using System.Reflection; using System.Threading.Tasks; namespace OmniLinkBridge.Notifications { public static class Notification { + private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + private static readonly List providers = new List() { new EmailNotification(), @@ -16,7 +21,14 @@ namespace OmniLinkBridge.Notifications { Parallel.ForEach(providers, (provider) => { - provider.Notify(source, description, priority); + try + { + provider.Notify(source, description, priority); + } + catch (Exception ex) + { + log.Error("Failed to send notification", ex); + } }); } }