예제 #1
0
        private async void CreateTrains()
        {
            for (int i = 0; i < Random.Next(4, 7); i++)
            {
                Trains.Add(await CreateRandomTrain());
            }

            this.ModifyTrain(null, EventArgs.Empty);
        }
 public void AddTrain(Train train)
 {
     if (train == null)
     {
         throw new ArgumentNullException(nameof(train));
     }
     if (Trains.Contains(train))
     {
         return;
     }
     Trains.Add(train);
 }
예제 #3
0
 public void ReciveTrainInfo(TrainInfo info)
 {
     if (TrainsDictionary.Keys.FirstOrDefault(a => a.Name == info.BaseTrain.Number) == null && tempProxy != null)
     {
         var train = new TrainUI
         {
             Name            = info.BaseTrain.Number,
             Location        = info.TrainPosition,
             PassangersCount = info.BaseTrain.CurrentPassengers,
             Speed           = int.Parse(info.BaseTrain.Speed.ToString())
         };
         TrainsDictionary.Add(train, tempProxy);
         Trains.Add(train);
         tempProxy = null;
     }
 }
예제 #4
0
 public static void TrainMapper()
 {
     string[] trainData = FetchData(trainDataPath);
     for (int i = 1; i < trainData.Length; i++)
     {
         string[] splitline = trainData[i].Split(';', ',', ':');
         Trains.Add(new Train(
                        int.Parse(splitline[0]),
                        splitline[1],
                        int.Parse(splitline[2]),
                        bool.Parse(splitline[3]),
                        new LogComponent(),
                        new EngineComponent(),
                        new PassengerCartComponent()));
     }
 }
예제 #5
0
        public bool Add(Train train) //add train
        {
            if (train == null)
            {
                throw new ArgumentNullException("train");
            }

            Train t = FindTrain(train.TrainUnit);

            if (t == null)
            {
                Trains.Add(train); //add to list
                if (OnTrainUpdate != null)
                {
                    OnTrainUpdate(this, train, true);
                }
                return(true);
            }
            return(false);
        }
예제 #6
0
 /// <summary>
 /// Method to generate trains
 /// </summary>
 public void GenerateTrain()
 {
     while (Go)
     {
         Random random = new Random();
         int    sleep  = random.Next(minCheckTime, maxCheckTime);
         Thread.Sleep(sleep);
         // Making list of empty tracks
         List <Track> emptyTracks = new List <Track>();
         foreach (var junction in Junctions)
         {
             foreach (var track in junction.EntryTracks)
             {
                 if (track.TrackMutex.WaitOne(10))
                 {
                     if (track.IsEmpty)
                     {
                         emptyTracks.Add(track);
                     }
                     else
                     {
                         track.TrackMutex.ReleaseMutex();
                     }
                 }
             }
         }
         // Adding train on random empty track if there is more than 1 empty track
         if (emptyTracks.Count > 1)
         {
             Track trackToGenerateTrain = emptyTracks.ElementAt(random.Next(0, emptyTracks.Count));
             Train train = new Train(this, trackToGenerateTrain, TrainId++);
             Trains.Add(train);
         }
         // Releasing mutex of empty tracks
         foreach (var track in emptyTracks)
         {
             track.TrackMutex.ReleaseMutex();
         }
     }
 }
예제 #7
0
        public void PreliminaryRead(string fileName, StreamReader scrStream, string separator)
        {
            string readLine;
            string restLine;
            int    firstCommentColumn = -1;

            // read first line - first character is separator, rest is train info
            readLine = scrStream.ReadLine();
            if (string.IsNullOrEmpty(separator))
            {
                separator = readLine.Substring(0, 1);
            }

            restLine = readLine.Substring(1);
            string[] separators = new string[] { separator };
            string[] parts      = restLine.Split(separators, StringSplitOptions.None);

            int columnIndex = 1;

            foreach (string header in parts)
            {
                if (header.Equals("#comment", StringComparison.OrdinalIgnoreCase))
                {
                    if (firstCommentColumn < 0)
                    {
                        firstCommentColumn = columnIndex;
                    }
                }
                else if (!string.IsNullOrEmpty(header) && !header.ToLower().Contains("$static"))
                {
                    Trains.Add(new TrainInformation(columnIndex, header));
                }
                columnIndex++;
            }

            // process comment row - cell at first comment row and column is description
            // process path and consist row

            Description = fileName;

            bool descFound    = false;
            bool pathFound    = false;
            bool consistFound = false;
            bool startFound   = false;

            readLine = scrStream.ReadLine();

            while (readLine != null && (!descFound || !pathFound || !consistFound || !startFound))
            {
                parts = readLine.Split(separators, StringSplitOptions.None);

                if (!descFound && firstCommentColumn > 0)
                {
                    if (parts[0].Equals("#comment", StringComparison.OrdinalIgnoreCase))
                    {
                        Description = parts[firstCommentColumn];
                        descFound   = true;
                    }
                }

                if (!pathFound)
                {
                    if (parts[0].Trim().Substring(0, 5).Equals("#path", StringComparison.OrdinalIgnoreCase))
                    {
                        foreach (TrainInformation train in Trains)
                        {
                            train.Path = parts[train.Column];
                        }
                        pathFound = true;
                    }
                }

                if (!consistFound)
                {
                    if (parts[0].Equals("#consist", StringComparison.OrdinalIgnoreCase))
                    {
                        foreach (TrainInformation train in Trains)
                        {
                            train.Consist        = parts[train.Column];
                            train.LeadingConsist = ExtractConsist(train.Consist, out bool reverse);
                            train.ReverseConsist = reverse;
                        }
                        consistFound = true;
                    }
                }

                if (!startFound)
                {
                    if (parts[0].Equals("#start", StringComparison.OrdinalIgnoreCase))
                    {
                        foreach (TrainInformation train in Trains)
                        {
                            train.StartTime = parts[train.Column];
                        }
                        startFound = true;
                    }
                }
                readLine = scrStream.ReadLine();
            }
        }
