public static async Task<List<Route>> ParsePredictionsAsync(XDocument document) { var routes = new List<Route>(); string routeNum; IEnumerable<XElement> rootElements; //If there was an error getting the xml, return an empty list if (document.Root == null) return routes; rootElements = document.Element("body").Elements("predictions"); foreach(XElement predictionElement in rootElements) { string routeTitle = predictionElement.Attribute("routeTitle").Value; routeNum = ParseRouteNum(predictionElement); //Check to see if the route has already been added to the collection if (!routes.Any(r => r.RouteNumber == routeNum)) { Route newRoute = new Route(routeTitle, routeNum); var subElement = predictionElement.Element("direction"); if (subElement != null) { var dirTitle = subElement.Attribute("title").Value; var times = ParseTimes(subElement); newRoute.Directions.Add(new RouteDirection(dirTitle, times)); routes.Add(newRoute); } } else { Route tempRoute = routes.Find(r => r.RouteNumber == routeNum); var subElement = predictionElement.Element("direction"); if (subElement != null) { var dirTitle = subElement.Attribute("title").Value; var times = ParseTimes(subElement); tempRoute.Directions.Add(new RouteDirection(dirTitle, times)); } } } return routes; }
public static async Task<XDocument> GetSearchPredictionsAsync(Stop stop, Route route) { var url = baseUrls["searchPrediction"] + stop.stopId + "&routeTag=" + route.RouteNumber; //Make sure to pull from the network and not cache everytime predictions are refreshed client.DefaultRequestHeaders.IfModifiedSince = DateTime.Now; try { var response = await client.GetAsync(new Uri(url)); response.EnsureSuccessStatusCode(); var reader = await response.Content.ReadAsStringAsync(); xmlDoc = XDocument.Parse(reader); } catch (Exception) { await ErrorHandler.NetworkError("Error getting predictions. Check network connection and try again."); throw; } return xmlDoc; }
public async static Task<XDocument> GetBusLocationsAsync(Route route) { //Because the API requires an epoch time offset for some reason... TimeSpan epoch = (DateTime.UtcNow - new DateTime(1970, 1, 1)); var timestamp = (long)epoch.TotalMilliseconds - TimeSpan.FromMinutes(5).TotalMilliseconds; //if (route.Equals("Powell/Mason Cable Car")) route = "59"; //else if (route.Equals("Powell/Hyde Cable Car")) route = "60"; //else if (route.Equals("California Cable Car")) route = "61"; //else if (route.Contains('-')) //{ // route = route.Substring(0, route.IndexOf('-')); //} var url = baseUrls["busLocations"] + route.RouteNumber + "&t=" + timestamp; try { var response = await client.GetAsync(new Uri(url)); response.EnsureSuccessStatusCode(); string reader = await response.Content.ReadAsStringAsync(); xmlDoc = await Task.Run(() => XDocument.Parse(reader)); } catch (Exception) { await ErrorHandler.NetworkError("Error getting route info. Check network connection and try again."); return null; } return xmlDoc; }
public async Task<List<List<BasicGeoposition>>> GetRoutePathAsync(Route route) { var xmlDoc = await WebHelper.GetRoutePathAsync(route.RouteNumber); return await Task.Run(() => MapHelper.ParseRoutePath(xmlDoc)); }
public async Task LoadDirectionsAsync(Route route) { SelectedRoute = route; SelectedStop = null; //if (route.Equals("Powell/Mason Cable Car")) route = "59"; //else if (route.Equals("Powell/Hyde Cable Car")) route = "60"; //else if (route.Equals("California Cable Car")) route = "61"; //else //{ // route = route.Substring(0, route.IndexOf('-')); //} try { var xmlDoc = await WebHelper.GetRouteDirections(route.RouteNumber); if (xmlDoc != null) { DirectionsList = await Task.Run(() => ParseHelper.ParseDirections(xmlDoc)); allStopsList = await Task.Run(() => ParseHelper.ParseStops(xmlDoc)); await Task.Run(() => ParseHelper.ParseStopTags(xmlDoc, inboundStops, outboundStops)); } } catch (Exception) { throw; } }
public RouteMapViewModel(Route route) { SelectedRoute = route; }