Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/iot/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
!*common.config.json
!*common.test.assets.config.json
*.etl
*launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
using System;
using System.Threading.Tasks;
using Azure.Iot.Hub.Service.Models;

namespace Azure.Iot.Hub.Service.Samples
{
/// <summary>
/// This sample goes through the lifecycle of a Module Identity for a device
/// </summary>
internal class ModuleIdentityLifecycleSamples
{
public readonly IoTHubServiceClient IoTHubServiceClient;
public const int MaxRandomValue = 200;
public readonly Random Random = new Random();

public ModuleIdentityLifecycleSamples(IoTHubServiceClient client)
{
IoTHubServiceClient = client;
}

public async Task RunSampleAsync()
{
string deviceId = $"device{Random.Next(MaxRandomValue)}";
string moduleId = $"module{Random.Next(MaxRandomValue)}";

// Create a DeviceIdentity.
DeviceIdentity deviceIdentity = await CreateDeviceIdentityAsync(deviceId);

// Create a ModuleIdentity.
ModuleIdentity moduleIdentity = await CreateModuleIdentityAsync(deviceIdentity, moduleId);

// List all modules within the device.
await ListAllModulesAsync();

// Get the Module Identity.
await GetModuleIdentityAsync();

// Update Module Identity.
await UpdateModuleIdentityAsync();

// Get Module Twin,
await GetModuleTwinAsync();

// Update Module Twin.
await UpdateModuleTwinAsync();

// Invoke a method on the ModuleTwin
await InvokeMethodAsync();

// Delete the module identity
await DeleteModuleIdentityAsync(moduleIdentity);

// Delete the device (cleanup)
await DeleteDeviceIdentityAsync(deviceIdentity);
}

/// <summary>
/// Creates a new device identity
/// </summary>
/// <param name="deviceId">Device Id for the device identity</param>
public async Task<DeviceIdentity> CreateDeviceIdentityAsync(string deviceId)
{
SampleLogger.PrintHeader("CREATE DEVICE IDENTITY");

// Construct the device identity object.
DeviceIdentity deviceIdentity = new DeviceIdentity
{
DeviceId = deviceId
};

try
{
Console.WriteLine($"Creating a new device with Id '{deviceId}'");

// Call APIs to create the device identity.
Response<DeviceIdentity> response = await IoTHubServiceClient.Devices.CreateOrUpdateIdentityAsync(deviceIdentity);

SampleLogger.PrintSuccessfulEvent($"Successfully create a new device identity with Id: '{deviceId}' - Status Code: {response.GetRawResponse().Status}");

return response.Value;
}
catch (Exception ex)
{
SampleLogger.FatalError($"Failed to create device identity due to:\n{ex}");
throw;
}
}

/// <summary>
/// Creates a new module identity
/// </summary>
/// <param name="deviceIdentity">Device identity</param>
/// <param name="moduleId">Module Id for the module identity</param>
public async Task<ModuleIdentity> CreateModuleIdentityAsync(DeviceIdentity deviceIdentity, string moduleId)
{
SampleLogger.PrintHeader("CREATE MODULE IDENTITY");

// Construct the module identity object.
ModuleIdentity moduleIdentity = new ModuleIdentity
{
DeviceId = deviceIdentity.DeviceId,
ModuleId = moduleId
};

try
{
Console.WriteLine($"Creating a new module with Id: '{moduleId}'");

// Call APIs to create the module identity.
Response<ModuleIdentity> response = await IoTHubServiceClient.Modules.CreateOrUpdateIdentityAsync(moduleIdentity);

SampleLogger.PrintSuccessfulEvent($"Successfully create a new module identity with Id: '{moduleId}' - Status Code: {response.GetRawResponse().Status}");

return response.Value;
}
catch (Exception ex)
{
SampleLogger.FatalError($"Failed to create module identity due to:\n{ex}");
throw;
}
}

public Task ListAllModulesAsync()
{
return Task.CompletedTask;
}

public Task GetModuleIdentityAsync()
{
return Task.CompletedTask;
}

public Task UpdateModuleIdentityAsync()
{
return Task.CompletedTask;
}

public Task GetModuleTwinAsync()
{
return Task.CompletedTask;
}

public Task UpdateModuleTwinAsync()
{
return Task.CompletedTask;
}

public Task InvokeMethodAsync()
{
return Task.CompletedTask;
}

public async Task DeleteModuleIdentityAsync(ModuleIdentity moduleIdentity)
{
SampleLogger.PrintHeader("DELETE MODULE IDENTITY");

try
{
Console.WriteLine($"Deleting module identity with Id: '{moduleIdentity.ModuleId}'");

// Call APIs to delete the module identity.
Response response = await IoTHubServiceClient.Modules.DeleteIdentityAsync(moduleIdentity, IfMatchPrecondition.UnconditionalIfMatch);

SampleLogger.PrintSuccessfulEvent($"Successfully deleted module identity with Id: '{moduleIdentity.ModuleId}' - Status Code: {response.Status}");
}
catch (Exception ex)
{
SampleLogger.FatalError($"Failed to delete module identity due to:\n{ex}");
}
}

/// <summary>
/// Deletes a device identity
/// </summary>
/// <param name="deviceId">Device identity to delete</param>
public async Task DeleteDeviceIdentityAsync(DeviceIdentity deviceIdentity)
{
SampleLogger.PrintHeader("DELETE DEVICE IDENTITY");

try
{
Console.WriteLine($"Deleting device identity with Id: '{deviceIdentity.DeviceId}'");

// Call APIs to delete the device identity.
Response response = await IoTHubServiceClient.Devices.DeleteIdentityAsync(deviceIdentity, IfMatchPrecondition.UnconditionalIfMatch);

SampleLogger.PrintSuccessfulEvent($"Successfully deleted device identity with Id: '{deviceIdentity.DeviceId}' - Status Code: {response.Status}");
}
catch (Exception ex)
{
SampleLogger.FatalError($"Failed to device identity due to:\n{ex}");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System;
using System.Threading.Tasks;
using CommandLine;

namespace Azure.Iot.Hub.Service.Samples
Expand All @@ -11,9 +12,9 @@ public class Program
/// <summary>
/// Main entry point to the sample.
/// </summary>
public static void Main(string[] args)
public static async Task Main(string[] args)
{
// Parse and validate paramters
// Parse and validate parameters

CommandLineOptions options = null;
ParserResult<CommandLineOptions> result = Parser.Default.ParseArguments<CommandLineOptions>(args)
Expand All @@ -25,6 +26,13 @@ public static void Main(string[] args)
{
Environment.Exit(1);
});

// Instantiate the client
IoTHubServiceClient hubClient = new IoTHubServiceClient(options.IotHubConnectionString);

// Run the samples
var moduleIdentityLifecycleSamples = new ModuleIdentityLifecycleSamples(hubClient);
await moduleIdentityLifecycleSamples.RunSampleAsync();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Azure.Iot.Hub.Service.Samples
{
internal static class SampleLogger
{
internal static void PrintHeader(string message)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.BackgroundColor = ConsoleColor.Black;
Console.WriteLine($"\n\n==={message.ToUpperInvariant()}===\n");
Console.ResetColor();
}

internal static void FatalError(string message)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(message);
Console.ResetColor();
Environment.Exit(0);
}

internal static void PrintSuccessfulEvent(string message)
{
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine($"\n{message}");
Console.ResetColor();
}

internal static void PrintWarning(string message)
{
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine($"\n{message}");
Console.ResetColor();
}
}
}