예제 #8
0
        public void Refresh(Area Area)
        {
            var SettingsResult = m_SettingsBll.GetSettings();

            if (!SettingsResult.Succeeded)
            {
                MessageBox.Show(SettingsResult.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            var Settings      = SettingsResult.Result;
            var CurrentTrains = new List <TrainStationViewModel>();

            foreach (var LiveTrain in Area.LiveTrains.Values)
            {
                if (LiveTrain.Train == null)
                {
                    continue;
                }

                var Train = LiveTrain.Train;

                foreach (var LiveSchedule in LiveTrain.Schedules)
                {
                    if (LiveSchedule.Schedule == null || LiveSchedule.Schedule.Station == null)
                    {
                        continue;
                    }

                    var Schedule = LiveSchedule.Schedule;
                    var Station  = Schedule.Station;

                    bool Visible = true;
                    bool IsNew   = false;

                    if (!Runtime.VisibleStations.Contains(Station))
                    {
                        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;
                    }

                    var Current = Trains.FirstOrDefault(t => t.Station.ShortSymbol == Station.ShortSymbol && t.TrainNumber == Train.Number && t.Schedule.Time == Schedule.Time);

                    if (Current == null)
                    {
                        Current = new TrainStationViewModel(LiveTrain, Schedule);
                        IsNew   = true;
                    }

                    Current.Delay             = LiveTrain.Delay;
                    Current.ExpectedArrival   = LiveSchedule.ExpectedArrival;
                    Current.ExpectedDeparture = LiveSchedule.ExpectedDeparture;
                    Current.LiveTrack         = LiveSchedule.LiveTrack == null || LiveSchedule.LiveTrack.Name == Schedule.Track?.Name ? null : LiveSchedule.LiveTrack;
                    Current.LocalOrders       = Schedule.LocalOrders.IsNotNullOrWhiteSpace() ? 'J' : ' ';

                    var NextSchedules = LiveTrain.Schedules.Skip(LiveTrain.Schedules.IndexOf(LiveSchedule) + 1);

                    if (NextSchedules.Any(s => (s.Schedule.Station.ShortSymbol != Station.ShortSymbol || s.Schedule.Time != Schedule.Time) && s.LiveArrival != null))
                    {
                        Visible = false;
                    }

                    var Delays            = LiveTrain.Schedules.SelectMany(s => s.Delays);
                    var UnjustifiedDelays = Delays.Where(d => d.Reason.IsNullOrWhiteSpace() && d.Schedule.Schedule.Station.ESTW.Stations.Any(s => Runtime.VisibleStations.Contains(s)));

                    if (UnjustifiedDelays.Any())
                    {
                        Current.DelayInfo = 'U';

                        if (UnjustifiedDelays.Any(d => d.Schedule.Schedule.Station.ShortSymbol == Schedule.Station.ShortSymbol))
                        {
                            Visible = true;
                        }
                    }
                    else if (Delays.Any(d => d.Reason.IsNotNullOrWhiteSpace()))
                    {
                        Current.DelayInfo = 'J';
                    }
                    else
                    {
                        Current.DelayInfo = ' ';
                    }

                    if (Visible)
                    {
                        if (LiveTrain.Block != null && LiveTrain.Block.Track != null && LiveTrain.Block.Track.Station != null)
                        {
                            Current.CurrentStation = LiveTrain.Block.Track.Station;

                            var CurrentSchedule = LiveTrain.Schedules.LastOrDefault(s => s.IsArrived && s.Schedule != null && s.Schedule.Station != null && s.Schedule.Station.ShortSymbol == LiveTrain.Block.Track.Station.ShortSymbol);

                            if (CurrentSchedule == null)
                            {
                                Current.State = LiveSchedule.IsDeparted ? "beendet" : "ab";
                            }
                            else
                            {
                                int CurrentScheduleIndex = LiveTrain.Schedules.IndexOf(CurrentSchedule);
                                int LiveScheduleIndex    = LiveTrain.Schedules.IndexOf(LiveSchedule);

                                if (LiveSchedule.Schedule.Station.ShortSymbol == CurrentSchedule.Schedule.Station.ShortSymbol && LiveSchedule.Schedule.Time == Current.Schedule.Time)
                                {
                                    CurrentScheduleIndex = LiveScheduleIndex;
                                }

                                if (CurrentScheduleIndex > LiveScheduleIndex)
                                {
                                    Current.State = "beendet";
                                }
                                else if (!CurrentSchedule.IsDeparted)
                                {
                                    if (CurrentSchedule.Schedule.Handling == eHandling.Start &&
                                        Settings.AutomaticReadyMessageEnabled &&
                                        CurrentSchedule.Schedule.Station.ESTW.Time >= CurrentSchedule.Schedule.Departure.AddMinutes(-Settings.AutomaticReadyMessageTime))
                                    {
                                        Current.State = "fertig";
                                    }
                                    else
                                    {
                                        Current.State = "an";
                                    }
                                }
                                else
                                {
                                    if (CurrentScheduleIndex == LiveScheduleIndex)
                                    {
                                        Current.State = "beendet";
                                    }
                                    else
                                    {
                                        Current.State = "ab";
                                    }
                                }
                            }
                        }

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

                        CurrentTrains.Add(Current);
                    }
                }
            }

            var RemovedTrains = Trains.Except(CurrentTrains).ToList();

            Dispatcher.Invoke(() => RemovedTrains.ForEach(t => Trains.Remove(t)));
        }