Пример #1
0
        private async Task <bool> AddOrUpdateFlightAsync(bool force = false)
        {
            if (!force && IsSaving)
            {
                if (!isLate)
                {
                    isLate = true;
                    logger.LogInformation("Last save is still running. Skip this save!");
                }
                return(false);
            }

            try
            {
                IsSaving        = true;
                lastSaveAttempt = DateTime.Now;

                var stopWatch = new Stopwatch();
                stopWatch.Start();
                if (FlightData.Id == null)
                {
                    var newData = await flightsAPIClient.AddFlightAsync(FlightData);

                    // Copy data since it might be changed already;
                    newData.Title      = FlightData.Title;
                    newData.Aircraft   = FlightData.Aircraft;
                    newData.FlightPlan = FlightData.FlightPlan;

                    FlightData = newData;
                    HandleFlightDataUpdated();

                    if (FlightRoute.Any())
                    {
                        await flightsAPIClient.PostRouteAsync(FlightData.Id, FlightRoute.Cast <FlightStatus>().ToList());
                    }
                }
                else
                {
                    if (updated)
                    {
                        await flightsAPIClient.UpdateFlightAsync(FlightData.Id, FlightData);
                    }
                    if (FlightRoute.Any())
                    {
                        await flightsAPIClient.PostRouteAsync(FlightData.Id, FlightRoute.Cast <FlightStatus>().ToList());
                    }
                }

                if (isLate)
                {
                    isLate = false;
                    logger.LogWarning($"This save took {stopWatch.ElapsedMilliseconds}ms!");
                }
                stopWatch.Stop();
            }
            catch (HttpRequestException ex)
            {
                logger.LogError(ex, $"Cannot add/update flight! Error: {ex.GetType().FullName} {ex.Message}");
                return(false);
            }
            catch (TaskCanceledException ex)
            {
                logger.LogError(ex, $"Cannot add/update flight! Error: {ex.GetType().FullName} {ex.Message}");
                return(false);
            }
            finally
            {
                IsSaving = false;
            }

            return(true);
        }