private async Task <bool> Provision() { var dpsGlobalEndpoint = _appConfigService.DpsGlobalEndpoint; var dpsIdScope = _appConfigService.DpsIdScope; var deviceId = _deviceInfoService.GetDeviceId(); var dpsSymetricKey = IoTHelper.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); } } }
public async Task <bool> ConnectAsync() { var deviceId = _deviceInfoService.GetDeviceId(); if (string.IsNullOrEmpty(_appConfigService.AssignedEndPoint)) { await Provision(); } var symetricKey = IoTHelper.GenerateSymmetricKey(deviceId, _appConfigService.DpsSymetricKey); var sasToken = IoTHelper.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); }