Example #1
0
        private ValueTask <DeviceClient> BuildDeviceClientAsync(DeviceServiceSettings deviceServiceSettings)
        {
            if (cachedDeviceClient != null)
            {
                return(new ValueTask <DeviceClient>(cachedDeviceClient));
            }

            return(new ValueTask <DeviceClient>(CreateAndCacheDeviceClient()));

            async Task <DeviceClient> CreateAndCacheDeviceClient()
            {
                var registryManager = RegistryManager.CreateFromConnectionString(deviceServiceSettings.IoTHubConnectionString);
                var device          = await registryManager.GetDeviceAsync(deviceServiceSettings.DeviceName);

                if (device == null)
                {
                    var message = $"Device {deviceServiceSettings.DeviceName} is not registered!";
                    DeviceSimulatorActorEventSource.Current.ExceptionOccured(message);
                    throw new InvalidOperationException(message);
                }

                var deviceKeyInfo = new DeviceAuthenticationWithRegistrySymmetricKey(deviceServiceSettings.DeviceName, device.Authentication.SymmetricKey.PrimaryKey);
                var iotHubConnectionStringBuilder = IotHubConnectionStringBuilder.Create(deviceServiceSettings.IoTHubConnectionString);

                cachedDeviceClient = DeviceClient.Create(
                    iotHubConnectionStringBuilder.HostName,
                    deviceKeyInfo,
                    DeviceClientTransportSettings);

                return(cachedDeviceClient);
            }
        }
Example #2
0
        public async Task CleanDevicesAsync(DeviceServiceSettings deviceServiceSettings, CancellationToken cancellationToken)
        {
            var registryManager = BuildRegistryManager(deviceServiceSettings);
            var deviceQuery     = registryManager.CreateQuery("select * from devices", 100);

            do
            {
                var deviceTwins = await deviceQuery.GetNextAsTwinAsync();

                var devices = new List <Device>();
                foreach (var deviceTwin in deviceTwins)
                {
                    devices.Add(new Device(deviceTwin.DeviceId)
                    {
                        ETag = deviceTwin.ETag,
                    });
                }

                var bulkRegistryOperationResult = await registryManager.RemoveDevices2Async(devices, true, cancellationToken);

                if (!bulkRegistryOperationResult.IsSuccessful)
                {
                    foreach (var error in bulkRegistryOperationResult.Errors)
                    {
                        DeviceSimulatorActorEventSource.Current.ExceptionOccured($"Error deleting device {error.DeviceId} because {error.ErrorCode} : {error.ErrorStatus}");
                    }
                }
            } while (deviceQuery.HasMoreResults);
        }
 public DeviceServiceSettings With(DeviceServiceSettings deviceServiceSettings)
 {
     return(new DeviceServiceSettings(
                baseUrl: GetPropertyValue(nameof(DeviceServiceSettings.BaseUrl), deviceServiceSettings.BaseUrl),
                accessToken: GetPropertyValue(nameof(DeviceServiceSettings.AccessToken), deviceServiceSettings.AccessToken),
                httpProxyUrl: GetPropertyValue(nameof(DeviceServiceSettings.HttpProxyUrl), deviceServiceSettings.HttpProxyUrl),
                devices: GetPropertyValue(nameof(DeviceServiceSettings.Devices), deviceServiceSettings.Devices)));
 }
Example #4
0
        private static ConfigurationProvider InitializeConfigurationProvider(DeviceServiceSettings deviceServiceSettings, AppSettings appSettings)
        {
            var configSettingsRoot = new ConfigSettingsRoot
            {
                AppSettings   = appSettings,
                DeviceService = deviceServiceSettings
            };

            return(InitializeConfigurationProvider(configSettingsRoot));
        }
