Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Initial Module Identity Samples commit
  • Loading branch information
azabbasi committed Jun 17, 2020
commit db0c78b93f1ff57d15a94800c224958277d72ae0
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,196 @@
using System;
using System.Runtime.InteropServices;
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 of 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 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");

// Construt 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");

// Construt 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 InvokeMethodAsync()
{
return Task.CompletedTask;
}

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

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

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

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

public Task GetModuleIdentityAsync()
{
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,7 +12,7 @@ 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

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 PrintUnsuccessfulEvent(string message)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"\n{message}");
Console.ResetColor();
}
}
}