Esempio n. 1
0
        public static void TimetableFromCsv(object sender, DoWorkEventArgs e)
        {
            DateTime ThresholdDate = new DateTime(2010, 1, 1);

            if (DataContainer.model == null)
            {
                DataContainer.model = new railml();
            }
            DataContainer.model.timetable = new timetable();
            Dictionary <string, int> categories = new Dictionary <string, int>();
            string           filename           = e.Argument as string;
            BackgroundWorker worker             = sender as BackgroundWorker;

            CsvDefinition def = new CsvDefinition()
            {
                FieldSeparator = ','
            };
            CsvFileReader <TimetableEntry> reader = new CsvFileReader <TimetableEntry>(filename, def);
            int              count      = 0;
            RuntimeRep       rep        = new RuntimeRep();
            List <StopDelay> stopdelays = new List <StopDelay>();
            string           traincode  = "";

            foreach (TimetableEntry entry in reader)
            {
                if (entry.TrainDate < ThresholdDate)
                {
                    count++; continue;
                }



                string category = entry.TrainOrigin + "to" + entry.TrainDestination;
                if (categories.ContainsKey(category))
                {
                    categories[category] = (int)categories[category] + 1;
                }
                else
                {
                    categories.Add(category, 1);
                    //eCategory cat = new eCategory();
                    //cat.id = category;
                    //cat.name = category;
                    //DataContainer.model.timetable.categories.Add(cat);
                    //eRostering roster = new eRostering();
                    //DataContainer.IDGenerator(roster);
                    //roster.name = category;
                    //DataContainer.model.timetable.rosterings.Add(roster);
                }

                if (entry.TrainDate != date || entry.TrainCode != traincode)
                {
                    AddStopDelays(stopdelays, traincode, date);
                    stopdelays = new List <StopDelay>();
                    if (rep.traincode != null)
                    {
                        trainvariations.AddRep(rep, date);
                    }
                    if (entry.TrainCode != traincode)
                    {
                        if (trainvariations != null)
                        {
                            string cat = categories.FirstOrDefault(x => x.Value == categories.Values.Max()).Key;
                            trainvariations.category = cat;
                            categories = new Dictionary <string, int>();
                            if (!DataContainer.model.timetable.rosterings.Any(x => x.name == cat))
                            {
                                eRostering roster = new eRostering()
                                {
                                    name  = cat,
                                    scope = "scheduled"
                                };
                                DataContainer.IDGenerator(roster);
                                DataContainer.model.timetable.rosterings.Add(roster);
                            }
                            ProcessTimetableVariations(trainvariations);
                        }
                        trainvariations           = new TrainVariations();
                        trainvariations.traincode = entry.TrainCode;
                        worker.ReportProgress(0, new string[] { traincode, count.ToString() });
                        traincode = entry.TrainCode;
                    }

                    rep             = new RuntimeRep();
                    date            = entry.TrainDate;
                    rep.traincode   = entry.TrainCode;
                    rep.destination = entry.TrainDestination;
                    rep.origin      = entry.TrainOrigin;
                }

                if (entry.LocationType == "O")
                {
                    rep.departuretime = default(DateTime).Add(entry.ScheduledDeparture.TimeOfDay);
                }
                else if (entry.LocationType == "D")
                {
                    rep.arrivaltime = default(DateTime).Add(entry.ScheduledArrival.TimeOfDay);
                }
                else if (entry.LocationType == "S")
                {
                    Stop stop = new Stop()
                    {
                        location = entry.LocationCode, arrival = entry.ScheduledArrival, departure = entry.ScheduledDeparture
                    };
                    rep.stops.Add(stop);
                    if (entry.Arrival != default(DateTime) && entry.Departure != default(DateTime))
                    {
                        StopDelay delay = new StopDelay()
                        {
                            location = entry.LocationCode, departuredelay = (entry.Departure - entry.ScheduledDeparture).TotalSeconds, arrivaldelay = (entry.Arrival - entry.ExpectedArrival).TotalSeconds
                        };
                        stopdelays.Add(delay);
                    }
                }

                //if (count > 300000) { break; }
                count++;
            }
            // Append the last trainvariation to the totalvariations list
            if (trainvariations != null)
            {
                string cat = categories.FirstOrDefault(x => x.Value == categories.Values.Max()).Key;
                trainvariations.category = cat;
                categories = new Dictionary <string, int>();
                ProcessTimetableVariations(trainvariations);
                if (!DataContainer.model.timetable.rosterings.Any(x => x.name == cat))
                {
                    eRostering roster = new eRostering()
                    {
                        name  = cat,
                        scope = "scheduled"
                    };
                    DataContainer.IDGenerator(roster);
                    DataContainer.model.timetable.rosterings.Add(roster);
                }
            }

            e.Result = unhandled;
        }