Ejemplo n.º 1
0
        public void LeibitTimeTest_ParseDays1()
        {
            var Expected = new List <eDaysOfService> {
                eDaysOfService.Tuesday, eDaysOfService.Wednesday, eDaysOfService.Thursday, eDaysOfService.Saturday
            };
            var Actual = LeibitTime.ParseDays("Di-Do+Sa");

            LeibitComparer <eDaysOfService> .CompareList(Expected, Actual, DayComparer.Instance, "Days");
        }
Ejemplo n.º 2
0
        public void LeibitTimeTest_ParseDays3()
        {
            var Expected = new List <eDaysOfService> {
                eDaysOfService.Monday, eDaysOfService.Tuesday, eDaysOfService.Wednesday
            };
            var Actual = LeibitTime.ParseDays("Mo-Mi+Ma");

            LeibitComparer <eDaysOfService> .CompareList(Expected, Actual, DayComparer.Instance, "Days");
        }
Ejemplo n.º 3
0
        public void LeibitTimeTest_AddMinutes3()
        {
            LeibitTime time = new LeibitTime(eDaysOfService.Tuesday, 23, 7);

            time = time.AddMinutes(105);

            Assert.AreEqual(eDaysOfService.Wednesday, time.Day);
            Assert.AreEqual(0, time.Hour);
            Assert.AreEqual(52, time.Minute);
        }
Ejemplo n.º 4
0
        public void LeibitTimeTest_AddMinutes7()
        {
            LeibitTime time = new LeibitTime(eDaysOfService.Monday, 1, 7);

            time = time.AddMinutes(-153);

            Assert.AreEqual(eDaysOfService.Sunday, time.Day);
            Assert.AreEqual(22, time.Hour);
            Assert.AreEqual(34, time.Minute);
        }
Ejemplo n.º 5
0
        public void LeibitTimeTest_AddHours4()
        {
            LeibitTime time = new LeibitTime(eDaysOfService.Tuesday, 13, 7);

            time = time.AddHours(-75);

            Assert.AreEqual(eDaysOfService.Saturday, time.Day);
            Assert.AreEqual(10, time.Hour);
            Assert.AreEqual(7, time.Minute);
        }
Ejemplo n.º 6
0
        public void LeibitTimeTest_AddMinutes6()
        {
            LeibitTime time = new LeibitTime(eDaysOfService.Friday, 23, 7);

            time = time.AddMinutes(-63);

            Assert.AreEqual(eDaysOfService.Friday, time.Day);
            Assert.AreEqual(22, time.Hour);
            Assert.AreEqual(4, time.Minute);
        }
Ejemplo n.º 7
0
        public void LeibitTimeTest_AddMinutes4()
        {
            LeibitTime time = new LeibitTime(eDaysOfService.Sunday, 23, 7);

            time = time.AddMinutes(55);

            Assert.AreEqual(eDaysOfService.Monday, time.Day);
            Assert.AreEqual(0, time.Hour);
            Assert.AreEqual(2, time.Minute);
        }
Ejemplo n.º 8
0
        public void LeibitTimeTest_AddMinutes2()
        {
            LeibitTime time = new LeibitTime(eDaysOfService.Tuesday, 13, 7);

            time = time.AddMinutes(156);

            Assert.AreEqual(eDaysOfService.Tuesday, time.Day);
            Assert.AreEqual(15, time.Hour);
            Assert.AreEqual(43, time.Minute);
        }
Ejemplo n.º 9
0
        public void LeibitTimeTest_Subtract1()
        {
            LeibitTime time1  = new LeibitTime(13, 52);
            LeibitTime time2  = new LeibitTime(13, 24);
            LeibitTime result = time1 - time2;

            Assert.AreEqual(0, result.Hour);
            Assert.AreEqual(28, result.Minute);
            Assert.AreEqual(28, result.TotalMinutes);
        }
