Exemplo n.º 1
0
        public Timetable BuildTimetable(ITransportInterface data)
        {
            DateTime startLoad = DateTime.Now;

            Timetable timetable = new Timetable();
            int totalTrainsPerWeek = 0;
            List<string> timetableSources = data.TimetableUrls;

            // download all the webpages concurrently
            WebDownloader downloader = new WebDownloader();
            Dictionary<string, string> downloadedPages = downloader.downloadWebpagesConcurrently(timetableSources);

            foreach (string s in timetableSources)
            {
                string webPage = downloadedPages[s];
                ITimetableExtractor timetableExtractor = data.TimetableExtractor;
                Service service = timetableExtractor.ExtractTimetable(webPage);
                timetable.AddService(service);
                totalTrainsPerWeek += service.NumberRoutes;
            }

            DateTime finishLoad = DateTime.Now;
            TimeSpan diff = finishLoad - startLoad;
            Console.WriteLine("Timetable data downloaded in {0}s{1}ms", (int)diff.TotalSeconds, diff.Milliseconds);
            return timetable;
        }
Exemplo n.º 2
0
        public static bool LoadTimetable(FileInfo sqlDB, Timetable timetable)
        {
            DateTime startLoad = DateTime.Now;

            // LOAD THE NETWORK
            Dictionary<string, int> aliases = getNetworkAliases(sqlDB);

            using (SQLiteConnection sql_connection = SQLiteDatabase.GetConnection(sqlDB))
            {
                sql_connection.Open();
                using (SQLiteTransaction sql_Transaction = sql_connection.BeginTransaction())
                {
                    Dictionary<string, string> dic;

                    // insert the timetable
                    dic = new Dictionary<string, string>();
                    dic.Add(SQLFieldNames.TIMETABLE_NAME, "NULL");
                    int timetable_id = SQLiteDatabase.Insert(sql_connection, SQLFieldNames.TIMETABLES, dic);
                    timetable.ID = timetable_id;

                    // insert the services
                    foreach (Service s in timetable.services)
                    {
                        // get the date for this
                        int startdate = 0;
                        if (!s.StartDate.Equals(DateTime.MinValue))
                            startdate = GetSQLDate(s.StartDate);

                        int enddate = 0;
                        if (!s.EndDate.Equals(DateTime.MaxValue))
                            enddate = GetSQLDate(s.EndDate);

                        dic = new Dictionary<string, string>();
                        dic.Add(SQLFieldNames.SERVICES_NAME, s.Name);
                        dic.Add(SQLFieldNames.SERVICES_SUMMARY, s.Summary);
                        dic.Add(SQLFieldNames.SERVICES_TIMETABLE_ID, timetable_id.ToString());
                        dic.Add(SQLFieldNames.SERVICES_DIRECTION, s.Direction);
                        dic.Add(SQLFieldNames.SERVICES_ENDDATE, enddate.ToString());
                        dic.Add(SQLFieldNames.SERVICES_STARTDATE, startdate.ToString());
                        dic.Add(SQLFieldNames.SERVICES_TYPE, s.Type.ToString());

                        int service_id = SQLiteDatabase.Insert(sql_connection, SQLFieldNames.SERVICES, dic);
                        s.ID = service_id;

                        // add the routes
                        foreach (Route route in s.routes)
                        {
                            int start_date = 0;
                            if (!route.StartDate.Equals(DateTime.MinValue))
                                start_date = GetSQLDate(route.StartDate);

                            int end_date = 0;
                            if (!route.EndDate.Equals(DateTime.MaxValue))
                                end_date = GetSQLDate(route.EndDate);

                            dic = new Dictionary<string, string>();
                            dic.Add(SQLFieldNames.ROUTES_SERVICE_ID, service_id.ToString());
                            dic.Add(SQLFieldNames.ROUTES_START_DATE, start_date.ToString());
                            dic.Add(SQLFieldNames.ROUTES_END_DATE, end_date.ToString());
                            dic.Add(SQLFieldNames.ROUTES_START_TIME, route.StartTime);
                            dic.Add(SQLFieldNames.ROUTES_END_TIME, route.EndTime);
                            dic.Add(SQLFieldNames.ROUTES_MON, BoolToInt(route.isRunning(DayOfWeek.Monday)).ToString());
                            dic.Add(SQLFieldNames.ROUTES_TUE, BoolToInt(route.isRunning(DayOfWeek.Tuesday)).ToString());
                            dic.Add(SQLFieldNames.ROUTES_WED, BoolToInt(route.isRunning(DayOfWeek.Wednesday)).ToString());
                            dic.Add(SQLFieldNames.ROUTES_THU, BoolToInt(route.isRunning(DayOfWeek.Thursday)).ToString());
                            dic.Add(SQLFieldNames.ROUTES_FRI, BoolToInt(route.isRunning(DayOfWeek.Friday)).ToString());
                            dic.Add(SQLFieldNames.ROUTES_SAT, BoolToInt(route.isRunning(DayOfWeek.Saturday)).ToString());
                            dic.Add(SQLFieldNames.ROUTES_SUN, BoolToInt(route.isRunning(DayOfWeek.Sunday)).ToString());

                            int route_id = SQLiteDatabase.Insert(sql_connection, SQLFieldNames.ROUTES, dic);

                            // add the stops
                            foreach (Stop stop in route.stops)
                            {
                                int location_id = aliases[stop.Name];

                                dic = new Dictionary<string, string>();
                                dic.Add(SQLFieldNames.STOPS_ROUTE_ID, route_id.ToString());
                                dic.Add(SQLFieldNames.STOPS_LOCATION_ID, location_id.ToString());
                                dic.Add(SQLFieldNames.STOPS_TIME, stop.TimeValue.ToString());
                                dic.Add(SQLFieldNames.STOPS_DROPOFF, BoolToInt(stop.DropOff).ToString());
                                dic.Add(SQLFieldNames.STOPS_PICKUP, BoolToInt(stop.PickUp).ToString());

                                int stop_id = SQLiteDatabase.Insert(sql_connection, SQLFieldNames.STOPS, dic);
                                stop.ID = stop_id;
                            }
                        }
                    }

                    sql_Transaction.Commit();
                }

            }

            DateTime finishLoad = DateTime.Now;
            TimeSpan diff = finishLoad - startLoad;
            Console.WriteLine("Timetable Data Entered in {0}s{1}ms", (int)diff.TotalSeconds, diff.Milliseconds);

            return true;
        }