Exemplo n.º 1
0
        public async Task <bool> connectAsync()
        {
            if (string.IsNullOrEmpty(_appConfigService.AssignedEndPoint))
            {
                await ProvisionAsync();
            }

            var deviceId    = _deviceInfoService.GetDeviceId();
            var symetricKey = IoTHelpers.GenerateSymmetricKey(deviceId, _appConfigService.DpsSymetricKey);

            var sasToken = IoTHelpers.GenerateSasToken(_appConfigService.AssignedEndPoint, symetricKey, null);

            _deviceClient = DeviceClient.Create(_appConfigService.AssignedEndPoint,
                                                new DeviceAuthenticationWithToken(deviceId, sasToken),
                                                TransportType.Mqtt_WebSocket_Only);

            _deviceClient.SetConnectionStatusChangesHandler(ConnectionStatusChangesHandler);

            await _deviceClient.OpenAsync();

            return(true);
        }
Exemplo n.º 2
0
        private async Task <bool> ProvisionAsync()
        {
            var dpsGlobalEndpoint = _appConfigService.DpsGlobalEndpoint;
            var dpsIdScope        = _appConfigService.DpsIdScope;
            var deviceId          = _deviceInfoService.GetDeviceId();
            var dpsSymetricKey    = IoTHelpers.GenerateSymmetricKey(deviceId, _appConfigService.DpsSymetricKey);

            using (var security = new SecurityProviderSymmetricKey(deviceId, dpsSymetricKey, dpsSymetricKey))
            {
                using (var transport = new ProvisioningTransportHandlerHttp())
                {
                    var provisioningClient = ProvisioningDeviceClient.Create(dpsGlobalEndpoint, dpsIdScope, security, transport);

                    var regResult = await provisioningClient.RegisterAsync();

                    if (regResult.Status == ProvisioningRegistrationStatusType.Assigned)
                    {
                        _appConfigService.AssignedEndPoint = regResult.AssignedHub;
                    }
                    return(true);
                }
            }
        }