using MesServerWork;
using MisDataFunDll;
using MisDataSaveDate;
using System.Collections.Generic;
using System.Threading;
using System.Xml.Linq;

namespace MesServerFunctions
{
    public partial class FunctionMES
    {
        object lockFunWork;
        //public void FunWork(DoWhatPlc.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();

                var e = new DeviceDriver_BasicData.CustomeEvetnArgs();// = Newtonsoft.Json.JsonConvert.DeserializeObject<DoWhatPlc.CustomeEvetnArgs>(m_Value);
                string tagValue;

                switch (m_Source)
                {
                    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 "44":
                                fun_EngineTypeGetOverPLC(e);
                                break;
                            //MES 设备报警复位
                            case "61":
                                MIS_BASE.Write_MesErrorType(m_OpName, 0);
                                break;
                        }
                        break;
                }
            }
        }

        //1.工件到位后，PLC给MES传递机型(DBX0.4)(44)，
        //MES判断条码是否全部扫描完成
        //PLC给出允许保存(DBX100.2)(19)，MES见到(DBX100.2)(19)为1后给出保存完成(DBX102.2)(20)，
        //PLC见到保存完成(DBX102.2)(20)为1后，清空允许保存(DBX100.2)(19)，
        //MES见到允许保存(DBX100.2)(19)为0后，清空保存完成(DBX102.2)(20)
        //MES判断总合格标志(INT104)(21)为1后，给出合格放行(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_FalseGo(DeviceDriver_BasicData.CustomeEvetnArgs e)
        {
            ThreadPool.QueueUserWorkItem(FalseGO, e);
        }

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

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


        /// <summary>
        /// 工位是否合格
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        public void fun_IsOk(DeviceDriver_BasicData.CustomeEvetnArgs e)
        {
            ThreadPool.QueueUserWorkItem(IsOk, e);
        }

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


        /// <summary>
        /// 空料握手plc
        /// </summary>
        public void fun_Handshake(DeviceDriver_BasicData.CustomeEvetnArgs e)
        {
            ThreadPool.QueueUserWorkItem(Handshake_Fun, e);
        }


        /// <summary>
        /// MES实时保存质量数据
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        public void fun_MesSaveNowData(DeviceDriver_BasicData.CustomeEvetnArgs e)
        {
            ThreadPool.QueueUserWorkItem(MesSaveNowData, e);
        }


    }
}