Ejemplo n.º 10
0
        public void LeibitTimeTest_AddDays4()
        {
            LeibitTime time = new LeibitTime(eDaysOfService.Thursday, 13, 7);

            time = time.AddDays(-9);

            Assert.AreEqual(eDaysOfService.Tuesday, time.Day);
            Assert.AreEqual(13, time.Hour);
            Assert.AreEqual(7, time.Minute);
        }
Ejemplo n.º 11
0
        public void LeibitTimeTest_Subtract2()
        {
            LeibitTime time1  = new LeibitTime(13, 52);
            LeibitTime time2  = new LeibitTime(10, 36);
            LeibitTime result = time1 - time2;

            Assert.AreEqual(3, result.Hour);
            Assert.AreEqual(16, result.Minute);
            Assert.AreEqual(196, result.TotalMinutes);
        }
Ejemplo n.º 12
0
        public void LeibitTimeTest_AddMinutes5()
        {
            LeibitTime time = new LeibitTime(eDaysOfService.Sunday, 23, 50);

            time = time.AddMinutes(-27);

            Assert.AreEqual(eDaysOfService.Sunday, time.Day);
            Assert.AreEqual(23, time.Hour);
            Assert.AreEqual(23, time.Minute);
        }
Ejemplo n.º 13
0
        private bool __AreTimesClose(LeibitTime time1, LeibitTime time2)
        {
            if (time1 == null || time2 == null)
            {
                return(false);
            }

            var diff = time1 - time2;

            return(Math.Abs(diff.TotalMinutes) < 15);
        }
Ejemplo n.º 14
0
        public void LeibitTimeTest_Compare9()
        {
            LeibitTime time1 = new LeibitTime(15, 19);
            LeibitTime time2 = new LeibitTime(16, 24);

            Assert.IsFalse(time1 == time2);
            Assert.IsTrue(time1 != time2);
            Assert.IsTrue(time1 < time2);
            Assert.IsFalse(time1 > time2);
            Assert.IsTrue(time1 <= time2);
            Assert.IsFalse(time1 >= time2);
        }
Ejemplo n.º 15
0
        public void LeibitTimeTest_Compare10()
        {
            LeibitTime time1 = new LeibitTime(23, 57);
            LeibitTime time2 = new LeibitTime(0, 5);

            Assert.IsFalse(time1 == time2);
            Assert.IsTrue(time1 != time2);
            Assert.IsTrue(time1 < time2);
            Assert.IsFalse(time1 > time2);
            Assert.IsTrue(time1 <= time2);
            Assert.IsFalse(time1 >= time2);
        }
Ejemplo n.º 16
0
        public void LeibitTimeTest_Compare11()
        {
            LeibitTime time1 = new LeibitTime(10, 17);
            LeibitTime time2 = new LeibitTime(22, 16);

            Assert.IsFalse(time1 == time2);
            Assert.IsTrue(time1 != time2);
            Assert.IsTrue(time1 < time2);
            Assert.IsFalse(time1 > time2);
            Assert.IsTrue(time1 <= time2);
            Assert.IsFalse(time1 >= time2);
        }
Ejemplo n.º 17
0
        public void LeibitTimeTest_Compare12()
        {
            LeibitTime time1 = new LeibitTime(eDaysOfService.Thursday, 2, 43);
            LeibitTime time2 = new LeibitTime(eDaysOfService.Thursday, 16, 24);

            Assert.IsFalse(time1 == time2);
            Assert.IsTrue(time1 != time2);
            Assert.IsTrue(time1 < time2);
            Assert.IsFalse(time1 > time2);
            Assert.IsTrue(time1 <= time2);
            Assert.IsFalse(time1 >= time2);
        }
Ejemplo n.º 18
0
        public void LeibitTimeTest_Compare8()
        {
            LeibitTime time1 = new LeibitTime(eDaysOfService.Monday, 16, 24);
            LeibitTime time2 = new LeibitTime(eDaysOfService.Sunday, 16, 24);

            Assert.IsFalse(time1 == time2);
            Assert.IsTrue(time1 != time2);
            Assert.IsFalse(time1 < time2);
            Assert.IsTrue(time1 > time2);
            Assert.IsFalse(time1 <= time2);
            Assert.IsTrue(time1 >= time2);
        }
