/// <summary>
        /// Deletes a twin configuration on the IoT Hub for automatic device/module management
        /// </summary>
        /// <param name="configuration">Twin configuration to delete</param>
        /// <param name="precondition">The condition on which to perform this operation. If no ETag is present on the twin configuration, then the condition must be equal to <see cref="IfMatchPrecondition.UnconditionalIfMatch"/>."/>.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The http response <see cref="Response{T}"/>.</returns>
        public virtual Response DeleteConfiguration(TwinConfiguration configuration, IfMatchPrecondition precondition = IfMatchPrecondition.IfMatch, CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNull(configuration, nameof(configuration));
            string ifMatchHeaderValue = IfMatchPreconditionExtensions.GetIfMatchHeaderValue(precondition, configuration.Etag);

            return(_configurationRestClient.Delete(configuration.Id, ifMatchHeaderValue, cancellationToken));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Update a module's twin.
        /// </summary>
        /// <param name="twinUpdate">The properties to update. Any existing properties not referenced by this patch will be unaffected by this patch.</param>
        /// <param name="precondition">The condition for which this operation will execute.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The new representation of the module's twin and the http response <see cref="Response{T}"/>.</returns>
        public virtual Response <TwinData> UpdateTwin(TwinData twinUpdate, IfMatchPrecondition precondition = IfMatchPrecondition.IfMatch, CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNull(twinUpdate, nameof(twinUpdate));
            string ifMatchHeaderValue = IfMatchPreconditionExtensions.GetIfMatchHeaderValue(precondition, twinUpdate.Etag);

            return(_modulesRestClient.UpdateTwin(twinUpdate.DeviceId, twinUpdate.ModuleId, twinUpdate, ifMatchHeaderValue, cancellationToken));
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Get the ifMatch header value for an HTTP request that targets a particular service resource.
 /// </summary>
 /// <param name="precondition">The user supplied if match precondition.</param>
 /// <param name="ETag">The ETag of the resource that the HTTP request is targeting.</param>
 /// <returns>The ifMatch header value.</returns>
 internal static string GetIfMatchHeaderValue(IfMatchPrecondition precondition, string ETag)
 {
     return(precondition switch
     {
         IfMatchPrecondition.IfMatch => $"\"{ETag}\"",
         IfMatchPrecondition.UnconditionalIfMatch => "*",
         _ => null,
     });
Ejemplo n.º 4
0
        /// <summary>
        /// Create a module identity.
        /// </summary>
        /// <param name="moduleIdentity">The module identity to create or update.</param>
        /// <param name="precondition">The condition on which to perform this operation.
        /// In case of create, the condition must be equal to <see cref="IfMatchPrecondition.IfMatch"/>.
        /// In case of update, if no ETag is present on the device, then the condition must be equal to <see cref="IfMatchPrecondition.UnconditionalIfMatch"/>.
        /// </param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The created module identity and the http response <see cref="Response{T}"/>.</returns>
        public virtual Response <ModuleIdentity> CreateOrUpdateIdentity(
            ModuleIdentity moduleIdentity,
            IfMatchPrecondition precondition    = IfMatchPrecondition.IfMatch,
            CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNull(moduleIdentity, nameof(moduleIdentity));
            string ifMatchHeaderValue = IfMatchPreconditionExtensions.GetIfMatchHeaderValue(precondition, moduleIdentity.Etag);

            return(_modulesRestClient.CreateOrUpdateIdentity(moduleIdentity.DeviceId, moduleIdentity.ModuleId, moduleIdentity, ifMatchHeaderValue, cancellationToken));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Create or update a device identity.
        /// </summary>
        /// <param name="deviceIdentity">the device identity to create or update.</param>
        /// <param name="precondition">The condition on which to perform this operation.
        /// In case of create, the condition must be equal to <see cref="IfMatchPrecondition.IfMatch"/>.
        /// In case of update, if no ETag is present on the device, then the condition must be equal to <see cref="IfMatchPrecondition.UnconditionalIfMatch"/>.
        /// </param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The created device identity and the http response <see cref="Response{T}"/>.</returns>
        /// <code snippet="Snippet:IotHubCreateDeviceIdentity">
        /// Response&lt;DeviceIdentity&gt; response = await IoTHubServiceClient.Devices.CreateOrUpdateIdentityAsync(deviceIdentity);
        ///
        /// SampleLogger.PrintSuccess($&quot;Successfully create a new device identity with Id: &apos;{response.Value.DeviceId}&apos;, ETag: &apos;{response.Value.Etag}&apos;&quot;);
        /// </code>
        /// <code snippet="Snippet:IotHubUpdateDeviceIdentity">
        /// Response&lt;DeviceIdentity&gt; getResponse = await IoTHubServiceClient.Devices.GetIdentityAsync(deviceId);
        ///
        /// DeviceIdentity deviceIdentity = getResponse.Value;
        /// Console.WriteLine($&quot;Current device identity: DeviceId: &apos;{deviceIdentity.DeviceId}&apos;, Status: &apos;{deviceIdentity.Status}&apos;, ETag: &apos;{deviceIdentity.Etag}&apos;&quot;);
        ///
        /// Console.WriteLine($&quot;Updating device identity with Id: &apos;{deviceIdentity.DeviceId}&apos;. Disabling device so it cannot connect to IoT Hub.&quot;);
        /// deviceIdentity.Status = DeviceStatus.Disabled;
        ///
        /// Response&lt;DeviceIdentity&gt; response = await IoTHubServiceClient.Devices.CreateOrUpdateIdentityAsync(deviceIdentity);
        ///
        /// DeviceIdentity updatedDevice = response.Value;
        ///
        /// SampleLogger.PrintSuccess($&quot;Successfully updated device identity: DeviceId: &apos;{updatedDevice.DeviceId}&apos;, DeviceId: &apos;{updatedDevice.DeviceId}&apos;, Status: &apos;{updatedDevice.Status}&apos;, ETag: &apos;{updatedDevice.Etag}&apos;&quot;);
        /// </code>
        public virtual Task <Response <DeviceIdentity> > CreateOrUpdateIdentityAsync(
            DeviceIdentity deviceIdentity,
            IfMatchPrecondition precondition    = IfMatchPrecondition.IfMatch,
            CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNull(deviceIdentity, nameof(deviceIdentity));
            string ifMatchHeaderValue = IfMatchPreconditionExtensions.GetIfMatchHeaderValue(precondition, deviceIdentity.Etag);

            return(_devicesRestClient.CreateOrUpdateIdentityAsync(deviceIdentity.DeviceId, deviceIdentity, ifMatchHeaderValue, cancellationToken));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Delete a single module identity.
        /// </summary>
        /// <param name="moduleIdentity">The module identity to delete. If no ETag is present on the module identity, then the condition must be equal to <see cref="IfMatchPrecondition.UnconditionalIfMatch"/>.</param>
        /// <param name="precondition">The condition on which to delete the module identity.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The http response <see cref="Response{T}"/>.</returns>
        public virtual Response DeleteIdentity(
            ModuleIdentity moduleIdentity,
            IfMatchPrecondition precondition    = IfMatchPrecondition.IfMatch,
            CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNull(moduleIdentity, nameof(moduleIdentity));
            string ifMatchHeaderValue = IfMatchPreconditionExtensions.GetIfMatchHeaderValue(precondition, moduleIdentity.Etag);

            return(_registryManagerClient.DeleteModule(moduleIdentity.DeviceId, moduleIdentity.ModuleId, ifMatchHeaderValue, cancellationToken));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Delete a single device identity.
        /// </summary>
        /// <param name="deviceIdentity">the device identity to delete. If no ETag is present on the device, then the condition must be equal to <see cref="IfMatchPrecondition.UnconditionalIfMatch"/>."/>.</param>
        /// <param name="precondition">The condition on which to delete the device.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The http response <see cref="Response{T}"/>.</returns>
        public virtual Task <Response> DeleteIdentityAsync(
            DeviceIdentity deviceIdentity,
            IfMatchPrecondition precondition    = IfMatchPrecondition.IfMatch,
            CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNull(deviceIdentity, nameof(deviceIdentity));
            string ifMatchHeaderValue = IfMatchPreconditionExtensions.GetIfMatchHeaderValue(precondition, deviceIdentity.Etag);

            return(_registryManagerClient.DeleteDeviceAsync(deviceIdentity.DeviceId, ifMatchHeaderValue, cancellationToken));
        }
        /// <summary>
        /// Create or update a twin configuration on the IoT Hub for automatic device/module management
        /// </summary>
        /// <param name="configuration">Twin configuration to update</param>
        /// <param name="precondition">The condition on which to perform this operation</param>
        /// In case of create, the condition must be equal to <see cref="IfMatchPrecondition.IfMatch"/>.
        /// In case of update, if no ETag is present on the twin configuration, then the condition must be equal to <see cref="IfMatchPrecondition.UnconditionalIfMatch"/>.
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The created automatic device/module management twin configuration</returns>
        public virtual Task <Response <TwinConfiguration> > CreateOrUpdateConfigurationAsync(TwinConfiguration configuration, IfMatchPrecondition precondition = IfMatchPrecondition.IfMatch, CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNull(configuration, nameof(configuration));
            string ifMatchHeaderValue = IfMatchPreconditionExtensions.GetIfMatchHeaderValue(precondition, configuration.Etag);

            return(_configurationRestClient.CreateOrUpdateAsync(configuration.Id, configuration, ifMatchHeaderValue, cancellationToken));
        }