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

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


            {
                List<string> dataArray = DoWhatPlc.MessageText.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();

                DoWhatPlc.CustomeEvetnArgs e =new DoWhatPlc.CustomeEvetnArgs();// = Newtonsoft.Json.JsonConvert.DeserializeObject<DoWhatPlc.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<DoWhatPlc.CustomeEvetnArgs>(m_Value);
                        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 "44":
                                fun_EngineTypeGetOverPLC(e);
                                break;
                            //MES 合格放行
                            case "51":
                                fun_ConfirmOkGo(e);
                                break;
                            //MES 不合格放行
                            case "8":
                                fun_FalseGo(e);
                                break;
                            //MES 系统健康心跳
                            case "14":
                                fun_HeartBeatMIS(e);
                                break;
                            case "63":
                                string S_plcStatus;//DBX501.5 PLC直通，1 强制放行 0 正常循环
                                MIS_BASE.Read_TagTypeID(63, m_OpName, out S_plcStatus);
                                if (S_plcStatus.ToLower() != "true" || S_plcStatus.ToLower() == "1")
                                {
                                    SendMessageFun.SendInfomation("PlcStatus", "0", m_OpName);
                                }
                                else
                                {
                                    SendMessageFun.SendInfomation("PlcStatus", "1", m_OpName);
                                }
                                break;
                        }
                        break;
                }
            }
        }
        //public void  FunWork(DoWhatPlc.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(DoWhatPlc.CustomeEvetnArgs e)
        {
            ThreadPool.QueueUserWorkItem(Barcode, e);
        }


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

        /// <summary>
        /// 合格放行
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        public void fun_ConfirmOkGo(DoWhatPlc.CustomeEvetnArgs e)
        {
            ThreadPool.QueueUserWorkItem(ConfirmOkGo, e);
        }
        /// <summary>
        /// 系统健康心跳监测
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        public void fun_HeartBeatMIS(DoWhatPlc.CustomeEvetnArgs e)
        {
            ThreadPool.QueueUserWorkItem(HeartBeat, e);
        }
        /// <summary>
        /// 不合格放行
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        public void fun_FalseGo(DoWhatPlc.CustomeEvetnArgs e)
        {
            ThreadPool.QueueUserWorkItem(FalseGO, e);
        }
        

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

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

        /// <summary>
        /// 工件到位离开(清空所有的MES信号)
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        public void fun_WorkStart(DoWhatPlc.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(DoWhatPlc.CustomeEvetnArgs e)
        {
            ThreadPool.QueueUserWorkItem(WebSubmit, e);
        }

        public int fun_SubLineLoadPartCountMatch()
        {
            return SubLineLoadPartCountMatch;
        }

    }
}
