Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Azure.Iot.Hub.Service.Models;

Expand Down Expand Up @@ -30,22 +32,19 @@ public async Task RunSampleAsync()
ModuleIdentity moduleIdentity = await CreateModuleIdentityAsync(deviceIdentity, moduleId);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - afaik, these are IoT hub modules, and not edge modules. We should confirm that, and if true, call that out in our comments.


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

// Get the Module Identity.
await GetModuleIdentityAsync();
moduleIdentity = await GetModuleIdentityAsync(deviceIdentity, moduleIdentity.ModuleId);

// Update Module Identity.
await UpdateModuleIdentityAsync();
moduleIdentity = await UpdateModuleIdentityAsync(moduleIdentity);

// Get Module Twin,
await GetModuleTwinAsync();
TwinData moduleTwin = await GetModuleTwinAsync(moduleIdentity);

// Update Module Twin.
await UpdateModuleTwinAsync();

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

// Delete the module identity
await DeleteModuleIdentityAsync(moduleIdentity);
Expand Down Expand Up @@ -75,7 +74,7 @@ public async Task<DeviceIdentity> CreateDeviceIdentityAsync(string deviceId)
// Call APIs to create the device identity.
Response<DeviceIdentity> response = await IoTHubServiceClient.Devices.CreateOrUpdateIdentityAsync(deviceIdentity);

SampleLogger.PrintSuccess($"Successfully create a new device identity with Id: '{deviceId}'");
SampleLogger.PrintSuccess($"Successfully create a new device identity with Id: '{deviceId}', ETag: '{response.Value.Etag}'");

return response.Value;
}
Expand Down Expand Up @@ -109,7 +108,7 @@ public async Task<ModuleIdentity> CreateModuleIdentityAsync(DeviceIdentity devic
// Call APIs to create the module identity.
Response<ModuleIdentity> response = await IoTHubServiceClient.Modules.CreateOrUpdateIdentityAsync(moduleIdentity);

SampleLogger.PrintSuccess($"Successfully created a new module identity with Id: '{moduleId}'");
SampleLogger.PrintSuccess($"Successfully created a new module identity with Id: '{moduleId}', ETag: '{response.Value.Etag}'");

return response.Value;
}
Expand All @@ -120,34 +119,149 @@ public async Task<ModuleIdentity> CreateModuleIdentityAsync(DeviceIdentity devic
}
}

public Task ListAllModulesAsync()
/// <summary>
/// List all modules within a Device
/// </summary>
/// <param name="deviceIdentity">Device Identity to query</param>
public async Task<IReadOnlyList<ModuleIdentity>> ListAllModulesAsync(DeviceIdentity deviceIdentity)
{
return Task.CompletedTask;
}
SampleLogger.PrintHeader("LIST ALL MODULES");

