Пример #1
0
        protected async Task GetOrCreateEdgeDeviceIdentity()
        {
            Console.WriteLine("Getting or Creating device Identity.");
            var settings = new HttpTransportSettings();

            this.proxy.ForEach(p => settings.Proxy = p);
            IotHubConnectionStringBuilder builder = IotHubConnectionStringBuilder.Create(this.iothubConnectionString);
            RegistryManager rm = RegistryManager.CreateFromConnectionString(builder.ToString(), settings);

            Device device = await rm.GetDeviceAsync(this.deviceId);

            if (device != null)
            {
                Console.WriteLine($"Device '{device.Id}' already registered on IoT hub '{builder.HostName}'");
                Console.WriteLine($"Clean up Existing device? {this.cleanUpExistingDeviceOnSuccess}");
                this.context = new DeviceContext(device, this.iothubConnectionString, rm, this.cleanUpExistingDeviceOnSuccess);
            }
            else
            {
                // if dpsAttestion is enabled, do not create a device as the
                // ESD will register with DPS to create the device in IoT Hub
                if (this.dpsAttestation.HasValue)
                {
                    this.context = new DeviceContext(this.deviceId, this.iothubConnectionString, rm, this.cleanUpExistingDeviceOnSuccess);
                }
                else
                {
                    await this.CreateEdgeDeviceIdentity(rm);
                }
            }
        }
Пример #2
0
        protected async Task GetOrCreateEdgeDeviceIdentity()
        {
            Console.WriteLine("Getting or Creating device Identity.");
            var settings = new HttpTransportSettings();

            this.proxy.ForEach(p => settings.Proxy = p);
            IotHubConnectionStringBuilder builder = IotHubConnectionStringBuilder.Create(this.iothubConnectionString);
            RegistryManager rm = RegistryManager.CreateFromConnectionString(builder.ToString(), settings);

            Device device = await rm.GetDeviceAsync(this.deviceId);

            if (device != null)
            {
                Console.WriteLine($"Device '{device.Id}' already registered on IoT hub '{builder.HostName}'");
                Console.WriteLine($"Clean up Existing device? {this.cleanUpExistingDeviceOnSuccess}");

                this.context = new DeviceContext
                {
                    Device = device,
                    IotHubConnectionString = this.iothubConnectionString,
                    RegistryManager        = rm,
                    RemoveDevice           = this.cleanUpExistingDeviceOnSuccess
                };
            }
            else
            {
                await this.CreateEdgeDeviceIdentity(rm);
            }
        }
        private async Task JobClient_ScheduleAndRunTwinJob(HttpTransportSettings httpTransportSettings)
        {
            Twin twin = new Twin(JobDeviceId);

            twin.Tags = new TwinCollection();
            twin.Tags[JobTestTagName] = JobDeviceId;

            using (JobClient jobClient = JobClient.CreateFromConnectionString(s_connectionString, httpTransportSettings))
            {
                int tryCount = 0;
                while (true)
                {
                    try
                    {
                        string      jobId             = "JOBSAMPLE" + Guid.NewGuid().ToString();
                        string      query             = $"DeviceId IN ['{JobDeviceId}']";
                        JobResponse createJobResponse = await jobClient.ScheduleTwinUpdateAsync(jobId, query, twin, DateTime.UtcNow, (long)TimeSpan.FromMinutes(2).TotalSeconds).ConfigureAwait(false);

                        break;
                    }
                    // Concurrent jobs can be rejected, so implement a retry mechanism to handle conflicts with other tests
                    catch (ThrottlingException) when(++tryCount < MaxIterationWait)
                    {
                        Logger.Trace($"ThrottlingException... waiting.");
                        await Task.Delay(_waitDuration).ConfigureAwait(false);

                        continue;
                    }
                }
            }
        }
        public async Task JobClient_ScheduleAndRunTwinJob_WithProxy()
        {
            HttpTransportSettings httpTransportSettings = new HttpTransportSettings();

            httpTransportSettings.Proxy = new WebProxy(s_proxyServerAddress);

            await JobClient_ScheduleAndRunTwinJob(httpTransportSettings).ConfigureAwait(false);
        }
        public async Task RegistryManager_AddAndRemoveDevice_WithProxy()
        {
            HttpTransportSettings httpTransportSettings = new HttpTransportSettings();

            httpTransportSettings.Proxy = new WebProxy(s_proxyServerAddress);

            await RegistryManager_AddDevice(httpTransportSettings).ConfigureAwait(false);
        }
