using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace MisDataFunDll
{
    public partial class MisDataFun
    {
        /// <summary>
        /// 保存质量数据和过站信息
        /// 处理function
        /// </summary>
        /// <param name="text"></param>
        public static void TransferInfToMES_WebSubmit(object text)
        {

            try
            {
                string recData = text.ToString();
                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();

                //if (!m_WorkStart_OpName.ContainsKey(m_OpName)) return;  //当前系统是否存在该工位 

                switch (m_Source)
                {
                    case "BarCode":
                        //处理条码逻辑
                        if (m_Command == "Bar")
                        {
                            MesServerFunction(recData);
                        }
                        break;
                    case "WEB":
                        if (m_Command == "Barcode")
                        {
                            MesServerFunction(recData);

                        }
                        else
                        {
                            string Content = m_Command + "|" + m_Value;
                            MesServerFunction(recData);
                        }
                        break;
                }

            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "TransferInf");
            }

        }

        public static void TransferInfToMES(object text)
        {



            try
            {

                string recData = text.ToString();
                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();


                string mesServerdllName;
                //获得工位对应的 dll
                if (!GetMesServerDllNameByOpName(out mesServerdllName, m_OpName)) return;
                if (mesServerdllName == null) return;
                if (mesServerdllName.Length < 1) return;

                string ResultOK = "";
                object[] Paramsters = new object[1];
                Paramsters[0] = text;

                System.Reflection.Assembly assembly = null;

                bool IsOK = Hashtable_assembly.ContainsKey(mesServerdllName);
                if (IsOK)
                {
                    assembly = (System.Reflection.Assembly)Hashtable_assembly[mesServerdllName];
                }
                else
                {
                    assembly = System.Reflection.Assembly.Load(mesServerdllName);
                    if (assembly == null)
                    {
                        return;
                    }
                    Hashtable_assembly.Add(mesServerdllName, assembly);
                }
                ResultOK = Reflection.ReflectionMethod(assembly, "MesServerFunctions", "FunctionMES", "FunWork", Paramsters);

            }
            catch (Exception err)
            {
                ////ApplicationLog.WriteLog(err, "TransferInf");
            }

        }
        /// <summary>
        /// 获得工位对应的 dll
        /// </summary>
        /// <param name="mesServerdllName"></param>
        /// <param name="opName"></param>
        /// <returns></returns>
        private static bool GetMesServerDllNameByOpName(out string mesServerdllName, string opName)
        {
            try
            {
                //HashtableList hsList = DtWebApi.OpcDemo.GetHashtableList();
                //if(opName == "J07ET055")
                //{
                //    mesServerdllName = "Function1061";
                //}
                //else
                //{
                mesServerdllName = hashtable_MesDll[opName].ToString();
                //}
                return true;
            }
            catch
            {
                mesServerdllName = "";
                return false;
            }
        }



    }
}