public Task GetModuleIdentityAsync()
{
return Task.CompletedTask;
try
{
Console.WriteLine($"Listing all modules in device with Id: '{deviceIdentity.DeviceId}'\n");

Response<IReadOnlyList<ModuleIdentity>> response = await IoTHubServiceClient.Modules.GetIdentitiesAsync(deviceIdentity.DeviceId);

foreach (ModuleIdentity moduleIdentity in response.Value)
{
SampleLogger.PrintSuccess($"\t- Module Id: '{moduleIdentity.ModuleId}', Device Id: '{moduleIdentity.DeviceId}', ETag: '{moduleIdentity.Etag}'");
}

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

public Task UpdateModuleIdentityAsync()
/// <summary>
/// Get a module identity
/// </summary>
/// <param name="deviceIdentity">The device identity module belongs to.</param>
/// <param name="moduleId">The unique identifier of the module to get.</param>
public async Task<ModuleIdentity> GetModuleIdentityAsync(DeviceIdentity deviceIdentity, string moduleId)
{
return Task.CompletedTask;
}
SampleLogger.PrintHeader("GET A MODULE");

public Task GetModuleTwinAsync()
try
{
Console.WriteLine($"Getting module identity with Id: '{moduleId}'\n");

Response<ModuleIdentity> response = await IoTHubServiceClient.Modules.GetIdentityAsync(deviceIdentity.DeviceId, moduleId);

ModuleIdentity moduleIdentity = response.Value;

SampleLogger.PrintSuccess($"\t- Module Id: '{moduleIdentity.ModuleId}', Device Id: '{moduleIdentity.DeviceId}', ETag: '{moduleIdentity.Etag}'");

return moduleIdentity;
}
catch (Exception ex)
{
SampleLogger.FatalError($"Failed to get a module identity due to:\n{ex}");
throw;
}
}

/// <summary>
/// Update a module identity
/// </summary>
/// <param name="moduleIdentity">Module identity to be updated.</param>
public async Task<ModuleIdentity> UpdateModuleIdentityAsync(ModuleIdentity moduleIdentity)
{
return Task.CompletedTask;
SampleLogger.PrintHeader("UPDATE A MODULE");

try
{
Console.WriteLine($"Updating module identity with Id: '{moduleIdentity.ModuleId}'. Setting 'ManagedBy' property to: '{Environment.UserName}'\n");
moduleIdentity.ManagedBy = Environment.UserName;

Response<ModuleIdentity> response = await IoTHubServiceClient.Modules.CreateOrUpdateIdentityAsync(moduleIdentity, IfMatchPrecondition.UnconditionalIfMatch);

ModuleIdentity updatedModule = response.Value;

SampleLogger.PrintSuccess($"Successfully updated module identity with Id: '{updatedModule.ModuleId}', ManagedBy: '{updatedModule.ManagedBy}', ETag: '{updatedModule.Etag}'");

return updatedModule;
}
catch (Exception ex)
{
SampleLogger.FatalError($"Failed to update a module identity due to:\n{ex}");
throw;
}
}

public Task UpdateModuleTwinAsync()
/// <summary>
/// Get module twin.
/// </summary>
/// <param name="moduleIdentity">The module identity.</param>
public async Task<TwinData> GetModuleTwinAsync(ModuleIdentity moduleIdentity)
{
return Task.CompletedTask;
SampleLogger.PrintHeader("GET A MODULE TWIN");

try
{
Console.WriteLine($"Getting module twin with Id: '{moduleIdentity.ModuleId}'\n");

Response<TwinData> response = await IoTHubServiceClient.Modules.GetTwinAsync(moduleIdentity.DeviceId, moduleIdentity.ModuleId);

SampleLogger.PrintSuccess($"\t- Module Twin Status: '{response.Value.Status}', ETag: '{response.Value.Etag}'");

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

public Task InvokeMethodAsync()
/// <summary>
/// Update a module twin desired properties
/// </summary>
/// <param name="moduleTwin">Module twin.</param>
public async Task<TwinData> UpdateModuleTwinAsync(TwinData moduleTwin)
{
return Task.CompletedTask;
SampleLogger.PrintHeader("UPDATE A MODULE TWIN");

string userPropName = "user";

try
{
Console.WriteLine($"Updating module twin with Id: '{moduleTwin.ModuleId}'. Setting Desired property {userPropName} to: '{Environment.UserName}'\n");

moduleTwin.Properties.Desired.Add(new KeyValuePair<string, object>(userPropName, Environment.UserName));

Response<TwinData> response = await IoTHubServiceClient.Modules.UpdateTwinAsync(moduleTwin, IfMatchPrecondition.UnconditionalIfMatch);

TwinData updatedTwin = response.Value;

var userPropValue = (string) updatedTwin.Properties.Desired
.Where(p => p.Key == userPropName)
.First()
.Value;

SampleLogger.PrintSuccess($"Successfully updated module twin with Id: '{updatedTwin.ModuleId}' ETag: '{updatedTwin.Etag}', {userPropName}: '{userPropValue}'");

return updatedTwin;
}
catch (Exception ex)
{
SampleLogger.FatalError($"Failed to update a module identity due to:\n{ex}");
throw;
}
}

/// <summary>
Expand Down
14 changes: 7 additions & 7 deletions sdk/iot/Azure.Iot.Hub.Service/src/IoTHubServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,27 @@ public class IoTHubServiceClient
private readonly DeviceMethodRestClient _deviceMethodRestClient;

/// <summary>
/// place holder for Devices
/// place holder for Devices.
/// </summary>
public virtual DevicesClient Devices { get; private set; }

/// <summary>
/// place holder for Modules
/// place holder for Modules.
/// </summary>
public virtual ModulesClient Modules { get; private set; }

/// <summary>
/// place holder for Statistics
/// place holder for Statistics.
/// </summary>
public virtual StatisticsClient Statistics { get; private set; }

/// <summary>
/// place holder for Messages
/// place holder for Messages.
/// </summary>
public virtual CloudToDeviceMessagesClient Messages { get; private set; }

/// <summary>
/// place holder for Files
/// place holder for Files.
/// </summary>
public virtual FilesClient Files { get; private set; }

Expand Down Expand Up @@ -95,7 +95,7 @@ public IoTHubServiceClient(string connectionString, IoTHubServiceClientOptions o
_httpPipeline = HttpPipelineBuilder.Build(options);

_registryManagerRestClient = new RegistryManagerRestClient(_clientDiagnostics, _httpPipeline, _endpoint, options.GetVersionString());
_twinRestClient = new TwinRestClient(_clientDiagnostics, _httpPipeline, null, options.GetVersionString());
_twinRestClient = new TwinRestClient(_clientDiagnostics, _httpPipeline, _endpoint, options.GetVersionString());
_deviceMethodRestClient = new DeviceMethodRestClient(_clientDiagnostics, _httpPipeline, _endpoint, options.GetVersionString());

Devices = new DevicesClient(_registryManagerRestClient, _twinRestClient, _deviceMethodRestClient);
Expand Down Expand Up @@ -144,7 +144,7 @@ public IoTHubServiceClient(Uri endpoint, TokenCredential credential, IoTHubServi
_httpPipeline = HttpPipelineBuilder.Build(options);

_registryManagerRestClient = new RegistryManagerRestClient(_clientDiagnostics, _httpPipeline, _endpoint, options.GetVersionString());
_twinRestClient = new TwinRestClient(_clientDiagnostics, _httpPipeline, null, options.GetVersionString());
_twinRestClient = new TwinRestClient(_clientDiagnostics, _httpPipeline, _endpoint, options.GetVersionString());
_deviceMethodRestClient = new DeviceMethodRestClient(_clientDiagnostics, _httpPipeline, _endpoint, options.GetVersionString());

Devices = new DevicesClient(_registryManagerRestClient, _twinRestClient, _deviceMethodRestClient);
Expand Down