Beispiel #1
0
        /// <summary>
        /// Tries to obtain station info and return window with results.
        /// </summary>
        /// <param name="stationName">Station name.</param>
        /// <param name="dt">Datetime.</param>
        /// <param name="count">Number of departures.</param>
        /// <param name="isStation">Indicates whether it is station or stop.</param>
        /// <param name="routeLabel">Route label.</param>
        /// <param name="win">Window with request.</param>
        /// <returns>Window with results.</returns>
        public static async Task <DepartureBoardResultsWindow> GetStationInfoWindowAsync(string stationName, DateTime dt, int count, bool isStation, string routeLabel, NewStationInfoWindow win)
        {
            Structures.Basic.StationsBasic.StationBasic     station = GetStationFromString(stationName);
            Structures.Basic.RoutesInfoBasic.RouteInfoBasic route   = GetRouteInfoFromLabel(routeLabel);

            if (station == null || (route == null && !string.IsNullOrWhiteSpace(routeLabel)))
            {
                return(null);
            }

            var dbRequest  = new StationInfoRequest(station.ID, dt, count, isStation, route == null ? -1 : route.ID);
            var dbResponse = await SendDepartureBoardRequestAsync(dbRequest);

            return(dbResponse == null ? null : new DepartureBoardResultsWindow(dbResponse, station.Name, dt, true, win));
        }
        private async void FindButtonClicked(object sender, EventArgs e)
        {
            try
            {
                Structures.Basic.StationsBasic.StationBasic station = Request.GetStationFromString(stopEntry.Text);

                if (station == null)
                {
                    return;
                }

                Structures.Basic.RoutesInfoBasic.RouteInfoBasic route = linePicker.SelectedItem == null ? null : Request.GetRouteInfoFromLabel(linePicker.SelectedItem.ToString());

                if (route == null && linePicker.SelectedItem != null)
                {
                    return;
                }

                var dbRequest = new StationInfoRequest(station.ID, leavingTimeDatePicker.Date.Add(leavingTimeTimePicker.Time),
                                                       (int)countSlider.Value, true, route == null ? -1 : route.ID);

                findButton.IsEnabled = false;
                var dbResponse = await Request.SendDepartureBoardRequestAsync(dbRequest);

                findButton.IsEnabled = true;

                if (dbResponse != null)
                {
                    await Navigation.PushAsync(new DepartureBoardResultsPage(dbResponse, true, station.Name), true);
                }
            }
            catch
            {
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                Request.CheckBasicDataValidity();
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
            }
        }
Beispiel #3
0
 /// <summary>
 /// Caches the departures according to departure board request.
 /// </summary>
 private static async Task <bool> CacheDepartureBoardAsync(StationInfoRequest dbRequest, bool forceCache = false) =>
 StationInfoCached.CacheResults(DataFeedDesktop.Basic.Stations.FindByIndex(dbRequest.StopID), DataFeedDesktop.OfflineMode ? new DepartureBoardResponse() : await SendDepartureBoardRequestAsync(dbRequest, forceCache)) != null;
Beispiel #4
0
        private static async Task <DepartureBoardResponse> SendDepartureBoardRequestAsync(StationInfoRequest dbRequest, bool forceCache = false)
        {
            DepartureBoardResponse dbResponse = null;

            if (DataFeedDesktop.OfflineMode)
            {
                await Task.Run(() =>
                {
                    using (var dbProcessing = new Interop.DepartureBoardManaged(DataFeedDesktop.Full, dbRequest))
                    {
                        dbProcessing.ObtainDepartureBoard();
                        dbResponse = dbProcessing.ShowDepartureBoard();
                    }
                });
            }

            else
            {
                using (var dbProcessing = new DepartureBoardProcessing())
                {
                    var cached = StationInfoCached.Select(dbRequest.StopID);

                    if (cached == null || (cached.ShouldBeUpdated || forceCache))
                    {
                        try
                        {
                            if (!await CheckBasicDataValidity())
                            {
                                var results = cached?.FindResultsSatisfyingRequest(dbRequest);
                                return(results?.Departures.Count == 0 ? null : results);
                            }

                            // Process the request immediately so the user does not have to wait until the caching is completed.

                            dbResponse = await dbProcessing.ProcessAsync(dbRequest, dbRequest.Count == -1?int.MaxValue : Settings.TimeoutDuration);

                            // Then update the cache.

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                            if (cached != null && cached.ShouldBeUpdated && dbRequest.Count != -1)
                            {
                                Task.Run(async() => cached.UpdateCache(await dbProcessing.ProcessAsync(cached.ConstructNewRequest(), int.MaxValue)));
                            }
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                        }
                        catch (System.Net.WebException)
                        {
                            MessageBox.Show(Settings.Localization.UnreachableHost, Settings.Localization.Offline, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }

                    else
                    {
                        dbResponse = cached?.FindResultsSatisfyingRequest(dbRequest);
                        if (dbResponse?.Departures.Count == 0)
                        {
                            dbResponse = await dbProcessing.ProcessAsync(dbRequest, Settings.TimeoutDuration);
                        }
                    }
                }
            }
            return(dbResponse);
        }
Beispiel #5
0
 /// <summary>
 /// Caches the departures according to departure board request.
 /// </summary>
 private static async Task <bool> CacheDepartureBoardAsync(StationInfoRequest dbRequest, bool forceUpdate = false) => CanBeCached?
 StationInfoCached.CacheResults(DataFeedClient.Basic.Stations.FindByIndex(dbRequest.StopID), await SendDepartureBoardRequestAsync(dbRequest, forceUpdate)) != null : false;