|
| 1 | +/* Code generated by Azure.Iot.Operations.ProtocolCompiler v0.10.0.0; DO NOT EDIT. */ |
| 2 | + |
| 3 | +#nullable enable |
| 4 | + |
| 5 | +namespace Azure.Iot.Operations.Services.Observability.AkriObservabilityService |
| 6 | +{ |
| 7 | + using System; |
| 8 | + using System.Collections.Generic; |
| 9 | + using System.Linq; |
| 10 | + using System.Threading; |
| 11 | + using System.Threading.Tasks; |
| 12 | + using Azure.Iot.Operations.Protocol.Models; |
| 13 | + using Azure.Iot.Operations.Protocol; |
| 14 | + using Azure.Iot.Operations.Protocol.RPC; |
| 15 | + using Azure.Iot.Operations.Protocol.Telemetry; |
| 16 | + using Azure.Iot.Operations.Services.Observability; |
| 17 | + |
| 18 | + [CommandTopic("akri/observability/{ex:connectorClientId}/metrics")] |
| 19 | + [System.CodeDom.Compiler.GeneratedCode("Azure.Iot.Operations.ProtocolCompiler", "0.10.0.0")] |
| 20 | + public static partial class AkriObservabilityService |
| 21 | + { |
| 22 | + public abstract partial class Service : IAsyncDisposable |
| 23 | + { |
| 24 | + private ApplicationContext applicationContext; |
| 25 | + private IMqttPubSubClient mqttClient; |
| 26 | + private readonly PublishMetricsCommandExecutor publishMetricsCommandExecutor; |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Construct a new instance of this service. |
| 30 | + /// </summary> |
| 31 | + /// <param name="applicationContext">The shared context for your application.</param> |
| 32 | + /// <param name="mqttClient">The MQTT client to use.</param> |
| 33 | + /// <param name="topicTokenMap"> |
| 34 | + /// The topic token replacement map to use for all operations by default. Generally, this will include the token values |
| 35 | + /// for topic tokens such as "modelId" which should be the same for the duration of this service's lifetime. Note that |
| 36 | + /// additional topic tokens can be specified when starting the service with <see cref="StartAsync(Dictionary{string, string}?, int?, CancellationToken)"/> and |
| 37 | + /// can be specified per-telemetry message. |
| 38 | + /// </param> |
| 39 | + public Service(ApplicationContext applicationContext, IMqttPubSubClient mqttClient, Dictionary<string, string>? topicTokenMap = null) |
| 40 | + { |
| 41 | + this.applicationContext = applicationContext; |
| 42 | + this.mqttClient = mqttClient; |
| 43 | + |
| 44 | + string? clientId = this.mqttClient.ClientId; |
| 45 | + if (string.IsNullOrEmpty(clientId)) |
| 46 | + { |
| 47 | + throw new InvalidOperationException("No MQTT client Id configured. Must connect to MQTT broker before invoking command."); |
| 48 | + } |
| 49 | + |
| 50 | + this.publishMetricsCommandExecutor = new PublishMetricsCommandExecutor(applicationContext, mqttClient) { OnCommandReceived = PublishMetricsInt }; |
| 51 | + |
| 52 | + if (topicTokenMap != null) |
| 53 | + { |
| 54 | + foreach (string topicTokenKey in topicTokenMap.Keys) |
| 55 | + { |
| 56 | + this.publishMetricsCommandExecutor.TopicTokenMap.TryAdd("ex:" + topicTokenKey, topicTokenMap[topicTokenKey]); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + this.publishMetricsCommandExecutor.TopicTokenMap.TryAdd("executorId", clientId); |
| 61 | + } |
| 62 | + |
| 63 | + public PublishMetricsCommandExecutor PublishMetricsCommandExecutor { get => this.publishMetricsCommandExecutor; } |
| 64 | + |
| 65 | + public abstract Task<ExtendedResponse<PublishMetricsResponsePayload>> PublishMetricsAsync(PublishMetricsRequestPayload request, CommandRequestMetadata requestMetadata, CancellationToken cancellationToken); |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Begin accepting command invocations for all command executors. |
| 69 | + /// </summary> |
| 70 | + /// <param name="additionalTopicTokenMap"> |
| 71 | + /// The topic token replacements to use in addition to any topic tokens specified in the constructor. If this map |
| 72 | + /// contains any keys that topic tokens provided in the constructor also has, then values specified in this map will take precedence. |
| 73 | + /// </param> |
| 74 | + /// <param name="preferredDispatchConcurrency">The dispatch concurrency count for the command response cache to use.</param> |
| 75 | + /// <param name="cancellationToken">Cancellation token.</param> |
| 76 | + /// <remarks> |
| 77 | + /// Specifying custom topic tokens in <paramref name="additionalTopicTokenMap"/> allows you to make command executors only |
| 78 | + /// accept commands over a specific topic. |
| 79 | + /// |
| 80 | + /// Note that a given command executor can only be started with one set of topic token replacements. If you want a command executor |
| 81 | + /// to only handle commands for several specific sets of topic token values (as opposed to all possible topic token values), then you will |
| 82 | + /// instead need to create a command executor per topic token set. |
| 83 | + /// </remarks> |
| 84 | + public async Task StartAsync(int? preferredDispatchConcurrency = null, CancellationToken cancellationToken = default) |
| 85 | + { |
| 86 | + string? clientId = this.mqttClient.ClientId; |
| 87 | + if (string.IsNullOrEmpty(clientId)) |
| 88 | + { |
| 89 | + throw new InvalidOperationException("No MQTT client Id configured. Must connect to MQTT broker before starting service."); |
| 90 | + } |
| 91 | + |
| 92 | + await Task.WhenAll( |
| 93 | + this.publishMetricsCommandExecutor.StartAsync(preferredDispatchConcurrency, cancellationToken)).ConfigureAwait(false); |
| 94 | + } |
| 95 | + |
| 96 | + public async Task StopAsync(CancellationToken cancellationToken = default) |
| 97 | + { |
| 98 | + await Task.WhenAll( |
| 99 | + this.publishMetricsCommandExecutor.StopAsync(cancellationToken)).ConfigureAwait(false); |
| 100 | + } |
| 101 | + |
| 102 | + private async Task<ExtendedResponse<PublishMetricsResponsePayload>> PublishMetricsInt(ExtendedRequest<PublishMetricsRequestPayload> req, CancellationToken cancellationToken) |
| 103 | + { |
| 104 | + ExtendedResponse<PublishMetricsResponsePayload> extended = await this.PublishMetricsAsync(req.Request!, req.RequestMetadata!, cancellationToken); |
| 105 | + return new ExtendedResponse<PublishMetricsResponsePayload> { Response = extended.Response, ResponseMetadata = extended.ResponseMetadata }; |
| 106 | + } |
| 107 | + |
| 108 | + public async ValueTask DisposeAsync() |
| 109 | + { |
| 110 | + await this.publishMetricsCommandExecutor.DisposeAsync().ConfigureAwait(false); |
| 111 | + } |
| 112 | + |
| 113 | + public async ValueTask DisposeAsync(bool disposing) |
| 114 | + { |
| 115 | + await this.publishMetricsCommandExecutor.DisposeAsync(disposing).ConfigureAwait(false); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + public abstract partial class Client : IAsyncDisposable |
| 120 | + { |
| 121 | + private ApplicationContext applicationContext; |
| 122 | + private IMqttPubSubClient mqttClient; |
| 123 | + private readonly PublishMetricsCommandInvoker publishMetricsCommandInvoker; |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// Construct a new instance of this client. |
| 127 | + /// </summary> |
| 128 | + /// <param name="applicationContext">The shared context for your application.</param> |
| 129 | + /// <param name="mqttClient">The MQTT client to use.</param> |
| 130 | + /// <param name="topicTokenMap"> |
| 131 | + /// The topic token replacement map to use for all operations by default. Generally, this will include the token values |
| 132 | + /// for topic tokens such as "modelId" which should be the same for the duration of this client's lifetime. Note that |
| 133 | + /// additional topic tokens can be specified when starting the client with <see cref="StartAsync(Dictionary{string, string}?, int?, CancellationToken)"/>. |
| 134 | + /// </param> |
| 135 | + public Client(ApplicationContext applicationContext, IMqttPubSubClient mqttClient, Dictionary<string, string>? topicTokenMap = null) |
| 136 | + { |
| 137 | + this.applicationContext = applicationContext; |
| 138 | + this.mqttClient = mqttClient; |
| 139 | + |
| 140 | + this.publishMetricsCommandInvoker = new PublishMetricsCommandInvoker(applicationContext, mqttClient); |
| 141 | + if (topicTokenMap != null) |
| 142 | + { |
| 143 | + foreach (string topicTokenKey in topicTokenMap.Keys) |
| 144 | + { |
| 145 | + this.publishMetricsCommandInvoker.TopicTokenMap.TryAdd("ex:" + topicTokenKey, topicTokenMap[topicTokenKey]); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + public PublishMetricsCommandInvoker PublishMetricsCommandInvoker { get => this.publishMetricsCommandInvoker; } |
| 151 | + |
| 152 | + /// <summary> |
| 153 | + /// Invoke a command. |
| 154 | + /// </summary> |
| 155 | + /// <param name="requestMetadata">The metadata for this command request.</param> |
| 156 | + /// <param name="additionalTopicTokenMap"> |
| 157 | + /// The topic token replacement map to use in addition to the topic tokens specified in the constructor. If this map |
| 158 | + /// contains any keys that the topic tokens specified in the constructor also has, then values specified in this map will take precedence. |
| 159 | + /// </param> |
| 160 | + /// <param name="commandTimeout">How long the command will be available on the broker for an executor to receive.</param> |
| 161 | + /// <param name="cancellationToken">Cancellation token.</param> |
| 162 | + /// <returns>The command response.</returns> |
| 163 | + public RpcCallAsync<PublishMetricsResponsePayload> PublishMetricsAsync(PublishMetricsRequestPayload request, CommandRequestMetadata? requestMetadata = null, Dictionary<string, string>? additionalTopicTokenMap = null, TimeSpan? commandTimeout = default, CancellationToken cancellationToken = default) |
| 164 | + { |
| 165 | + string? clientId = this.mqttClient.ClientId; |
| 166 | + if (string.IsNullOrEmpty(clientId)) |
| 167 | + { |
| 168 | + throw new InvalidOperationException("No MQTT client Id configured. Must connect to MQTT broker before invoking command."); |
| 169 | + } |
| 170 | + |
| 171 | + CommandRequestMetadata metadata = requestMetadata ?? new CommandRequestMetadata(); |
| 172 | + additionalTopicTokenMap ??= new(); |
| 173 | + |
| 174 | + Dictionary<string, string> prefixedAdditionalTopicTokenMap = new(); |
| 175 | + foreach (string key in additionalTopicTokenMap.Keys) |
| 176 | + { |
| 177 | + prefixedAdditionalTopicTokenMap["ex:" + key] = additionalTopicTokenMap[key]; |
| 178 | + } |
| 179 | + |
| 180 | + prefixedAdditionalTopicTokenMap["invokerClientId"] = clientId; |
| 181 | + |
| 182 | + return new RpcCallAsync<PublishMetricsResponsePayload>(this.publishMetricsCommandInvoker.InvokeCommandAsync(request, metadata, prefixedAdditionalTopicTokenMap, commandTimeout, cancellationToken), metadata.CorrelationId); |
| 183 | + } |
| 184 | + |
| 185 | + public async ValueTask DisposeAsync() |
| 186 | + { |
| 187 | + await this.publishMetricsCommandInvoker.DisposeAsync().ConfigureAwait(false); |
| 188 | + } |
| 189 | + |
| 190 | + public async ValueTask DisposeAsync(bool disposing) |
| 191 | + { |
| 192 | + await this.publishMetricsCommandInvoker.DisposeAsync(disposing).ConfigureAwait(false); |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments