//


        #region MachineConfig

        /// <summary>
        /// Called when [Machine config action].
        /// </summary>
        /// <param name="threadData">The thread data.</param>
        private void OnMachineConfigAction(MachineConfigThreadData threadData)
        {
            if (!threadData.CheckAndWriteLog("OnMachineConfigAction", "Method Invoked successfully."))
            {
                return;
            }

#if DEBUG
            //System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            //watch.Start();
            //if (!threadData.CheckAndWriteLog("OnMachineConfigAction", "Method Started at : " + DateTime.Now.ToString())) return;
#endif

            try
            {
                if (threadData.Installation_Float_Status == 1 && DBBuilder.GetSettingFromDB("DISABLE_MACHINE_ON_DROP", "FALSE").ToUpper() == "TRUE")
                {
                    LogManager.WriteLog("Disabling the Installation - " + threadData.Installation_No.ToString(), LogManager.enumLogLevel.Info);
                    threadData.Enable = false; threadData.datapakCurrentState = 0;

                    EnableDisableMachine(threadData);

                    //ThreadPool.QueueUserWorkItem(new WaitCallback(EnableDisableMachine), _autoEnableDisable);
                    string strComment = "Machine Floated and Setting DISABLE_MACHINE_ON_DROP {True} hence disabling.";
                    LogManager.WriteLog(strComment, LogManager.enumLogLevel.Info);

                    DBBuilder.UpdateCommentsForAAMS(threadData.badId.ToString(), strComment, 3, 0);
                }
                else
                {
                    if (threadData.enterprisestatus)
                    {
                        threadData.Enable = true; threadData.datapakCurrentState = 1;
                        EnableDisableMachine(threadData);

                        LogManager.WriteLog("Enabling the Installation - " + threadData.Installation_No.ToString(), LogManager.enumLogLevel.Info);
                    }
                    else
                    {
                        threadData.Enable = false; threadData.datapakCurrentState = 0;
                        EnableDisableMachine(threadData);
                        string strComment = "Disabling the Installation  - " + threadData.Installation_No.ToString();
                        LogManager.WriteLog(strComment, LogManager.enumLogLevel.Info);
                        DBBuilder.UpdateCommentsForAAMS(threadData.Installation_No.ToString(), strComment, 2, 0);
                    }
                }
            }
            finally
            {
#if DEBUG
                //watch.Stop();
                //threadData.CheckAndWriteLog("OnMachineConfigAction", "Method Ended at : " + DateTime.Now.ToString());
                //threadData.CheckAndWriteLog("OnMachineConfigAction", "Time Taken to execute : " + watch.Elapsed.ToString());
#endif
                Interlocked.Increment(ref nMachineThreadComplete);
            }
        }
        //
        public void EnableDisableMachine(object _machineConfig)
        {
            int result;
            MachineConfigThreadData objMachineConfigThreadData = _machineConfig as MachineConfigThreadData;

            try
            {
#if DEBUG
                //System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
                //watch.Start();
                //LogManager.WriteLog("EnableMachineFromUI | "+"Method Started at : " + DateTime.Now.ToString(), LogManager.enumLogLevel.Info);
#endif
                result = objMachineConfigThreadData.Enable ? EnableMachineFromUI(objMachineConfigThreadData.Installation_No) : DisableMachineFromUI(objMachineConfigThreadData.Installation_No);
#if DEBUG
                //watch.Stop();
                //LogManager.WriteLog("EnableMachineFromUI | "+ "Method Ended at : " + DateTime.Now.ToString(), LogManager.enumLogLevel.Info);
                //LogManager.WriteLog("EnableMachineFromUI | " + "Time Taken : "+ watch.Elapsed.ToString(), LogManager.enumLogLevel.Info);
#endif

                if (result == 0)
                {
                    DBBuilder.UpdateAAMSStatus(objMachineConfigThreadData.badId, objMachineConfigThreadData.datapakCurrentState.ToString(), "",
                                               objMachineConfigThreadData.entityType, objMachineConfigThreadData.Installation_No,
                                               objMachineConfigThreadData.updateDate);

                    LogManager.WriteLog("Update Bar Position machine status", LogManager.enumLogLevel.Info);
                    DBBuilder.UpdateBarPosition(objMachineConfigThreadData.Installation_No, objMachineConfigThreadData.Enable);
                    LogManager.WriteLog(
                        "Installation No: " + objMachineConfigThreadData.Installation_No.ToString() + " --- ACK Status was True",
                        LogManager.enumLogLevel.Info);
                }
                LogManager.WriteLog("Requesting command result: " + result + " --- DataPak Number:" + objMachineConfigThreadData.Installation_No, LogManager.enumLogLevel.Info);
            }
            catch (Exception ex)
            {
                LogManager.WriteLog("Enable/Disable Command Failed, DataPak Number:" + objMachineConfigThreadData.Installation_No, LogManager.enumLogLevel.Info);
                ExceptionManager.Publish(ex);
            }
        }
        //
        public void MachineConfig()
        {
            if (nMachineThreadComplete != nMachineTotal)
            {
                LogManager.WriteLog("MachineConfig pending items to be processed.Returning to Main.", LogManager.enumLogLevel.Info);
                return;
            }

            nMachineThreadComplete = 0;

            string PROC = "|=> MachineConfig() : ";

            if (this.IsObjectInactive)
            {
                LogManager.WriteLog(PROC + "Service was instructed to stop.", LogManager.enumLogLevel.Info);
                return;
            }


            try
            {
                DataTable getMachineDetails;
                LogManager.WriteLog("Inside MachineConfig method", LogManager.enumLogLevel.Info);

                getMachineDetails = DBBuilder.GetAAMSDetails(3);


                if (getMachineDetails.Rows.Count <= 0)
                {
                    return;
                }
                else
                {
                    nMachineTotal = getMachineDetails.Rows.Count;
                }

                foreach (DataRow row in getMachineDetails.Rows)
                {
                    if (this.IsObjectInactive)
                    {
                        LogManager.WriteLog(PROC + "Service was instructed to stop.", LogManager.enumLogLevel.Info);
                        return;
                    }

                    MachineConfigThreadData threadData = new MachineConfigThreadData()
                    {
                        Installation_No           = row["BAD_Reference_ID"] != DBNull.Value ? Convert.ToInt32(row["BAD_Reference_ID"].ToString()) : 0,
                        Enable                    = false,
                        badId                     = Convert.ToInt32(row["BAD_ID"]),
                        datapakCurrentState       = 0,
                        entityType                = 3,
                        Installation_Float_Status = row["Installation_Float_Status"] != DBNull.Value ? Convert.ToInt32(row["Installation_Float_Status"]) : 0,
                        enterprisestatus          = row["BMC_Enterprise_Status"] != DBNull.Value
                                                   ? Convert.ToBoolean(row["BMC_Enterprise_Status"].ToString())
                                                   : true,
                        updateDate = Convert.ToDateTime(row["BAD_Updated_Date"])
                    };
#if DEBUG
                    if (_machineDispatcher == null)
                    {
                        LogManager.WriteLog("|==> DANGER: _machineDispatcher is null.", LogManager.enumLogLevel.Info);
                    }
#endif
                    _machineDispatcher.AddThreadData(threadData);
                }
            }
            catch (Exception Ex)
            {
                LogManager.WriteLog("MachineConfig Error " + Ex.Message, LogManager.enumLogLevel.Info);
                ExceptionManager.Publish(Ex);
            }
        }
        //
        public void MachineConfig()
        {

            if (nMachineThreadComplete != nMachineTotal)
            {
                LogManager.WriteLog("MachineConfig pending items to be processed.Returning to Main.", LogManager.enumLogLevel.Info);
                return;
            }

            nMachineThreadComplete = 0;

            string PROC = "|=> MachineConfig() : ";
            if (this.IsObjectInactive)
            {
                LogManager.WriteLog(PROC + "Service was instructed to stop.", LogManager.enumLogLevel.Info);
                return;
            }
                      

            try
            {
                DataTable getMachineDetails;
                LogManager.WriteLog("Inside MachineConfig method", LogManager.enumLogLevel.Info);

                getMachineDetails = DBBuilder.GetAAMSDetails(3);

                
                if (getMachineDetails.Rows.Count <= 0)
                    return;
                else
                {
                    nMachineTotal = getMachineDetails.Rows.Count;
                }

                foreach (DataRow row in getMachineDetails.Rows)
                {
                    if (this.IsObjectInactive)
                    {
                        LogManager.WriteLog(PROC + "Service was instructed to stop.", LogManager.enumLogLevel.Info);
                        return;
                    }

                    MachineConfigThreadData threadData = new MachineConfigThreadData()
                    {
                        Installation_No = row["BAD_Reference_ID"] != DBNull.Value ? Convert.ToInt32(row["BAD_Reference_ID"].ToString()) : 0,
                        Enable = false,
                        badId = Convert.ToInt32(row["BAD_ID"]),
                        datapakCurrentState = 0,
                        entityType = 3,
                        Installation_Float_Status = row["Installation_Float_Status"] != DBNull.Value ? Convert.ToInt32(row["Installation_Float_Status"]) : 0,
                        enterprisestatus = row["BMC_Enterprise_Status"] != DBNull.Value
                                                   ? Convert.ToBoolean(row["BMC_Enterprise_Status"].ToString())
                                                   : true,
                        updateDate = Convert.ToDateTime(row["BAD_Updated_Date"])
                    };
#if DEBUG
                    if (_machineDispatcher == null)
                    {
                        LogManager.WriteLog("|==> DANGER: _machineDispatcher is null.", LogManager.enumLogLevel.Info);
                    }
#endif
                    _machineDispatcher.AddThreadData(threadData);
                }
            }
            catch (Exception Ex)
            {
                LogManager.WriteLog("MachineConfig Error " + Ex.Message, LogManager.enumLogLevel.Info);
                ExceptionManager.Publish(Ex);
            }
        }
        //


        #region MachineConfig

        /// <summary>
        /// Called when [Machine config action].
        /// </summary>
        /// <param name="threadData">The thread data.</param>
        private void OnMachineConfigAction(MachineConfigThreadData threadData)
        {
            
            if (!threadData.CheckAndWriteLog("OnMachineConfigAction", "Method Invoked successfully.")) return;

#if DEBUG
                //System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
                //watch.Start();
                //if (!threadData.CheckAndWriteLog("OnMachineConfigAction", "Method Started at : " + DateTime.Now.ToString())) return;
#endif

            try
            {
                if (threadData.Installation_Float_Status == 1 && DBBuilder.GetSettingFromDB("DISABLE_MACHINE_ON_DROP", "FALSE").ToUpper() == "TRUE")
                {
                    LogManager.WriteLog("Disabling the Installation - " + threadData.Installation_No.ToString(), LogManager.enumLogLevel.Info);
                    threadData.Enable = false; threadData.datapakCurrentState = 0;

                    EnableDisableMachine(threadData);

                    //ThreadPool.QueueUserWorkItem(new WaitCallback(EnableDisableMachine), _autoEnableDisable);
                    string strComment = "Machine Floated and Setting DISABLE_MACHINE_ON_DROP {True} hence disabling.";
                    LogManager.WriteLog(strComment, LogManager.enumLogLevel.Info);

                    DBBuilder.UpdateCommentsForAAMS(threadData.badId.ToString(), strComment, 3, 0);
                }
                else
                {
                    if (threadData.enterprisestatus)
                    {
                        threadData.Enable = true; threadData.datapakCurrentState = 1;
                        EnableDisableMachine(threadData);

                        LogManager.WriteLog("Enabling the Installation - " + threadData.Installation_No.ToString(), LogManager.enumLogLevel.Info);
                    }
                    else
                    {
                        threadData.Enable = false; threadData.datapakCurrentState = 0;
                        EnableDisableMachine(threadData);
                        string strComment = "Disabling the Installation  - " + threadData.Installation_No.ToString();
                        LogManager.WriteLog(strComment, LogManager.enumLogLevel.Info);
                        DBBuilder.UpdateCommentsForAAMS(threadData.Installation_No.ToString(), strComment, 2, 0);
                    }
                }
            }
            finally
            {
#if DEBUG
                //watch.Stop();
                //threadData.CheckAndWriteLog("OnMachineConfigAction", "Method Ended at : " + DateTime.Now.ToString());
                //threadData.CheckAndWriteLog("OnMachineConfigAction", "Time Taken to execute : " + watch.Elapsed.ToString());
#endif
                 Interlocked.Increment(ref nMachineThreadComplete);
            }
        }