/// <inheritdoc /> public override async Task <ISpatialCoordinate> LocalizeAsync(CancellationToken cancellationToken) { DebugLog($"Localizing, CanBeCanceled:{cancellationToken.CanBeCanceled}, IsCancellationRequested:{cancellationToken.IsCancellationRequested}"); ISpatialCoordinate coordinate = null; using (var cancellableCTS = CancellationTokenSource.CreateLinkedTokenSource(defaultCancellationToken, cancellationToken)) { if (!TrySendMarkerVisualDiscoveryMessage()) { Debug.LogWarning("Failed to send marker visual discovery message, spatial localization failed."); return(null); } // Receive marker to show DebugLog("Waiting to have a coordinate id assigned"); await Task.WhenAny(coordinateAssigned.Task, Task.Delay(-1, cancellableCTS.Token)); if (string.IsNullOrEmpty(coordinateId)) { DebugLog("Failed to assign coordinate id"); return(null); } using (var cts = CancellationTokenSource.CreateLinkedTokenSource(discoveryCTS.Token, cancellableCTS.Token)) { DebugLog($"Attempting to discover coordinate: {coordinateId}, CanBeCanceled:{cts.Token.CanBeCanceled}, IsCancellationRequested:{cts.Token.IsCancellationRequested}"); if (await coordinateService.TryDiscoverCoordinatesAsync(cts.Token, new string[] { coordinateId.ToString() })) { DebugLog($"Coordinate discovery completed: {coordinateId}"); if (!coordinateService.TryGetKnownCoordinate(coordinateId, out coordinate)) { DebugLog("Failed to find spatial coordinate although discovery completed."); } } else { DebugLog("TryDiscoverCoordinatesAsync failed."); } } DebugLog($"Waiting for coordinate to be found: {coordinateId}"); await Task.WhenAny(coordinateFound.Task, Task.Delay(-1, cancellableCTS.Token)); } return(coordinate); }
/// <inheritdoc /> public override async Task <ISpatialCoordinate> LocalizeAsync(CancellationToken cancellationToken) { if (!defaultCancellationToken.CanBeCanceled) { Debug.LogError("Session is invalid. No localization performed."); return(null); } DebugLog($"Waiting for marker visual, CanBeCanceled:{cancellationToken.CanBeCanceled}, IsCancellationRequested:{cancellationToken.IsCancellationRequested}"); using (var cancellableCTS = CancellationTokenSource.CreateLinkedTokenSource(defaultCancellationToken, cancellationToken)) { await Task.WhenAny(coordinateAssigned.Task, Task.Delay(-1, cancellableCTS.Token)); if (string.IsNullOrEmpty(coordinateId)) { DebugLog("Failed to assign coordinate id"); return(null); } ISpatialCoordinate coordinate = null; using (var cts = CancellationTokenSource.CreateLinkedTokenSource(discoveryCTS.Token, cancellableCTS.Token)) { DebugLog($"Attempting to discover coordinate: {coordinateId}, CanBeCanceled:{cts.Token.CanBeCanceled}, IsCancellationRequested:{cts.Token.IsCancellationRequested}"); if (await coordinateService.TryDiscoverCoordinatesAsync(cts.Token, new string[] { coordinateId.ToString() })) { DebugLog($"Coordinate discovery completed: {coordinateId}"); if (!coordinateService.TryGetKnownCoordinate(coordinateId, out coordinate)) { DebugLog("Failed to find spatial coordinate although discovery completed."); } else { SendCoordinateFound(coordinate.Id); return(coordinate); } } else { DebugLog("TryDiscoverCoordinatesAsync failed."); } } } return(null); }