/*#--------------------------------------------------------------------------#*/
 /*  Description: FiddleYardSimTrainDetect Constructor
  *
  *  Input(s)   :
  *
  *  Output(s)  :
  *
  *  Returns    :
  *
  *  Pre.Cond.  :
  *
  *  Post.Cond. :
  *
  *  Notes      :
  */
 /*#--------------------------------------------------------------------------#*/
 public FiddleYardSimTrainDetect(iFiddleYardSimulator iFYSim, Log2LoggingFile FiddleYardSimulatorLogging, FiddleYardSimulatorVariables FYSimVar, FiddleYardSimMove FYMove)
 {
     m_iFYSim = iFYSim;
     m_FYSimLog = FiddleYardSimulatorLogging;
     m_FYSimVar = FYSimVar;
     m_FYMove = FYMove;
     FiddleTrDtState = 0;
     AliveUpdateCnt = 0;
 }
        private UInt32[] TrackForward = new UInt32[12] { 0, 0, 42800, 85600, 128400, 171200, 214000, 256800, 299600, 342400, 385200, 428000 }; // New track coordinates forward movement 1 --> 11 to be moved and get from Application Variables

        #endregion Fields

        #region Constructors

        /*#--------------------------------------------------------------------------#*/
        /*  Description: FiddleYardSimulator constructor
         *
         *  Input(s)   :
         *
         *  Output(s)  :
         *
         *  Returns    :
         *
         *  Pre.Cond.  :
         *
         *  Post.Cond. :
         *
         *  Notes      :
         *
         */
        /*#--------------------------------------------------------------------------#*/
        public FiddleYardSimulator(string Instance, FiddleYardIOHandleVariables FYIOHandleVar)
        {
            m_FYIOHandleVar = FYIOHandleVar;
            m_instance = Instance;

            if ("TOP" == m_instance)
            {
                path = @"c:\localdata\Siebwalde\" + DateTime.Now.Day + "-" + DateTime.Now.Month + "-" + DateTime.Now.Year + "_FiddleYardSimulatorTOP.txt"; //  different logging file per target, this is default
                FiddleYardSimulatorLogging = new Log2LoggingFile(path);
            }
            else if ("BOT" == m_instance)
            {
                path = @"c:\localdata\Siebwalde\" + DateTime.Now.Day + "-" + DateTime.Now.Month + "-" + DateTime.Now.Year + "_FiddleYardSimulatorBOT.txt"; //  different logging file per target, this is default
                FiddleYardSimulatorLogging = new Log2LoggingFile(path);
            }

            FYSimVar = new FiddleYardSimulatorVariables();
            FYSimMove = new FiddleYardSimMove(this, FiddleYardSimulatorLogging, FYSimVar);
            FYSimTrDt = new FiddleYardSimTrainDetect(this, FiddleYardSimulatorLogging, FYSimVar, FYSimMove);

            FYSimVar.TrackNo.Count = 1;

            Sensor Sns_FYSimSpeedSetting = new Sensor("FYSimSpeedSetting", " FYSimSpeedSetting ", 0, (name, val, log) => SimulatorSettings(name, val, log)); // initialize and subscribe sensors
            Siebwalde_Application.Properties.Settings.Default.FYSimSpeedSetting.Attach(Sns_FYSimSpeedSetting);
        }
        public FiddleYardSimulator(string Instance, iFiddleYardController iFYCtrl)
        {
            m_iFYCtrl = iFYCtrl;    // connect to FYController interface, save interface in variable
            m_instance = Instance;

            TargetAlive = new SensorUpdater();
            FYMove = new FiddleYardSimMove(this);
            FYTrDt = new FiddleYardSimTrainDetect(this);

            if ("FiddleYardTOP" == m_instance)
            {
                path = @"c:\localdata\FiddleYardSimTOPLogging.txt"; // different logging file per target, this is default
                Message Msg_TargetAliveTop = new Message("TargetAlive", " Target Alive ", (name, log) => SetMessage(name, log)); // initialize and subscribe readback action, Message
                m_iFYCtrl.GetIoHandler().TargetAliveTop.Attach(Msg_TargetAliveTop);

                for (int i = 1; i < NoOfSimTrains; i++)
                {
                    TrainsOnFYSim[i]=rng.Next(0, 2);
                    current = new FiddleYardSimTrain(m_instance, this, m_iFYCtrl);
                    current.FYSimtrainInstance = current.ClassName + i.ToString();
                    if (TrainsOnFYSim[i] == 1)
                    {
                        current.SimTrainLocation = TrackNoToTrackString(i);
                    }
                    else
                    {
                        current.SimTrainLocation = TrackNoToTrackString(0);
                    }
                    FYSimTrains.Add(current);
                }
                TrackNo.Count = 1;
            }
            else if ("FiddleYardBOT" == m_instance)
            {
                path = @"c:\localdata\FiddleYardSimBOTLogging.txt"; // different logging file per target, this is default
                Message Msg_TargetAliveBot = new Message("TargetAlive", " Target Alive ", (name, log) => SetMessage(name, log)); // initialize and subscribe readback action, Message
                m_iFYCtrl.GetIoHandler().TargetAliveBot.Attach(Msg_TargetAliveBot);

                for (int i = 1; i < NoOfSimTrains; i++)
                {
                    rng.Next(0, 2);
                }
                for (int i = 1; i < NoOfSimTrains; i++)
                {
                    TrainsOnFYSim[i] = rng.Next(0, 2);
                    current = new FiddleYardSimTrain(m_instance, this, m_iFYCtrl);
                    current.FYSimtrainInstance = current.ClassName + i.ToString();
                    if (TrainsOnFYSim[i] == 1)
                    {
                        current.SimTrainLocation = TrackNoToTrackString(i);
                    }
                    else
                    {
                        current.SimTrainLocation = TrackNoToTrackString(0);
                    }
                    FYSimTrains.Add(current);
                }
                TrackNo.Count = 1;
            }
        }