/// <summary> /// Adds the provided device to the IoT hub with the provided security keys /// </summary> /// <param name="device"></param> /// <param name="securityKeys"></param> /// <returns></returns> public async Task<dynamic> AddDeviceAsync(dynamic device, SecurityKeys securityKeys) { Device iotHubDevice = new Device(DeviceSchemaHelper.GetDeviceID(device)); var authentication = new AuthenticationMechanism { SymmetricKey = new SymmetricKey { PrimaryKey = securityKeys.PrimaryKey, SecondaryKey = securityKeys.SecondaryKey } }; iotHubDevice.Authentication = authentication; await AzureRetryHelper.OperationWithBasicRetryAsync(async () => await _deviceManager.AddDeviceAsync(iotHubDevice)); return device; }
/// <summary> /// Attempts to add the device as a new device and swallows all exceptions /// </summary> /// <param name="oldIotHubDevice">The IoT Hub Device to add back into the IoT Hub</param> /// <returns>true if the device was added successfully, false if there was a problem adding the device</returns> public async Task<bool> TryAddDeviceAsync(Device oldIotHubDevice) { try { // the device needs to be added as a new device as the one that was saved // has an eTag value that cannot be provided when registering a new device var newIotHubDevice = new Device(oldIotHubDevice.Id) { Authentication = oldIotHubDevice.Authentication, Status = oldIotHubDevice.Status }; await AzureRetryHelper.OperationWithBasicRetryAsync(async () => await _deviceManager.AddDeviceAsync(newIotHubDevice)); } catch (Exception) { return false; } return true; }