public void SubscribeTopic(string topicName) { if (!SubscribedTopics.Contains(topicName)) { SubscribedTopics.Add(topicName); } }
/// <summary> /// Initializes a new instance of the <see cref="HarmonyMqttService"/> class. /// </summary> /// <param name="logger">Logging instance.</param> /// <param name="harmonyClient">The Harmony client.</param> /// <param name="harmonyName">The Harmony name.</param> /// <param name="harmonyKeyPressLength">The Harmony key press length.</param> /// <param name="brokerSettings">MQTT broker settings.</param> public HarmonyMqttService( ILogger <HarmonyMqttService> logger, IClient harmonyClient, string harmonyName, int harmonyKeyPressLength, BrokerSettings brokerSettings) : base(logger, brokerSettings, "harmony/" + harmonyName) { _log = logger; _topicActionMap = new Dictionary <string, string>(); SubscribedTopics.Add(TopicRoot + "/devices/+/+/+/set"); SubscribedTopics.Add(TopicRoot + "/activity/set"); SubscribedTopics.Add(TopicRoot + "/activity/+/set"); // Setup harmony client _client = harmonyClient; _harmonyName = harmonyName; _harmonyKeyPressLength = harmonyKeyPressLength; _client.CurrentActivityUpdated += Harmony_CurrentActivityUpdated; // Harmony client logging _client.MessageSent += (object sender, MessageSentEventArgs e) => { _log.LogInformation("Harmony Message sent: " + e.Message); }; _client.MessageReceived += (object sender, MessageReceivedEventArgs e) => { _log.LogInformation("Harmony Message received: " + e.Message); }; _client.Error += (object sender, System.IO.ErrorEventArgs e) => { var exception = e.GetException(); _log.LogError(exception, exception.Message); throw new Exception("Harmony connection lost"); }; }
/// <summary> /// Subscribes to the MQTT topics in SubscribedTopics. /// </summary> /// <param name="cancellationToken">Cancelation token.</param> /// <returns>Awaitable <see cref="Task" />.</returns> protected virtual async Task SubscribeAsync(CancellationToken cancellationToken = default(CancellationToken)) { _serviceLog.LogInformation("MQTT subscribing to the following topics: " + string.Join(", ", SubscribedTopics)); await MqttClient.SubscribeAsync(SubscribedTopics .Select(topic => new TopicFilterBuilder() .WithTopic(topic) .WithAtLeastOnceQoS() .Build())) .ConfigureAwait(false); }
/// <summary> /// Initializes a new instance of the <see cref="NeatoMqttService"/> class. /// </summary> /// <param name="logger">Logging instance.</param> /// <param name="neatoClient">Neato client.</param> /// <param name="neatoName">Neato name.</param> /// <param name="refreshInterval">Refresh interval.</param> /// <param name="brokerSettings">MQTT broker settings.</param> public NeatoMqttService( ILogger <NeatoMqttService> logger, IRobot neatoClient, string neatoName, int refreshInterval, BrokerSettings brokerSettings) : base(logger, brokerSettings, "neato/" + neatoName) { _log = logger; _refreshInterval = refreshInterval * 1000; SubscribedTopics.Add(TopicRoot + "/+/set"); _client = neatoClient; }
/// <summary> /// Initializes a new instance of the <see cref="EcobeeMqttService"/> class. /// </summary> /// <param name="logger">Logging instance.</param> /// <param name="ecobeeClient">The Ecobee client.</param> /// <param name="ecobeeName">The target Ecobee name.</param> /// <param name="refreshInterval">The refresh interval.</param> /// <param name="brokerSettings">MQTT broker settings.</param> public EcobeeMqttService( ILogger <EcobeeMqttService> logger, Client ecobeeClient, string ecobeeName, int refreshInterval, BrokerSettings brokerSettings) : base(logger, brokerSettings, "ecobee/" + ecobeeName) { _log = logger; _refreshInterval = refreshInterval * 1000; SubscribedTopics.Add(TopicRoot + "/+/+/set"); _client = ecobeeClient; _revisionStatusCache = new Dictionary <string, RevisionStatus>(); _thermostatStatus = new Dictionary <string, ThermostatStatus>(); }
/// <summary> /// Initializes a new instance of the <see cref="ApexMqttService"/> class. /// </summary> /// <param name="logger">Logging instance.</param> /// <param name="apexClient">Apex client.</param> /// <param name="apexName">Apex name.</param> /// <param name="refreshInterval">Refresh interval.</param> /// <param name="publishOnlyChangedValues">Only publish values when they change from previous value.</param> /// <param name="brokerSettings">MQTT broker settings.</param> public ApexMqttService( ILogger <ApexMqttService> logger, Client apexClient, string apexName, int refreshInterval, bool publishOnlyChangedValues, BrokerSettings brokerSettings) : base(logger, brokerSettings, "apex/" + apexName) { _log = logger; _refreshInterval = refreshInterval * 1000; _publishOnlyChangedValues = publishOnlyChangedValues; SubscribedTopics.Add(TopicRoot + "/outlets/+/set"); SubscribedTopics.Add(TopicRoot + "/feedCycle/set"); _client = apexClient; }
/// <summary> /// Initializes a new instance of the <see cref="MqttService"/> class. /// </summary> /// <param name="logger">Logging instance.</param> /// <param name="messageHub">Message hub.</param> /// <param name="brokerSettings">MQTT broker settings.</param> /// <param name="deviceRepository">Device repository.</param> /// <param name="stateCache">State cache,</param> public MqttService( ILogger <MqttService> logger, IMessageHub messageHub, BrokerSettings brokerSettings, IGoogleDeviceRepository deviceRepository, StateCache stateCache) : base(logger, brokerSettings, "google/home") { _log = logger ?? throw new ArgumentException(nameof(logger)); _messageHub = messageHub ?? throw new ArgumentException(nameof(messageHub)); _deviceRepository = deviceRepository ?? throw new ArgumentException(nameof(deviceRepository)); _stateCache = stateCache ?? throw new ArgumentException(nameof(stateCache)); // Subscribe to google home based topics SubscribedTopics.Add(TopicRoot + "/#"); // Subscribe to all monitored state topics foreach (var topic in _stateCache.Keys) { SubscribedTopics.Add(topic); } }
/// <summary> /// Initializes a new instance of the <see cref="TiVoMqttService"/> class. /// </summary> /// <param name="logger">Logging instance.</param> /// <param name="tivoClient">TiVo client.</param> /// <param name="tivoName">TiVo name.</param> /// <param name="brokerSettings">MQTT broker settings.</param> public TiVoMqttService( ILogger <TiVoMqttService> logger, IClient tivoClient, string tivoName, BrokerSettings brokerSettings) : base(logger, brokerSettings, "tivo/" + tivoName) { _log = logger; SubscribedTopics.Add(TopicRoot + "/controls/+/set"); _client = tivoClient; _client.EventReceived += TiVo_EventReceived; // TiVo client logging _client.MessageSent += (object sender, MessageSentEventArgs e) => { _log.LogInformation("TiVo Message sent: " + e.Message); }; _client.MessageReceived += (object sender, MessageReceivedEventArgs e) => { _log.LogInformation("TiVo Message received: " + e.Message); }; _client.Error += (object sender, System.IO.ErrorEventArgs e) => { var exception = e.GetException(); _log.LogError(exception, exception.Message); System.Environment.Exit(1); }; }
/// <summary> /// Initializes a new instance of the <see cref="MqttService"/> class. /// </summary> /// <param name="logger">Logging instance.</param> /// <param name="deviceConfiguration">Ddevice configuration.</param> /// <param name="stateCache">State cache,</param> /// <param name="messageHub">Message hub.</param> /// <param name="googleHomeGraphClient">Google Home Graph API client.</param> /// <param name="brokerSettings">MQTT broker settings.</param> public MqttService( ILogger <MqttService> logger, DeviceConfiguration deviceConfiguration, StateCache stateCache, IMessageHub messageHub, GoogleHomeGraphClient googleHomeGraphClient, BrokerSettings brokerSettings) : base(logger, brokerSettings, "google/home/") { _log = logger; _deviceConfig = deviceConfiguration; _stateCache = stateCache; _messageHub = messageHub; _googleHomeGraphClient = googleHomeGraphClient; // Subscribe to google home based topics SubscribedTopics.Add(TopicRoot + "#"); // Subscribe to all monitored state topics foreach (var topic in _stateCache.Keys) { SubscribedTopics.Add(topic); } }
/// <summary> /// Handler for device config change events. /// </summary> /// <param name="changeEvent">The change event to handle.</param> private async void HandleConfigSubscriptionChange(ConfigSubscriptionChangeEvent changeEvent) { // Stop listening to removed topics foreach (var topic in changeEvent.DeletedSubscriptions.Distinct()) { // Check if actually subscribed and remove MQTT subscription if (SubscribedTopics.Contains(topic)) { _log.LogInformation("MQTT unsubscribing to the following topic: {Topic}", topic); await MqttClient.UnsubscribeAsync(new List <string> { topic }); SubscribedTopics.Remove(topic); } // Check that state cache actually contains topic if (_stateCache.ContainsKey(topic)) { if (_stateCache.TryRemove(topic, out string _)) { _log.LogInformation("Successfully removed topic {Topic} from internal state cache", topic); } else { _log.LogWarning("Failed to remove topic {Topic} from internal state cache", topic); } } } // Begin listening to added topics foreach (var topic in changeEvent.AddedSubscriptions.Distinct()) { // Ensure the that state cache doesn't contain topic and add if (!_stateCache.ContainsKey(topic)) { if (_stateCache.TryAdd(topic, string.Empty)) { _log.LogInformation("Successfully added topic {Topic} to internal state cache", topic); } else { _log.LogWarning("Failed to add topic {Topic} to internal state cache", topic); } } // Check if already subscribed and subscribe to MQTT topic if (!SubscribedTopics.Contains(topic)) { _log.LogInformation("MQTT subscribing to the following topic: {Topic}", topic); await MqttClient.SubscribeAsync( new List <MqttTopicFilter> { new MqttTopicFilterBuilder() .WithTopic(topic) .WithAtLeastOnceQoS() .Build() }); SubscribedTopics.Add(topic); } } }