Exemple #1
0
        /// <summary>
        /// Accepts a packet from Run8.
        /// </summary>
        /// <param name="data">The data packet.</param>
        public void UpdateFromRun8(TrainData data)
        {
            // Sanity check.
            Debug.Assert(data.TrainID == ID);

            // Timestamp the new arrival.
            DataLastUpdated = DateTime.UtcNow;

            // Update general properties.
            Company      = data.RailroadInitials;
            LocoNumber   = data.LocoNumber;
            Tag          = data.TrainSymbol;
            Speed        = (int)data.TrainSpeedMph;
            SpeedLimit   = data.TrainSpeedLimitMPH;
            EngineerType = data.EngineerType;
            EngineerName = (EngineerType == EEngineerType.AI) ? "AI" : (EngineerType == EEngineerType.None) ? "No Driver" : data.EngineerName;
            Length       = data.TrainLengthFeet;
            Weight       = data.TrainWeightTons;
            HPt          = data.HpPerTon;

            // Update AI hold/relinquish orders. We don't use the property setters here because the property setters send order packets to Run8, as they are intended for invocation by the signaller. Instead set the backing fields emit property change notifications directly.
            SetProperty(ref AIHoldImpl, data.HoldingForDispatcher, nameof(AIHold));
            SetProperty(ref AIRelinquishImpl, data.RelinquishWhenStopped, nameof(AIRelinquish));

            // Update location, keeping the old string if not available.
            SubArea      sub = null;
            TrackCircuit tc  = null;

            if (data.BlockID >= 0)
            {
                ushort subAreaID, tcID;
                if (data.BlockID >= 100000)
                {
                    subAreaID = (ushort)(data.BlockID / 1000);
                    tcID      = (ushort)(data.BlockID % 1000);
                }
                else if (data.BlockID >= 10000)
                {
                    subAreaID = (ushort)(data.BlockID / 100);
                    tcID      = (ushort)(data.BlockID % 100);
                }
                else
                {
                    subAreaID = (ushort)(data.BlockID / 10);
                    tcID      = (ushort)(data.BlockID % 10);
                }
                if (World.Region.SubAreas.TryGetValue(subAreaID, out sub))
                {
                    if (tcID < sub.TrackCircuits.Count)
                    {
                        tc = sub.TrackCircuits[tcID];
                    }
                }
            }
            if (tc != null)
            {
                SubArea  = sub.Name;
                Location = tc.LocationName;
            }
            LocationCurrent = tc != null;

            // Fix up which track circuit we are in.
            TrackCircuit berth = tc?.GetBerth();

            if (berth != TrackCircuit)
            {
                if (TrackCircuit != null)
                {
                    TrackCircuit.Trains.Remove(this);
                }
                TrackCircuit = berth;
                if (TrackCircuit != null)
                {
                    TrackCircuit.Trains.Add(this);
                }
            }
        }