Example #1
0
        public async Task GetRouteDetails(Route route)
        {
            //Device.BeginInvokeOnMainThread(() => UserDialogs.Instance.ShowLoading("Configuring Details from DB...", MaskType.Black));
            //UserDialogs.Instance.ShowLoading($"Configuring Details from DB...", MaskType.Black);

            var count = await _dbHelper.ConfigureRoute(route);

            //UserDialogs.Instance.HideLoading();

            if (!route.IsConfigured)
            {
                UserDialogs.Instance.ShowLoading($"Configuring Details from Service", MaskType.Black);
                await Task.Delay(TimeSpan.FromMilliseconds(1));

                //http://webservices.nextbus.com/service/publicXMLFeed?command=routeConfig&a=sf-muni&r=N

                string xml = _client.GetStringAsync(
                    EndPoints.RouteConfigUrl(route.AgencyTag, route.Tag)).Result;

                var url = EndPoints.RouteConfigUrl(route.AgencyTag, route.Tag);

                var doc = XDoc.LoadXml(xml);


                var stops = new List <Stop>();
                foreach (var stopNode in doc.GetDescendantElements("stop"))
                {
                    var stop = new Stop();
                    stop.Lat = 0;
                    stop.Lon = 0;
                    var lat = stopNode.GetAttribute("lat");
                    var lon = stopNode.GetAttribute("lon");
                    try
                    {
                        try
                        {
                            stop.Lat = Convert.ToDouble(stopNode.GetAttribute("lat"));
                            stop.Lon = Convert.ToDouble(stopNode.GetAttribute("lon"));
                        }
                        catch
                        {
                        }

                        stop.Tag    = stopNode.GetAttribute("tag");
                        stop.Title  = stopNode.GetAttribute("title");
                        stop.StopId = stopNode.GetAttribute("stopId");

                        stop.RouteTag    = route.Tag;
                        stop.AgencyTag   = route.AgencyTag;
                        stop.RouteTitle  = route.Title;
                        stop.AgencyTitle = route.AgencyTitle;
                        stops.Add(stop);
                    }
                    catch (Exception ex)
                    {
                        var ss = ex.Message;
                    }
                }


                //foreach (XElement directionNode in dirN
                //{

                //}

                //directions
                route.Directions.Clear();
                foreach (XElement directionNode in doc.GetDescendantElements("direction"))
                {
                    var direction = new Direction();

                    direction.Tag      = directionNode.GetAttribute("tag");
                    direction.Id       = $"{route.Id}.{direction.Tag}";
                    direction.ParentId = route.Id;
                    direction.Title    = directionNode.GetAttribute("title");
                    direction.Name     = directionNode.GetAttribute("name");
                    direction.UseForUI = directionNode.GetAttribute("useForUI");
                    direction.RouteTag = route.Tag;


                    route.Directions.Add(direction);

                    //get stops
                    direction.Stops.Clear();
                    var order = 1;

                    foreach (var stopIdNode in directionNode.Descendants())
                    {
                        var tag  = stopIdNode.GetAttribute("tag");
                        var stop = stops.FirstOrDefault(x => x.Tag == tag);
                        if (stop != null)
                        {
                            stop.Id    = $"{direction.Id}.{stop.Tag}";
                            stop.Order = order;
                            order++;

                            stop.ParentId       = direction.Id;
                            stop.DirectionTitle = direction.Title;
                            stop.AgencyTitle    = route.AgencyTitle;
                            direction.Stops.Add(stop);
                            stop.DirectionTag = direction.Tag;
                            if (stop.Tag != tag)
                            {
                                stop.OtherStops.Add(stop);
                            }
                            _dbHelper.SaveStop(stop);
                        }
                    }


                    _dbHelper.SaveDirection(direction);
                }

                //var pathXml = "<path>";
                //foreach (var pathNode in doc.Descendants().Where(x => x.Name == "point"))
                //{
                //    var geoPoint = new GeoPoint
                //    {
                //        ParentId = route.Tag,
                //        Lat = Convert.ToDouble(pathNode.GetAttribute("lat")),
                //        Lon = Convert.ToDouble(pathNode.GetAttribute("lon"))
                //    };
                //    //DbHelper.Instance.SaveEntity(geoPoint);
                //    route.Path.Add(geoPoint);
                //    pathXml += pathNode.ToString();
                //}
                //pathXml += "</path>";
                //route.PathData = pathXml;
                route.IsConfigured = true;
                _dbHelper.SaveRoute(route);



                UserDialogs.Instance.HideLoading();
            }



            //?command=predictions&a=sf-muni&r=2&s=6594&useShortTitles=true
            //http://webservices.nextbus.com/service/publicXMLFeed?command=predictions&a=sf-muni&r=2&s=6594&useShortTitles=true
        }