Пример #6
0
 public HttpTransport(BusSettings settings, HttpTransportSettings httpSettings, JasperRuntime runtime, IPersistence persistence, CompositeTransportLogger logger)
 {
     _settings     = settings;
     _httpSettings = httpSettings;
     _runtime      = runtime;
     _persistence  = persistence;
     _logger       = logger;
 }
Пример #7
0
        private ProvisioningServiceClient CreateProvisioningServiceWithProxy(string proxyServerAddress)
        {
            HttpTransportSettings transportSettings = new HttpTransportSettings();

            transportSettings.Proxy = new WebProxy(proxyServerAddress);

            return(ProvisioningServiceClient.CreateFromConnectionString(Configuration.Provisioning.ConnectionString, transportSettings));
        }
Пример #8
0
 public HttpTransport(MessagingSettings settings, JasperRuntime runtime, IDurableMessagingFactory factory, ITransportLogger logger)
 {
     _settings                = settings;
     _httpSettings            = settings.Http;
     _runtime                 = runtime;
     _durableMessagingFactory = factory;
     _logger = logger;
 }
Пример #9
0
        public HttpSenderProtocol(BusSettings settings, HttpTransportSettings httpSettings)
        {
            _client = new HttpClient
            {
                Timeout = httpSettings.ConnectionTimeout
            };

            _settings = settings;
        }
        private async Task RegistryManager_AddDevice(HttpTransportSettings httpTransportSettings)
        {
            string deviceName = DevicePrefix + Guid.NewGuid();

            RegistryManager registryManager = RegistryManager.CreateFromConnectionString(ConnectionString, httpTransportSettings);
            await registryManager.AddDeviceAsync(new Device(deviceName)).ConfigureAwait(false);

            await registryManager.RemoveDeviceAsync(deviceName).ConfigureAwait(false);
        }
Пример #11
0
        /// <summary>
        /// Creates the provisioning service client instance
        /// </summary>
        /// <param name="proxyServerAddress">The address of the proxy to be used, or null/empty if no proxy will be used</param>
        /// <returns>the provisioning service client instance</returns>
        public static ProvisioningServiceClient CreateProvisioningService(string proxyServerAddress)
        {
            HttpTransportSettings transportSettings = new HttpTransportSettings();

            if (!string.IsNullOrWhiteSpace(proxyServerAddress))
            {
                transportSettings.Proxy = new WebProxy(proxyServerAddress);
            }

            return(ProvisioningServiceClient.CreateFromConnectionString(Configuration.Provisioning.ConnectionString, transportSettings));
        }
Пример #12
0
        public async Task RegistryManager_AddDeviceWithProxy()
        {
            string deviceId          = _idPrefix + Guid.NewGuid();
            var    transportSettings = new HttpTransportSettings
            {
                Proxy = new WebProxy(TestConfiguration.IoTHub.ProxyServerAddress)
            };

            using var registryManager = RegistryManager.CreateFromConnectionString(TestConfiguration.IoTHub.ConnectionString, transportSettings);
            var device = new Device(deviceId);
            await registryManager.AddDeviceAsync(device).ConfigureAwait(false);
        }