Ejemplo n.º 19
0
        private eDaysOfService __GetDayOfService(Schedule schedule, LeibitTime time)
        {
            foreach (eDaysOfService day in Enum.GetValues(typeof(eDaysOfService)))
            {
                if (schedule.Days.Contains(day) || !schedule.Days.Any())
                {
                    var ScheduleTime = new LeibitTime(day, schedule.Time.Hour, schedule.Time.Minute);

                    if (ScheduleTime > time.AddHours(-12) && ScheduleTime <= time.AddHours(12))
                    {
                        return(day);
                    }
                }
            }

            return(eDaysOfService.None);
        }
Ejemplo n.º 20
0
        public OperationResult <List <Schedule> > GetSchedulesByTime(IList <Schedule> Schedules, LeibitTime time)
        {
            try
            {
                var Result = new OperationResult <List <Schedule> >();
                Result.Result = new List <Schedule>();

                LeibitTime FirstTime = null;

                foreach (var Schedule in Schedules.OrderBy(s => s.Time))
                {
                    foreach (eDaysOfService value in Enum.GetValues(typeof(eDaysOfService)))
                    {
                        if (Schedule.Days.Contains(value) || !Schedule.Days.Any())
                        {
                            var ScheduleTime  = new LeibitTime(value, Schedule.Time.Hour, Schedule.Time.Minute);
                            var ReferenceTime = FirstTime == null ? time : FirstTime;

                            if (ScheduleTime > ReferenceTime.AddHours(-12) && ScheduleTime <= ReferenceTime.AddHours(12))
                            {
                                Result.Result.Add(Schedule);

                                if (FirstTime == null)
                                {
                                    FirstTime = ScheduleTime;
                                }
                            }
                        }
                    }
                }

                Result.Succeeded = true;
                return(Result);
            }
            catch (Exception ex)
            {
                return(new OperationResult <List <Schedule> > {
                    Message = ex.Message
                });
            }
        }
Ejemplo n.º 21
0
        public Schedule(Train train, LeibitTime arrival, LeibitTime departure, Track track, List <eDaysOfService> days, eScheduleDirection direction, eHandling handling, string remark)
        {
            m_Train     = train;
            m_Arrival   = arrival;
            m_Departure = departure;
            m_Track     = track;
            m_Days      = days;
            m_Direction = direction;
            m_Handling  = handling;
            m_Remark    = remark;

            if (m_Train != null)
            {
                m_Train.AddSchedule(this);
            }

            if (m_Track != null)
            {
                m_Track.Station.Schedules.Add(this);
                m_Station = m_Track.Station;
            }
        }
