Exemplo n.º 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);
        }
Exemplo n.º 2
0
        public async Task <IBleResult> WriteAsync(IWriterBehavior <TIn> writer, TOut value, CancellationToken cancellationToken)
        {
            IBleResult result = null;

            try
            {
                using (var rawDataSequenceEnumerator = _valueConverter.Invoke(value).GetEnumerator())
                {
                    while (rawDataSequenceEnumerator.MoveNext() && result == null)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        var writeChunkResult = await writer.WriteAsync(rawDataSequenceEnumerator.Current, cancellationToken).ConfigureAwait(false);

                        if (!writeChunkResult.IsOperationCompleted)
                        {
                            result = writeChunkResult;
                        }
                    }
                }

                result = result ?? BleResult.Success();
            }
            catch (OperationCanceledException)
            {
                result = BleResult.Failure(BleFailure.OperationCancelled);
            }
            catch (Exception e)
            {
                result = BleResult.Failure(e);
            }

            return(result);
        }
Exemplo n.º 3
0
        public static async Task <IBleResult> WriteAsync <T>(this IWriterBehavior <T> writerBehavior, Func <T> valueProvider, CancellationToken cancellationToken = default)
        {
            IBleResult result;

            try
            {
                result = await writerBehavior.WriteAsync(valueProvider.Invoke(), cancellationToken).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                result = BleResult.Failure(e);
            }

            return(result);
        }
Exemplo n.º 4
0
        public async Task <IBleResult> WriteAsync(IWriterBehavior <TIn> writer, TOut value, CancellationToken cancellationToken)
        {
            IBleResult result;

            try
            {
                var convertedValue = _valueConverter.Invoke(value);
                cancellationToken.ThrowIfCancellationRequested();
                result = await writer.WriteAsync(convertedValue, cancellationToken).ConfigureAwait(false);
            }
            catch (OperationCanceledException)
            {
                result = BleResult.Failure(BleFailure.OperationCancelled);
            }
            catch (Exception e)
            {
                result = BleResult.Failure(e);
            }

            return(result);
        }
Exemplo n.º 5
0
 public Task <IBleResult> ExecuteAsync(CancellationToken cancellationToken)
 {
     return(_writerBehavior.WriteAsync(_constantValue, cancellationToken));
 }
Exemplo n.º 6
0
 public Task <IBleResult> WriteAsync(IWriterBehavior <T> writer, T value, CancellationToken cancellationToken)
 {
     return(_executor.TimeoutBleOperationAsync(ct => writer.WriteAsync(value, ct), _timeout, cancellationToken));
 }