using bizFacade;
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)
        {


            lockFunWork = new object();
            lock (lockFunWork)
            {
                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;
                            //PLC 工位合格标志
                            case "22":
                                fun_IsOk(e);
                                break;
                            //PLC 螺栓补拧结束
                            case "30":
                                int value = Convert.ToInt32(MIS_BASE.ReadPLC(30, m_OpName));
                                if (value == 0)
                                {
                                    MIS_BASE.WritePLC(77, m_OpName, 0);
                                    MIS_BASE.WritePLC(88, m_OpName, 0);
                                    //先修改按钮显示
                                    BaseDataSystemMES.SetButtomShouOrHide(m_OpName, "MES_BoltAddWorkStart", 1, 1, "MES_BoltAddWorkEnd", 0);
                                    //通知前端刷新按钮
                                    SendMessageFun.SendInfomation("SetButtomShouOrHide", "1", m_OpName);
                                }
                                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 (Convert.ToInt32(S_plcStatus) == 0)
                                {
                                    SendMessageFun.SendInfomation("PlcStatus", "0", m_OpName);
                                }
                                else
                                {
                                    SendMessageFun.SendInfomation("PlcStatus", "1", m_OpName);
                                }
                                break;
                            case "64":
                                // MA040确认返工 (501.6)
                                SendMessageFun.SendInfomation("ShowBackWorkBox", "1", m_OpName);
                                break;
                            // PLC 程序号
                            case "125":
                                // 将程序号转发给前端页面
                                SendMessageFun.SendInfomation("CodeNumber", e.TagValue.ToString(), m_OpName);
                                break;
                            // PLC 程序号的第几颗螺丝
                            case "126":
                                SendMessageFun.SendInfomation("CodeNumberWorkNumber", e.TagValue.ToString(), m_OpName);
                                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(DoWhatPlc.CustomeEvetnArgs e)
        {
            ThreadPool.QueueUserWorkItem(Barcode, 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_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_FalseGo(DoWhatPlc.CustomeEvetnArgs e)
        {
            ThreadPool.QueueUserWorkItem(FalseGO, e);
        }

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

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

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