示例#1
0
 public static Task PublishAsync(this IApplicationMessagePublisher publisher, string topic, object data, MqttQualityOfServiceLevel qualityOfServiceLevel = MqttQualityOfServiceLevel.ExactlyOnce)
 {
     try
     {
         if (publisher == null)
         {
             throw new ArgumentNullException(nameof(publisher));
         }
         if (topic == null)
         {
             throw new ArgumentNullException(nameof(topic));
         }
         if (data == null)
         {
             throw new ArgumentNullException(nameof(data));
         }
         string value = JsonConvert.SerializeObject(data,
                                                    new JsonSerializerSettings
         {
             ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(),
             DateFormatString = "yyyy-MM-dd HH:mm:ss"
         });
         return(publisher.PublishAsync(new[]
         {
             new MqttApplicationMessageBuilder().WithTopic(topic).WithPayload(value).WithQualityOfServiceLevel(qualityOfServiceLevel).Build()
         }));
     }
     catch (Exception ex)
     {
         return(null);
     }
 }
示例#2
0
 public MqttPublisher(IApplicationMessagePublisher client,
                      MqttPublishSettings settings,
                      Action <Inverter, Measurement, Exception>?publishFailed = null)
 {
     _client        = client;
     _settings      = settings;
     _publishFailed = publishFailed;
 }
        public static Task <MqttClientPublishResult> PublishAsync(this IApplicationMessagePublisher publisher, Func <MqttApplicationMessageBuilder, MqttApplicationMessageBuilder> builder)
        {
            if (publisher == null)
            {
                throw new ArgumentNullException(nameof(publisher));
            }

            var message = builder(new MqttApplicationMessageBuilder()).Build();

            return(publisher.PublishAsync(message, CancellationToken.None));
        }
示例#4
0
        public static Task <MqttClientPublishResult> PublishEventAsync(
            this IApplicationMessagePublisher applicationMessagePublisher,
            EventBase @event,
            CancellationToken cancellationToken = default)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return(Task.FromCanceled <MqttClientPublishResult>(cancellationToken));
            }

            return(applicationMessagePublisher.PublishAsync(@event.BuildMqttMessage(), cancellationToken));
        }
示例#5
0
        public static MqttClientPublishResult PublishEvent(
            this IApplicationMessagePublisher applicationMessagePublisher,
            EventBase @event,
            CancellationToken cancellationToken = default)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                throw new OperationCanceledException(cancellationToken);
            }

            return(applicationMessagePublisher.PublishAsync(@event.BuildMqttMessage(), cancellationToken).GetAwaiter().GetResult());
        }
        public static Task PublishAsync(this IApplicationMessagePublisher publisher, string topic)
        {
            if (publisher == null)
            {
                throw new ArgumentNullException(nameof(publisher));
            }
            if (topic == null)
            {
                throw new ArgumentNullException(nameof(topic));
            }

            return(publisher.PublishAsync(new MqttApplicationMessageBuilder().WithTopic(topic).Build()));
        }
        public static Task PublishAsync(this IApplicationMessagePublisher client, params MqttApplicationMessage[] applicationMessages)
        {
            if (client == null)
            {
                throw new ArgumentNullException(nameof(client));
            }
            if (applicationMessages == null)
            {
                throw new ArgumentNullException(nameof(applicationMessages));
            }

            return(client.PublishAsync(applicationMessages));
        }
        public static Task PublishAsync(this IApplicationMessagePublisher publisher, string topic, string payload, MqttQualityOfServiceLevel qualityOfServiceLevel)
        {
            if (publisher == null)
            {
                throw new ArgumentNullException(nameof(publisher));
            }
            if (topic == null)
            {
                throw new ArgumentNullException(nameof(topic));
            }

            return(publisher.PublishAsync(new MqttApplicationMessageBuilder().WithTopic(topic).WithPayload(payload).WithQualityOfServiceLevel(qualityOfServiceLevel).Build()));
        }
        public static Task <MqttClientPublishResult> PublishAsync(this IApplicationMessagePublisher publisher, MqttApplicationMessage applicationMessage)
        {
            if (publisher == null)
            {
                throw new ArgumentNullException(nameof(publisher));
            }
            if (applicationMessage == null)
            {
                throw new ArgumentNullException(nameof(applicationMessage));
            }

            return(publisher.PublishAsync(applicationMessage, CancellationToken.None));
        }
        public static Task <MqttClientPublishResult> PublishAsync(this IApplicationMessagePublisher publisher, string topic)
        {
            if (publisher == null)
            {
                throw new ArgumentNullException(nameof(publisher));
            }
            if (topic == null)
            {
                throw new ArgumentNullException(nameof(topic));
            }

            return(publisher.PublishAsync(builder => builder
                                          .WithTopic(topic)));
        }
