using DataLinkMesWork1;

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;

namespace MisDataFunDll
{
    public partial class MisDataFun
    {
        static object locker = new object();
        /// <summary>
        ///引用程序集Hashtable
        /// </summary>
        public static Hashtable Hashtable_assembly = new Hashtable();
        public static Hashtable hashtable_MesServerCode = new Hashtable();
        public static Hashtable hashtable_MesDll = new Hashtable();
        public static Hashtable hashtable_TagTypeTagName = new Hashtable();
        public static Hashtable hashtable_TagName = new Hashtable();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="hashtable_TagName">变量名称对应的变量类型代码</param>
        /// <param name="hashtable_TagTypeTagName">变量类型代码对应的变量名称</param>
        public static void GetBaseData_tagTable_TagName(out Hashtable hashtable_TagName, out Hashtable hashtable_TagTypeTagName)
        {
            hashtable_TagName = new Hashtable();
            hashtable_TagTypeTagName = new Hashtable();

            string tagName;
            string tagTypeCode;
            DataTable dt;//GetMesServerCodeFromMIS
            dt = BaseData_tagTable_TagName();
            if (dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    tagName = dt.Rows[i]["TagName"].ToString();
                    tagTypeCode = dt.Rows[i]["变量类型代码"].ToString();
                    if (!hashtable_TagName.Contains(tagName))
                    {
                        hashtable_TagName.Add(tagName, tagTypeCode);
                    }
                    if (!hashtable_TagTypeTagName.Contains(tagTypeCode))
                    {
                        hashtable_TagTypeTagName.Add(tagTypeCode, tagName);
                    }
                }
            }
        }

        public static void GetMesServerCodeAndMesDll(out Hashtable hashtable_MesServerCode, out Hashtable hashtable_MesDll)
        {
            hashtable_MesServerCode = new Hashtable();
            hashtable_MesDll = new Hashtable();

            string OpName;
            string MesServerCode;
            string MesDll;
            DataTable dt;//GetMesServerCodeFromMIS
            dt = GetMesServerCodeFromMIS();
            if (dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    OpName = dt.Rows[i]["工位号"].ToString();
                    MesServerCode = dt.Rows[i]["MesServerCode"].ToString();
                    MesDll = dt.Rows[i]["MesDll"].ToString();
                    if (!hashtable_MesDll.Contains(OpName))
                    {
                        hashtable_MesDll.Add(OpName, MesDll);
                    }
                    if (!hashtable_MesServerCode.Contains(OpName))
                    {
                        hashtable_MesServerCode.Add(OpName, MesServerCode);
                    }
                }
            }
        }

  
        /// <summary>
        /// MesServerFunction
        /// </summary>
        /// <param name="OpName"></param>
        /// <param name="tagValue"></param>
        /// <param name="fun"></param>
        public static void TransferInfToMES(object text)
        {
            try
            {
                string recData = text.ToString();
                List<string> dataArray = GetCmdStringList(recData);
                //消息来源
                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
            {
                mesServerdllName = hashtable_MesDll[opName].ToString();
                return true;
            }
            catch
            {
                mesServerdllName = "";
                return false;
            }
        }



    }
}