| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Globalization; |
| | 6 | | using System.Linq; |
| | 7 | | using System.Threading; |
| | 8 | | using System.Threading.Tasks; |
| | 9 | | using Azure.Core; |
| | 10 | | using Azure.Iot.Hub.Service.Models; |
| | 11 | |
|
| | 12 | | namespace Azure.Iot.Hub.Service |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// Modules client to interact with device modules and module twins including CRUD operations and method invocations |
| | 16 | | /// See <see href="https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-portal-csharp-module-twin-getstarted"> Get |
| | 17 | | /// </summary> |
| | 18 | | public class ModulesClient |
| | 19 | | { |
| | 20 | | private const string ContinuationTokenHeader = "x-ms-continuation"; |
| | 21 | | private const string HubModuleQuery = "select * from devices.modules"; |
| | 22 | |
|
| | 23 | | private readonly DevicesRestClient _devicesRestClient; |
| | 24 | | private readonly ModulesRestClient _modulesRestClient; |
| | 25 | | private readonly QueryRestClient _queryRestClient; |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Initializes a new instance of ModulesClient. |
| | 29 | | /// </summary> |
| 190 | 30 | | protected ModulesClient() |
| | 31 | | { |
| 190 | 32 | | } |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Initializes a new instance of DevicesClient. |
| | 36 | | /// <param name="devicesRestClient"> The REST client to perform bulk operations on the module. </param> |
| | 37 | | /// <param name="modulesRestClient"> The REST client to perform module and module twin operations. </param> |
| | 38 | | /// <param name="queryRestClient"> The REST client to perform query operations for the device. </param> |
| | 39 | | /// </summary> |
| 76 | 40 | | internal ModulesClient(DevicesRestClient devicesRestClient, ModulesRestClient modulesRestClient, QueryRestClient |
| | 41 | | { |
| 76 | 42 | | Argument.AssertNotNull(devicesRestClient, nameof(devicesRestClient)); |
| 76 | 43 | | Argument.AssertNotNull(modulesRestClient, nameof(modulesRestClient)); |
| 76 | 44 | | Argument.AssertNotNull(queryRestClient, nameof(queryRestClient)); |
| | 45 | |
|
| 76 | 46 | | _devicesRestClient = devicesRestClient; |
| 76 | 47 | | _modulesRestClient = modulesRestClient; |
| 76 | 48 | | _queryRestClient = queryRestClient; |
| 76 | 49 | | } |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// Create a module identity. |
| | 53 | | /// </summary> |
| | 54 | | /// <param name="moduleIdentity">The module identity to create or update.</param> |
| | 55 | | /// <param name="precondition">The condition on which to perform this operation. |
| | 56 | | /// In case of create, the condition must be equal to <see cref="IfMatchPrecondition.IfMatch"/>. |
| | 57 | | /// In case of update, if no ETag is present on the device, then the condition must be equal to <see cref="IfMat |
| | 58 | | /// </param> |
| | 59 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 60 | | /// <returns>The created module identity and the http response <see cref="Response{T}"/>.</returns> |
| | 61 | | public virtual Task<Response<ModuleIdentity>> CreateOrUpdateIdentityAsync( |
| | 62 | | ModuleIdentity moduleIdentity, |
| | 63 | | IfMatchPrecondition precondition = IfMatchPrecondition.IfMatch, |
| | 64 | | CancellationToken cancellationToken = default) |
| | 65 | | { |
| 46 | 66 | | Argument.AssertNotNull(moduleIdentity, nameof(moduleIdentity)); |
| 46 | 67 | | string ifMatchHeaderValue = IfMatchPreconditionExtensions.GetIfMatchHeaderValue(precondition, moduleIdentity |
| 46 | 68 | | return _modulesRestClient.CreateOrUpdateIdentityAsync(moduleIdentity.DeviceId, moduleIdentity.ModuleId, modu |
| | 69 | | } |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// Create a module identity. |
| | 73 | | /// </summary> |
| | 74 | | /// <param name="moduleIdentity">The module identity to create or update.</param> |
| | 75 | | /// <param name="precondition">The condition on which to perform this operation. |
| | 76 | | /// In case of create, the condition must be equal to <see cref="IfMatchPrecondition.IfMatch"/>. |
| | 77 | | /// In case of update, if no ETag is present on the device, then the condition must be equal to <see cref="IfMat |
| | 78 | | /// </param> |
| | 79 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 80 | | /// <returns>The created module identity and the http response <see cref="Response{T}"/>.</returns> |
| | 81 | | public virtual Response<ModuleIdentity> CreateOrUpdateIdentity( |
| | 82 | | ModuleIdentity moduleIdentity, |
| | 83 | | IfMatchPrecondition precondition = IfMatchPrecondition.IfMatch, |
| | 84 | | CancellationToken cancellationToken = default) |
| | 85 | | { |
| 46 | 86 | | Argument.AssertNotNull(moduleIdentity, nameof(moduleIdentity)); |
| 46 | 87 | | string ifMatchHeaderValue = IfMatchPreconditionExtensions.GetIfMatchHeaderValue(precondition, moduleIdentity |
| 46 | 88 | | return _modulesRestClient.CreateOrUpdateIdentity(moduleIdentity.DeviceId, moduleIdentity.ModuleId, moduleIde |
| | 89 | | } |
| | 90 | |
|
| | 91 | | /// <summary> |
| | 92 | | /// Get a single module identity. |
| | 93 | | /// </summary> |
| | 94 | | /// <param name="deviceId">The unique identifier of the device identity.</param> |
| | 95 | | /// <param name="moduleId">The unique identifier of the module to get.</param> |
| | 96 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 97 | | /// <returns>The retrieved module identity and the http response <see cref="Response{T}"/>.</returns> |
| | 98 | | public virtual Task<Response<ModuleIdentity>> GetIdentityAsync(string deviceId, string moduleId, CancellationTok |
| | 99 | | { |
| 22 | 100 | | return _modulesRestClient.GetIdentityAsync(deviceId, moduleId, cancellationToken); |
| | 101 | | } |
| | 102 | |
|
| | 103 | | /// <summary> |
| | 104 | | /// Get a single module identity. |
| | 105 | | /// </summary> |
| | 106 | | /// <param name="deviceId">The unique identifier of the device identity.</param> |
| | 107 | | /// <param name="moduleId">The unique identifier of the module to get.</param> |
| | 108 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 109 | | /// <returns>The retrieved module identity and the http response <see cref="Response{T}"/>.</returns> |
| | 110 | | public virtual Response<ModuleIdentity> GetIdentity(string deviceId, string moduleId, CancellationToken cancella |
| | 111 | | { |
| 22 | 112 | | return _modulesRestClient.GetIdentity(deviceId, moduleId, cancellationToken); |
| | 113 | | } |
| | 114 | |
|
| | 115 | | /// <summary> |
| | 116 | | /// Get a set of module identities for a specific device. |
| | 117 | | /// </summary> |
| | 118 | | /// <param name="deviceId">The unique identifier of the device.</param> |
| | 119 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 120 | | /// <returns>A list of modules identities within a device and the http response <see cref="Response{T}"/>.</retu |
| | 121 | | public virtual Task<Response<IReadOnlyList<ModuleIdentity>>> GetIdentitiesAsync(string deviceId, CancellationTok |
| | 122 | | { |
| 2 | 123 | | Argument.AssertNotNullOrEmpty(deviceId, nameof(deviceId)); |
| 2 | 124 | | return _modulesRestClient.GetModulesOnDeviceAsync(deviceId, cancellationToken); |
| | 125 | | } |
| | 126 | |
|
| | 127 | | /// <summary> |
| | 128 | | /// Get a set of module identities for a specific device. |
| | 129 | | /// </summary> |
| | 130 | | /// <param name="deviceId">The unique identifier of the device.</param> |
| | 131 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 132 | | /// <returns>A list of modules identities within a device and the http response <see cref="Response{T}"/>.</retu |
| | 133 | | public virtual Response<IReadOnlyList<ModuleIdentity>> GetIdentities(string deviceId, CancellationToken cancella |
| | 134 | | { |
| 2 | 135 | | Argument.AssertNotNullOrEmpty(deviceId, nameof(deviceId)); |
| 2 | 136 | | return _modulesRestClient.GetModulesOnDevice(deviceId, cancellationToken); |
| | 137 | | } |
| | 138 | |
|
| | 139 | | /// <summary> |
| | 140 | | /// Delete a single module identity. |
| | 141 | | /// </summary> |
| | 142 | | /// <param name="moduleIdentity">The module identity to delete. If no ETag is present on the module identity, th |
| | 143 | | /// <param name="precondition">The condition on which to delete the module identity.</param> |
| | 144 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 145 | | /// <returns>The http response <see cref="Response{T}"/>.</returns> |
| | 146 | | public virtual Task<Response> DeleteIdentityAsync( |
| | 147 | | ModuleIdentity moduleIdentity, |
| | 148 | | IfMatchPrecondition precondition = IfMatchPrecondition.IfMatch, |
| | 149 | | CancellationToken cancellationToken = default) |
| | 150 | | { |
| 0 | 151 | | Argument.AssertNotNull(moduleIdentity, nameof(moduleIdentity)); |
| 0 | 152 | | string ifMatchHeaderValue = IfMatchPreconditionExtensions.GetIfMatchHeaderValue(precondition, moduleIdentity |
| 0 | 153 | | return _modulesRestClient.DeleteIdentityAsync(moduleIdentity.DeviceId, moduleIdentity.ModuleId, ifMatchHeade |
| | 154 | | } |
| | 155 | |
|
| | 156 | | /// <summary> |
| | 157 | | /// Delete a single module identity. |
| | 158 | | /// </summary> |
| | 159 | | /// <param name="moduleIdentity">The module identity to delete. If no ETag is present on the module identity, th |
| | 160 | | /// <param name="precondition">The condition on which to delete the module identity.</param> |
| | 161 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 162 | | /// <returns>The http response <see cref="Response{T}"/>.</returns> |
| | 163 | | public virtual Response DeleteIdentity( |
| | 164 | | ModuleIdentity moduleIdentity, |
| | 165 | | IfMatchPrecondition precondition = IfMatchPrecondition.IfMatch, |
| | 166 | | CancellationToken cancellationToken = default) |
| | 167 | | { |
| 0 | 168 | | Argument.AssertNotNull(moduleIdentity, nameof(moduleIdentity)); |
| 0 | 169 | | string ifMatchHeaderValue = IfMatchPreconditionExtensions.GetIfMatchHeaderValue(precondition, moduleIdentity |
| 0 | 170 | | return _modulesRestClient.DeleteIdentity(moduleIdentity.DeviceId, moduleIdentity.ModuleId, ifMatchHeaderValu |
| | 171 | | } |
| | 172 | |
|
| | 173 | | /// <summary> |
| | 174 | | /// Create multiple modules with an initial twin. A maximum of 100 creations can be done per call, |
| | 175 | | /// and each creation must have a unique module identity. Multiple modules may be created on a single device. |
| | 176 | | /// All devices that these new modules will belong to must already exist. |
| | 177 | | /// For larger scale operations, consider using <see href="https://docs.microsoft.com/en-us/azure/iot-hub/iot-hu |
| | 178 | | /// </summary> |
| | 179 | | /// <param name="modules">The pairs of modules and their twins that will be created. For fields such as deviceId |
| | 180 | | /// where device and twin have a definition, the device value will override the twin value.</param> |
| | 181 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 182 | | /// <returns>The result of the bulk operation and the http response <see cref="Response{T}"/>.</returns> |
| | 183 | | public virtual Task<Response<BulkRegistryOperationResponse>> CreateIdentitiesWithTwinAsync(IDictionary<ModuleIde |
| | 184 | | { |
| 2 | 185 | | IEnumerable<ExportImportDevice> registryOperations = modules |
| 22 | 186 | | .Select(x => new ExportImportDevice() |
| 22 | 187 | | { |
| 22 | 188 | | Id = x.Key.DeviceId, |
| 22 | 189 | | ModuleId = x.Key.ModuleId, |
| 22 | 190 | | Authentication = x.Key.Authentication, |
| 22 | 191 | | ImportMode = ExportImportDeviceImportMode.Create |
| 22 | 192 | | }.WithTags(x.Value.Tags).WithPropertiesFrom(x.Value.Properties)); |
| | 193 | |
|
| 2 | 194 | | return _devicesRestClient.BulkRegistryOperationsAsync(registryOperations, cancellationToken); |
| | 195 | | } |
| | 196 | |
|
| | 197 | | /// <summary> |
| | 198 | | /// Create multiple modules with an initial twin. A maximum of 100 creations can be done per call, |
| | 199 | | /// and each creation must have a unique module identity. Multiple modules may be created on a single device. |
| | 200 | | /// All devices that these new modules will belong to must already exist. |
| | 201 | | /// For larger scale operations, consider using <see href="https://docs.microsoft.com/en-us/azure/iot-hub/iot-hu |
| | 202 | | /// </summary> |
| | 203 | | /// <param name="modules">The pairs of modules and their twins that will be created. For fields such as deviceId |
| | 204 | | /// where device and twin have a definition, the device value will override the twin value.</param> |
| | 205 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 206 | | /// <returns>The result of the bulk operation and the http response <see cref="Response{T}"/>.</returns> |
| | 207 | | public virtual Response<BulkRegistryOperationResponse> CreateIdentitiesWithTwin(IDictionary<ModuleIdentity, Twin |
| | 208 | | { |
| 2 | 209 | | IEnumerable<ExportImportDevice> registryOperations = modules |
| 22 | 210 | | .Select(x => new ExportImportDevice() |
| 22 | 211 | | { |
| 22 | 212 | | Id = x.Key.DeviceId, |
| 22 | 213 | | ModuleId = x.Key.ModuleId, |
| 22 | 214 | | Authentication = x.Key.Authentication, |
| 22 | 215 | | ImportMode = ExportImportDeviceImportMode.Create |
| 22 | 216 | | }.WithTags(x.Value.Tags).WithPropertiesFrom(x.Value.Properties)); |
| | 217 | |
|
| 2 | 218 | | return _devicesRestClient.BulkRegistryOperations(registryOperations, cancellationToken); |
| | 219 | | } |
| | 220 | |
|
| | 221 | | /// <summary> |
| | 222 | | /// Create multiple modules. A maximum of 100 creations can be done per call, and each module identity must be u |
| | 223 | | /// All devices that these modules will belong to must already exist. Multiple modules can be created at a time |
| | 224 | | /// For larger scale operations, consider using <see href="https://docs.microsoft.com/en-us/azure/iot-hub/iot-hu |
| | 225 | | /// </summary> |
| | 226 | | /// <param name="moduleIdentities">The module identities to create.</param> |
| | 227 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 228 | | /// <returns>The result of the bulk operation and the http response <see cref="Response{T}"/>.</returns> |
| | 229 | | public virtual Task<Response<BulkRegistryOperationResponse>> CreateIdentitiesAsync(IEnumerable<ModuleIdentity> m |
| | 230 | | { |
| 6 | 231 | | IEnumerable<ExportImportDevice> registryOperations = moduleIdentities |
| 66 | 232 | | .Select(x => new ExportImportDevice() |
| 66 | 233 | | { |
| 66 | 234 | | Id = x.DeviceId, |
| 66 | 235 | | ModuleId = x.ModuleId, |
| 66 | 236 | | Authentication = x.Authentication, |
| 66 | 237 | | ImportMode = ExportImportDeviceImportMode.Create |
| 66 | 238 | | }); |
| | 239 | |
|
| 6 | 240 | | return _devicesRestClient.BulkRegistryOperationsAsync(registryOperations, cancellationToken); |
| | 241 | | } |
| | 242 | |
|
| | 243 | | /// <summary> |
| | 244 | | /// Create multiple modules. A maximum of 100 creations can be done per call, and each module identity must be u |
| | 245 | | /// All devices that these modules will belong to must already exist. Multiple modules can be created at a time |
| | 246 | | /// For larger scale operations, consider using <see href="https://docs.microsoft.com/en-us/azure/iot-hub/iot-hu |
| | 247 | | /// </summary> |
| | 248 | | /// <param name="moduleIdentities">The module identities to create.</param> |
| | 249 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 250 | | /// <returns>The result of the bulk operation and the http response <see cref="Response{T}"/>.</returns> |
| | 251 | | public virtual Response<BulkRegistryOperationResponse> CreateIdentities(IEnumerable<ModuleIdentity> moduleIdenti |
| | 252 | | { |
| 6 | 253 | | IEnumerable<ExportImportDevice> registryOperations = moduleIdentities |
| 66 | 254 | | .Select(x => new ExportImportDevice() |
| 66 | 255 | | { |
| 66 | 256 | | Id = x.DeviceId, |
| 66 | 257 | | ModuleId = x.ModuleId, |
| 66 | 258 | | Authentication = x.Authentication, |
| 66 | 259 | | ImportMode = ExportImportDeviceImportMode.Create |
| 66 | 260 | | }); |
| | 261 | |
|
| 6 | 262 | | return _devicesRestClient.BulkRegistryOperations(registryOperations, cancellationToken); |
| | 263 | | } |
| | 264 | |
|
| | 265 | | /// <summary> |
| | 266 | | /// Update multiple modules. A maximum of 100 updates can be done per call, and each operation must be done on a |
| | 267 | | /// </summary> |
| | 268 | | /// <param name="moduleIdentities">The modules to update.</param> |
| | 269 | | /// <param name="precondition">The condition on which to update each module identity.</param> |
| | 270 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 271 | | /// <returns>The result of the bulk operation and the http response <see cref="Response{T}"/>.</returns> |
| | 272 | | public virtual Task<Response<BulkRegistryOperationResponse>> UpdateIdentitiesAsync( |
| | 273 | | IEnumerable<ModuleIdentity> moduleIdentities, |
| | 274 | | BulkIfMatchPrecondition precondition = BulkIfMatchPrecondition.IfMatch, |
| | 275 | | CancellationToken cancellationToken = default) |
| | 276 | | { |
| 2 | 277 | | IEnumerable<ExportImportDevice> registryOperations = moduleIdentities |
| 22 | 278 | | .Select(x => new ExportImportDevice() |
| 22 | 279 | | { |
| 22 | 280 | | Id = x.DeviceId, |
| 22 | 281 | | ModuleId = x.ModuleId, |
| 22 | 282 | | Authentication = x.Authentication, |
| 22 | 283 | | ImportMode = precondition == BulkIfMatchPrecondition.Unconditional ? ExportImportDeviceImportMode.Up |
| 22 | 284 | | }); |
| | 285 | |
|
| 2 | 286 | | return _devicesRestClient.BulkRegistryOperationsAsync(registryOperations, cancellationToken); |
| | 287 | | } |
| | 288 | |
|
| | 289 | | /// <summary> |
| | 290 | | /// Update multiple modules. A maximum of 100 updates can be done per call, and each operation must be done on a |
| | 291 | | /// </summary> |
| | 292 | | /// <param name="moduleIdentities">The modules to update.</param> |
| | 293 | | /// <param name="precondition">The condition on which to update each module identity.</param> |
| | 294 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 295 | | /// <returns>The result of the bulk operation and the http response <see cref="Response{T}"/>.</returns> |
| | 296 | | public virtual Response<BulkRegistryOperationResponse> UpdateIdentities( |
| | 297 | | IEnumerable<ModuleIdentity> moduleIdentities, |
| | 298 | | BulkIfMatchPrecondition precondition = BulkIfMatchPrecondition.IfMatch, |
| | 299 | | CancellationToken cancellationToken = default) |
| | 300 | | { |
| 2 | 301 | | IEnumerable<ExportImportDevice> registryOperations = moduleIdentities |
| 22 | 302 | | .Select(x => new ExportImportDevice() |
| 22 | 303 | | { |
| 22 | 304 | | Id = x.DeviceId, |
| 22 | 305 | | ModuleId = x.ModuleId, |
| 22 | 306 | | Authentication = x.Authentication, |
| 22 | 307 | | ImportMode = precondition == BulkIfMatchPrecondition.Unconditional ? ExportImportDeviceImportMode.Up |
| 22 | 308 | | }); |
| | 309 | |
|
| 2 | 310 | | return _devicesRestClient.BulkRegistryOperations(registryOperations, cancellationToken); |
| | 311 | | } |
| | 312 | |
|
| | 313 | | /// <summary> |
| | 314 | | /// Delete multiple modules. A maximum of 100 deletions can be done per call. For larger scale operations, consi |
| | 315 | | /// </summary> |
| | 316 | | /// <param name="moduleIdentities">The modules to delete.</param> |
| | 317 | | /// <param name="precondition">The condition on which to delete each device identity.</param> |
| | 318 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 319 | | /// <returns>The result of the bulk deletion and the http response <see cref="Response{T}"/>.</returns> |
| | 320 | | public virtual Task<Response<BulkRegistryOperationResponse>> DeleteIdentitiesAsync( |
| | 321 | | IEnumerable<ModuleIdentity> moduleIdentities, |
| | 322 | | BulkIfMatchPrecondition precondition = BulkIfMatchPrecondition.IfMatch, |
| | 323 | | CancellationToken cancellationToken = default) |
| | 324 | | { |
| 0 | 325 | | IEnumerable<ExportImportDevice> registryOperations = moduleIdentities |
| 0 | 326 | | .Select(x => new ExportImportDevice() |
| 0 | 327 | | { |
| 0 | 328 | | Id = x.DeviceId, |
| 0 | 329 | | ModuleId = x.ModuleId, |
| 0 | 330 | | ETag = x.Etag, |
| 0 | 331 | | ImportMode = precondition == BulkIfMatchPrecondition.Unconditional |
| 0 | 332 | | ? ExportImportDeviceImportMode.Delete |
| 0 | 333 | | : ExportImportDeviceImportMode.DeleteIfMatchETag |
| 0 | 334 | | }); |
| | 335 | |
|
| 0 | 336 | | return _devicesRestClient.BulkRegistryOperationsAsync(registryOperations, cancellationToken); |
| | 337 | | } |
| | 338 | |
|
| | 339 | | /// <summary> |
| | 340 | | /// Delete multiple modules. A maximum of 100 deletions can be done per call. For larger scale operations, consi |
| | 341 | | /// </summary> |
| | 342 | | /// <param name="moduleIdentities">The devices to delete.</param> |
| | 343 | | /// <param name="precondition">The condition on which to delete each device identity.</param> |
| | 344 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 345 | | /// <returns>The result of the bulk deletion and the http response <see cref="Response{T}"/>.</returns> |
| | 346 | | public virtual Response<BulkRegistryOperationResponse> DeleteIdentities( |
| | 347 | | IEnumerable<ModuleIdentity> moduleIdentities, |
| | 348 | | BulkIfMatchPrecondition precondition = BulkIfMatchPrecondition.IfMatch, |
| | 349 | | CancellationToken cancellationToken = default) |
| | 350 | | { |
| 0 | 351 | | IEnumerable<ExportImportDevice> registryOperations = moduleIdentities |
| 0 | 352 | | .Select(x => new ExportImportDevice() |
| 0 | 353 | | { |
| 0 | 354 | | Id = x.DeviceId, |
| 0 | 355 | | ModuleId = x.ModuleId, |
| 0 | 356 | | ETag = x.Etag, |
| 0 | 357 | | ImportMode = precondition == BulkIfMatchPrecondition.Unconditional |
| 0 | 358 | | ? ExportImportDeviceImportMode.Delete |
| 0 | 359 | | : ExportImportDeviceImportMode.DeleteIfMatchETag |
| 0 | 360 | | }); |
| | 361 | |
|
| 0 | 362 | | return _devicesRestClient.BulkRegistryOperations(registryOperations, cancellationToken); |
| | 363 | | } |
| | 364 | |
|
| | 365 | | /// <summary> |
| | 366 | | /// List a set of module twins. |
| | 367 | | /// </summary> |
| | 368 | | /// <param name="pageSize">The size of each page to be retrieved from the service. Service may override this siz |
| | 369 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 370 | | /// <returns>A pageable set of module twins <see cref="AsyncPageable{T}"/>.</returns> |
| | 371 | | public virtual AsyncPageable<TwinData> GetTwinsAsync(int? pageSize = null, CancellationToken cancellationToken = |
| | 372 | | { |
| | 373 | | async Task<Page<TwinData>> FirstPageFunc(int? pageSizeHint) |
| | 374 | | { |
| 2 | 375 | | var querySpecification = new QuerySpecification |
| 2 | 376 | | { |
| 2 | 377 | | Query = HubModuleQuery |
| 2 | 378 | | }; |
| | 379 | |
|
| 2 | 380 | | Response<IReadOnlyList<TwinData>> response = |
| 2 | 381 | | await _queryRestClient.GetTwinsAsync(querySpecification, null, pageSizeHint?.ToString(CultureInfo.In |
| | 382 | |
|
| 2 | 383 | | response.GetRawResponse().Headers.TryGetValue(ContinuationTokenHeader, out string continuationToken); |
| | 384 | |
|
| 2 | 385 | | return Page.FromValues(response.Value, continuationToken, response.GetRawResponse()); |
| 2 | 386 | | } |
| | 387 | |
|
| | 388 | | async Task<Page<TwinData>> NextPageFunc(string nextLink, int? pageSizeHint) |
| | 389 | | { |
| 0 | 390 | | var querySpecification = new QuerySpecification(); |
| | 391 | |
|
| 0 | 392 | | Response<IReadOnlyList<TwinData>> response = |
| 0 | 393 | | await _queryRestClient.GetTwinsAsync(querySpecification, nextLink, pageSizeHint?.ToString(CultureInf |
| | 394 | |
|
| 0 | 395 | | response.GetRawResponse().Headers.TryGetValue(ContinuationTokenHeader, out string continuationToken); |
| 0 | 396 | | return Page.FromValues(response.Value, continuationToken, response.GetRawResponse()); |
| 0 | 397 | | } |
| | 398 | |
|
| 2 | 399 | | return PageableHelpers.CreateAsyncEnumerable(FirstPageFunc, NextPageFunc, pageSize); |
| | 400 | | } |
| | 401 | |
|
| | 402 | | /// <summary> |
| | 403 | | /// List a set of module twins. |
| | 404 | | /// </summary> |
| | 405 | | /// <param name="pageSize">The size of each page to be retrieved from the service. Service may override this siz |
| | 406 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 407 | | /// <returns>A pageable set of module twins <see cref="Pageable{T}"/>.</returns> |
| | 408 | | public virtual Pageable<TwinData> GetTwins(int? pageSize = null, CancellationToken cancellationToken = default) |
| | 409 | | { |
| | 410 | | Page<TwinData> FirstPageFunc(int? pageSizeHint) |
| | 411 | | { |
| 4 | 412 | | var querySpecification = new QuerySpecification |
| 4 | 413 | | { |
| 4 | 414 | | Query = HubModuleQuery |
| 4 | 415 | | }; |
| | 416 | |
|
| 4 | 417 | | Response<IReadOnlyList<TwinData>> response = _queryRestClient.GetTwins( |
| 4 | 418 | | querySpecification, |
| 4 | 419 | | null, |
| 4 | 420 | | pageSizeHint?.ToString(CultureInfo.InvariantCulture), |
| 4 | 421 | | cancellationToken); |
| | 422 | |
|
| 4 | 423 | | response.GetRawResponse().Headers.TryGetValue(ContinuationTokenHeader, out string continuationToken); |
| | 424 | |
|
| 4 | 425 | | return Page.FromValues(response.Value, continuationToken, response.GetRawResponse()); |
| | 426 | | } |
| | 427 | |
|
| | 428 | | Page<TwinData> NextPageFunc(string nextLink, int? pageSizeHint) |
| | 429 | | { |
| 0 | 430 | | var querySpecification = new QuerySpecification(); |
| 0 | 431 | | Response<IReadOnlyList<TwinData>> response = _queryRestClient.GetTwins( |
| 0 | 432 | | querySpecification, |
| 0 | 433 | | nextLink, |
| 0 | 434 | | pageSizeHint?.ToString(CultureInfo.InvariantCulture), |
| 0 | 435 | | cancellationToken); |
| | 436 | |
|
| 0 | 437 | | response.GetRawResponse().Headers.TryGetValue(ContinuationTokenHeader, out string continuationToken); |
| 0 | 438 | | return Page.FromValues(response.Value, continuationToken, response.GetRawResponse()); |
| | 439 | | } |
| | 440 | |
|
| 4 | 441 | | return PageableHelpers.CreateEnumerable(FirstPageFunc, NextPageFunc, pageSize); |
| | 442 | | } |
| | 443 | |
|
| | 444 | | /// <summary> |
| | 445 | | /// Get a module's twin. |
| | 446 | | /// </summary> |
| | 447 | | /// <param name="deviceId">The unique identifier of the device identity.</param> |
| | 448 | | /// <param name="moduleId">The unique identifier of the module identity.</param> |
| | 449 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 450 | | /// <returns>The module's twin, including reported properties and desired properties and the http response <see |
| | 451 | | public virtual Task<Response<TwinData>> GetTwinAsync(string deviceId, string moduleId, CancellationToken cancell |
| | 452 | | { |
| 6 | 453 | | return _modulesRestClient.GetTwinAsync(deviceId, moduleId, cancellationToken); |
| | 454 | | } |
| | 455 | |
|
| | 456 | | /// <summary> |
| | 457 | | /// Get a module's twin. |
| | 458 | | /// </summary> |
| | 459 | | /// <param name="deviceId">The unique identifier of the device identity.</param> |
| | 460 | | /// <param name="moduleId">The unique identifier of the module identity.</param> |
| | 461 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 462 | | /// <returns>The module's twin, including reported properties and desired properties and the http response <see |
| | 463 | | public virtual Response<TwinData> GetTwin(string deviceId, string moduleId, CancellationToken cancellationToken |
| | 464 | | { |
| 6 | 465 | | return _modulesRestClient.GetTwin(deviceId, moduleId, cancellationToken); |
| | 466 | | } |
| | 467 | |
|
| | 468 | | /// <summary> |
| | 469 | | /// Update a module's twin. |
| | 470 | | /// </summary> |
| | 471 | | /// <param name="twinUpdate">The properties to update. Any existing properties not referenced by this patch will |
| | 472 | | /// <param name="precondition">The condition for which this operation will execute.</param> |
| | 473 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 474 | | /// <returns>The new representation of the module's twin and the http response <see cref="Response{T}"/>.</retur |
| | 475 | | public virtual Task<Response<TwinData>> UpdateTwinAsync( |
| | 476 | | TwinData twinUpdate, |
| | 477 | | IfMatchPrecondition precondition = IfMatchPrecondition.IfMatch, |
| | 478 | | CancellationToken cancellationToken = default) |
| | 479 | | { |
| 6 | 480 | | Argument.AssertNotNull(twinUpdate, nameof(twinUpdate)); |
| 6 | 481 | | string ifMatchHeaderValue = IfMatchPreconditionExtensions.GetIfMatchHeaderValue(precondition, twinUpdate.Eta |
| 6 | 482 | | return _modulesRestClient.UpdateTwinAsync(twinUpdate.DeviceId, twinUpdate.ModuleId, twinUpdate, ifMatchHeade |
| | 483 | | } |
| | 484 | |
|
| | 485 | | /// <summary> |
| | 486 | | /// Update a module's twin. |
| | 487 | | /// </summary> |
| | 488 | | /// <param name="twinUpdate">The properties to update. Any existing properties not referenced by this patch will |
| | 489 | | /// <param name="precondition">The condition for which this operation will execute.</param> |
| | 490 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 491 | | /// <returns>The new representation of the module's twin and the http response <see cref="Response{T}"/>.</retur |
| | 492 | | public virtual Response<TwinData> UpdateTwin(TwinData twinUpdate, IfMatchPrecondition precondition = IfMatchPrec |
| | 493 | | { |
| 6 | 494 | | Argument.AssertNotNull(twinUpdate, nameof(twinUpdate)); |
| 6 | 495 | | string ifMatchHeaderValue = IfMatchPreconditionExtensions.GetIfMatchHeaderValue(precondition, twinUpdate.Eta |
| 6 | 496 | | return _modulesRestClient.UpdateTwin(twinUpdate.DeviceId, twinUpdate.ModuleId, twinUpdate, ifMatchHeaderValu |
| | 497 | | } |
| | 498 | |
|
| | 499 | | /// <summary> |
| | 500 | | /// Update multiple modules' twins. A maximum of 100 updates can be done per call, and each operation must be do |
| | 501 | | /// </summary> |
| | 502 | | /// <param name="twinUpdates">The new twins to replace the twins on existing devices.</param> |
| | 503 | | /// <param name="precondition">The condition on which to update each device twin.</param> |
| | 504 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 505 | | /// <returns>The result of the bulk operation and the http response <see cref="Response{T}"/>.</returns> |
| | 506 | | public virtual Task<Response<BulkRegistryOperationResponse>> UpdateTwinsAsync( |
| | 507 | | IEnumerable<TwinData> twinUpdates, |
| | 508 | | BulkIfMatchPrecondition precondition = BulkIfMatchPrecondition.IfMatch, |
| | 509 | | CancellationToken cancellationToken = default) |
| | 510 | | { |
| 0 | 511 | | IEnumerable<ExportImportDevice> registryOperations = twinUpdates |
| 0 | 512 | | .Select(x => new ExportImportDevice() |
| 0 | 513 | | { |
| 0 | 514 | | Id = x.DeviceId, |
| 0 | 515 | | ModuleId = x.ModuleId, |
| 0 | 516 | | TwinETag = x.Etag, |
| 0 | 517 | | ImportMode = precondition == BulkIfMatchPrecondition.Unconditional ? ExportImportDeviceImportMode.Up |
| 0 | 518 | | }.WithTags(x.Tags).WithPropertiesFrom(x.Properties)); |
| | 519 | |
|
| 0 | 520 | | return _devicesRestClient.BulkRegistryOperationsAsync(registryOperations, cancellationToken); |
| | 521 | | } |
| | 522 | |
|
| | 523 | | /// <summary> |
| | 524 | | /// Update multiple modules' twins. A maximum of 100 updates can be done per call, and each operation must be do |
| | 525 | | /// </summary> |
| | 526 | | /// <param name="twinUpdates">The new twins to replace the twins on existing devices.</param> |
| | 527 | | /// <param name="precondition">The condition on which to update each device twin.</param> |
| | 528 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 529 | | /// <returns>The result of the bulk operation and the http response <see cref="Response{T}"/>.</returns> |
| | 530 | | public virtual Response<BulkRegistryOperationResponse> UpdateTwins( |
| | 531 | | IEnumerable<TwinData> twinUpdates, |
| | 532 | | BulkIfMatchPrecondition precondition = BulkIfMatchPrecondition.IfMatch, |
| | 533 | | CancellationToken cancellationToken = default) |
| | 534 | | { |
| 0 | 535 | | IEnumerable<ExportImportDevice> registryOperations = twinUpdates |
| 0 | 536 | | .Select(x => new ExportImportDevice() |
| 0 | 537 | | { |
| 0 | 538 | | Id = x.DeviceId, |
| 0 | 539 | | ModuleId = x.ModuleId, |
| 0 | 540 | | TwinETag = x.Etag, |
| 0 | 541 | | ImportMode = precondition == BulkIfMatchPrecondition.Unconditional |
| 0 | 542 | | ? ExportImportDeviceImportMode.UpdateTwin |
| 0 | 543 | | : ExportImportDeviceImportMode.UpdateTwinIfMatchETag |
| 0 | 544 | | }.WithTags(x.Tags).WithPropertiesFrom(x.Properties)); |
| | 545 | |
|
| 0 | 546 | | return _devicesRestClient.BulkRegistryOperations(registryOperations, cancellationToken); |
| | 547 | | } |
| | 548 | |
|
| | 549 | | /// <summary> |
| | 550 | | /// Invoke a method on a module. |
| | 551 | | /// </summary> |
| | 552 | | /// <param name="deviceId">The unique identifier of the device.</param> |
| | 553 | | /// <param name="moduleId">The unique identifier of the module identity to invoke the method on.</param> |
| | 554 | | /// <param name="directMethodRequest">The details of the method to invoke.</param> |
| | 555 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 556 | | /// <returns>The result of the method invocation and the http response <see cref="Response{T}"/>.</returns> |
| | 557 | | public virtual Task<Response<CloudToDeviceMethodResponse>> InvokeMethodAsync( |
| | 558 | | string deviceId, |
| | 559 | | string moduleId, |
| | 560 | | CloudToDeviceMethodRequest directMethodRequest, |
| | 561 | | CancellationToken cancellationToken = default) |
| | 562 | | { |
| 0 | 563 | | return _modulesRestClient.InvokeMethodAsync(deviceId, moduleId, directMethodRequest, cancellationToken); |
| | 564 | | } |
| | 565 | |
|
| | 566 | | /// <summary> |
| | 567 | | /// Invoke a method on a module. |
| | 568 | | /// </summary> |
| | 569 | | /// <param name="deviceId">The unique identifier of the device.</param> |
| | 570 | | /// <param name="moduleId">The unique identifier of the module identity to invoke the method on.</param> |
| | 571 | | /// <param name="directMethodRequest">The details of the method to invoke.</param> |
| | 572 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | 573 | | /// <returns>The result of the method invocation and the http response <see cref="Response{T}"/>.</returns> |
| | 574 | | public virtual Response<CloudToDeviceMethodResponse> InvokeMethod( |
| | 575 | | string deviceId, |
| | 576 | | string moduleId, |
| | 577 | | CloudToDeviceMethodRequest directMethodRequest, |
| | 578 | | CancellationToken cancellationToken = default) |
| | 579 | | { |
| 0 | 580 | | return _modulesRestClient.InvokeMethod(deviceId, moduleId, directMethodRequest, cancellationToken); |
| | 581 | | } |
| | 582 | | } |
| | 583 | | } |