Пример #13
0
        public MessagingSettings()
        {
            ListenForMessagesFrom(TransportConstants.RetryUri);
            ListenForMessagesFrom(TransportConstants.ScheduledUri);
            ListenForMessagesFrom(TransportConstants.RepliesUri);

            _machineName = Environment.MachineName;
            ServiceName  = "Jasper";

            UniqueNodeId = Guid.NewGuid().ToString().GetHashCode();

            Http = new HttpTransportSettings(this);
        }
        private async Task JobClient_ScheduleAndRunTwinJob(HttpTransportSettings httpTransportSettings)
        {
            string jobId = "JOBSAMPLE" + Guid.NewGuid().ToString();
            string query = $"DeviceId IN ['{JobDeviceId}']";

            Twin twin = new Twin(JobDeviceId);

            twin.Tags = new TwinCollection();
            twin.Tags[JobTestTagName] = JobDeviceId;

            JobClient   jobClient         = JobClient.CreateFromConnectionString(ConnectionString, httpTransportSettings);
            JobResponse createJobResponse = await jobClient.ScheduleTwinUpdateAsync(jobId, query, twin, DateTime.UtcNow, (long)TimeSpan.FromMinutes(2).TotalSeconds).ConfigureAwait(false);

            JobResponse jobResponse = await jobClient.GetJobAsync(jobId).ConfigureAwait(false);
        }
Пример #15
0
        protected async Task GetOrCreateDeviceIdentityAsync()
        {
            var settings = new HttpTransportSettings();

            this.proxy.ForEach(p => settings.Proxy = p);
            IotHubConnectionStringBuilder builder = IotHubConnectionStringBuilder.Create(this.iothubConnectionString);
            RegistryManager rm = RegistryManager.CreateFromConnectionString(builder.ToString(), settings);

            Option <string> edgeScope = await this.edgeDeviceId
                                        .Map(id => GetScopeIfExitsAsync(rm, id))
                                        .GetOrElse(() => Task.FromResult <Option <string> >(Option.None <string>()));

            Device device = await rm.GetDeviceAsync(this.deviceId);

            if (device != null)
            {
                Console.WriteLine($"Device '{device.Id}' already registered on IoT hub '{builder.HostName}'");

                if (this.authType == AuthenticationType.SelfSigned)
                {
                    var thumbprints = this.thumbprints.Expect(() => new InvalidOperationException("Missing thumbprints list"));
                    if (!thumbprints.Contains(device.Authentication.X509Thumbprint.PrimaryThumbprint) ||
                        !thumbprints.Contains(device.Authentication.X509Thumbprint.SecondaryThumbprint))
                    {
                        // update the thumbprints before attempting to run any tests to ensure consistency
                        device.Authentication.X509Thumbprint = new X509Thumbprint {
                            PrimaryThumbprint = thumbprints[0], SecondaryThumbprint = thumbprints[1]
                        };
                    }
                }

                edgeScope.ForEach(s => device.Scope = s);
                await rm.UpdateDeviceAsync(device);

                this.context = new DeviceContext
                {
                    Device = device,
                    IotHubConnectionString = this.iothubConnectionString,
                    RegistryManager        = rm,
                    RemoveDevice           = false,
                    MessageGuid            = Guid.NewGuid().ToString()
                };
            }
            else
            {
                await this.CreateDeviceIdentityAsync(rm, edgeScope);
            }
        }
Пример #16
0
        public IotHub(string iotHubConnectionString, string eventHubEndpoint, Option <Uri> proxyUri)
        {
            this.eventHubEndpoint       = eventHubEndpoint;
            this.iotHubConnectionString = iotHubConnectionString;
            Option <IWebProxy> proxy = proxyUri.Map(p => new WebProxy(p) as IWebProxy);

            this.registryManager = new Lazy <RegistryManager>(
                () =>
            {
                var settings = new HttpTransportSettings();
                proxy.ForEach(p => settings.Proxy = p);
                return(RegistryManager.CreateFromConnectionString(
                           this.iotHubConnectionString,
                           settings));
            });

            this.serviceClient = new Lazy <ServiceClient>(
                () =>
            {
                var settings = new ServiceClientTransportSettings();
                proxy.ForEach(p => settings.HttpProxy = p);
                return(ServiceClient.CreateFromConnectionString(
                           this.iotHubConnectionString,
                           DeviceTransportType.Amqp_WebSocket_Only,
                           settings));
            });

            this.eventHubClient = new Lazy <EventHubClient>(
                () =>
            {
                var builder = new EventHubsConnectionStringBuilder(this.eventHubEndpoint)
                {
                    TransportType = EventHubTransportType.AmqpWebSockets
                };
                var client = EventHubClient.CreateFromConnectionString(builder.ToString());
                proxy.ForEach(p => client.WebProxy = p);
                return(client);
            });
        }