Beispiel #1
0
        public void Refresh(Area Area)
        {
            var CurrentSchedules = new List <TimeTableItemViewModel>();

            var SchedulesResult = m_CalculationBll.GetSchedulesByTime(CurrentStation.Schedules, CurrentStation.ESTW.Time);

            if (!SchedulesResult.Succeeded)
            {
                Dispatcher.Invoke(() => MessageBox.Show(SchedulesResult.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error));
                return;
            }

            foreach (var Schedule in SchedulesResult.Result)
            {
                if (Schedule.Train == null)
                {
                    continue;
                }

                bool Visible = true;
                bool IsNew   = false;

                if (Schedule.Time < CurrentStation.ESTW.Time)
                {
                    Visible = false;
                }

                var Current = Schedules.FirstOrDefault(s => s.TrainNumber == Schedule.Train.Number);

                if (Current == null)
                {
                    Current = new TimeTableItemViewModel(Schedule);
                    IsNew   = true;
                }

                if (Area.LiveTrains.ContainsKey(Current.TrainNumber))
                {
                    var LiveTrain = Area.LiveTrains[Current.TrainNumber];
                    Current.Delay = LiveTrain.Delay;

                    var LiveSchedule = LiveTrain.Schedules.FirstOrDefault(s => s.Schedule == Schedule);

                    if (LiveSchedule != null)
                    {
                        Visible           = true;
                        Current.LiveTrack = LiveSchedule.LiveTrack;

                        var Index = LiveTrain.Schedules.IndexOf(LiveSchedule);

                        if (LiveSchedule.LiveDeparture != null || LiveTrain.Schedules.Where((s, i) => i > Index && s.LiveArrival != null).Count() > 0)
                        {
                            Visible = false;
                        }
                    }

                    LeibitTime ActualTime;

                    if (LiveTrain.Block != null && LiveTrain.Block.Track != null)
                    {
                        ActualTime = LiveTrain.Block.Track.Station.ESTW.Time;
                    }
                    else
                    {
                        ActualTime = Area.ESTWs.Where(e => e.IsLoaded).DefaultIfEmpty().Max(e => e.Time);
                    }

                    if (ActualTime != null && LiveTrain.LastModified != null && ActualTime > LiveTrain.LastModified.AddMinutes(1))
                    {
                        Visible = false;
                    }

                    if (Visible)
                    {
                        var DelayInfos = LiveTrain.Schedules.SelectMany(s => s.Delays).Where(d => d.Reason.IsNotNullOrWhiteSpace()).Select(d => d.Reason);

                        if (DelayInfos.Count() > 0)
                        {
                            Current.DelayReason = String.Join(", ", DelayInfos);
                        }

                        var StartSchedule = LiveTrain.Schedules.SingleOrDefault(s => s.Schedule.Handling == eHandling.Start && s.Schedule.Station == CurrentStation);
                        Current.IsReady = StartSchedule != null && StartSchedule.Schedule.Station.ESTW.Time >= StartSchedule.Schedule.Departure.AddMinutes(-2);
                    }
                }

                if (Visible)
                {
                    if (IsNew)
                    {
                        Dispatcher.Invoke(() => Schedules.Add(Current));
                    }

                    CurrentSchedules.Add(Current);
                }
            }

            var RemovedItems = Schedules.Except(CurrentSchedules).ToList();

            Dispatcher.Invoke(() => RemovedItems.ForEach(s => Schedules.Remove(s)));
        }