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

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

            // Sub programs
            FYAppVar = new FiddleYardApplicationVariables(m_FYIOHandleVar);                 // FiddleYard Application variables class, holds all variables and functions regarding variables
            FYMIP50 = new FiddleYardMip50(m_instance, m_FYIOHandleVar, m_iFYIOH, FYAppVar);
            FYAppRun = new FiddleYardAppRun(m_FYIOHandleVar, m_iFYIOH, FYAppVar, FYMIP50, FiddleYardApplicationLogging);
            FYTDT = new FiddleYardTrainDetection(m_FYIOHandleVar, FYAppVar, FYMIP50, FiddleYardApplicationLogging);
            FYAppInit = new FiddleYardAppInit(m_FYIOHandleVar, FYAppVar, FYMIP50, FYTDT, FiddleYardApplicationLogging);
            FYFORM = new FiddleYardForm(this);//, FYMip50SettingsForm);

            //Init and setup FYFORM (after the creation of the sensors and commands)
            if ("TOP" == m_instance)
                FYFORM.Name = "FiddleYardTOP";
            else if ("BOT" == m_instance)
                FYFORM.Name = "FiddleYardBOT";

            FYFORM.Show();
            FYFORM.Hide();
            FYFORM.Connect(m_FYIOHandleVar, FYAppVar); // connect the Form to the FYIOHandle interface
        }
 /*#--------------------------------------------------------------------------#*/
 /*  Description: FiddleYardAppInit()
  *               Constructor
  *
  *
  *  Input(s)   :
  *
  *  Output(s)  :
  *
  *  Returns    :
  *
  *  Pre.Cond.  :
  *
  *  Post.Cond. :
  *
  *  Notes      :
  */
 /*#--------------------------------------------------------------------------#*/
 public FiddleYardAppInit(FiddleYardIOHandleVariables FYIOHandleVar, FiddleYardApplicationVariables FYAppVar, FiddleYardMip50 FYMIP50, FiddleYardTrainDetection FYTDT, Log2LoggingFile FiddleYardApplicationLogging)
 {
     m_FYIOHandleVar = FYIOHandleVar;
     m_FYAppVar = FYAppVar;
     m_FYMIP50 = FYMIP50;
     m_FYTDT = FYTDT;
     m_FYAppLog = FiddleYardApplicationLogging;
     FiddleYardInitStarted = new MessageUpdater();
     State_Machine = State.Idle;
     Message Msg_uControllerReady = new Message("uControllerReady", " uControllerReady ", (name, log) => SetMessage(name, log)); // initialize and subscribe readback action, Message
     m_FYIOHandleVar.uControllerReady.Attach(Msg_uControllerReady);
 }
        /*#--------------------------------------------------------------------------#*/
        /*  Description: FiddleYardTrainDetection
         *               Constructor
         *
         *
         *  Input(s)   :
         *
         *  Output(s)  :
         *
         *  Returns    :
         *
         *  Pre.Cond.  :
         *
         *  Post.Cond. :
         *
         *  Notes      :
         */
        /*#--------------------------------------------------------------------------#*/
        public FiddleYardTrainDetection(FiddleYardIOHandleVariables FYIOHandleVar, FiddleYardApplicationVariables FYAppVar, FiddleYardMip50 FYMIP50, Log2LoggingFile FiddleYardApplicationLogging)
        {
            m_FYIOHandleVar = FYIOHandleVar;
            m_FYAppVar = FYAppVar;
            m_FYMIP50 = FYMIP50;
            m_FYAppLog = FiddleYardApplicationLogging;
            State_Machine = State.Idle;

            Sensor Sns_CL_10_Heart = new Sensor("CL10Heart", " CL 10 Heart ", 0, (name, val, log) => SetLedIndicator(name, val, log)); // initialize and subscribe sensors
            FYAppVar.CL10Heart.Attach(Sns_CL_10_Heart);
            Sensor Sns_F10 = new Sensor("F10", " F10 ", 0, (name, val, log) => SetLedIndicator(name, val, log)); // initialize and subscribe sensors
            FYAppVar.F10.Attach(Sns_F10);
        }
        /*#--------------------------------------------------------------------------#*/
        /*  Description: Constructor
         *
         *  Input(s)   : FiddleYardMip50
         *
         *  Output(s)  :
         *
         *  Returns    :
         *
         *  Pre.Cond.  :
         *
         *  Post.Cond. :
         *
         *  Notes      :
         */
        /*#--------------------------------------------------------------------------#*/
        public FiddleYardMip50SettingsForm(string instance, FiddleYardMip50 FYMip50, FiddleYardApplicationVariables FYAppVar)
        {
            InitializeComponent();
            m_FYMip50 = FYMip50;
            m_FYAppVar = FYAppVar;
            m_instance = instance;

            //config.AppSettings.Settings[m_instance + "_" + "Track1_Abs_Position"].Value = "50";

            this.FormClosing += new FormClosingEventHandler(FiddleYardMip50SettingsForm_Close);
            try
            {
                TB_MIP50PositioningVelocity.AppendText(config.AppSettings.Settings[m_instance + "_" + "MIP50PositioningVelocity"].Value);
                TB_MIP50PositioningAcceleration.AppendText(config.AppSettings.Settings[m_instance + "_" + "MIP50PositioningAcceleration"].Value);
                TB_TRACK1_ABS_POS.AppendText(config.AppSettings.Settings[m_instance + "_" + "Track1_Abs_Position"].Value);
                TB_TRACK2_ABS_POS.AppendText(config.AppSettings.Settings[m_instance + "_" + "Track2_Abs_Position"].Value);
                TB_TRACK3_ABS_POS.AppendText(config.AppSettings.Settings[m_instance + "_" + "Track3_Abs_Position"].Value);
                TB_TRACK4_ABS_POS.AppendText(config.AppSettings.Settings[m_instance + "_" + "Track4_Abs_Position"].Value);
                TB_TRACK5_ABS_POS.AppendText(config.AppSettings.Settings[m_instance + "_" + "Track5_Abs_Position"].Value);
                TB_TRACK6_ABS_POS.AppendText(config.AppSettings.Settings[m_instance + "_" + "Track6_Abs_Position"].Value);
                TB_TRACK7_ABS_POS.AppendText(config.AppSettings.Settings[m_instance + "_" + "Track7_Abs_Position"].Value);
                TB_TRACK8_ABS_POS.AppendText(config.AppSettings.Settings[m_instance + "_" + "Track8_Abs_Position"].Value);
                TB_TRACK9_ABS_POS.AppendText(config.AppSettings.Settings[m_instance + "_" + "Track9_Abs_Position"].Value);
                TB_TRACK10_ABS_POS.AppendText(config.AppSettings.Settings[m_instance + "_" + "Track10_Abs_Position"].Value);
                TB_TRACK11_ABS_POS.AppendText(config.AppSettings.Settings[m_instance + "_" + "Track11_Abs_Position"].Value);
                TB_TRACK1_BKOFFSET.AppendText(config.AppSettings.Settings[m_instance + "_" + "Track1_Back_Offset"].Value);
                TB_TRACK2_BKOFFSET.AppendText(config.AppSettings.Settings[m_instance + "_" + "Track2_Back_Offset"].Value);
                TB_TRACK3_BKOFFSET.AppendText(config.AppSettings.Settings[m_instance + "_" + "Track3_Back_Offset"].Value);
                TB_TRACK4_BKOFFSET.AppendText(config.AppSettings.Settings[m_instance + "_" + "Track4_Back_Offset"].Value);
                TB_TRACK5_BKOFFSET.AppendText(config.AppSettings.Settings[m_instance + "_" + "Track5_Back_Offset"].Value);
                TB_TRACK6_BKOFFSET.AppendText(config.AppSettings.Settings[m_instance + "_" + "Track6_Back_Offset"].Value);
                TB_TRACK7_BKOFFSET.AppendText(config.AppSettings.Settings[m_instance + "_" + "Track7_Back_Offset"].Value);
                TB_TRACK8_BKOFFSET.AppendText(config.AppSettings.Settings[m_instance + "_" + "Track8_Back_Offset"].Value);
                TB_TRACK9_BKOFFSET.AppendText(config.AppSettings.Settings[m_instance + "_" + "Track9_Back_Offset"].Value);
                TB_TRACK10_BKOFFSET.AppendText(config.AppSettings.Settings[m_instance + "_" + "Track10_Back_Offset"].Value);
                TB_TRACK11_BKOFFSET.AppendText(config.AppSettings.Settings[m_instance + "_" + "Track11_Back_Offset"].Value);
            }
            catch
            {
                MessageBox.Show("Reading " + m_instance + " Appsettings error in constructor FiddleYardMip50SettingsForm");
            }

            TB_MIP50PositioningVelocity.Click += new EventHandler(TB_MIP50PositioningVelocity_OnClick);
            TB_MIP50PositioningVelocity.TextChanged += new EventHandler(TB_MIP50PositioningVelocity_TextChanged);
            TB_MIP50PositioningAcceleration.Click += new EventHandler(TB_MIP50PositioningAcceleration_OnClick);
            TB_MIP50PositioningAcceleration.TextChanged += new EventHandler(TB_MIP50PositioningAcceleration_TextChanged);

            Sensor Sns_ReceivedDataFromMip50 = new Sensor("Mip50Rec", " Mip50ReceivedCmd ", 0, (name, val, log) => ReceivedMIP50Data(name, val, log));  // initialize sensors
            m_FYAppVar.ReceivedDataFromMip50.Attach(Sns_ReceivedDataFromMip50);                                                                         // Attach
        }
        /*#--------------------------------------------------------------------------#*/
        /*  Description: FiddleYardAppTrainDrive()
         *               Constructor
         *
         *
         *  Input(s)   :
         *
         *  Output(s)  :
         *
         *  Returns    :
         *
         *  Pre.Cond.  :
         *
         *  Post.Cond. :
         *
         *  Notes      :
         */
        /*#--------------------------------------------------------------------------#*/
        public FiddleYardAppTrainDrive(FiddleYardIOHandleVariables FYIOHandleVar, iFiddleYardIOHandle iFYIOH, FiddleYardApplicationVariables FYAppVar,
            FiddleYardMip50 FYMIP50, Log2LoggingFile FiddleYardApplicationLogging)
        {
            m_FYIOHandleVar = FYIOHandleVar;
            m_iFYIOH = iFYIOH;
            m_FYAppVar = FYAppVar;
            m_FYMIP50 = FYMIP50;
            m_FYAppLog = FiddleYardApplicationLogging;
            TrainDriveIn_Machine = State.Start;
            TrainDriveOut_Machine = State.Start;
            TrainDriveThrough_Machine = State.Start;

            Message Msg_uControllerReady = new Message("uControllerReady", " uControllerReady ", (name, log) => SetMessage(name, log)); // initialize and subscribe readback action, Message
            m_FYIOHandleVar.uControllerReady.Attach(Msg_uControllerReady);
        }
        /*#--------------------------------------------------------------------------#*/
        /*  Description: FiddleYardAppRun()
         *               Constructor
         *
         *
         *  Input(s)   :
         *
         *  Output(s)  :
         *
         *  Returns    :
         *
         *  Pre.Cond.  :
         *
         *  Post.Cond. :
         *
         *  Notes      :
         */
        /*#--------------------------------------------------------------------------#*/
        public FiddleYardAppRun(FiddleYardIOHandleVariables FYIOHandleVar, iFiddleYardIOHandle iFYIOH, FiddleYardApplicationVariables FYAppVar, FiddleYardMip50 FYMIP50, Log2LoggingFile FiddleYardApplicationLogging)
        {
            m_iFYIOH = iFYIOH;
            m_FYIOHandleVar = FYIOHandleVar;
            m_FYAppVar = FYAppVar;
            m_FYMIP50 = FYMIP50;
            m_FYAppLog = FiddleYardApplicationLogging;
            FYAppTrainDrive = new FiddleYardAppTrainDrive(m_FYIOHandleVar, m_iFYIOH, m_FYAppVar, m_FYMIP50, m_FYAppLog);
            State_Machine = State.Idle;

            Message Msg_TrackPower15VDown = new Message("TrackPower15VDown", " TrackPower15VDown ", (name, log) => SetMessage(name, 0, log)); // initialize and subscribe readback action, Message
            m_FYIOHandleVar.TrackPower15VDown.Attach(Msg_TrackPower15VDown);
            Sensor Sns_TrackPower15V = new Sensor("15VTrackPower", " 15V Track Power ", 0, (name, val, log) => SetMessage(name, val, log)); // initialize and subscribe sensors
            m_FYIOHandleVar.TrackPower15V.Attach(Sns_TrackPower15V);
            Command Act_Collect = new Command("Collect", (name) => SetMessage(name, 0, "")); // initialize and subscribe Commands
            m_FYAppVar.FormCollect.Attach(Act_Collect);
        }