- 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 OmniLinkII OmniLink { get; set; }
private IManagedMqttClient MqttClient { 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); private Regex regexTopic = new Regex(Global.mqtt_prefix + "/([A-Za-z]+)([0-9]+)/(.*)", RegexOptions.Compiled);
@ -52,8 +53,16 @@ namespace OmniLinkBridge.Modules
.Build(); .Build();
MqttClient = new MqttFactory().CreateManagedMqttClient(); MqttClient = new MqttFactory().CreateManagedMqttClient();
MqttClient.Connected += (sender, e) => { log.Debug("Connected"); }; MqttClient.Connected += (sender, e) =>
MqttClient.ConnectingFailed += (sender, e) => { log.Debug("Error " + e.Exception.Message); }; {
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); MqttClient.StartAsync(manoptions);
@ -256,7 +265,7 @@ namespace OmniLinkBridge.Modules
{ {
PublishConfig(); PublishConfig();
MqttClient.PublishAsync($"{Global.mqtt_prefix}/status", "online", MqttQualityOfServiceLevel.AtMostOnce, true); ControllerConnected = true;
} }
private void PublishConfig() private void PublishConfig()
@ -266,6 +275,8 @@ namespace OmniLinkBridge.Modules
PublishUnits(); PublishUnits();
PublishThermostats(); PublishThermostats();
PublishButtons(); PublishButtons();
MqttClient.PublishAsync($"{Global.mqtt_prefix}/status", "online", MqttQualityOfServiceLevel.AtMostOnce, true);
} }
private void PublishAreas() 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; using System.Threading.Tasks;
namespace OmniLinkBridge.Notifications namespace OmniLinkBridge.Notifications
{ {
public static class Notification public static class Notification
{ {
private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private static readonly List<INotification> providers = new List<INotification>() private static readonly List<INotification> providers = new List<INotification>()
{ {
new EmailNotification(), new EmailNotification(),
@ -15,8 +20,15 @@ namespace OmniLinkBridge.Notifications
public static void Notify(string source, string description, NotificationPriority priority = NotificationPriority.Normal) public static void Notify(string source, string description, NotificationPriority priority = NotificationPriority.Normal)
{ {
Parallel.ForEach(providers, (provider) => Parallel.ForEach(providers, (provider) =>
{
try
{ {
provider.Notify(source, description, priority); provider.Notify(source, description, priority);
}
catch (Exception ex)
{
log.Error("Failed to send notification", ex);
}
}); });
} }
} }