using MesServerWork;
using System.Collections.Generic;
using System.Threading;
using MisDataFunDll;
using System.Configuration;


namespace MesServerFunctions
{
    public partial class FunctionMES
    {
        object lockFunWork;
        //public void FunWork(DeviceDriver_BasicData.CustomeEvetnArgs e)
        public void FunWork(string recData)
        {


            lockFunWork = new object();
            lock (lockFunWork)
            {
                List<string> dataArray = MisDataFun.GetCmdStringList(recData);
                //string[] dataArray = recData.Split('|');
                //if (dataArray.Count != 4)
                //return;
                //消息来源
                string m_Source = dataArray[0].ToString();
                //消息类型 Barcode
                string m_Command = dataArray[1].ToString();
                //工位号
                string m_OpName = dataArray[2].ToString();
                //消息参数
                string m_Value = dataArray[3].ToString();

                DeviceDriver_BasicData.CustomeEvetnArgs e = new DeviceDriver_BasicData.CustomeEvetnArgs();// = Newtonsoft.Json.JsonConvert.DeserializeObject<DeviceDriver_BasicData.CustomeEvetnArgs>(m_Value);
                string tagValue;
                switch (m_Source)
                {
                    case "BarCode":
                        e.OpName = m_OpName;
                        e.TagValue = m_Value;
                        fun_Barcode(e);

                        break;
                    case "WEB":
                        e.OpName = m_OpName;
                        e.TagValue = m_Command + "|" + m_Value;
                        fun_WebSubmit(e);
                        break;
                    case "PLC":
                        e = Newtonsoft.Json.JsonConvert.DeserializeObject<DeviceDriver_BasicData.CustomeEvetnArgs>(m_Value);
                        string TagID = e.TagID.ToString();
                        switch (e.TagValue.ToString().ToLower())
                        {
                            case "false":
                                tagValue = "0";
                                break;
                            case "true":
                                tagValue = "1";
                                break;
                            default:
                                tagValue = e.TagValue.ToString();
                                break;
                        }
                        e.TagValue = tagValue;
                        switch (e.TagTypeCodeID.ToString())
                        {
                            //PLC 工件到位
                            case "2":
                                fun_WorkStart(e);
                                break;
                            //PLC 允许保存质量数据
                            case "19":
                                fun_MesRead(e);
                                break;
                            //MES 保存数据完成
                            case "20":
                                fun_MesReadOver(e);
                                break;
                            //PLC 申请上线
                            case "43":
                                PartLoad(e);
                                break;
                            //PLC 传递机型
                            case "44":
                                fun_EngineTypeGetOverPLC(e);
                                break;
                            //MES 合格放行
                            case "51":
                                fun_ConfirmOkGo(e);
                                break;
                            //MES 不合格放行
                            case "8":
                                fun_FalseGo(e);
                                break;
                            // PLC 系统健康心跳
                            case "14":
                                fun_HeartBeatMIS(e);
                                break;
                            //物料扫码完成（弃用！）
                            case "49":
                                fun_MaterislaScanOver(e);
                                break;
                            //物料扫码完成
                            case "65":
                                fun_MaterialsVerify(e);
                                break;
                            // 写入托盘号后通知MES
                            case "48":
                                string write_Pallet_res;
                                MIS_BASE.Read_TagTypeID(48, m_OpName, out write_Pallet_res);
                                if (write_Pallet_res.ToLower() == "true" || write_Pallet_res.ToLower() == "1")
                                {
                                    SendMessageFun.SendInfomation("writePallet", "1", m_OpName);
                                }
                                MIS_BASE.WritePLC(70, m_OpName, false);
                                break;
                            case "63":
                                fun_ReqEarlyFeeding(e);
                                break;
                            case "64":
                                fun_ReqTestResistor(e);
                                break;
                        }
                        if (ConfigurationManager.AppSettings["WebapiPostHeader_Systemcode"].ToString() == "MES_ZHDQ_126ZZ")
                        {
                            switch (e.TagTypeCodeID.ToString())
                            {
                                case "58": // 请求AGV进站（上线）
                                    if (tagValue == "1") MIS_BASE.WritePLC(28, m_OpName, true);
                                    else MIS_BASE.WritePLC(28, m_OpName, false);
                                    break;
                                case "59": // 请求前往总装点（上线）
                                    if (tagValue == "1") MIS_BASE.WritePLC(29, m_OpName, true);
                                    else MIS_BASE.WritePLC(29, m_OpName, false);
                                    break;
                                case "76": // 请求AGV离开（上线）
                                    if (tagValue == "1") MIS_BASE.WritePLC(30, m_OpName, true);
                                    else MIS_BASE.WritePLC(30, m_OpName, false);
                                    break;
                                case "61": // 请求AGV退出（自动）
                                    if (tagValue == "1") MIS_BASE.WritePLC(31, m_OpName, true);
                                    else MIS_BASE.WritePLC(31, m_OpName, false);
                                    break;
                                case "62": // 请求AGV进入（自动）
                                    if (tagValue == "1") MIS_BASE.WritePLC(32, m_OpName, true);
                                    else MIS_BASE.WritePLC(32, m_OpName, false);
                                    break;
                                case "63": // 请求AGV托盘举升
                                    if (tagValue == "1") MIS_BASE.WritePLC(33, m_OpName, true);
                                    else MIS_BASE.WritePLC(33, m_OpName, false);
                                    break;
                                case "64": // 请求AGV托盘下降
                                    if (tagValue == "1") MIS_BASE.WritePLC(34, m_OpName, true);
                                    else MIS_BASE.WritePLC(34, m_OpName, false);
                                    break;
                                case "65": // 请求AGV离站（自动）
                                    if (tagValue == "1") MIS_BASE.WritePLC(35, m_OpName, true);
                                    else MIS_BASE.WritePLC(35, m_OpName, false);
                                    break;
                                default:
                                    break;
                            }
                        }
                        break;
                }
            }
        }
        //public void  FunWork(DeviceDriver_BasicData.CustomeEvetnArgs e) 
        //{
        //    //lock1 = new object();
        //    //lock (lock1)
        //    {
        //        switch (e.TagTypeCodeID.ToString())
        //        {
        //            //PLC 工件到位
        //            case "2":
        //                fun_WorkStart(e);
        //                break;
        //            //PLC 允许保存质量数据
        //            case "19":
        //                fun_MesRead(e);
        //                break;
        //            //MES 保存数据完成
        //            case "20":
        //                fun_MesReadOver(e);
        //                break;
        //            //PLC 传递机型
        //            case "44":
        //                fun_EngineTypeGetOverPLC(e);
        //                break;
        //            //MES 合格放行
        //            case "51":
        //                fun_ConfirmOkGo(e);
        //                break;
        //            //MES 不合格放行
        //            case "8":
        //                fun_FalseGo(e);
        //                break;
        //            //MES 扫描条码
        //            case "Barcode":
        //                fun_Barcode(e);
        //                break;
        //            //WEB 处理页面传递过来的数据
        //            case "WebSubmit":
        //                fun_WebSubmit(e);
        //                break;
        //        }
        //    }