示例#11
0
        public static Task PublishAsync(this IApplicationMessagePublisher publisher, string topic, string payload)
        {
            if (publisher == null)
            {
                throw new ArgumentNullException(nameof(publisher));
            }
            if (topic == null)
            {
                throw new ArgumentNullException(nameof(topic));
            }

            return(publisher.PublishAsync(builder => builder
                                          .WithTopic(topic)
                                          .WithPayload(payload)));
        }
示例#12
0
        public static async Task PublishAsync(this IApplicationMessagePublisher publisher, IEnumerable <MqttApplicationMessage> applicationMessages)
        {
            if (publisher == null)
            {
                throw new ArgumentNullException(nameof(publisher));
            }
            if (applicationMessages == null)
            {
                throw new ArgumentNullException(nameof(applicationMessages));
            }

            foreach (var applicationMessage in applicationMessages)
            {
                await publisher.PublishAsync(applicationMessage).ConfigureAwait(false);
            }
        }
        public static async Task PublishAsync(this IApplicationMessagePublisher publisher, params MqttApplicationMessage[] applicationMessages)
        {
            if (publisher == null)
            {
                throw new ArgumentNullException(nameof(publisher));
            }
            if (applicationMessages == null)
            {
                throw new ArgumentNullException(nameof(applicationMessages));
            }

            foreach (var applicationMessage in applicationMessages)
            {
                await publisher.PublishAsync(applicationMessage, CancellationToken.None).ConfigureAwait(false);
            }
        }
        public static Task <MqttClientPublishResult> PublishAsync(this IApplicationMessagePublisher publisher, string topic, string payload, MqttQualityOfServiceLevel qualityOfServiceLevel, bool retain)
        {
            if (publisher == null)
            {
                throw new ArgumentNullException(nameof(publisher));
            }
            if (topic == null)
            {
                throw new ArgumentNullException(nameof(topic));
            }

            return(publisher.PublishAsync(builder => builder
                                          .WithTopic(topic)
                                          .WithPayload(payload)
                                          .WithQualityOfServiceLevel(qualityOfServiceLevel)
                                          .WithRetainFlag(retain)));
        }
示例#15
0
 public ProcessingCommandHandler(
     IProcessingCommandHandlerContextFactory contextFactory,
     IEventCommitter eventCommitter,
     IEventStore eventStore,
     IEventPublisher eventPublisher,
     IApplicationMessagePublisher applicationMessagePublisher,
     IStringObjectSerializer objectSerializer,
     IServiceProvider serviceProvider,
     ILogger <ProcessingCommandHandler> logger)
 {
     this.contextFactory = contextFactory;
     this.eventCommitter = eventCommitter;
     this.eventStore     = eventStore;
     this.eventPublisher = eventPublisher;
     this.applicationMessagePublisher = applicationMessagePublisher;
     this.objectSerializer            = objectSerializer;
     this.serviceProvider             = serviceProvider;
     this.logger         = logger;
     handlerMapping      = new ConcurrentDictionary <Type, ICommandHandler>();
     asyncHandlerMapping = new ConcurrentDictionary <Type, ICommandAsyncHandler>();
 }
示例#16
0
        public static Task PublishAsync(this IApplicationMessagePublisher publisher, Func <MqttApplicationMessageBuilder, MqttApplicationMessageBuilder> builder)
        {
            var message = builder(new MqttApplicationMessageBuilder()).Build();

            return(publisher.PublishAsync(message));
        }
示例#17
0
 public Publisher(IApplicationMessagePublisher mqttPublisher)
 {
     this.mqttPublisher           = mqttPublisher;
     this.cancellationTokenSource = new CancellationTokenSource();
 }