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); } }
public void PublishUpdate(Measurement measurement) { MqttApplicationMessage message; if (measurement?.Value != null && registeredMeasurements.TryGetValue(measurement, out message)) { message.Payload = JsonSerializer.Serialize(measurement.Value); _ = mqttPublisher.PublishAsync(message, cancellationTokenSource.Token); } }
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)); }
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)); }
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))); }
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))); }
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))); }
async Task PublishApplicationMessageAsync(ProcessingCommand processingCommand, IApplicationMessage applicationMessage) { var result = await Policy .Handle <Exception>() .OrResult <AsyncExecutedResult>(r => !r.Succeeded) .WaitAndRetryForeverAsync( retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (delegateResult, retryCount, retryAttempt) => logger.LogError(delegateResult.Exception ?? delegateResult.Result.Exception, $"命令产生的应用消息发布失败,重试。 [CommandType = {processingCommand.Command.GetType()}, CommandId = {processingCommand.Command.Id}, ApplicationMessageType = {applicationMessage.GetType()}, ApplicationMessageId = {applicationMessage.Id}, ApplicationMessageRoutingKey = {applicationMessage.GetRoutingKey()}, RetryCount = {retryCount}, RetryAttempt = {retryAttempt}]")) .ExecuteAsync(() => applicationMessagePublisher.PublishAsync(applicationMessage)); if (result.Succeeded) { logger.LogDebug($"命令产生的应用消息发布成功。 [CommandType = {processingCommand.Command.GetType()}, CommandId = {processingCommand.Command.Id}, ApplicationMessageType = {applicationMessage.GetType()}, ApplicationMessageId = {applicationMessage.Id}, ApplicationMessageRoutingKey = {applicationMessage.GetRoutingKey()}]"); await processingCommand.OnQueueProcessedAsync(CommandExecutedStatus.Succeeded, objectSerializer.Serialize(applicationMessage), applicationMessage.GetTag()); } else { logger.LogDebug(result.Exception, $"命令产生的应用消息发布失败。 [CommandType = {processingCommand.Command.GetType()}, CommandId = {processingCommand.Command.Id}, ApplicationMessageType = {applicationMessage.GetType()}, ApplicationMessageId = {applicationMessage.Id}, ApplicationMessageRoutingKey = {applicationMessage.GetRoutingKey()}]"); } }
public async Task Publish(Inverter inverter, Measurement measurement, CancellationToken cancellationToken) { var messages = new List <MqttApplicationMessage> { Create($"{_settings.RootTopic}/{inverter.Id}/temperature", measurement.Temperature.ToString(CultureInfo.InvariantCulture)), Create($"{_settings.RootTopic}/{inverter.Id}/power", measurement.Power.ToString(CultureInfo.InvariantCulture)), Create($"{_settings.RootTopic}/{inverter.Id}/creationDateTime", measurement.CreatedAt.ToString("o")), Create($"{_settings.RootTopic}/{inverter.Id}/fault", measurement.Fault.HasValue.ToString()) }; try { await Task.WhenAll(messages.Select(message => _client.PublishAsync(message, cancellationToken))).ConfigureAwait(false); } catch (Exception exception) { _publishFailed?.Invoke(inverter, measurement, exception); }
public static Task PublishAsync(this IApplicationMessagePublisher publisher, Func <MqttApplicationMessageBuilder, MqttApplicationMessageBuilder> builder) { var message = builder(new MqttApplicationMessageBuilder()).Build(); return(publisher.PublishAsync(message)); }