Exemplo n.º 1
0
        private async Task <Device> CreateAsync(string deviceId, CancellationToken cancellationToken)
        {
            try
            {
                this.log.Debug("Creating device", () => new { deviceId });
                var device = new Azure.Devices.Device(deviceId);
                device = await this.rateLimiting.LimitRegistryOperationsAsync(
                    () => this.registry.AddDeviceAsync(device, cancellationToken));

                var twin = new Twin();
                log.Debug("Writing device twin an adding the `IsSimulated` Tag",
                          () => new { device.Id, DeviceTwin.SIMULATED_TAG_KEY, DeviceTwin.SIMULATED_TAG_VALUE });
                twin.Tags[DeviceTwin.SIMULATED_TAG_KEY] = DeviceTwin.SIMULATED_TAG_VALUE;

                // TODO: when not discarding the twin, use the right ETag and manage conflicts
                //       https://github.com/Azure/device-simulation-dotnet/issues/83
                twin = await this.rateLimiting.LimitTwinWritesAsync(
                    () => this.registry.UpdateTwinAsync(device.Id, twin, "*", cancellationToken));

                return(new Device(device, twin, this.ioTHubHostName));
            }
            catch (Exception e)
            {
                if (e.InnerException != null && e.InnerException.GetType() == typeof(TaskCanceledException))
                {
                    // We get here when the cancellation token is triggered, which is fine
                    this.log.Debug("Get device task canceled", () => new { deviceId, e.Message });
                    return(null);
                }

                this.log.Error("Unable to fetch the IoT device", () => new { deviceId, e });
                throw new ExternalDependencyException("Unable to fetch the IoT device.");
            }
        }
Exemplo n.º 2
0
        public async Task <Device> GetAsync(string deviceId, bool loadTwin, CancellationToken cancellationToken)
        {
            Device result = null;

            try
            {
                Azure.Devices.Device device = null;
                Twin twin = null;

                if (loadTwin)
                {
                    var deviceTask = this.rateLimiting.LimitRegistryOperationsAsync(
                        () => this.registry.GetDeviceAsync(deviceId, cancellationToken));

                    var twinTask = this.rateLimiting.LimitTwinReadsAsync(
                        () => this.registry.GetTwinAsync(deviceId, cancellationToken));

                    await Task.WhenAll(deviceTask, twinTask);

                    device = deviceTask.Result;
                    twin   = twinTask.Result;
                }
                else
                {
                    device = await this.rateLimiting.LimitRegistryOperationsAsync(
                        () => this.registry.GetDeviceAsync(deviceId, cancellationToken));
                }

                if (device != null)
                {
                    result = new Device(device, twin, this.ioTHubHostName);
                }
            }
            catch (Exception e)
            {
                if (e.InnerException != null && e.InnerException.GetType() == typeof(TaskCanceledException))
                {
                    // We get here when the cancellation token is triggered, which is fine
                    this.log.Debug("Get device task canceled", () => new { deviceId, e.Message });
                    return(null);
                }

                this.log.Error("Unable to fetch the IoT device", () => new { deviceId, e });
                throw new ExternalDependencyException("Unable to fetch the IoT device.");
            }

            if (result == null)
            {
                throw new ResourceNotFoundException("The device doesn't exist.");
            }

            return(result);
        }
Exemplo n.º 3
0
        // Create a Device object using a predefined authentication secret key
        private Azure.Devices.Device PrepareDeviceObject(string id, string key)
        {
            var result = new Azure.Devices.Device(id)
            {
                Authentication = new AuthenticationMechanism
                {
                    Type         = AuthenticationType.Sas,
                    SymmetricKey = new SymmetricKey
                    {
                        PrimaryKey   = key,
                        SecondaryKey = key
                    }
                }
            };

            return(result);
        }
Exemplo n.º 4
0
        // Get the device from the registry
        public async Task <Device> GetAsync(string deviceId)
        {
            this.instance.InitRequired();

            this.log.Debug("Fetching device from registry", () => new { deviceId });

            var start = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

            long GetTimeSpentMsecs() => DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - start;

            Device result = null;

            try
            {
                Azure.Devices.Device device = await this.registry.GetDeviceAsync(deviceId);

                if (device != null)
                {
                    result = new Device(device, this.ioTHubHostName);
                }
                else
                {
                    var timeSpentMsecs = GetTimeSpentMsecs();
                    this.log.Debug("Device not found", () => new { timeSpentMsecs, deviceId });
                }
            }
            catch (Exception e) when(e is TaskCanceledException || e.InnerException is TaskCanceledException)
            {
                var timeSpentMsecs = GetTimeSpentMsecs();

                this.log.Error("Get device task timed out", () => new { timeSpentMsecs, deviceId, e.Message });

                throw new ExternalDependencyException("Get device task timed out", e);
            }
            catch (Exception e)
            {
                var          timeSpentMsecs = GetTimeSpentMsecs();
                const string MSG            = "Unable to fetch the IoT device";
                this.log.Error(MSG, () => new { timeSpentMsecs, deviceId, e });
                this.diagnosticsLogger.LogServiceError(MSG, new { timeSpentMsecs, deviceId, e.Message });
                throw new ExternalDependencyException(MSG);
            }

            return(result);
        }
Exemplo n.º 5
0
        // Register a new device
        public async Task <Device> CreateAsync(string deviceId)
        {
            this.CheckSetup();
            var start = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

            try
            {
                this.log.Debug("Creating device", () => new { deviceId });

                var device = new Azure.Devices.Device(deviceId);
                device = await this.registry.AddDeviceAsync(device);

                return(new Device(device, this.ioTHubHostName));
            }
            catch (Exception e)
            {
                var timeSpentMsecs = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - start;
                this.log.Error("Unable to create the device", () => new { timeSpentMsecs, deviceId, e });
                throw new ExternalDependencyException("Unable to create the device", e);
            }
        }
Exemplo n.º 6
0
        // Register a new device
        public async Task <Device> CreateAsync(string deviceId)
        {
            this.instance.InitRequired();

            var start = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

            try
            {
                this.log.Debug("Creating device", () => new { deviceId });

                var device = new Azure.Devices.Device(deviceId);
                device = await this.registry.AddDeviceAsync(device);

                return(new Device(device, this.ioTHubHostName));
            }
            catch (Exception e)
            {
                var timeSpentMsecs = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - start;
                var msg            = "Unable to create the device";
                this.log.Error(msg, () => new { timeSpentMsecs, deviceId, e });
                this.diagnosticsLogger.LogServiceError(msg, new { timeSpentMsecs, deviceId, e.Message });
                throw new ExternalDependencyException(msg, e);
            }
        }