Esempio n. 1
0
        private IDisposable BindObserver(
            IMqttMessageBus target,
            TEntity entity,
            TEntityDefinition entityDefinition,
            MqttQualityOfServiceLevel?qualityOfServiceLevel,
            bool retainMessages)
        => GetStateChangeDetector(entity)
        .Subscribe(
            state =>
        {
            var payload = GetStateMessagePayload(entityDefinition, entity);

            var messageBuilder = new MqttApplicationMessageBuilder()
                                 .WithTopic(entityDefinition.StateTopic)
                                 .WithPayload(payload)
                                 .WithRetainFlag(retainMessages);

            if (qualityOfServiceLevel.HasValue)
            {
                messageBuilder = messageBuilder.WithQualityOfServiceLevel(qualityOfServiceLevel.Value);
            }

            var message = messageBuilder.Build();

            if (message.Payload != null)
            {
                target.PublishMessage(message);
            }
        }
            );
        public virtual void Bind(IMqttMessageBus messageBus, bool sendDiscovery = true)
        {
            if (MessageBus != null)
            {
                throw new InvalidOperationException($"{nameof(Bind)} can only be called once");
            }

            MessageBus = messageBus ?? throw new ArgumentNullException(nameof(messageBus));

            if (sendDiscovery)
            {
                MessageBus.PublishMessage(EntityConfiguration.DiscoveryMessage);
            }
        }