Ejemplo n.º 22
0
        private void __LoadTrainRelations(ESTW estw, string path)
        {
            var routingFilesPath = Path.Combine(path, Constants.TRAIN_ROUTING_FOLDER);

            if (!Directory.Exists(routingFilesPath))
            {
                return;
            }

            foreach (var file in Directory.EnumerateFiles(routingFilesPath, "*.znu"))
            {
                using (var fileReader = File.OpenText(file))
                {
                    while (!fileReader.EndOfStream)
                    {
                        var line  = fileReader.ReadLine();
                        var parts = line.Split(' ');

                        if (parts.Length < 2)
                        {
                            continue;
                        }

                        var    sFromTrainNumber = parts[0];
                        var    sToTrainNumber = parts[1];
                        string sFromDay, sToDay;

                        if (parts.Length >= 4)
                        {
                            sFromDay = parts[2];
                            sToDay   = parts[3];
                        }
                        else
                        {
                            sFromDay = "1"; // Monday
                            sToDay   = "7"; // Sunday
                        }

                        if (!int.TryParse(sFromTrainNumber, out var fromTrainNumber) ||
                            !int.TryParse(sToTrainNumber, out var toTrainNumber) ||
                            !estw.Area.Trains.ContainsKey(fromTrainNumber) ||
                            !estw.Area.Trains.ContainsKey(toTrainNumber) ||
                            !LeibitTime.TryParseSingleDay(sFromDay, out var fromDay) ||
                            !LeibitTime.TryParseSingleDay(sToDay, out var toDay))
                        {
                            continue;
                        }

                        var daysOfService = new List <eDaysOfService>();

                        for (int i = (int)fromDay; i != (int)toDay;)
                        {
                            daysOfService.Add((eDaysOfService)i);

                            i *= 2;

                            if (i > 64)
                            {
                                i = 1;
                            }
                        }

                        daysOfService.Add(toDay);

                        var fromTrain = estw.Area.Trains[fromTrainNumber];
                        var toTrain   = estw.Area.Trains[toTrainNumber];

                        var trainRelation = new TrainRelation(toTrainNumber);
                        trainRelation.Days.AddRange(daysOfService);
                        fromTrain.FollowUpServices.Add(trainRelation);

                        trainRelation = new TrainRelation(fromTrainNumber);
                        trainRelation.Days.AddRange(daysOfService);
                        toTrain.PreviousServices.Add(trainRelation);
                    }
                }
            }
        }
Ejemplo n.º 23
0
 public OperationResult <List <Schedule> > GetSchedulesByTime(IEnumerable <Schedule> Schedules, LeibitTime time)
 {
     try
     {
         return(OperationResult <List <Schedule> > .Ok(__GetSchedulesByTime(Schedules, time)));
     }
     catch (Exception ex)
     {
         return(new OperationResult <List <Schedule> > {
             Message = ex.Message
         });
     }
 }
Ejemplo n.º 24
0
        private List <Schedule> __GetSchedulesByTime(IEnumerable <Schedule> Schedules, LeibitTime time)
        {
            var result = new List <Schedule>();

            foreach (var Schedule in Schedules.OrderBy(s => s.Time))
            {
                if (__GetDayOfService(Schedule, time) != eDaysOfService.None)
                {
                    result.Add(Schedule);
                }

                //foreach (eDaysOfService value in Enum.GetValues(typeof(eDaysOfService)))
                //{
                //    if (Schedule.Days.Contains(value) || !Schedule.Days.Any())
                //    {
                //        var ScheduleTime = new LeibitTime(value, Schedule.Time.Hour, Schedule.Time.Minute);

                //        if (ScheduleTime > time.AddHours(-12) && ScheduleTime <= time.AddHours(12))
                //        {
                //            Result.Result.Add(Schedule);
                //        }
                //    }
                //}
            }

            return(result);
        }
Ejemplo n.º 25
0
 public Schedule(Train train, LeibitTime arrival, LeibitTime departure, Track track, List <eDaysOfService> days, eScheduleDirection direction, eHandling handling, string remark, string localOrders)
     : this(train, arrival, departure, track, days, direction, handling, remark)
 {
     m_LocalOrders = localOrders;
 }
Ejemplo n.º 26
0
 internal ScheduleItem(LeibitTime referenceTime, Schedule schedule, LiveSchedule liveSchedule = null)
 {
     ReferenceTime = referenceTime;
     Schedule      = schedule;
     LiveSchedule  = liveSchedule;
 }