Example #5
0
        private RegistryManager BuildRegistryManager(DeviceServiceSettings deviceServiceSettings)
        {
            if (cachedRegistryManager != null)
            {
                return(cachedRegistryManager);
            }

            cachedRegistryManager = RegistryManager.CreateFromConnectionString(deviceServiceSettings.IoTHubConnectionString);
            return(cachedRegistryManager);
        }
        public async Task DeleteAllDevicesAsync(SimulationIoTHubOptions simulationIoTHubOptions)
        {
            var deleteDevicesActor    = ActorProxy.Create <IDeviceSimulator>(new ActorId(Guid.NewGuid().ToString()), deviceActorApplicationUri);
            var deviceServiceSettings = new DeviceServiceSettings()
            {
                IoTHubConnectionString = simulationIoTHubOptions.IotHubConnectionString,
                IoTHubName             = simulationIoTHubOptions.IoTHubName,
            };

            await deleteDevicesActor.CleanDevicesAsync(deviceServiceSettings, CancellationToken.None);
        }
Example #7
0
 public void DeviceFailedTwin(Actor actor, DeviceServiceSettings deviceInfo, int retryCount)
 {
     DeviceFailedTwin(actor.ActorService.Context.ServiceName.ToString(),
                      actor.ActorService.Context.ServiceTypeName,
                      actor.ActorService.Context.ReplicaId,
                      actor.ActorService.Context.PartitionId,
                      actor.ActorService.Context.CodePackageActivationContext.ApplicationName,
                      actor.ActorService.Context.CodePackageActivationContext.ApplicationTypeName,
                      actor.ActorService.Context.NodeContext.NodeName,
                      deviceInfo.DeviceName,
                      deviceInfo.DeviceType,
                      retryCount);
 }
        internal void GetDeviceServiceSettings_WhenAnyPropertyIsNull_ShouldThrow(DeviceServiceSettings deviceServiceSettings, string expectedExceptionMessage)
        {
            var appSettings = new AppSettings {
                HttpProxyUrl = ProxyUrl
            };
            var configurationProvider = InitializeConfigurationProvider(deviceServiceSettings, appSettings);

            try
            {
                // Act
                _ = configurationProvider.GetDeviceServiceSettings();
                throw new XunitException("We were expecting an Excption of type: ConfigurationSettingMissingException, but no exception was thrown");
            }
            catch (ConfigurationSettingMissingException e)
            {
                // Assert
                Assert.Equal(expectedExceptionMessage, e.Message);
            }
        }
Example #9
0
        public async Task AddDeviceAsync(DeviceServiceSettings deviceServiceSettings, CancellationToken cancellationToken)
        {
            var registryManager = BuildRegistryManager(deviceServiceSettings);

            var retryContext = new Context()
            {
                { nameof(DeviceSimulatorActor), this },
                { nameof(DeviceServiceSettings), deviceServiceSettings }
            };

            await DeviceServiceAddRetryPolicy.ExecuteAsync(
                async context =>
            {
                var device = await registryManager.GetDeviceAsync(deviceServiceSettings.DeviceName);
                if (device == null)
                {
                    device = await registryManager.AddDeviceAsync(new Device(deviceServiceSettings.DeviceName));
                }
            },
                retryContext);
        }
Example #10
0
        public async Task CreateDeviceTwinAsync(DeviceServiceSettings deviceServiceSettings, CancellationToken cancellationToken)
        {
            var registryManager = BuildRegistryManager(deviceServiceSettings);
            var device          = await registryManager.GetDeviceAsync(deviceServiceSettings.DeviceName);

            var twin = new Twin()
            {
                Tags = { ["IsSimulated"] = "Y" }
            };

            var retryContext = new Context()
            {
                { nameof(DeviceSimulatorActor), this },
                { nameof(DeviceServiceSettings), deviceServiceSettings }
            };

            await DeviceServiceTwinRetryPolicy.ExecuteAsync(
                async context =>
            {
                await registryManager.UpdateTwinAsync(device.Id, twin, "*");
            },
                retryContext);
        }