コード例 #1
0
        protected async Task GetOrCreateDeviceIdentity()
        {
            Microsoft.Azure.Devices.IotHubConnectionStringBuilder builder = Microsoft.Azure.Devices.IotHubConnectionStringBuilder.Create(this.iothubConnectionString);
            RegistryManager rm = RegistryManager.CreateFromConnectionString(builder.ToString());

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

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

                this.context = new DeviceContext
                {
                    Device = device,
                    IotHubConnectionString = this.iothubConnectionString,
                    RegistryManager        = rm,
                    RemoveDevice           = false,
                    MessageGuid            = Guid.NewGuid().ToString()
                };
            }
            else
            {
                await this.CreateDeviceIdentity(rm);
            }
        }
コード例 #2
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);
            }
        }
コード例 #3
0
        protected async Task GetOrCreateDeviceIdentityAsync()
        {
            IotHubConnectionStringBuilder builder = IotHubConnectionStringBuilder.Create(this.iothubConnectionString);
            RegistryManager rm = RegistryManager.CreateFromConnectionString(builder.ToString());

            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 thumprints 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]
                        };
                        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);
            }
        }