public override void Init(IMavlinkPayloadClient client)
 {
     base.Init(client);
     Register <Dictionary <string, string> >(WellKnownDiag.DiagStringsValueName).Subscribe(OnDataString, _cancel.Token);
     Register <Dictionary <string, double> >(WellKnownDiag.DiagDigitValueName).Subscribe(OnDataDigit, _cancel.Token);
     Register <Dictionary <string, string> >(WellKnownDiag.DiagSettingsValueName).Subscribe(OnDataSettings, _cancel.Token);
 }
Пример #2
0
        public static async Task <TOut> Send <TIn, TOut>(this IMavlinkPayloadClient src, string path, TIn data,
                                                         TimeSpan timeout, int attemptsCount, CancellationToken cancel, Action <int> progressCallback)
            where TOut : new()
        {
            progressCallback = progressCallback ?? (i => { });
            Exception lastError = null;
            var       result    = default(TOut);

            for (var i = 0; i < attemptsCount; i++)
            {
                progressCallback(i + 1);
                cancel.ThrowIfCancellationRequested();
                _logger.Debug(
                    $"Call {path}. Attempt {i + 1} of {attemptsCount} with {timeout:g} timeout. Args {JsonConvert.SerializeObject(data)}");
                var tokenWithTimeout = new CancellationTokenSource(timeout);
                using var linkedToken = CancellationTokenSource.CreateLinkedTokenSource(cancel, tokenWithTimeout.Token);
                try
                {
                    result = await src.Send <TIn, TOut>(path, data, linkedToken.Token).ConfigureAwait(false);

                    return(result);
                }
                catch (TimeoutException e)
                {
                    lastError = e;
                    _logger.Warn($"Timeout to call {path}. Attept {i + 1} of {attemptsCount}");
                }
                catch (PayloadClientException e)
                {
                    lastError = e;
                    _logger.Error(e.Message);
                    break;
                }
                catch (Exception e)
                {
                    lastError = e;
                    _logger.Warn($"Error occured to call {path}. Attept {i + 1} of {attemptsCount}");
                }
                finally
                {
                    linkedToken?.Dispose();
                    tokenWithTimeout?.Dispose();
                }
            }

            Debug.Assert(lastError != null);
            throw lastError;
        }
 public virtual void Init(IMavlinkPayloadClient client)
 {
     _client = client ?? throw new ArgumentNullException(nameof(client));
 }