Ejemplo n.º 27
0
        private void __GetSchedule(Station station, string path)
        {
            if (station == null || station.ScheduleFile.IsNullOrWhiteSpace())
            {
                return;
            }

            string ScheduleFile = Path.Combine(path, Constants.SCHEDULE_FOLDER, station.ScheduleFile);

            if (!File.Exists(ScheduleFile))
            {
                return;
            }

            // Encoding e.g. for German Umlaute
            using (var reader = new StreamReader(ScheduleFile, Encoding.GetEncoding("iso-8859-1")))
            {
                reader.ReadLine();

                var header = reader.ReadLine();

                if (!__GetBounds(header, "VON", out int startFrom, out int startLength) ||
                    !__GetBounds(header, "VT", out int daysFrom, out int daysLength) ||
                    !__GetBounds(header, "BEMERKUNGEN", out int remarkFrom, out int remarkLength) ||
                    !__GetBounds(header, "NACH", out int destinationFrom, out _))
                {
                    return;
                }

                while (!reader.EndOfStream)
                {
                    string ScheduleLine = reader.ReadLine();

                    if (ScheduleLine.StartsWith("240000 999999"))
                    {
                        break;
                    }

                    if (ScheduleLine.Length < 91)
                    {
                        continue;
                    }

                    string sHour       = ScheduleLine.Substring(0, 2);
                    string sMinute     = ScheduleLine.Substring(2, 2);
                    string sStop       = ScheduleLine.Substring(4, 2);
                    string sHandling   = ScheduleLine.Substring(6, 1);
                    string sTrain      = ScheduleLine.Substring(7, 6);
                    string sDirection  = ScheduleLine.Substring(13, 1);
                    string Type        = ScheduleLine.Substring(14, 3).Trim();
                    string sTrack      = ScheduleLine.Substring(18, 5).Trim();
                    string Start       = ScheduleLine.Substring(startFrom, startLength).Trim();
                    string sDays       = ScheduleLine.Substring(daysFrom, daysLength).Trim().ToLower();
                    string Remark      = ScheduleLine.Substring(remarkFrom, remarkLength).Trim();
                    string Destination = ScheduleLine.Substring(destinationFrom).Trim();

                    if (sTrain.Length > 5)
                    {
                        sTrain = sTrain.Substring(sTrain.Length - 5);
                    }

                    int TrainNr;
                    if (!Int32.TryParse(sTrain, out TrainNr))
                    {
                        continue;
                    }

                    Train Train = station.ESTW.Area.Trains.GetOrAdd(TrainNr, new Train(TrainNr, Type, Start, Destination));

                    int Hour;
                    if (!Int32.TryParse(sHour, out Hour))
                    {
                        continue;
                    }

                    int Minute;
                    if (!Int32.TryParse(sMinute, out Minute))
                    {
                        continue;
                    }

                    int Stop;
                    if (!Int32.TryParse(sStop, out Stop))
                    {
                        continue;
                    }

                    eScheduleDirection Direction;

                    switch (sDirection)
                    {
                    case "<":
                        Direction = eScheduleDirection.RightToLeft;
                        break;

                    case "L":
                        Direction = eScheduleDirection.LeftToLeft;
                        break;

                    case "R":
                        Direction = eScheduleDirection.RightToRight;
                        break;

                    default:
                        Direction = eScheduleDirection.LeftToRight;
                        break;
                    }

                    eHandling Handling = eHandling.Unknown;

                    if (Stop == 60)
                    {
                        Handling = eHandling.Start;
                    }
                    else if (sHandling == " ")
                    {
                        Handling = eHandling.StopPassengerTrain;
                    }
                    else if (sHandling == "X")
                    {
                        Handling = eHandling.StopFreightTrain;
                    }
                    else if (sHandling == "A" || sHandling == "Ä")
                    {
                        Handling = eHandling.Destination;
                    }
                    else if (sHandling == "L")
                    {
                        Handling = eHandling.StaffChange;
                    }
                    else if (sHandling == "D" || sHandling == "d")
                    {
                        Handling = eHandling.Transit;
                    }
                    else if (sHandling == "(")
                    {
                        Handling  = eHandling.Transit;
                        Direction = eScheduleDirection.RightToLeft;
                    }
                    else if (sHandling == ")")
                    {
                        Handling  = eHandling.Transit;
                        Direction = eScheduleDirection.LeftToRight;
                    }

                    var Days = LeibitTime.ParseDays(sDays);

                    LeibitTime Departure = new LeibitTime(Hour, Minute);
                    LeibitTime Arrival;

                    if (Handling == eHandling.Transit || Handling == eHandling.Start)
                    {
                        Arrival = null;
                    }
                    else if (Handling == eHandling.StaffChange && Stop == 0)
                    {
                        Arrival = Departure.AddMinutes(Constants.STAFF_CHANGE_STOPTIME * -1);
                    }
                    else if (Handling == eHandling.Destination)
                    {
                        Arrival   = Departure;
                        Departure = null;
                    }
                    else
                    {
                        Arrival = Departure.AddMinutes(Stop * -1);
                    }

                    Track Track = station.Tracks.FirstOrDefault(t => t.Name == sTrack);

                    if (Track == null)
                    {
                        Track = new Track(sTrack, true, false, station, null);
                    }

                    new Schedule(Train, Arrival, Departure, Track, Days, Direction, Handling, Remark);
                }
            }
        }
