Skip to content

Commit

Permalink
feat(samples): Twin configurations sample (#14683)
Browse files Browse the repository at this point in the history
  • Loading branch information
bikamani authored Aug 28, 2020
1 parent 3c69a12 commit 39cf3fa
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Azure.Iot.Hub.Service.Samples
/// bulk APIs that allow for creating, updating, and deleting of up to 100 device identities at a time.
/// There are also bulk APIs for updating twins on up to 100 devices at time.
/// </summary>
internal class BulkDeviceIdentityLifecycleSamples
internal class BulkDeviceIdentityLifecycleSample
{
public readonly IotHubServiceClient IoTHubServiceClient;
public const int MaxRandomValue = 200;
Expand All @@ -23,7 +23,7 @@ internal class BulkDeviceIdentityLifecycleSamples
// Can be 1 to 100, configures how many devices to create/update/delete per service API call.
public const int BulkCount = 20;

public BulkDeviceIdentityLifecycleSamples(IotHubServiceClient client)
public BulkDeviceIdentityLifecycleSample(IotHubServiceClient client)
{
IoTHubServiceClient = client;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Azure.Iot.Hub.Service.Samples
/// bulk APIs that allow for creating, updating, and deleting of up to 100 module identities at a time.
/// There are also bulk APIs for updating twins on up to 100 modules at time.
/// </summary>
internal class BulkModuleIdentityLifecycleSamples
internal class BulkModuleIdentityLifecycleSample
{
public readonly IotHubServiceClient IoTHubServiceClient;
public const int MaxRandomValue = 200;
Expand All @@ -22,7 +22,7 @@ internal class BulkModuleIdentityLifecycleSamples
// Can be 1 to 100, configures how many modules to create/update/delete per service API call.
public const int BulkCount = 20;

public BulkModuleIdentityLifecycleSamples(IotHubServiceClient client)
public BulkModuleIdentityLifecycleSample(IotHubServiceClient client)
{
IoTHubServiceClient = client;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
// 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 ConfigurationSample
{
public readonly IotHubServiceClient IoTHubServiceClient;
public const int MaxRandomValue = 200;
public static readonly Random Random = new Random();

public ConfigurationSample(IotHubServiceClient client)
{
IoTHubServiceClient = client;
}

public async Task RunSampleAsync()
{
string testConfigurationId = $"configuration{Random.Next(MaxRandomValue)}";
TwinConfiguration twinConfiguration = CreateSampleConfig(testConfigurationId);

// Create a Twin Configuration.
await CreateConfigurationAsync(twinConfiguration);

// Get the Twin Configuration.
await GetConfigurationAsync(twinConfiguration);

// Update the Twin Configuration.
await UpdateConfigurationAsync(twinConfiguration);

// Delete the Twin Configuration.
await DeleteConfigurationAsync(twinConfiguration);
}

/// <summary>
/// Creates a new twin configuration.
/// </summary>
/// <param name="twinConfiguration">Twin Configuration to create.</param>
public async Task<TwinConfiguration> CreateConfigurationAsync(TwinConfiguration twinConfiguration)
{
SampleLogger.PrintHeader("CREATE TWIN CONFIGURATION");
TwinConfiguration createdConfig;

try
{
// Create a twin configuration
#region Snippet:IotHubCreateConfiguration
Response<TwinConfiguration> createResponse = 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 DeleteConfigurationAsync(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> GetConfigurationAsync(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($"Configuration Id: '{responseConfiguration.Id}', ETag: '{responseConfiguration.Etag}'");

#endregion Snippet:IotHubGetConfiguration

return responseConfiguration;
}
catch (Exception ex)
{
// Try to cleanup before exiting with fatal error.
await DeleteConfigurationAsync(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> UpdateConfigurationAsync(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);

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 DeleteConfigurationAsync(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 delete.</param>
public async Task DeleteConfigurationAsync(TwinConfiguration twinConfiguration)
{
SampleLogger.PrintHeader("DELETE A CONFIGURATION");

try
{
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 = new TwinConfiguration
{
Id = testConfigurationId,
Labels =
{
{ "HostPlatform", "SomeValue" },
},
TargetCondition = "*",
Priority = Random.Next(MaxRandomValue),
};

twinConfiguration.Content = new ConfigurationContent();
twinConfiguration.Content.DeviceContent.Add("properties.desired.deviceContent_key", $"deviceContent_value-{twinConfiguration.Id}");

return twinConfiguration;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ namespace Azure.Iot.Hub.Service.Samples
/// <summary>
/// This sample goes through the lifecycle of a Device Identity for a device.
/// </summary>
internal class DeviceIdentityLifecycleSamples
internal class DeviceIdentityLifecycleSample
{
public readonly IotHubServiceClient IoTHubServiceClient;
public const int MaxRandomValue = 200;
public static readonly Random Random = new Random();

public DeviceIdentityLifecycleSamples(IotHubServiceClient client)
public DeviceIdentityLifecycleSample(IotHubServiceClient client)
{
IoTHubServiceClient = client;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ namespace Azure.Iot.Hub.Service.Samples
/// <summary>
/// This sample shows how to start the import and export jobs and check their status.
/// </summary>
internal class JobsSamples
internal class JobsSample
{
public readonly IotHubServiceClient IoTHubServiceClient;
public const int MaxRandomValue = 200;
public static readonly Random Random = new Random();
public readonly Uri ContainerSasUri;

public JobsSamples(IotHubServiceClient client, Uri containerSasUri)
public JobsSample(IotHubServiceClient client, Uri containerSasUri)
{
IoTHubServiceClient = client;
ContainerSasUri = containerSasUri;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ namespace Azure.Iot.Hub.Service.Samples
/// <summary>
/// This sample goes through the lifecycle of a Module Identity for a device.
/// </summary>
internal class ModuleIdentityLifecycleSamples
internal class ModuleIdentityLifecycleSample
{
public readonly IotHubServiceClient IoTHubServiceClient;
public const int MaxRandomValue = 200;
public static readonly Random Random = new Random();

public ModuleIdentityLifecycleSamples(IotHubServiceClient client)
public ModuleIdentityLifecycleSample(IotHubServiceClient client)
{
IoTHubServiceClient = client;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,31 @@ public static async Task Main(string[] args)

// Run the samples

var deviceIdentityLifecycleSamples = new DeviceIdentityLifecycleSamples(hubClient);
var deviceIdentityLifecycleSamples = new DeviceIdentityLifecycleSample(hubClient);
await deviceIdentityLifecycleSamples.RunSampleAsync();

var moduleIdentityLifecycleSamples = new ModuleIdentityLifecycleSamples(hubClient);
var moduleIdentityLifecycleSamples = new ModuleIdentityLifecycleSample(hubClient);
await moduleIdentityLifecycleSamples.RunSampleAsync();

var bulkDeviceIdentityLifecycleSamples = new BulkDeviceIdentityLifecycleSamples(hubClient);
var bulkDeviceIdentityLifecycleSamples = new BulkDeviceIdentityLifecycleSample(hubClient);
await bulkDeviceIdentityLifecycleSamples.RunSampleAsync();

var bulkModuledentityLifecycleSamples = new BulkModuleIdentityLifecycleSamples(hubClient);
var bulkModuledentityLifecycleSamples = new BulkModuleIdentityLifecycleSample(hubClient);
await bulkModuledentityLifecycleSamples.RunSampleAsync();

var querySamples = new QueryTwinSamples(hubClient);
var querySamples = new QueryTwinSample(hubClient);
await querySamples.RunSampleAsync();

var statisticsSample = new StatisticsSamples(hubClient);
var statisticsSample = new StatisticsSample(hubClient);
await statisticsSample.RunSampleAsync();

var configurationsSample = new ConfigurationSample(hubClient);
await configurationsSample.RunSampleAsync();

// Get SAS token to 'jobs' container in the storage account.
Uri containerSasUri = await GetSasUriAsync(options.StorageAccountConnectionString, "jobs").ConfigureAwait(false);

var jobsSample = new JobsSamples(hubClient, containerSasUri);
var jobsSample = new JobsSample(hubClient, containerSasUri);
await jobsSample.RunSampleAsync();

// Run samples that require the device sample to be running.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ namespace Azure.Iot.Hub.Service.Samples
/// <summary>
/// This sample shows how to query the device twins to get data.
/// </summary>
internal class QueryTwinSamples
internal class QueryTwinSample
{
public readonly IotHubServiceClient IoTHubServiceClient;
public const int MaxRandomValue = 200;
public static readonly Random Random = new Random();

public QueryTwinSamples(IotHubServiceClient client)
public QueryTwinSample(IotHubServiceClient client)
{
IoTHubServiceClient = client;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ namespace Azure.Iot.Hub.Service.Samples
/// <summary>
/// This sample goes through the usage of statistics on the IoT Hub.
/// </summary>
internal class StatisticsSamples
internal class StatisticsSample
{

public readonly IotHubServiceClient IoTHubServiceClient;

public StatisticsSamples(IotHubServiceClient client)
public StatisticsSample(IotHubServiceClient client)
{
IoTHubServiceClient = client;
}
Expand Down

0 comments on commit 39cf3fa

Please sign in to comment.