|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Azure.Iot.Hub.Service.Models; |
| 7 | + |
| 8 | +namespace Azure.Iot.Hub.Service.Samples |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// This sample goes through the usage of Configuration on the IoT Hub. |
| 12 | + /// </summary> |
| 13 | + internal class ConfigurationSample |
| 14 | + { |
| 15 | + public readonly IotHubServiceClient IoTHubServiceClient; |
| 16 | + public const int MaxRandomValue = 200; |
| 17 | + public static readonly Random Random = new Random(); |
| 18 | + |
| 19 | + public ConfigurationSample(IotHubServiceClient client) |
| 20 | + { |
| 21 | + IoTHubServiceClient = client; |
| 22 | + } |
| 23 | + |
| 24 | + public async Task RunSampleAsync() |
| 25 | + { |
| 26 | + string testConfigurationId = $"configuration{Random.Next(MaxRandomValue)}"; |
| 27 | + TwinConfiguration twinConfiguration = CreateSampleConfig(testConfigurationId); |
| 28 | + |
| 29 | + // Create a Twin Configuration. |
| 30 | + await CreateConfigurationAsync(twinConfiguration); |
| 31 | + |
| 32 | + // Get the Twin Configuration. |
| 33 | + await GetConfigurationAsync(twinConfiguration); |
| 34 | + |
| 35 | + // Update the Twin Configuration. |
| 36 | + await UpdateConfigurationAsync(twinConfiguration); |
| 37 | + |
| 38 | + // Delete the Twin Configuration. |
| 39 | + await DeleteConfigurationAsync(twinConfiguration); |
| 40 | + } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Creates a new twin configuration. |
| 44 | + /// </summary> |
| 45 | + /// <param name="twinConfiguration">Twin Configuration to create.</param> |
| 46 | + public async Task<TwinConfiguration> CreateConfigurationAsync(TwinConfiguration twinConfiguration) |
| 47 | + { |
| 48 | + SampleLogger.PrintHeader("CREATE TWIN CONFIGURATION"); |
| 49 | + TwinConfiguration createdConfig; |
| 50 | + |
| 51 | + try |
| 52 | + { |
| 53 | + // Create a twin configuration |
| 54 | + #region Snippet:IotHubCreateConfiguration |
| 55 | + Response<TwinConfiguration> createResponse = await IoTHubServiceClient.Configurations |
| 56 | + .CreateOrUpdateConfigurationAsync(twinConfiguration) |
| 57 | + .ConfigureAwait(false); |
| 58 | + createdConfig = createResponse.Value; |
| 59 | + |
| 60 | + Console.WriteLine($"Successfully created a new configuration with Id: '{createdConfig.Id}', ETag: '{createdConfig.Etag}'"); |
| 61 | + |
| 62 | + #endregion Snippet:IotHubCreateConfiguration |
| 63 | + |
| 64 | + return createdConfig; |
| 65 | + } |
| 66 | + catch (Exception ex) |
| 67 | + { |
| 68 | + // Try to cleanup before exiting with fatal error. |
| 69 | + await DeleteConfigurationAsync(twinConfiguration); |
| 70 | + SampleLogger.FatalError($"Failed to create twin configuration due to:\n{ex}"); |
| 71 | + throw; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Get a twin configuration. |
| 77 | + /// </summary> |
| 78 | + /// <param name="twinConfiguration">Twin Configuration</param> |
| 79 | + public async Task<TwinConfiguration> GetConfigurationAsync(TwinConfiguration twinConfiguration) |
| 80 | + { |
| 81 | + SampleLogger.PrintHeader("GET A CONFIGURATION"); |
| 82 | + |
| 83 | + try |
| 84 | + { |
| 85 | + Console.WriteLine($"Getting twin configuration with Id: '{twinConfiguration.Id}'\n"); |
| 86 | + |
| 87 | + #region Snippet:IotHubGetConfiguration |
| 88 | + |
| 89 | + Response<TwinConfiguration> getResponse = await IoTHubServiceClient.Configurations |
| 90 | + .GetConfigurationAsync(twinConfiguration.Id) |
| 91 | + .ConfigureAwait(false); |
| 92 | + |
| 93 | + TwinConfiguration responseConfiguration = getResponse.Value; |
| 94 | + |
| 95 | + SampleLogger.PrintSuccess($"Configuration Id: '{responseConfiguration.Id}', ETag: '{responseConfiguration.Etag}'"); |
| 96 | + |
| 97 | + #endregion Snippet:IotHubGetConfiguration |
| 98 | + |
| 99 | + return responseConfiguration; |
| 100 | + } |
| 101 | + catch (Exception ex) |
| 102 | + { |
| 103 | + // Try to cleanup before exiting with fatal error. |
| 104 | + await DeleteConfigurationAsync(twinConfiguration); |
| 105 | + SampleLogger.FatalError($"Failed to get a twin configuration due to:\n{ex}"); |
| 106 | + throw; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Update a twin configuration. |
| 112 | + /// </summary> |
| 113 | + /// <param name="twinConfiguration">Twin Configuration to to be updated.</param> |
| 114 | + public async Task<TwinConfiguration> UpdateConfigurationAsync(TwinConfiguration twinConfiguration) |
| 115 | + { |
| 116 | + SampleLogger.PrintHeader("UPDATE A CONFIGURATION"); |
| 117 | + |
| 118 | + try |
| 119 | + { |
| 120 | + #region Snippet:IotHubUpdateConfiguration |
| 121 | + |
| 122 | + twinConfiguration.Priority = Random.Next(MaxRandomValue); |
| 123 | + Console.WriteLine($"Updating twin configuration with Id: '{twinConfiguration.Id}''s priority to: '{twinConfiguration.Priority}'"); |
| 124 | + Response <TwinConfiguration> response = await IoTHubServiceClient.Configurations |
| 125 | + .CreateOrUpdateConfigurationAsync(twinConfiguration, IfMatchPrecondition.UnconditionalIfMatch) |
| 126 | + .ConfigureAwait(false); |
| 127 | + |
| 128 | + TwinConfiguration updatedConfiguration = response.Value; |
| 129 | + |
| 130 | + SampleLogger.PrintSuccess($"Successfully updated twin configuration: Id: '{updatedConfiguration.Id}', Priority: '{updatedConfiguration.Priority}', ETag: '{updatedConfiguration.Etag}'"); |
| 131 | + |
| 132 | + #endregion Snippet:IotHubUpdateConfiguration |
| 133 | + |
| 134 | + return updatedConfiguration; |
| 135 | + } |
| 136 | + catch (Exception ex) |
| 137 | + { |
| 138 | + // Try to cleanup before exiting with fatal error. |
| 139 | + await DeleteConfigurationAsync(twinConfiguration); |
| 140 | + SampleLogger.FatalError($"Failed to update a twin configuration due to:\n{ex}"); |
| 141 | + throw; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + /// <summary> |
| 146 | + /// Deletes a twin configuration. |
| 147 | + /// </summary> |
| 148 | + /// <param name="twinConfiguration">Twin Configuration to delete.</param> |
| 149 | + public async Task DeleteConfigurationAsync(TwinConfiguration twinConfiguration) |
| 150 | + { |
| 151 | + SampleLogger.PrintHeader("DELETE A CONFIGURATION"); |
| 152 | + |
| 153 | + try |
| 154 | + { |
| 155 | + Console.WriteLine($"Deleting twin configuration with Id: '{twinConfiguration.Id}'"); |
| 156 | + |
| 157 | + #region Snippet:IotHubDeleteConfiguration |
| 158 | + |
| 159 | + Response response = await IoTHubServiceClient.Configurations |
| 160 | + .DeleteConfigurationAsync(twinConfiguration, IfMatchPrecondition.UnconditionalIfMatch) |
| 161 | + .ConfigureAwait(false); |
| 162 | + |
| 163 | + SampleLogger.PrintSuccess($"Successfully deleted twin configuration with Id: '{twinConfiguration.Id}'"); |
| 164 | + |
| 165 | + #endregion Snippet:IotHubDeleteConfiguration |
| 166 | + } |
| 167 | + catch (Exception ex) |
| 168 | + { |
| 169 | + SampleLogger.FatalError($"Failed to delete twin configuration due to:\n{ex}"); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + private TwinConfiguration CreateSampleConfig(string testConfigurationId) |
| 174 | + { |
| 175 | + var twinConfiguration = new TwinConfiguration |
| 176 | + { |
| 177 | + Id = testConfigurationId, |
| 178 | + Labels = |
| 179 | + { |
| 180 | + { "HostPlatform", "SomeValue" }, |
| 181 | + }, |
| 182 | + TargetCondition = "*", |
| 183 | + Priority = Random.Next(MaxRandomValue), |
| 184 | + }; |
| 185 | + |
| 186 | + twinConfiguration.Content = new ConfigurationContent(); |
| 187 | + twinConfiguration.Content.DeviceContent.Add("properties.desired.deviceContent_key", $"deviceContent_value-{twinConfiguration.Id}"); |
| 188 | + |
| 189 | + return twinConfiguration; |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments