/// <summary> /// Recreates the Azure Speaker Recognition API profile for an existing user. /// Used when API profile experiece. /// /// Also updates the profile GUID for the corresponding record in the DiScribe database. /// </summary> /// <param name="client"></param> /// <param name="locale"></param> /// <returns></returns> private async Task <Guid> RefreshAPIProfile(User user, string locale = "en-us") { var taskComplete = new TaskCompletionSource <Guid>(); Task <CreateProfileResponse> profileTask = null; try { profileTask = EnrollmentClient.CreateProfileAsync(locale); await profileTask; user.ProfileGUID = profileTask.Result.ProfileId; } catch (AggregateException ex) { Console.Error.WriteLine("Error creating user profile with Azure Speaker Recognition endpoint\n" + ex.InnerException.Message); var failGUID = new Guid(); taskComplete.SetResult(failGUID); return(failGUID); } /*Synchronize object with DiScribe database. Note that if this operation fails, it is not fatal. * The db record will not be consistent with the User object in memory, but application * execution will continue normally otherwise regardless. */ try { user.Update(); } catch (Exception ex) { Console.Error.WriteLine("Unable to update User record in database:" + ex.ToString()); } taskComplete.SetResult(profileTask.Result.ProfileId); return(user.ProfileGUID); }
/// <summary> /// Creates a new user profile for a User in the DiScribe database. /// /// Also creates a corresponding profile with the Azure Speaker Recognition /// endpoint and returns the GUID for that profile on success. /// /// /// </summary> /// <param name="client"></param> /// <param name="locale"></param> /// <returns>Created profile GUID or GUID {00000000-0000-0000-0000-000000000000} on fail</returns> public async Task <Guid> CreateUserProfile(UserParams userParams) { foreach (var profile in UserProfiles) { /*Profile has already been enrolled */ if (profile.Email == userParams.Email) { return(profile.ProfileGUID); } } var taskComplete = new TaskCompletionSource <Guid>(); Task <CreateProfileResponse> profileTask = null; Guid failGuid = new Guid(); try { profileTask = EnrollmentClient.CreateProfileAsync(EnrollmentLocale); await profileTask; } catch (AggregateException ex) { Console.Error.WriteLine("Error creating user profile with Azure Speaker Recognition endpoint\n" + ex.InnerException.Message); taskComplete.SetResult(failGuid); return(failGuid); } userParams.ProfileGUID = profileTask.Result.ProfileId; /*Attempt to Create user profile in DB and add to list of user profiles */ User registeredUser = DatabaseController.CreateUser(userParams); if (registeredUser == null) { taskComplete.SetResult(failGuid); return(failGuid); } UserProfiles.Add(registeredUser); //Add profile to list of profiles managed by this instance taskComplete.SetResult(profileTask.Result.ProfileId); return(profileTask.Result.ProfileId); }