Ejemplo n.º 28
0
        private void __LoadLiveDataFromEstw(ESTW estw)
        {
            if (estw == null || estw.DataFile.IsNullOrWhiteSpace())
            {
                return;
            }

            var FilePath = Path.Combine(DataFilesPath, estw.DataFile);

            if (!File.Exists(FilePath))
            {
                return;
            }

            using (var stream = File.Open(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                using (var reader = new StreamReader(stream))
                {
                    // Parse time out of first line.
                    if (!reader.EndOfStream)
                    {
                        var FirstLine = reader.ReadLine();

                        if (FirstLine.Length >= 11)
                        {
                            string sHour   = FirstLine.Substring(0, 2);
                            string sMinute = FirstLine.Substring(3, 2);
                            string sDay    = FirstLine.Substring(9, 2);

                            int            Hour, Minute;
                            eDaysOfService Day = LeibitTime.ParseSingleDay(sDay);

                            if (Day != eDaysOfService.None && Int32.TryParse(sHour, out Hour) && Int32.TryParse(sMinute, out Minute))
                            {
                                var NewTime = new LeibitTime(Day, Hour, Minute);

                                /* Check if the new time is greater than the older one.
                                 * This is important because at midnight there can be a file with time 00:00 but still the old day, i.e.
                                 *
                                 * 1. DI 23:59
                                 * 2. DI 00:00 --> Ignore this!
                                 * 3. MI 00:00
                                 */
                                if (NewTime > estw.Time)
                                {
                                    estw.Time = NewTime;
                                }
                            }
                        }
                    }

                    if (!estw.IsLoaded)
                    {
                        InitializationBLL.LoadESTW(estw);

                        foreach (var Train in estw.Area.LiveTrains.Values)
                        {
                            __RefreshLiveSchedules(Train, estw);
                        }
                    }

                    while (!reader.EndOfStream)
                    {
                        var Line  = reader.ReadLine();
                        var Parts = Line.Split(' ');

                        if (Parts.Length != 6)
                        {
                            continue;
                        }

                        string BlockName    = Parts[1].Trim('/');
                        string sTrainNumber = Parts[2].Trim('_');
                        string sDelay       = Parts[3];
                        string sSpeed       = Parts[4];
                        string sDirection   = Parts[5];

                        if (sTrainNumber.Length > 5)
                        {
                            sTrainNumber = sTrainNumber.Substring(sTrainNumber.Length - 5);
                        }

                        int TrainNumber, Delay;

                        if (!Int32.TryParse(sTrainNumber, out TrainNumber) || !Int32.TryParse(sDelay, out Delay) || (sDirection != "L" && sDirection != "R"))
                        {
                            continue;
                        }

                        TrainInformation Train;

                        if (estw.Area.LiveTrains.ContainsKey(TrainNumber))
                        {
                            Train = estw.Area.LiveTrains[TrainNumber];
                        }
                        else
                        {
                            Train = __CreateLiveTrainInformation(TrainNumber, estw);
                            Train = estw.Area.LiveTrains.GetOrAdd(TrainNumber, Train);
                        }

                        if (Train.Schedules.All(s => s.Schedule.IsUnscheduled || s.LiveArrival == null))
                        {
                            Train.Delay = Delay;
                        }
                        else
                        {
                            // In this case delay is calculated by LeiBIT. Ignore the delay information from ESTWsim!
                        }

                        Train.Direction = sDirection == "L" ? eBlockDirection.Left : eBlockDirection.Right;

                        var Block = estw.Blocks.ContainsKey(BlockName) ? estw.Blocks[BlockName].FirstOrDefault(b => b.Direction == eBlockDirection.Both || b.Direction == Train.Direction) : null;

                        // When speed is not set, the direction in the file is random.
                        // When direction plays a role it's safer to not assign the block in that case.
                        if (Block != null && Block.Direction != eBlockDirection.Both && sSpeed == "0")
                        {
                            Block = null;
                        }

                        __RefreshTrainInformation(Train, Block, estw);
                    }
                }
            }

            estw.LastUpdatedOn = DateTime.Now;

            if (Debugger.IsAttached && DebugMode)
            {
                var fileInfo        = new FileInfo(FilePath);
                var backupDirectory = Path.Combine(fileInfo.DirectoryName, "debug");

                if (!Directory.Exists(backupDirectory))
                {
                    Directory.CreateDirectory(backupDirectory);
                }

                var fileName   = $"{fileInfo.Name.Substring(0, fileInfo.Name.Length - fileInfo.Extension.Length)}_{DateTime.Now:yyyyMMdd_HHmmss}{fileInfo.Extension}";
                var backupFile = Path.Combine(backupDirectory, fileName);
                File.Move(FilePath, backupFile);
            }
            else
            {
                File.Delete(FilePath);
            }
        }
Ejemplo n.º 29
0
        private void __LoadScheduleFromFile(Station station, string scheduleFile, List <string> tracks)
        {
            if (!File.Exists(scheduleFile))
            {
                if (Debugger.IsAttached)
                {
                    throw new OperationFailedException($"Die Datei '{scheduleFile}' existiert nicht.");
                }
                else
                {
                    return;
                }
            }

            // Encoding e.g. for German Umlaute
            using (var reader = new StreamReader(scheduleFile, Encoding.GetEncoding("iso-8859-1")))
            {
                reader.ReadLine();

                var header = reader.ReadLine();

                if (header.IsNullOrWhiteSpace())
                {
                    throw new OperationFailedException($"Die Datei '{scheduleFile}' hat ein falsches Format.");
                }

                if (!__GetBounds(header, "VON", out int startFrom, out int startLength) ||
                    !__GetBounds(header, "VT", out int daysFrom, out int daysLength) ||
                    !__GetBounds(header, "BEMERKUNGEN", out int remarkFrom, out int remarkLength) ||
                    !__GetBounds(header, "NACH", out int destinationFrom, out _))
                {
                    return;
                }

                while (!reader.EndOfStream)
                {
                    string ScheduleLine = reader.ReadLine();

                    if (ScheduleLine.StartsWith("240000 999999"))
                    {
                        break;
                    }

                    if (ScheduleLine.Length < 91)
                    {
                        continue;
                    }

                    string sHour       = ScheduleLine.Substring(0, 2);
                    string sMinute     = ScheduleLine.Substring(2, 2);
                    string sStop       = ScheduleLine.Substring(4, 2);
                    string sHandling   = ScheduleLine.Substring(6, 1);
                    string sTrain      = ScheduleLine.Substring(7, 6);
                    string sDirection  = ScheduleLine.Substring(13, 1);
                    string Type        = ScheduleLine.Substring(14, 3).Trim();
                    string sTrack      = ScheduleLine.Substring(18, 5).Trim();
                    string Start       = ScheduleLine.Substring(startFrom, startLength).Trim();
                    string sDays       = ScheduleLine.Substring(daysFrom, daysLength).Trim().ToLower();
                    string Remark      = ScheduleLine.Substring(remarkFrom, remarkLength).Trim();
                    string Destination = ScheduleLine.Substring(destinationFrom).Trim();

                    if (sTrain.Length > 5)
                    {
                        sTrain = sTrain.Substring(sTrain.Length - 5);
                    }

                    int TrainNr;
                    if (!Int32.TryParse(sTrain, out TrainNr))
                    {
                        continue;
                    }

                    if (tracks.Any() && !tracks.Contains(sTrack))
                    {
                        continue;
                    }

                    var Train = new Train(TrainNr, Type, Start, Destination);
                    __SetTrainLine(Train, station.ESTW.Area);
                    Train = station.ESTW.Area.Trains.GetOrAdd(TrainNr, Train);

                    int Hour;
                    if (!Int32.TryParse(sHour, out Hour))
                    {
                        continue;
                    }

                    int Minute;
                    if (!Int32.TryParse(sMinute, out Minute))
                    {
                        continue;
                    }

                    int Stop;
                    if (!Int32.TryParse(sStop, out Stop))
                    {
                        continue;
                    }

                    eScheduleDirection Direction;

                    switch (sDirection)
                    {
                    case "<":
                        Direction = eScheduleDirection.RightToLeft;
                        break;

                    case "L":
                        Direction = eScheduleDirection.LeftToLeft;
                        break;

                    case "R":
                        Direction = eScheduleDirection.RightToRight;
                        break;

                    default:
                        Direction = eScheduleDirection.LeftToRight;
                        break;
                    }

                    eHandling Handling = eHandling.Unknown;

                    if (Stop == 60)
                    {
                        Handling = eHandling.Start;
                    }
                    else if (sHandling == " ")
                    {
                        Handling = eHandling.StopPassengerTrain;
                    }
                    else if (sHandling == "X")
                    {
                        Handling = eHandling.StopFreightTrain;
                    }
                    else if (sHandling == "A" || sHandling == "Ä")
                    {
                        Handling = eHandling.Destination;
                    }
                    else if (sHandling == "L")
                    {
                        Handling = eHandling.StaffChange;
                    }
                    else if (sHandling == "D" || sHandling == "d")
                    {
                        Handling = eHandling.Transit;
                    }
                    else if (sHandling == "(")
                    {
                        Handling  = eHandling.Transit;
                        Direction = eScheduleDirection.RightToLeft;
                    }
                    else if (sHandling == ")")
                    {
                        Handling  = eHandling.Transit;
                        Direction = eScheduleDirection.LeftToRight;
                    }

                    var Days = LeibitTime.ParseDays(sDays);

                    LeibitTime Departure = new LeibitTime(Hour, Minute);
                    LeibitTime Arrival;

                    if (Handling == eHandling.Transit || Handling == eHandling.Start)
                    {
                        Arrival = null;
                    }
                    else if (Handling == eHandling.StaffChange && Stop == 0)
                    {
                        Arrival = Departure.AddMinutes(Constants.STAFF_CHANGE_STOPTIME * -1);
                    }
                    else if (Handling == eHandling.Destination)
                    {
                        Arrival   = Departure;
                        Departure = null;
                    }
                    else
                    {
                        Arrival = Departure.AddMinutes(Stop * -1);
                    }

                    Track Track = station.Tracks.FirstOrDefault(t => t.Name == sTrack);

                    if (Track == null)
                    {
                        Track = new Track(sTrack, true, false, station, null);
                    }

                    var schedule = new Schedule(Train, Arrival, Departure, Track, Days, Direction, Handling, Remark);
                    schedule.TrainType   = Type;
                    schedule.Start       = Start;
                    schedule.Destination = Destination;

                    Train.Type        = Train.Schedules.Select(s => s.TrainType).FirstOrDefault(t => t.IsPassengerTrain()) ?? Type;
                    Train.Start       = Train.Schedules.OrderBy(s => s.Time).First().Start;
                    Train.Destination = Train.Schedules.OrderBy(s => s.Time).Last().Destination;
                }
            }
        }