Beispiel #1
0
 public BusRouteStop(BusRoute route, BusStop stop, TimeSpan eta, int stopNumber)
 {
     Route      = route;
     Stop       = stop;
     ETA        = eta;
     StopNumber = stopNumber;
 }
Beispiel #2
0
        internal static BusRouteStop[] GetBusStops(BusRoute route, string csv)
        {
            if (csv == null)
            {
                return(null);
            }

            string[] lines     = csv.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            string[] stopNames = lines[0].Split(',');

            bool multipleRoutes = false;

            BusStop[] stops = new BusStop[stopNames.Length];
            for (int i = 0; i < stopNames.Length; i++)
            {
                if (string.Compare(stopNames[i], "route", true) == 0)
                {
                    multipleRoutes = true;
                    continue;
                }

                //Parse individual stop
                //int lastDash = stopNames[i].LastIndexOf('-')
                string[] stopArgs = stopNames[i].Split(new string[] { " - " }, StringSplitOptions.RemoveEmptyEntries);
                if (stopArgs.Length < 2)
                {
                    continue;
                }

                string crossSt    = HttpUtility.HtmlDecode(stopArgs[stopArgs.Length > 2 ? 1 : 0].Trim('*', ' '));
                string stopName   = HttpUtility.HtmlDecode(stopArgs.Length > 2 ? stopArgs[0].Trim('*', ' ') : crossSt);
                string stopNumStr = stopArgs[stopArgs.Length - 1].Trim('*', ' ');

                int stopNum = -1;
                if (stopNumStr.StartsWith("Stop #"))
                {
                    int.TryParse(stopNumStr.Substring(6), out stopNum);
                }

                BusStop stop = new BusStop(stopName, crossSt, stopNum);
                stops[i] = stop;
            }

            BusRouteStop[] routeStops = new BusRouteStop[(lines.Length - 1) * stops.Length];
            for (int i = 1; i < lines.Length; i++)
            {
                //string routeTimes
                string[] times = lines[i].Split(',');
                if (multipleRoutes)
                {
                    string routeNameStr = times[0];
                    if (string.Compare(routeNameStr, route.RouteName) != 0)
                    {
                        continue;
                    }
                }

                for (int t = 0; t < times.Length; t++)
                {
                    string time = times[t].Length > 8 ? times[t].Substring(0, times[t].LastIndexOf(' ')) : times[t];
                    time = time.Replace("\'B\'", "").Replace("\'AB\'", "").Replace("\'H\'", "").Trim('\'').Trim(' ');
                    if (time.Contains("---") || time.Length > 8 || time.Length < 4)
                    {
                        continue;
                    }

                    DateTime?date = null;
                    try
                    {
                        date = DateTime.ParseExact(time, "h:mm tt", CultureInfo.InvariantCulture);
                    }
                    catch
                    {
                        try
                        {
                            date = DateTime.ParseExact(time, "h:mm", CultureInfo.InvariantCulture);
                        }
                        catch
                        {
                        }
                    }

                    if (date.HasValue)
                    {
                        TimeSpan     eta       = date.Value.TimeOfDay;
                        BusRouteStop routeStop = new BusRouteStop(route, stops[t], eta, t);
                        routeStops[(i - 1) * times.Length + t] = routeStop;
                    }
                }
            }

            return(routeStops);
        }
