-
Notifications
You must be signed in to change notification settings - Fork 5.1k
feat(samples): Initial CREATE/DELETE sample for ModuleI #12850
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
azabbasi
merged 7 commits into
feature/IoT-Hub
from
feature/iot/azabbasi/modulesSamples
Jun 18, 2020
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
db0c78b
Initial Module Identity Samples commit
azabbasi c374b08
Spell check
azabbasi b72cb8d
Update ModuleIdentityLifecycleSamples.cs
azabbasi 03474fc
Format changes.
azabbasi e5e8b56
Update repo root gitignore to exclude launchSettings.json
azabbasi e7ce37b
Remove status from the output logs.
azabbasi 2d00cec
Address comments
azabbasi 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
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 |
|---|---|---|
|
|
@@ -2,3 +2,4 @@ | |
| !*common.config.json | ||
| !*common.test.assets.config.json | ||
| *.etl | ||
| *launchSettings.json | ||
2 changes: 1 addition & 1 deletion
2
sdk/iot/Azure.Iot.Hub.Service/samples/IotHubClientSamples/IotHubClientSamples.csproj
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
195 changes: 195 additions & 0 deletions
195
sdk/iot/Azure.Iot.Hub.Service/samples/IotHubClientSamples/ModuleIdentityLifecycleSamples.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,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); | ||
azabbasi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // 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 | ||
azabbasi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| 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) | ||
azabbasi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| SampleLogger.PrintHeader("DELETE MODULE IDENTITY"); | ||
|
|
||
| try | ||
| { | ||
| Console.WriteLine($"Deleting module identity with Id: '{moduleIdentity.ModuleId}'"); | ||
|
|
||
| // Call APIs to delete the module identity. | ||
azabbasi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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}"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
39 changes: 39 additions & 0 deletions
39
sdk/iot/Azure.Iot.Hub.Service/samples/IotHubClientSamples/SampleLogger.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,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) | ||
azabbasi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| Console.ForegroundColor = ConsoleColor.DarkGreen; | ||
| Console.WriteLine($"\n{message}"); | ||
azabbasi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Console.ResetColor(); | ||
| } | ||
|
|
||
| internal static void PrintWarning(string message) | ||
| { | ||
| Console.ForegroundColor = ConsoleColor.DarkYellow; | ||
| Console.WriteLine($"\n{message}"); | ||
| Console.ResetColor(); | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.