using System;
using System.Threading;

namespace RGD.WCS
{
    /// <summary>
    /// 新增公共配置20220310
    /// </summary>
    public class CConfig
    {
        #region 仓储管理系统对接

        /// <summary>
        /// 管理系统类型 1安徽mds 2rgd 后续开发
        /// </summary>
        public static int WMSType = 1;

        /// <summary>
        /// 管理系统服务地址
        /// </summary>
        public static string WMSServiceUrl = "http://10.138.18.151:17004/client/wms/services/WMSWebService?wsdl";

        #endregion 仓储管理系统对接

        #region 默认配置站台

        /// <summary>
        /// 射频站台
        /// </summary>
        public static int Device_Rfid;

        /// <summary>
        /// 叠盘站台
        /// </summary>
        public static int Device_Stacker;

        /// <summary>
        /// 拆盘站台
        /// </summary>
        public static int Device_DStacker;

        /// <summary>
        /// 视觉站台
        /// </summary>
        public static int Device_Vision;

        /// <summary>
        /// 贴标站台
        /// </summary>
        public static int Device_Labeling;

        /// <summary>
        /// 贴标机
        /// </summary>
        public static int Device_LableM;

        /// <summary>
        /// 拣选站台
        /// </summary>
        public static int Device_Pick;

        /// <summary>
        /// 拣选站台
        /// </summary>
        public static int Device_PickM;

        /// <summary>
        /// 扫码头设备
        /// </summary>
        public static int Device_ScanCode;

        /// <summary>
        /// 出库站台
        /// </summary>
        public static int Device_Out;

        #endregion 默认配置站台

        /// <summary>
        /// 加载配置文件 20220310 暂时写死
        /// </summary>
        public static void LoadConfig()
        {
            //DataTable dt = DBUtility.DbHelperSQL.Query("select * from T_Base_PLC_Ask").Tables[0];
            //foreach (DataRow row in dt.Rows)
            //{
            //    switch (row["F_Askkind"].ToString())
            //    {
            //        case "1":
            //            Device_Rfid = int.Parse(row["F_BindingDevice"].ToString());
            //            break;
            //        case "2":
            //            Device_Rfid = int.Parse(row["F_BindingDevice"].ToString());
            //            break;
            //        case "3":
            //            Device_Rfid = int.Parse(row["F_BindingDevice"].ToString());
            //            break;
            //    }
            //}
            Device_Rfid = 12002;
            Device_Stacker = 9999;
            Device_DStacker = 12030;
            Device_Vision = 0;

            Device_LableM = 0;
            Device_PickM = 0;
            Device_Out = 12001;
        }

        public CConfig()
        {
        }

        #region 运行时

        private static Object thisLock = new Object(); //锁定timer的一次执行
        private static System.Timers.Timer getAllReturnTimer = new System.Timers.Timer();
        private static System.Timers.Timer getStateTimer = new System.Timers.Timer();
        private static System.Timers.Timer controlTimer = new System.Timers.Timer();
        private static System.Timers.Timer disTaskTimer = new System.Timers.Timer();
        private static bool isGetStateRun = false;

        public static void StartAll()
        {
            getAllReturnTimer.Interval = 300;
            getAllReturnTimer.Elapsed += AllReturnsOnTimedEvent;
            getAllReturnTimer.Enabled = true;
            getAllReturnTimer.Start();
            /////////////////////////////////////////////////
            getStateTimer.Interval = 300;
            getStateTimer.Elapsed += GetStateOnTimedEvent;
            getStateTimer.Enabled = true;
            getStateTimer.Start();
            ///////////////////////////////////////////////
            controlTimer.Interval = 300;
            controlTimer.Elapsed += controlTimedEvent;
            controlTimer.Enabled = true;
            controlTimer.Start();
            //////////////////////////////////////////////
            disTaskTimer.Interval = 500;
            disTaskTimer.Elapsed += disTaskTimedEvent;
            disTaskTimer.Enabled = true;
            disTaskTimer.Start();
        }

        public static void StopAll()
        {
            getAllReturnTimer.Stop();
            /////////////////////////////////////////////////
            getStateTimer.Stop();
            ///////////////////////////////////////////////
            controlTimer.Stop();
            //////////////////////////////////////////////
            disTaskTimer.Stop();
        }

        private static void AllReturnsOnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
        {
            DateTime dt = DateTime.Now;
            if (!CStaticClass.GetAllReturnFlag)
            {
                CStaticClass.GetAllReturns();
            }
            FrmControlMonitor.FormInstance.label12.Text = "GetAllReturn300:" + (DateTime.Now - dt).TotalMilliseconds + "|" + Thread.CurrentThread.ManagedThreadId.ToString();
        }

        private static void GetStateOnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
        {
            DateTime dt = DateTime.Now;
            if (CStaticClass.Order == true)
            {
                //DateTime dt = DateTime.Now;
                if (!isGetStateRun)
                {
                    isGetStateRun = true;
                    new CGetState().GetDeviceState();
                    isGetStateRun = false;
                }
                //label82.Text = "nGetState300:" + (DateTime.Now - dt).TotalMilliseconds + "|" + Thread.CurrentThread.ManagedThreadId.ToString();
            }
            try
            {
                FrmControlMonitor.FormInstance.label82.Text = "nGetState300:" + (DateTime.Now - dt).TotalMilliseconds + "|" + Thread.CurrentThread.ManagedThreadId.ToString();
            }
            catch (Exception ex)
            {
            }
        }

        private static void controlTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
        {
            DateTime dt = DateTime.Now;
            if (CStaticClass.Order == true) // 自动监听plc
            {
                string ss1 = DateTime.Now.Second.ToString() + "-" + DateTime.Now.Millisecond.ToString();

                lock (thisLock)
                {
                    new CControl().StartOrder(); //发送命令
                }

                lock (thisLock)
                {
                    new CListenPLCAsk().DealwithPLCAsk();//处理主动上报状态的设备 叠盘/扫码/贴标等
                }
            }
            try
            {
                FrmControlMonitor.FormInstance.label63.Text = "Control300:" + (DateTime.Now - dt).TotalMilliseconds + "|" + Thread.CurrentThread.ManagedThreadId.ToString();
            }
            catch (Exception ex)
            {
            }
        }

        private static void disTaskTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
        {
            DateTime dt = DateTime.Now;
            if ((CStaticClass.Order == true))
            {
                //启动拆分进程MyTaskIntoSteps();
                int nSubCount = 0;
                lock (thisLock)
                {
                    nSubCount = CDisassembleTask.MyTaskIntoSteps();
                }
            }
            FrmControlMonitor.FormInstance.label79.Text = "Disassemble900:" + (DateTime.Now - dt).TotalMilliseconds + "|" + Thread.CurrentThread.ManagedThreadId.ToString();
        }

        #endregion 运行时
    }
}