コード例 #1
0
        private void InitializeDeviceClients()
        {
            _logger.LogInformation("Initializing device clients...");

            var tenantConfiguration = new List <TenantConfiguration>();

            _configuration.Bind("SimulatedDevices:Tenants", tenantConfiguration);

            foreach (var tenant in tenantConfiguration)
            {
                var deviceClients = new List <DeviceInfo>();
                for (int i = 0; i < tenant.NumberOfDevices; i++)
                {
                    deviceClients.Add(new DeviceInfo
                    {
                        DeviceId     = TenantConfiguration.BuildDeviceName(tenant, i),
                        DeviceClient = DeviceClient.CreateFromConnectionString(
                            _configuration["IotHubConnectionString"],
                            TenantConfiguration.BuildDeviceName(tenant, i))
                    });
                }
                _tenantDeviceClients.Add(tenant, deviceClients);
            }

            _logger.LogInformation($"Registered {_tenantDeviceClients.Sum(t => t.Value.Count)} device clients");
        }
コード例 #2
0
        private async Task StartSendingMessagesForDeviceAsync(TenantConfiguration tenantConfiguration,
                                                              DeviceInfo deviceInfo, CancellationToken cancellationToken)
        {
            var random = new Random();

            while (!cancellationToken.IsCancellationRequested)
            {
                var objectId = _objectIdsPool
                               .Tenants.First(t => t.Id == tenantConfiguration.Id)
                               .Devices.First(d => d.DeviceId == deviceInfo.DeviceId)
                               .ObjectId;

                var message = _messageFactory.CreateMessage(tenantConfiguration, deviceInfo.DeviceId, objectId, random);

                try
                {
                    await deviceInfo.DeviceClient.SendEventAsync(message, cancellationToken).ConfigureAwait(false);

                    _logger.LogInformation($"Message for Tenant \"{tenantConfiguration.Id}\" DeviceId \"{deviceInfo.DeviceId}\" ObjectId \"{objectId}\" sent.");
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, $"Device: {deviceInfo.DeviceId}; Error sending device message: {ex.Message}");
                }

                await Task.Delay(tenantConfiguration.SendingInterval, cancellationToken).ConfigureAwait(false);
            }
        }
コード例 #3
0
        public async Task RegisterDevicesAsync()
        {
            _logger.LogInformation("Preparing device registrations...");
            Task[] tasks = new Task[_tenantConfigurations.Sum(t => t.NumberOfDevices)];
            int    i     = 0;

            foreach (var tenant in _tenantConfigurations)
            {
                for (int j = 0; j < tenant.NumberOfDevices; j++)
                {
                    tasks[i++] = RegisterDeviceAsync(TenantConfiguration.BuildDeviceName(tenant, j));
                }
            }

            await Task.WhenAll(tasks).ConfigureAwait(false);

            _logger.LogInformation($"Registered {tasks.Length} devices");
        }