Beispiel #3
0
        internal static string GenerateRouteInserts(List <BusRoute> routes)
        {
            //Get list of stops
            List <BusStop> stops        = new List <BusStop>();
            List <int>     availableIds = Enumerable.Range(10000, 89999).ToList();

            for (int i = 0; i < routes.Count; i++)
            {
                for (int j = 0; j < routes[i].Stops.Length; j++)
                {
                    if (routes[i].Stops[j] != null)
                    {
                        BusStop curStop = routes[i].Stops[j].Stop;
                        if (curStop != null && !stops.Contains(curStop))
                        {
                            stops.Add(curStop);
                            if (curStop.StopID != -1)
                            {
                                availableIds.Remove(curStop.StopID);
                            }
                        }
                    }
                }
            }

            //Give stops without an ID an ID.
            Random rng = new Random();

            for (int j = 0; j < stops.Count; j++)
            {
                if (stops[j].StopID == -1)
                {
                    int index    = rng.Next(availableIds.Count);
                    int uniqueId = availableIds[index];
                    availableIds.RemoveAt(index);
                    stops[j].StopID = uniqueId;
                }
            }

            StringBuilder bldr = new StringBuilder();

            bldr.AppendLine("-- Insert routes");
            for (int i = 0; i < routes.Count; i++)
            {
                bldr.AppendFormat("INSERT INTO BUS_ROUTE Values (\"{0}\", \"{1}\", \"{2}\", {3});\r\n",
                                  routes[i].ToName,
                                  routes[i].FromName,
                                  routes[i].RouteNumber,
                                  routes[i].DaysOfOperation
                                  );
            }

            bldr.AppendLine();
            bldr.AppendLine("-- Insert stops");
            for (int i = 0; i < stops.Count; i++)
            {
                bldr.AppendFormat("INSERT INTO BUS_STOP Values ({0}, \"{1}\", \"{2}\");\r\n",
                                  stops[i].StopID,
                                  stops[i].Name,
                                  stops[i].CrossStreet
                                  );
            }

            bldr.AppendLine();
            bldr.AppendLine("-- Insert route stops");
            HashSet <string> routesInserted = new HashSet <string>();

            for (int i = 0; i < routes.Count; i++)
            {
                for (int j = 0; j < routes[i].Stops.Length; j++)
                {
                    BusRoute     route     = routes[i];
                    BusRouteStop routeStop = route?.Stops[j];
                    BusStop      stop      = routeStop?.Stop;
                    if (routeStop != null && stop != null)
                    {
                        if (stop.StopID == -1)
                        {
                            stop = stops.Find(t => t.Equals(stop));
                        }

                        if (stop != null)
                        {
                            string routeStr = string.Format("INSERT INTO BUS_ROUTE_STOPS Values (\"{0}\", \"{1}\", \"{2}\", {3}, \"{4}\", {5});",
                                                            route.FromName,
                                                            route.ToName,
                                                            route.RouteNumber,
                                                            stop.StopID,
                                                            new DateTime(routeStop.ETA.Ticks).ToString("hh:mm:sss"),
                                                            routeStop.StopNumber
                                                            );
                            if (!routesInserted.Contains(routeStr))
                            {
                                routesInserted.Add(routeStr);
                                bldr.AppendLine(routeStr);
                            }
                        }
                    }
                }
            }

            return(bldr.ToString());
        }
Beispiel #4
0
        internal static void GenerateBusInserts()
        {
            Random rng = new Random();

            int        busCount         = 1540;
            List <int> availableNumbers = Enumerable.Range(1, 9998).ToList();

            List <BusRoute> routes        = new List <BusRoute>();
            List <string>   driverIds     = null;
            List <string>   baseAddresses = null;

            using (MetroDB db = new MetroDB("guest", "guest"))
            {
                if (db.Connect())
                {
                    var routeData = db.GetTable("BUS_ROUTE");
                    for (int i = 0; i < routeData[0].Count; i++)
                    {
                        BusRoute br = new BusRoute();
                        br.ToName          = routeData[0][i];
                        br.FromName        = routeData[1][i];
                        br.RouteNumber     = int.Parse(routeData[2][i]);
                        br.RouteName       = routeData[2][i];
                        br.DaysOfOperation = byte.Parse(routeData[3][i]);
                        routes.Add(br);
                    }

                    var driverDat = db.GetTable("DRIVER");
                    driverIds = driverDat[0];

                    var basesDat = db.GetTable("BASE");
                    baseAddresses = basesDat[0];
                }
                else
                {
                    Console.WriteLine("Unable to retrieve data. Cannot generate bus data.");
                    return;
                }
            }

            StringBuilder   bldr   = new StringBuilder();
            List <BusRoute> distro = new List <BusRoute>(routes);

            for (int i = 0; i < busCount; i++)
            {
                int bIndex = rng.Next(availableNumbers.Count);
                int bNum   = availableNumbers[bIndex];
                availableNumbers.RemoveAt(bIndex);

                int    dIndex = rng.Next(driverIds.Count);
                string dId    = driverIds[dIndex];
                driverIds.RemoveAt(dIndex);

                int      rIndex = rng.Next(distro.Count);
                BusRoute route  = distro[rIndex];
                distro.RemoveAt(rIndex);
                if (distro.Count == 0)
                {
                    distro.AddRange(routes);
                }

                int    mIndex  = rng.Next(models.Length);
                string model   = models[rng.Next(mIndex)];
                int    seatCnt = seats[mIndex];

                double rngNorm     = rng.NextNormal(0, 1);
                int    milesDriven = (int)((3 + rngNorm) * ((models.Length - mIndex) * 25000));

                int    baIndex = rng.Next(baseAddresses.Count);
                string bAddr   = baseAddresses[baIndex];


                bldr.AppendFormat("INSERT INTO BUS Values ({0}, {1}, \"{2}\", \"{3}\", {4}, \"{5}\", {6}, {7}, \"{8}\");\r\n",
                                  bNum,
                                  dId,
                                  route.ToName,
                                  route.FromName,
                                  route.RouteNumber,
                                  model,
                                  seatCnt,
                                  milesDriven,
                                  bAddr);
            }

            string queries = bldr.ToString();
            string curDir  = Environment.CurrentDirectory;

            File.WriteAllText(Path.Combine(curDir, "buses.sql"), queries);
        }
