예제 #1
0
        /// <summary>
        /// Gets the travel time.
        /// </summary>
        /// <param name="destination">The destination.</param>
        /// <returns></returns>
        public IObservable<TravelTime> GetTravelTime(string destination)
        {
            return new AnonymousObservable<TravelTime>(observer =>
            {
                IObservable<string> observable = ObservableHelper.TextFromUri(new Uri(DfwTranstarUri.BaseUri + destination));

                observable.Subscribe(result =>
                {
                    if (result != null)
                    {
                        int tableStart = result.IndexOf("<span id=\"RoadwayList\"");
                        int tableEnd = result.IndexOf("</span>", tableStart);

                        XElement xmlDoc = XElement.Parse(result.Substring(tableStart, (tableEnd + 7) - tableStart));

                        int i = 0;
                        TravelTime travelTime = new TravelTime();

                        foreach (var node in xmlDoc.Nodes())
                        {
                            switch (i)
                            {
                                case 4:
                                    travelTime.Roadway = node.ToString();
                                    break;

                                case 6:
                                    travelTime.Route = node.ToString();
                                    break;

                                case 8:
                                    travelTime.Distance = node.ToString();
                                    break;

                                case 10:
                                    travelTime.Time = node.ToString().Replace("Travel Time (mm:ss) - ", "");
                                    break;

                                case 12:
                                    travelTime.Speed = node.ToString();
                                    break;
                            }

                            i++;
                        }

                        observer.OnNext(travelTime);
                    }

                    observer.OnCompleted();
                }, ex =>
                {
                    observer.OnError(new Exception("Unable to retrieve traffic data.", ex));
                });

                return Disposable.Empty;
            });
        }
        /// <summary>
        /// Loads the data.
        /// </summary>
        /// <param name="destination">The destination.</param>
        public void LoadData(string destination)
        {
            this.IsDataLoading = true;

            var observable = this._dallasTranstarService.GetTravelTime(destination);

            observable
                .ObserveOnDispatcher()
                .Subscribe(tt =>
                {
                    if (tt != null)
                    {
                        this.TravelTime = tt;
                    }
                }, ex =>
                {
                    this._dataRetrievalErrorInteractionRequest.Raise(new InteractionReq.Notification()
                    {
                        Title = "Error",
                        Content = "Unable to retrieve traffic data."
                    });

                    this.IsDataLoading = false;
                }, () =>
                {
                    this.IsDataLoading = false;
                });
        }