using System;
using System.Reflection;
using System.Collections;
using System.Configuration;
using ICommLayer;
using System.Data;
using DBFactory;
namespace CommLayerFactory
{
    /// <summary>
    /// 通讯方式的类工厂
    /// Creator:Richard.liu
    /// </summary>
    public sealed  class CommModeCreate
    {
        //static DBOperator dbo = new DBOperator();
        private static string path = "";
        private static Model.MDevice devinfo;//20100127
        CommModeCreate()
        {
            
        }
        //Assembly.Load (AssemblyName) 在给定程序集的 AssemblyName 的情况下，加载程序集,return Assembly 
        // Assembly.CreateInstance(className)
        // 从此程序集中查找IStack类型，然后使用系统激活器创建它的实例。 
        /// <summary>
        /// 根据设备索引创建获取设备状态的派生类
        /// </summary>
        /// <param name="Devices"></param>
        /// <returns></returns>
        public static IGetDeviceState CreateGetDeviceState(int  Devices)
        {
            
            try
            {
                path = GetPath(Devices);
                string classname = path + ".CGetDeviceState";
                return (IGetDeviceState)Assembly.Load(path).CreateInstance(classname);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
        ///// <summary>
        ///// 在串口读取数据
        ///// </summary>
        ///// <param name="Devices"></param>
        ///// <returns></returns>
        //public static IGetSerialData CreateGetSerialData(int Devices)
        //{

        //    try
        //    {
        //        path = GetPath(Devices);
        //        string classname = path + ".CGetSerialData";
        //        return (IGetSerialData)Assembly.Load(path).CreateInstance(classname);
        //    }
        //    catch (Exception ex)
        //    {
        //        throw (ex);
        //    }
        //}
        /// <summary>
        /// 根据设备索引创建发送命令的派生类
        /// </summary>
        /// <param name="Devices"></param>
        /// <returns></returns>
        public static ISendDeviceOrder CreateSendDeviceOrder(int Devices)
        {

            try
            {
                path = GetPath(Devices);
                string classname = path + ".CSendDeviceOrder";
                return (ISendDeviceOrder)Assembly.Load(path).CreateInstance(classname);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
        /// <summary>
        /// 根据设备索引获得通讯接口的派生类
        /// </summary>
        /// <param name="Devices"></param>
        /// <returns></returns>
        static string  GetPath(int  Devices)
        {
            try
            {
                //20100127
                devinfo = Model.CGetInfo.GetDeviceInfo(Devices);
                //DataSet ds = dbo.ExceSQL("select F_CommType from T_Base_Device where (F_CommType IS NOT NULL) AND F_DeviceIndex=" + Devices);
                if (devinfo.CommType!=null)
                {
                    //dbo.Close();
                    return devinfo.CommType;//ds.Tables[0].DefaultView[0]["F_CommType"].ToString();
                }
                else
                {
                    //dbo.Close();
                    return null;
                }
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
    }

}