Beispiel #5
0
        internal static BusRoute[] GetKingCountyRoute(HtmlWeb webLoader, Uri uri)
        {
            HtmlDocument routeDoc = webLoader.Load(uri);

            if (uri.AbsoluteUri.EndsWith(".pdf"))
            {
                return(new BusRoute[0]);
            }

            HtmlNode routeNode = routeDoc.DocumentNode;

            string routeNumber = HttpUtility.HtmlDecode(StripHTML(GetParameter(routeNode.OuterHtml, "var routeNumber=\'", "\'")));
            string routeName   = HttpUtility.HtmlDecode(StripHTML(GetParameter(routeNode.OuterHtml, "var routeName=\'", "\'")))?.TrimStart('0');
            string aDestShort  = HttpUtility.HtmlDecode(StripHTML(GetParameter(routeNode.OuterHtml, "var aDestShort=\'", "\'")))?.Replace("To ", "").Trim();
            string bDestShort  = HttpUtility.HtmlDecode(StripHTML(GetParameter(routeNode.OuterHtml, "var bDestShort=\'", "\'")))?.Replace("To ", "").Trim();

            HtmlNode daysNode = routeNode.SelectSingleNode("//*[@id=\"schedule_wrapper\"]/ul[1]");

            if (daysNode == null)
            {
                return(new BusRoute[0]);
            }

            byte daysOfOperation = 0b1111100;
            bool onlyWeekday     = routeNode.OuterHtml.Contains("/* Hiding Saturday and Sunday buttons */");

            if (!onlyWeekday)
            {
                daysOfOperation = 0b1111111;
            }

            int routeId = -1;

            if (!int.TryParse(routeNumber, out routeId))
            {
                return(new BusRoute[0]);
            }

            BusRoute toRoute = new BusRoute();

            toRoute.RouteName       = routeName;
            toRoute.RouteNumber     = routeId;
            toRoute.FromName        = aDestShort;
            toRoute.ToName          = bDestShort;
            toRoute.DaysOfOperation = daysOfOperation;

            string toCsv = GetRouteTableCSV(routeNumber, true);

            BusRouteStop[] routeStops = GetBusStops(toRoute, toCsv);
            toRoute.Stops = routeStops;

            BusRoute fromRoute = new BusRoute();

            fromRoute.RouteName       = routeName;
            fromRoute.RouteNumber     = routeId;
            fromRoute.FromName        = bDestShort;
            fromRoute.ToName          = aDestShort;
            fromRoute.DaysOfOperation = daysOfOperation;

            string fromCsv = GetRouteTableCSV(routeNumber, false);

            BusRouteStop[] fromStopData = GetBusStops(fromRoute, fromCsv);
            fromRoute.Stops = fromStopData;

            return(new BusRoute[]
            {
                toRoute,
                fromRoute
            });
        }