Пример #1
0
        public async Task <IBleResult> WriteAsync(IWriterBehavior <T> writer, T value, CancellationToken cancellationToken)
        {
            IBleResult result;

            try
            {
                if (_connector.IsConnected)
                {
                    result = await writer.WriteAsync(value, cancellationToken).ConfigureAwait(false);
                }
                else
                {
                    var connectionResult = await _connector.ConnectAsync(cancellationToken);

                    if (connectionResult.IsOperationCompleted)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        result = await writer.WriteAsync(value, cancellationToken);
                    }
                    else
                    {
                        result = BleResult.Failure(BleFailure.DeviceNotConnected, connectionResult);
                    }
                }
            }
            catch (OperationCanceledException)
            {
                result = BleResult.Failure(BleFailure.OperationCancelled);
            }

            return(result);
        }
        public override async Task <IBleResult> StartObservingAsync(IObserverBehavior <T> decoratedObserver, CancellationToken cancellationToken)
        {
            IBleResult result;

            try
            {
                if (_connector.IsConnected)
                {
                    result = await decoratedObserver.StartObservingAsync(cancellationToken).ConfigureAwait(false);
                }
                else
                {
                    var connectionResult = await _connector.ConnectAsync(cancellationToken);

                    if (connectionResult.IsOperationCompleted)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        result = await decoratedObserver.StartObservingAsync(cancellationToken);
                    }
                    else
                    {
                        result = BleResult.Failure(BleFailure.DeviceNotConnected, connectionResult);
                    }
                }
            }
            catch (OperationCanceledException)
            {
                result = BleResult.Failure(BleFailure.OperationCancelled);
            }

            return(result);
        }
        public async Task <IBleResult <TOut> > ReadAsync(IParametrizedReaderBehavior <TIn, TOut> parametrizedReader, TIn parameter, CancellationToken cancellationToken)
        {
            IBleResult <TOut> result;

            try
            {
                if (_connector.IsConnected)
                {
                    result = await parametrizedReader.ReadAsync(parameter, cancellationToken).ConfigureAwait(false);
                }
                else
                {
                    var connectionResult = await _connector.ConnectAsync(cancellationToken);

                    if (connectionResult.IsOperationCompleted)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        result = await parametrizedReader.ReadAsync(parameter, cancellationToken);
                    }
                    else
                    {
                        result = BleResult.Failure <TOut>(BleFailure.DeviceNotConnected, connectionResult);
                    }
                }
            }
            catch (OperationCanceledException)
            {
                result = BleResult.Failure <TOut>(BleFailure.OperationCancelled);
            }

            return(result);
        }
Пример #4
0
 public virtual Task <IBleResult> ConnectAsync(IConnectBehavior connector, CancellationToken cancellationToken)
 {
     return(connector.ConnectAsync(cancellationToken));
 }