public HassConnectedEntityService(IOptions <HassConnectedEntityServiceConfig> options,
                                          IManagedMqttClient mqttClient,
                                          MqttEvents mqttEvents,
                                          HassMqttManager hassMqttManager,
                                          HassMqttTopicBuilder topicBuilder)
        {
            _config          = options.Value;
            _mqttClient      = mqttClient;
            _mqttEvents      = mqttEvents;
            _hassMqttManager = hassMqttManager;

            StateTopic = topicBuilder.GetServiceTopic(_config.DeviceId, _config.EntityId, "state");
        }
示例#2
0
        public MqttClientLifetimeService(IManagedMqttClient client, IManagedMqttClientOptions options, IServiceProvider serviceProvider)
        {
            _client  = client;
            _options = options;

            // Initialize initial receives
            MqttEvents mqttEvents = serviceProvider.GetService <MqttEvents>();

            if (mqttEvents != null)
            {
                IEnumerable <IMqttEventReceiver> receivers = serviceProvider.GetServices <IMqttEventReceiver>();

                foreach (IMqttEventReceiver receiver in receivers)
                {
                    mqttEvents.OnConnect    += receiver.OnConnect;
                    mqttEvents.OnDisconnect += receiver.OnDisconnect;
                }
            }
        }
示例#3
0
    public static IManagedMqttClient ConfigureMqttEvents(this IManagedMqttClient mqttClient, IServiceProvider serviceProvider, CancellationToken cancellationToken)
    {
        MqttEvents mqttEvents = serviceProvider.GetService <MqttEvents>();

        if (mqttEvents == null)
        {
            throw new InvalidOperationException($"Unable to find {nameof(MqttEvents)} service. Did you forget to call {nameof(ServiceCollectionExtensions.AddMqttEvents)}()?");
        }

        mqttClient.UseDisconnectedHandler(async args =>
        {
            await mqttEvents.InvokeDisconnectHandler(args, cancellationToken);
        });
        mqttClient.UseConnectedHandler(async args =>
        {
            await mqttEvents.InvokeConnectHandler(args, cancellationToken);
        });

        return(mqttClient);
    }