- Publish config when reconnecting to MQTT

This commit is contained in:
Ryan Wagoner 2018-10-25 21:27:20 -04:00
parent 96093fbebd
commit fd701b269c
2 changed files with 28 additions and 5 deletions

View file

@ -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()

View file

@ -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<INotification> providers = new List<INotification>()
{
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);
}
});
}
}