Exemplo n.º 1
0
            public async void UpdateConditions()
            {
                if (_saveable.IsLoading)
                {
                    return;
                }
                _updateCancellationTokenSource?.Cancel();

                if (!RealConditions)
                {
                    IsTemperatureClamped  = false;
                    IsWeatherNotSupported = false;
                    ManualTime            = true;
                    RealWeather           = null;
                    _weatherTypeHelper.Reset();
                }
                else
                {
                    ManualTime = RealConditionsManualTime;

                    using (var cancellation = new CancellationTokenSource()) {
                        _updateCancellationTokenSource = cancellation;

                        try {
                            await RealConditionsHelper.UpdateConditions(SelectedTrack, RealConditionsLocalWeather, RealConditionsTimezones,
                                                                        RealConditionsManualTime?default(Action <int>) : TryToSetTime, weather => {
                                RealWeather = weather;
                                TryToSetTemperature(weather.Temperature);
                                SelectedWeatherType = weather.Type;
                                TryToSetWeather();
                            }, cancellation.Token);
                        } catch (TaskCanceledException) {} catch (Exception e) {
                            Logging.Warning(e);
                        }

                        _updateCancellationTokenSource = null;
                    }
                }
            }
            private async Task UpdateConditions(bool reset)
            {
                _conditionsTokenSource?.Cancel();

                var round = _acObject.CurrentRound;

                if (round == null)
                {
                    return;
                }

                if (reset)
                {
                    _weatherTypeHelper.Reset();
                }

                if (!_currentSet || !_acObject.RealConditions || _acObject.RealConditionsManualTime)
                {
                    _currentRoundTime = round.Time;
                }

                if (!_currentSet || !_acObject.RealConditions)
                {
                    _currentSet = true;
                    _currentRoundTemperature = round.Temperature;
                    _currentRoundWeather     = round.Weather;
                }

                if (!_acObject.RealConditions)
                {
                    OnPropertyChanged(nameof(CurrentRoundWeather));
                    OnPropertyChanged(nameof(CurrentRoundTemperature));
                    return;
                }

                WeatherType?weatherType = null;

                ConditionsLoading = true;

                using (var cancellation = new CancellationTokenSource()) {
                    _conditionsTokenSource = cancellation;
                    try {
                        await RealConditionsHelper.UpdateConditions(round.Track, false, true,
                                                                    _acObject.RealConditionsManualTime
                                                                    ?default(Action <int>)
                                                                    : t => {
                            _currentRoundTime = t.Clamp(CommonAcConsts.TimeMinimum, CommonAcConsts.TimeMaximum);
                        }, w => {
                            _currentRoundTemperature = w.Temperature.Clamp(CommonAcConsts.TemperatureMinimum,
                                                                           CommonAcConsts.TemperatureMaximum);
                            weatherType = w.Type;
                        }, cancellation.Token);

                        if (cancellation.Token.IsCancellationRequested)
                        {
                            return;
                        }
                    } finally {
                        OnPropertyChanged(nameof(CurrentRoundTemperature));
                        OnPropertyChanged(nameof(CurrentRoundTime));
                        if (ReferenceEquals(_conditionsTokenSource, cancellation))
                        {
                            _conditionsTokenSource = null;
                        }
                    }
                }

                ConditionsLoading = false;

                if (weatherType != null)
                {
                    _weatherTypeHelper.SetParams(CurrentRoundTime, CurrentRoundTemperature);
                    var weather = CurrentRoundWeather;
                    if (_weatherTypeHelper.TryToGetWeather(weatherType.Value, ref weather))
                    {
                        CurrentRoundWeather = weather;
                    }
                    else
                    {
                        OnPropertyChanged(nameof(CurrentRoundWeather));
                    }
                }
            }