/// <summary>
        /// Removes a device from the underlying repositories
        /// </summary>
        /// <param name="deviceId">ID of the device to remove</param>
        /// <returns></returns>
        public async Task RemoveDeviceAsync(string deviceId)
        {
            ExceptionDispatchInfo capturedException = null;

            Azure.Devices.Device iotHubDevice = await _iotHubRepository.GetIotHubDeviceAsync(deviceId);

            // if the device isn't already in the IotHub throw an exception and let the caller know
            if (iotHubDevice == null)
            {
                throw new DeviceNotRegisteredException(deviceId);
            }

            // Attempt to remove the device from the IotHub.  If this fails an exception will be thrown
            // and the remainder of the code not run, which is by design
            await _iotHubRepository.RemoveDeviceAsync(deviceId);

            try
            {
                await _deviceRegistryCrudRepository.RemoveDeviceAsync(deviceId);
            }
            catch (Exception ex)
            {
                // if there is an exception while attempting to remove the device from the Device Registry
                // capture it so a rollback can be done on the Identity Registry
                capturedException = ExceptionDispatchInfo.Capture(ex);
            }

            if (capturedException == null)
            {
                try
                {
                    await _virtualDeviceStorage.RemoveDeviceAsync(deviceId);
                }
                catch (Exception ex)
                {
                    //if an exception occurs while attempting to remove the
                    //simulated device from table storage do not roll back the changes.
                    Trace.TraceError("Failed to remove simulated device : {0}", ex.Message);
                }

                await _deviceRulesLogic.RemoveAllRulesForDeviceAsync(deviceId);
            }
            else
            {
                // The "rollback" is an attempt to add the device back in to the Identity Registry
                // It is assumed that if an exception has occured in the Device Registry, the device
                // is still in that store and this works to ensure that both repositories have the same
                // devices registered
                // A more robust rollback may be needed in some scenarios.
                await _iotHubRepository.TryAddDeviceAsync(iotHubDevice);

                capturedException.Throw();
            }
        }
Esempio n. 2
0
        public async Task UpdateDeviceEnabledStatusAsync(string deviceId, bool isEnabled)
        {
            Azure.Devices.Device iotHubDevice =
                await AzureRetryHelper.OperationWithBasicRetryAsync <Azure.Devices.Device>(async() =>
                                                                                           await _deviceManager.GetDeviceAsync(deviceId));

            iotHubDevice.Status = isEnabled ? DeviceStatus.Enabled : DeviceStatus.Disabled;

            await AzureRetryHelper.OperationWithBasicRetryAsync(async() =>
                                                                await _deviceManager.UpdateDeviceAsync(iotHubDevice));
        }
Esempio n. 3
0
 public Device(Azure.Devices.Device azureDevice, string ioTHubHostName) :
     this(
         eTag : azureDevice.ETag,
         id : azureDevice.Id,
         c2DMessageCount : azureDevice.CloudToDeviceMessageCount,
         lastActivity : azureDevice.LastActivityTime,
         connected : azureDevice.ConnectionState.Equals(DeviceConnectionState.Connected),
         enabled : azureDevice.Status.Equals(DeviceStatus.Enabled),
         lastStatusUpdated : azureDevice.StatusUpdatedTime,
         ioTHubHostName : ioTHubHostName,
         primaryKey : azureDevice.Authentication.SymmetricKey.PrimaryKey)
 {
 }
Esempio n. 4
0
        public async Task <SecurityKeys> GetDeviceKeysAsync(string deviceId)
        {
            Azure.Devices.Device iotHubDevice = await _deviceManager.GetDeviceAsync(deviceId);

            if (iotHubDevice == null)
            {
                // this is the case if the device does not exist on the hub
                return(null);
            }
            else
            {
                return(new SecurityKeys(iotHubDevice.Authentication.SymmetricKey.PrimaryKey, iotHubDevice.Authentication.SymmetricKey.SecondaryKey));
            }
        }
Esempio n. 5
0
        public async Task <bool> IsDeviceOnline(string deviceId)
        {
            if (string.IsNullOrEmpty(deviceId))
            {
                throw new ArgumentNullException("deviceId");
            }

            Azure.Devices.Device iotHubDevice = await this._iotHubRepository.GetIotHubDeviceAsync(deviceId);

            if (iotHubDevice == null)
            {
                throw new DeviceNotRegisteredException(deviceId);
            }

            bool connectionState = iotHubDevice.ConnectionState == DeviceConnectionState.Connected ? true : false;

            return(connectionState);
        }
Esempio n. 6
0
        /// <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)
        {
            Azure.Devices.Device iotHubDevice = new Azure.Devices.Device(DeviceSchemaHelper.GetDeviceID(device));

            var authentication = new AuthenticationMechanism
            {
                SymmetricKey = new SymmetricKey
                {
                    PrimaryKey   = securityKeys.PrimaryKey,
                    SecondaryKey = securityKeys.SecondaryKey
                }
            };

            iotHubDevice.Authentication = authentication;

            await AzureRetryHelper.OperationWithBasicRetryAsync <Azure.Devices.Device>(async() =>
                                                                                       await _deviceManager.AddDeviceAsync(iotHubDevice));

            return(device);
        }
        /// <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)
        {

            Azure.Devices.Device iotHubDevice = new Azure.Devices.Device(DeviceSchemaHelper.GetDeviceID(device));

            var authentication = new AuthenticationMechanism
            {
                SymmetricKey = new SymmetricKey
                {
                    PrimaryKey = securityKeys.PrimaryKey,
                    SecondaryKey = securityKeys.SecondaryKey
                }
            };

            iotHubDevice.Authentication = authentication;

            await AzureRetryHelper.OperationWithBasicRetryAsync<Azure.Devices.Device>(async () =>
                await _deviceManager.AddDeviceAsync(iotHubDevice));

            return device;
        }
Esempio n. 8
0
        /// <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(Azure.Devices.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 Azure.Devices.Device(oldIotHubDevice.Id)
                {
                    Authentication = oldIotHubDevice.Authentication,
                    Status         = oldIotHubDevice.Status
                };

                await AzureRetryHelper.OperationWithBasicRetryAsync <Azure.Devices.Device>(async() =>
                                                                                           await _deviceManager.AddDeviceAsync(newIotHubDevice));
            }
            catch (Exception)
            {
                return(false);
            }

            return(true);
        }
        /// <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(Azure.Devices.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 Azure.Devices.Device(oldIotHubDevice.Id)
                {
                    Authentication = oldIotHubDevice.Authentication,
                    Status = oldIotHubDevice.Status
                };

                await AzureRetryHelper.OperationWithBasicRetryAsync<Azure.Devices.Device>(async () =>
                    await _deviceManager.AddDeviceAsync(newIotHubDevice));
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }
Esempio n. 10
0
 public Device(Azure.Devices.Device azureDevice, Twin azureTwin, string ioTHubHostName) :
     this(azureDevice, new DeviceTwin(azureTwin), ioTHubHostName)
 {
 }