public OperationResult <SerializationContainer> Open(string Filename) { try { var SettingsResult = SettingsBll.GetSettings(); ValidateResult(SettingsResult); var Settings = SettingsResult.Result; var Container = new SerializationContainer(); var File = new FileInfo(Filename); if (!File.Exists) { throw new OperationFailedException(String.Format("File '{0}' does not exist.", Filename)); } SerializedRoot Root; using (var Stream = File.OpenRead()) { Root = m_Formatter.Deserialize(Stream) as SerializedRoot; } if (Root == null) { throw new OperationFailedException("Invalid file."); } var AreaResult = InitializationBll.GetAreaInformation(); ValidateResult(AreaResult); var Area = AreaResult.Result.FirstOrDefault(a => a.Id == Root.AreaId); if (Area == null) { throw new OperationFailedException("Invalid area."); } foreach (var SerializedEstw in Root.LoadedESTWs) { if (!Settings.LoadInactiveEstws && !SerializedEstw.IsActive) { continue; } var Estw = Area.ESTWs.FirstOrDefault(e => e.Id == SerializedEstw.ESTWId); if (Estw == null) { throw new OperationFailedException("Invalid ESTW."); } var LoadResult = InitializationBll.LoadESTW(Estw); ValidateResult(LoadResult); Estw.Time = SerializedEstw.Time; Estw.StartTime = SerializedEstw.StartTime; } foreach (var SerializedTrain in Root.LiveTrains) { var Estw = Area.ESTWs.SingleOrDefault(e => e.Id == SerializedTrain.CurrentEstwId); if (Estw?.IsLoaded == false || !Area.Trains.ContainsKey(SerializedTrain.TrainNumber)) { continue; } var Train = Area.Trains[SerializedTrain.TrainNumber]; var LiveTrain = new TrainInformation(Train); LiveTrain.Delay = SerializedTrain.Delay; LiveTrain.LastModified = SerializedTrain.LastModified; LiveTrain.CreatedOn = SerializedTrain.CreatedOn; LiveTrain.Direction = SerializedTrain.TrainDirection; LiveTrain.IsDestinationStationCancelled = SerializedTrain.IsDestinationStationCancelled; if (SerializedTrain.BlockHistory != null) { foreach (var block in SerializedTrain.BlockHistory) { var estw2 = Area.ESTWs.SingleOrDefault(e => e.Id == block.EstwId); if (estw2 == null || !estw2.Blocks.ContainsKey(block.Name)) { continue; } LiveTrain.BlockHistory.AddIfNotNull(estw2.Blocks[block.Name].FirstOrDefault(b => b.Direction == block.Direction)); } } if (SerializedTrain.Block != null && Estw.Blocks.ContainsKey(SerializedTrain.Block)) { LiveTrain.Block = Estw.Blocks[SerializedTrain.Block].FirstOrDefault(b => b.Direction == SerializedTrain.BlockDirection); } var SchedulesResult = CalculationBll.GetSchedulesByTime(Train.Schedules, Estw?.Time ?? SerializedTrain.LastModified); ValidateResult(SchedulesResult); foreach (var SerializedSchedule in SerializedTrain.Schedules) { var Schedule = SchedulesResult.Result.FirstOrDefault(s => s.Station.ShortSymbol == SerializedSchedule.StationShortSymbol && s.Time == SerializedSchedule.StationTime && (s.Station.ESTW.Id == SerializedSchedule.EstwId || SerializedSchedule.EstwId == null)); if (Schedule == null) { continue; } var LiveSchedule = new LiveSchedule(LiveTrain, Schedule); LiveSchedule.LiveArrival = SerializedSchedule.LiveArrival; LiveSchedule.LiveDeparture = SerializedSchedule.LiveDeparture; LiveSchedule.IsArrived = SerializedSchedule.IsArrived; LiveSchedule.IsDeparted = SerializedSchedule.IsDeparted; LiveSchedule.ExpectedArrival = SerializedSchedule.ExpectedArrival; LiveSchedule.ExpectedDeparture = SerializedSchedule.ExpectedDeparture; LiveSchedule.IsComposed = SerializedSchedule.IsComposed; LiveSchedule.IsPrepared = SerializedSchedule.IsPrepared; LiveSchedule.IsCancelled = SerializedSchedule.IsCancelled; LiveSchedule.IsManuallyModified = SerializedSchedule.IsManuallyModified; if (SerializedSchedule.ExpectedDelay.HasValue) { // Ensure compatibility LiveSchedule.ExpectedDelayDeparture = SerializedSchedule.ExpectedDelay; } else { LiveSchedule.ExpectedDelayArrival = SerializedSchedule.ExpectedDelayArrival; LiveSchedule.ExpectedDelayDeparture = SerializedSchedule.ExpectedDelayDeparture; } if (SerializedSchedule.LiveTrack.IsNotNullOrEmpty()) { LiveSchedule.LiveTrack = Schedule.Station.Tracks.SingleOrDefault(t => t.Name == SerializedSchedule.LiveTrack); } if (LiveSchedule.LiveArrival != null) { LiveSchedule.IsArrived = true; } if (LiveSchedule.LiveDeparture != null) { LiveSchedule.IsDeparted = true; } foreach (var SerializedDelay in SerializedSchedule.Delays) { var Delay = LiveSchedule.AddDelay(SerializedDelay.Minutes, SerializedDelay.Type); Delay.Reason = SerializedDelay.Reason; Delay.CausedBy = SerializedDelay.CausedBy; } LiveTrain.AddSchedule(LiveSchedule); } if (Estw != null) { // Don't validate result here. When this fails, it's not so dramatic... var prevResult = CalculationBll.GetPreviousService(Train, Estw); if (prevResult.Succeeded) { LiveTrain.PreviousService = prevResult.Result; } var followUpResult = CalculationBll.GetFollowUpService(Train, Estw); if (followUpResult.Succeeded) { LiveTrain.FollowUpService = followUpResult.Result; } } if (LiveTrain.Schedules.Any()) { Area.LiveTrains.TryAdd(Train.Number, LiveTrain); } } foreach (var Estw in Area.ESTWs) { if (Estw.StartTime == null) { var schedules = Area.LiveTrains.Values.SelectMany(t => t.Schedules).Where(s => s.Schedule.Station.ESTW == Estw); if (schedules.Any()) { Estw.StartTime = schedules.Min(s => s.LiveArrival); var minStartTime = Estw.Time.AddHours(-12); if (Estw.StartTime < minStartTime) { Estw.StartTime = minStartTime; } } else { Estw.StartTime = Estw.Time; } } } Container.Area = Area; Container.VisibleStations = Root.VisibleStations; Container.VisibleTrains = Root.VisibleTrains; Container.HiddenSchedules = Root.HiddenSchedules; Container.Windows = Root.Windows; Container.IsOldVersion = Root.Version != Assembly.GetEntryAssembly().GetCustomAttribute <AssemblyInformationalVersionAttribute>()?.InformationalVersion; var Result = new OperationResult <SerializationContainer>(); Result.Result = Container; Result.Succeeded = true; return(Result); } catch (Exception ex) { return(new OperationResult <SerializationContainer> { Message = ex.Message }); } }
public OperationResult <SerializationContainer> Open(string Filename) { try { var SettingsResult = SettingsBll.GetSettings(); ValidateResult(SettingsResult); var Settings = SettingsResult.Result; var Container = new SerializationContainer(); var File = new FileInfo(Filename); if (!File.Exists) { throw new OperationFailedException(String.Format("File '{0}' does not exist.", Filename)); } SerializedRoot Root; using (var Stream = File.OpenRead()) { Root = m_Formatter.Deserialize(Stream) as SerializedRoot; } if (Root == null) { throw new OperationFailedException("Invalid file."); } var AreaResult = InitializationBll.GetAreaInformation(); ValidateResult(AreaResult); var Area = AreaResult.Result.FirstOrDefault(a => a.Id == Root.AreaId); if (Area == null) { throw new OperationFailedException("Invalid area."); } foreach (var SerializedEstw in Root.LoadedESTWs) { if (!Settings.LoadInactiveEstws && !SerializedEstw.IsActive) { continue; } var Estw = Area.ESTWs.FirstOrDefault(e => e.Id == SerializedEstw.ESTWId); if (Estw == null) { throw new OperationFailedException("Invalid ESTW."); } var LoadResult = InitializationBll.LoadESTW(Estw); ValidateResult(LoadResult); Estw.Time = SerializedEstw.Time; } foreach (var SerializedTrain in Root.LiveTrains) { var Estw = Area.ESTWs.SingleOrDefault(e => e.Id == SerializedTrain.CurrentEstwId); if (Estw == null || !Estw.IsLoaded || !Area.Trains.ContainsKey(SerializedTrain.TrainNumber)) { continue; } var Train = Area.Trains[SerializedTrain.TrainNumber]; var LiveTrain = new TrainInformation(Train); LiveTrain.Delay = SerializedTrain.Delay; LiveTrain.LastModified = SerializedTrain.LastModified; LiveTrain.Direction = SerializedTrain.TrainDirection; if (Estw.Blocks.ContainsKey(SerializedTrain.Block)) { LiveTrain.Block = Estw.Blocks[SerializedTrain.Block].FirstOrDefault(b => b.Direction == SerializedTrain.BlockDirection); } var SchedulesResult = CalculationBll.GetSchedulesByTime(Train.Schedules, Estw.Time); ValidateResult(SchedulesResult); foreach (var SerializedSchedule in SerializedTrain.Schedules) { var Schedule = SchedulesResult.Result.FirstOrDefault(s => s.Station.ShortSymbol == SerializedSchedule.StationShortSymbol && s.Time == SerializedSchedule.StationTime && (s.Station.ESTW.Id == SerializedSchedule.EstwId || SerializedSchedule.EstwId == null)); if (Schedule == null) { continue; } var LiveSchedule = new LiveSchedule(LiveTrain, Schedule); LiveSchedule.LiveArrival = SerializedSchedule.LiveArrival; LiveSchedule.LiveDeparture = SerializedSchedule.LiveDeparture; LiveSchedule.IsArrived = SerializedSchedule.IsArrived; LiveSchedule.IsDeparted = SerializedSchedule.IsDeparted; LiveSchedule.ExpectedArrival = SerializedSchedule.ExpectedArrival; LiveSchedule.ExpectedDeparture = SerializedSchedule.ExpectedDeparture; LiveSchedule.ExpectedDelay = SerializedSchedule.ExpectedDelay; if (SerializedSchedule.LiveTrack.IsNotNullOrEmpty()) { LiveSchedule.LiveTrack = Schedule.Station.Tracks.SingleOrDefault(t => t.Name == SerializedSchedule.LiveTrack); } foreach (var SerializedDelay in SerializedSchedule.Delays) { var Delay = LiveSchedule.AddDelay(SerializedDelay.Minutes, SerializedDelay.Type); Delay.Reason = SerializedDelay.Reason; Delay.CausedBy = SerializedDelay.CausedBy; } LiveTrain.AddSchedule(LiveSchedule); } if (LiveTrain.Schedules.Any()) { Area.LiveTrains.TryAdd(Train.Number, LiveTrain); } } Container.Area = Area; Container.VisibleStations = Root.VisibleStations; Container.Windows = Root.Windows; var Result = new OperationResult <SerializationContainer>(); Result.Result = Container; Result.Succeeded = true; return(Result); } catch (Exception ex) { return(new OperationResult <SerializationContainer> { Message = ex.Message }); } }