-
Notifications
You must be signed in to change notification settings - Fork 5.1k
feat(samples): Twin configurations sample #14683
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
189 changes: 189 additions & 0 deletions
189
sdk/iot/Azure.Iot.Hub.Service/samples/IotHubClientSamples/ConfigurationSamples.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Threading.Tasks; | ||
| using Azure.Iot.Hub.Service.Models; | ||
|
|
||
| namespace Azure.Iot.Hub.Service.Samples | ||
| { | ||
| /// <summary> | ||
| /// This sample goes through the usage of Configuration on the IoT Hub. | ||
| /// </summary> | ||
| internal class ConfigurationSamples | ||
| { | ||
| public readonly IotHubServiceClient IoTHubServiceClient; | ||
| public const int MaxRandomValue = 200; | ||
| public static readonly Random Random = new Random(); | ||
|
|
||
| public ConfigurationSamples(IotHubServiceClient client) | ||
| { | ||
| IoTHubServiceClient = client; | ||
| } | ||
|
|
||
| public async Task RunSampleAsync() | ||
| { | ||
| string testConfigurationId = $"configuration{Random.Next(MaxRandomValue)}"; | ||
| TwinConfiguration twinConfiguration = CreateSampleConfig(testConfigurationId); | ||
|
|
||
| // Create a Twin Configuration. | ||
| await CreateConfiguratioAsync(twinConfiguration); | ||
bikamani marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Get the Twin Configuration. | ||
| await GetConfiguratioAsync(twinConfiguration); | ||
|
|
||
| // Update the Twin Configuration. | ||
| await UpdateConfiguratioAsync(twinConfiguration); | ||
|
|
||
| // Delete the Twin Configuration. | ||
| await DeleteConfiguratioAsync(twinConfiguration); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates a new twin configuration. | ||
| /// </summary> | ||
| /// <param name="twinConfiguration">Twin Configuration to create.</param> | ||
| public async Task<TwinConfiguration> CreateConfiguratioAsync(TwinConfiguration twinConfiguration) | ||
| { | ||
| SampleLogger.PrintHeader("CREATE TWIN CONFIGURATION"); | ||
| TwinConfiguration createdConfig; | ||
|
|
||
| try | ||
| { | ||
| // Create a twin configuration | ||
| #region Snippet:IotHubCreateConfiguration | ||
| Response<TwinConfiguration> createResponse = | ||
bikamani marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| await IoTHubServiceClient.Configurations.CreateOrUpdateConfigurationAsync(twinConfiguration).ConfigureAwait(false); | ||
| createdConfig = createResponse.Value; | ||
|
|
||
| Console.WriteLine($"Successfully created a new configuration with Id: '{createdConfig.Id}', ETag: '{createdConfig.Etag}'"); | ||
|
|
||
| #endregion Snippet:IotHubCreateConfiguration | ||
|
|
||
| return createdConfig; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| // Try to cleanup before exiting with fatal error. | ||
| await DeleteConfiguratioAsync(twinConfiguration); | ||
| SampleLogger.FatalError($"Failed to create twin configuration due to:\n{ex}"); | ||
| throw; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Get a twin configuration. | ||
| /// </summary> | ||
| /// <param name="twinConfiguration">Twin Configuration</param> | ||
| public async Task<TwinConfiguration> GetConfiguratioAsync(TwinConfiguration twinConfiguration) | ||
| { | ||
| SampleLogger.PrintHeader("GET A CONFIGURATION"); | ||
|
|
||
| try | ||
| { | ||
| Console.WriteLine($"Getting twin configuration with Id: '{twinConfiguration.Id}'\n"); | ||
|
|
||
| #region Snippet:IotHubGetConfiguration | ||
|
|
||
| Response<TwinConfiguration> getResponse = await IoTHubServiceClient.Configurations.GetConfigurationAsync(twinConfiguration.Id).ConfigureAwait(false); | ||
|
|
||
| TwinConfiguration responseConfiguration = getResponse.Value; | ||
|
|
||
| SampleLogger.PrintSuccess($"\t- Configuration Id: '{responseConfiguration.Id}', ETag: '{responseConfiguration.Etag}'"); | ||
|
|
||
| #endregion Snippet:IotHubGetConfiguration | ||
|
|
||
| return responseConfiguration; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| // Try to cleanup before exiting with fatal error. | ||
| await DeleteConfiguratioAsync(twinConfiguration); | ||
| SampleLogger.FatalError($"Failed to get a twin configuration due to:\n{ex}"); | ||
| throw; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Update a twin configuration. | ||
| /// </summary> | ||
| /// <param name="twinConfiguration">Twin Configuration to to be updated.</param> | ||
| public async Task<TwinConfiguration> UpdateConfiguratioAsync(TwinConfiguration twinConfiguration) | ||
| { | ||
| SampleLogger.PrintHeader("UPDATE A CONFIGURATION"); | ||
|
|
||
| try | ||
| { | ||
| #region Snippet:IotHubUpdateConfiguration | ||
|
|
||
| twinConfiguration.Priority = Random.Next(MaxRandomValue); | ||
| Console.WriteLine($"Updating twin configuration with Id: '{twinConfiguration.Id}''s priority to: '{twinConfiguration.Priority}'"); | ||
| Response < TwinConfiguration > response = await IoTHubServiceClient.Configurations.CreateOrUpdateConfigurationAsync(twinConfiguration, IfMatchPrecondition.UnconditionalIfMatch).ConfigureAwait(false); | ||
bikamani marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| TwinConfiguration updatedConfiguration = response.Value; | ||
|
|
||
| SampleLogger.PrintSuccess($"Successfully updated twin configuration: Id: '{updatedConfiguration.Id}', Priority: '{updatedConfiguration.Priority}', ETag: '{updatedConfiguration.Etag}'"); | ||
|
|
||
| #endregion Snippet:IotHubUpdateConfiguration | ||
|
|
||
| return updatedConfiguration; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| // Try to cleanup before exiting with fatal error. | ||
| await DeleteConfiguratioAsync(twinConfiguration); | ||
| SampleLogger.FatalError($"Failed to update a twin configuration due to:\n{ex}"); | ||
| throw; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Deletes a twin configuration. | ||
| /// </summary> | ||
| /// <param name="twinConfiguration">Twin Configuration to deletec.</param> | ||
bikamani marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public async Task DeleteConfiguratioAsync(TwinConfiguration twinConfiguration) | ||
| { | ||
| SampleLogger.PrintHeader("DELETE A CONFIGURATION"); | ||
|
|
||
| try | ||
| { | ||
|
|
||
bikamani marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Console.WriteLine($"Deleting twin configuration with Id: '{twinConfiguration.Id}'"); | ||
|
|
||
| #region Snippet:IotHubDeleteConfiguration | ||
|
|
||
| Response response = await IoTHubServiceClient.Configurations.DeleteConfigurationAsync(twinConfiguration, IfMatchPrecondition.UnconditionalIfMatch).ConfigureAwait(false); | ||
|
|
||
| SampleLogger.PrintSuccess($"Successfully deleted twin configuration with Id: '{twinConfiguration.Id}'"); | ||
|
|
||
| #endregion Snippet:IotHubDeleteConfiguration | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| SampleLogger.FatalError($"Failed to delete twin configuration due to:\n{ex}"); | ||
| } | ||
| } | ||
|
|
||
| private TwinConfiguration CreateSampleConfig(string testConfigurationId) | ||
| { | ||
| var twinConfiguration = | ||
bikamani marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| new TwinConfiguration | ||
| { | ||
| Id = testConfigurationId | ||
| }; | ||
|
|
||
| // Labels are optional but adding here due to null check failure in deserialization | ||
| // Also note that we are not setting Host Platform value from Environment since that'll fail in our build pipeline | ||
| twinConfiguration.Labels.Add("HostPlatform", "SomeValue"); | ||
bikamani marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| twinConfiguration.Content = new ConfigurationContent(); | ||
| twinConfiguration.Content.DeviceContent.Add("properties.desired.deviceContent_key", $"deviceContent_value-{twinConfiguration.Id}"); | ||
|
|
||
| // Specifying '*' to target all devices | ||
| twinConfiguration.TargetCondition = "*"; | ||
|
|
||
| // Assign any integer value for priority | ||
| twinConfiguration.Priority = Random.Next(MaxRandomValue); | ||
| return twinConfiguration; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
currently adding sample for CRUD, we will modify and add edge config and test queries when we have implementation for the same.