public async static Task DeleteAllDevicesInHubAsync(IotHubServiceClient hubClient) { Console.WriteLine($"\nTrying to clean up all devices\n"); try { AsyncPageable <TwinData> asyncPageableResponse = hubClient.Devices.GetTwinsAsync(); List <TwinData> deviceTwins = new List <TwinData>(); await foreach (TwinData twin in asyncPageableResponse) { deviceTwins.Add(twin); } foreach (TwinData twin in deviceTwins) { DeviceIdentity device = await hubClient.Devices.GetIdentityAsync(twin.DeviceId); await hubClient.Devices.DeleteIdentityAsync(device); } SampleLogger.PrintSuccess($"Cleanup succeeded\n"); } catch (Exception ex) { SampleLogger.PrintWarning($"Cleanup failed due to:\n{ex.Message}\n"); } }
/// <summary> /// Update multiple module twin desired properties. /// </summary> /// <param name="moduleTwins">Collection of module twins to be updated.</param> public async Task UpdateModuleTwinsAsync(IEnumerable <TwinData> moduleTwins) { SampleLogger.PrintHeader("UPDATE MODULE TWINS"); string userPropName = "user"; try { Console.WriteLine($"Setting a new desired property {userPropName} to: '{Environment.UserName}' for each twin"); #region Snippet:IotHubUpdateModuleTwins foreach (TwinData twin in moduleTwins) { twin.Properties.Desired.Add(new KeyValuePair <string, object>(userPropName, Environment.UserName)); } Response <BulkRegistryOperationResponse> response = await IoTHubServiceClient.Modules.UpdateTwinsAsync(moduleTwins); var bulkResponse = response.Value; if (bulkResponse.IsSuccessful ?? true) { SampleLogger.PrintSuccess("Successfully updated the module twins"); } else { SampleLogger.PrintWarning("Failed to update the module twins"); foreach (var bulkOperationError in bulkResponse.Errors) { SampleLogger.PrintWarning($"Module id that failed: {bulkOperationError.ModuleId}, for device {bulkOperationError.DeviceId}, error code: {bulkOperationError.ErrorCode}"); } } #endregion Snippet:IotHubUpdateModuleTwins } catch (Exception ex) { // Try to cleanup before exiting with fatal error. await CleanupHelper.DeleteAllDevicesInHubAsync(IoTHubServiceClient); SampleLogger.FatalError($"Failed to update a module identity due to:\n{ex}"); throw; } }
/// <summary> /// Update multiple device identity. /// </summary> /// <param name="deviceIdentities">Collection of device identities to be updated.</param> public async Task UpdateDeviceIdentitiesAsync(IEnumerable <DeviceIdentity> deviceIdentities) { SampleLogger.PrintHeader("UPDATE DEVICE IDENTITIES"); try { Console.WriteLine($"Disabling multiple devices so they cannot connect to IoT Hub."); #region Snippet:IotHubUpdateDeviceIdentities foreach (var identity in deviceIdentities) { identity.Status = DeviceStatus.Disabled; } Response <BulkRegistryOperationResponse> response = await IoTHubServiceClient.Devices.UpdateIdentitiesAsync(deviceIdentities, BulkIfMatchPrecondition.IfMatch); var bulkResponse = response.Value; if (bulkResponse.IsSuccessful ?? true) { SampleLogger.PrintSuccess("Successfully disabled the device identities"); } else { SampleLogger.PrintWarning("Failed to disable the device identities"); foreach (var bulkOperationError in bulkResponse.Errors) { SampleLogger.PrintWarning($"Device id that failed: {bulkOperationError.DeviceId}, error code: {bulkOperationError.ErrorCode}"); } } #endregion Snippet:IotHubUpdateDeviceIdentities } catch (Exception ex) { // Try to cleanup before exiting with fatal error. await CleanupHelper.DeleteAllDevicesInHubAsync(IoTHubServiceClient); SampleLogger.FatalError($"Failed to update a device identity due to:\n{ex}"); throw; } }
/// <summary> /// Create multiple device identities. /// </summary> /// <param name="deviceIdentities">Collection of device identities to be created.</param> public async Task CreateDeviceIdentitiesAsync(IEnumerable <DeviceIdentity> deviceIdentities) { SampleLogger.PrintHeader("CREATE DEVICE IDENTITIES"); try { Console.WriteLine($"Creating {BulkCount} new devices"); #region Snippet:IotHubCreateDeviceIdentities Response <BulkRegistryOperationResponse> response = await IoTHubServiceClient.Devices.CreateIdentitiesAsync(deviceIdentities); var bulkResponse = response.Value; if (bulkResponse.IsSuccessful ?? true) { SampleLogger.PrintSuccess("Successfully created new device identities"); } else { SampleLogger.PrintWarning("Failed to create new device identities"); foreach (var bulkOperationError in bulkResponse.Errors) { SampleLogger.PrintWarning($"Device id that failed: {bulkOperationError.DeviceId}, error code: {bulkOperationError.ErrorCode}"); } } #endregion Snippet:IotHubCreateDeviceIdentities } catch (Exception ex) { // Try to cleanup before exiting with fatal error. await CleanupHelper.DeleteAllDevicesInHubAsync(IoTHubServiceClient); SampleLogger.FatalError($"Failed to create device identity due to:\n{ex}"); throw; } }
/// <summary> /// Delete multiple module identities. /// </summary> /// <param name="moduleIdentities">Collection of module identities to be deleted.</param> public async Task DeleteModuleIdentitiesAsync(IEnumerable <ModuleIdentity> moduleIdentities) { SampleLogger.PrintHeader("DELETE MODULE IDENTITIES"); try { Console.WriteLine($"Deleting bulk module identities"); #region Snippet:IotHubDeleteModuleIdentities Response <BulkRegistryOperationResponse> response = await IoTHubServiceClient.Modules.DeleteIdentitiesAsync(moduleIdentities); var bulkResponse = response.Value; if (bulkResponse.IsSuccessful ?? true) { SampleLogger.PrintSuccess("Successfully deleted the module identities"); } else { SampleLogger.PrintWarning("Failed to delete the module identities"); foreach (var bulkOperationError in bulkResponse.Errors) { SampleLogger.PrintWarning($"Module id that failed: {bulkOperationError.ModuleId}, for device {bulkOperationError.DeviceId}, error code: {bulkOperationError.ErrorCode}"); } } #endregion Snippet:IotHubDeleteModuleIdentities } catch (Exception ex) { // Try to cleanup before exiting with fatal error. await CleanupHelper.DeleteAllDevicesInHubAsync(IoTHubServiceClient); SampleLogger.FatalError($"Failed to delete module identity due to:\n{ex}"); } }