        //}
        //1.工件到位后，PLC给MES传递机型(DBX0.4)(44)，
        //2.MES判断是否扫描完成
        //3.若扫描完成，则MES给出合格放行(DBX2.5)(51)
        /// <summary>
        /// 扫描条码
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="barcode"></param>
        public void fun_Barcode(DeviceDriver_BasicData.CustomeEvetnArgs e)
        {
            ThreadPool.QueueUserWorkItem(Barcode, e);
        }


        /// <summary>
        /// 传递机型
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        public void fun_EngineTypeGetOverPLC(DeviceDriver_BasicData.CustomeEvetnArgs e)
        {
            ThreadPool.QueueUserWorkItem(EngineTypeGetOver_PLC, e);
        }

        /// <summary>
        /// 合格放行
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        public void fun_ConfirmOkGo(DeviceDriver_BasicData.CustomeEvetnArgs e)
        {
            //// ThreadPool.QueueUserWorkItem(ConfirmOkGo, e);
        }


        /// <summary>
        /// 提前上料
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        public void fun_ReqEarlyFeeding(DeviceDriver_BasicData.CustomeEvetnArgs e)
        {
            ThreadPool.QueueUserWorkItem(ReqEarlyFeeding, e);
        }

        /// <summary>
        /// 请求测试电阻 调用电阻仪
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        public void fun_ReqTestResistor(DeviceDriver_BasicData.CustomeEvetnArgs e)
        {
            ThreadPool.QueueUserWorkItem( ReqTestResistor, e);
        }

        /// <summary>
        /// 不合格放行
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        public void fun_FalseGo(DeviceDriver_BasicData.CustomeEvetnArgs e)
        {
            // ThreadPool.QueueUserWorkItem(FalseGO, e);
        }
        

        /// <summary>
        /// 保存完成 20
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        public void fun_MesReadOver(DeviceDriver_BasicData.CustomeEvetnArgs e)
        {
            // ThreadPool.QueueUserWorkItem(MesReadOver, e);
        }

        /// <summary>
        /// 物料扫码完成
        /// </summary>
        public void fun_MaterislaScanOver(DeviceDriver_BasicData.CustomeEvetnArgs e)
        {
            ThreadPool.QueueUserWorkItem(MaterialsScanOver, e);
        }

        /// <summary>
        /// 物料扫码完成
        /// </summary>
        public void fun_MaterialsVerify(DeviceDriver_BasicData.CustomeEvetnArgs e)
        {
            ThreadPool.QueueUserWorkItem(MaterialsVerify, e);
        }

        /// <summary>
        /// 保存完成 19
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        public void fun_MesRead(DeviceDriver_BasicData.CustomeEvetnArgs e)
        {
            ThreadPool.QueueUserWorkItem(MesRead, e);
        }

        /// <summary>
        /// 工件到位离开(清空所有的MES信号)
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        public void fun_WorkStart(DeviceDriver_BasicData.CustomeEvetnArgs e)
        {
            ThreadPool.QueueUserWorkItem(WorkStart, e);
        }

        /// <summary>
        /// 处理WEB页面发送过来的请求（m_Command + "|" + m_Value;）
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="Content"></param>
        public void fun_WebSubmit(DeviceDriver_BasicData.CustomeEvetnArgs e)
        {
            ThreadPool.QueueUserWorkItem(WebSubmit, e);
        }

        public int fun_SubLineLoadPartCountMatch()
        {
            return SubLineLoadPartCountMatch;
        }


        /// <summary>
        /// MES系统健康心跳监测
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        public void fun_HeartBeatMIS(DeviceDriver_BasicData.CustomeEvetnArgs e)
        {
            // ThreadPool.QueueUserWorkItem(HeartBeatMES, e);
        }
        /// <summary>
        /// PLC系统健康心跳监测
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        public void fun_HeartBeatPLC(DeviceDriver_BasicData.CustomeEvetnArgs e)
        {
            //ThreadPool.QueueUserWorkItem(HeartBeatPLC, e);
        }
    }
}
