public static bool TryParseSingleDay(string day, out eDaysOfService result) { switch (day.Trim().ToLower()) { case "mo": case "1": result = eDaysOfService.Monday; return(true); case "di": case "2": result = eDaysOfService.Tuesday; return(true); case "mi": case "3": result = eDaysOfService.Wednesday; return(true); case "do": case "4": result = eDaysOfService.Thursday; return(true); case "fr": case "5": result = eDaysOfService.Friday; return(true); case "sa": case "6": result = eDaysOfService.Saturday; return(true); case "so": case "7": result = eDaysOfService.Sunday; return(true); default: result = eDaysOfService.None; return(false); } }
public LeibitTime(eDaysOfService day, int hour, int minute) { m_Day = day; m_Hour = hour; m_Minute = minute; }
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); } }