StoreConfig.cs 8.8 KB
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
 
namespace OnlineStore.LoadCSVLibrary
{
    public class StoreConfig
    {
        public static readonly ILog LOGGER = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        /// <summary>
        /// ID
        /// </summary>
        public int Id { get; set; }
        /// <summary>
        /// 编号,与服务器通信的唯一标识
        /// </summary>
        public string CID { get; set; }
        /// <summary>
        /// 料层类型
        /// </summary>
        public string StoreType { get; set; }
        /// <summary>
        /// 配置文件路径
        /// </summary>
        public string ConfigFilePath { get; set; }

        /// <summary>
        /// 输入IO配置
        /// 手动料仓输入Io列表,key=对应的坐标位置的positionNum
        /// </summary>
        public Dictionary<String, ConfigIO> StoreDIList { get; set; }
        /// <summary>
        /// 输出IO配置
        /// 手动料仓输出Io列表,key=对应的坐标位置的positionNum
        /// </summary>
        public Dictionary<String, ConfigIO> StoreDOList { get; set; }
        /// <summary>
        /// 料仓所有用到的IO卡名称
        /// </summary>
        public List<string> DIODeviceNameList { get; set; }
        /// <summary>
        /// 料仓所有用到的运动卡名称
        /// </summary>
        public List<string> SMCDeviceNameList { get; set; }
        /// <summary>
        /// 必须拥有的DI列表
        /// </summary>
        protected List<string> MustHaveDIList { get; set; }
        /// <summary>
        /// 必须拥有的DO列表
        /// </summary>
        protected List<string> MustHaveDOList { get; set; }

        protected virtual void initMustHavePro()
        {
            MustHaveDIList = new List<string>();
            MustHaveDOList = new List<string>();
        }
        public StoreConfig()
        {
            initMustHavePro();
        }

        public StoreConfig(int id, string cid, string type, string filepath)
        {
            initMustHavePro();
            MustHaveDIList = new List<string>();
            MustHaveDOList = new List<string>();
            this.Id = id;
            this.CID = cid;
            this.StoreType = type;
            this.ConfigFilePath = filepath;
        }

        public virtual void LoadConfig(List<ConfigBase> configList)
        {
            StoreDIList = new Dictionary<string, ConfigIO>();
            StoreDOList = new Dictionary<string, ConfigIO>();
            DIODeviceNameList = new List<string>();
            SMCDeviceNameList = new List<string>();
            //取得属性集合
            PropertyInfo[] props = GetType().GetProperties();
            Dictionary<string, string> proMap = CSVReaderBase.getConfigProAttributeMap(GetType());

            List<string> checkProList = new List<string>(proMap.Keys);//用来检测attribute属性都应该要配置值
            List<string> IoTypeList = IO_Type.GetTypeList();
            foreach (ConfigBase con in configList)
            {
                if (con.ProType == ConfigItemType.AXIS || con.ProType == ConfigItemType.PRO)
                {
                    if (proMap.ContainsKey(con.ProName))
                    {
                        string proName = proMap[con.ProName];
                        checkProList.Remove(con.ProName);
                        PropertyInfo prop = props.First(c => c.Name == proName);//获取同名属性
                        if (prop != null)
                        {
                            if (con.ProType == ConfigItemType.AXIS)
                            {
                                //如果属性存在
                                prop.SetValue(this, Convert.ChangeType(con, prop.PropertyType), null);//赋值****在这里需要考虑类型问题
                                ConfigMoveAxis axis = (ConfigMoveAxis)con;
                                if (!SMCDeviceNameList.Contains(axis.DeviceName) && axis.GetAxisValue() >= 0)
                                {
                                    SMCDeviceNameList.Add(axis.DeviceName);
                                }
                            }
                            else if (con.ProType == ConfigItemType.PRO)
                            {
                                prop.SetValue(this, Convert.ChangeType(con.ProVale, prop.PropertyType), null);//赋值 
                            }
                        }
                    }
                    else
                    {
                        LOGGER.Error("配置文件:" + this.ConfigFilePath + ",属性名=" + con.ProName + "的属性未找到匹配字段!");
                    }
                }
                else if (con.ProType == ConfigItemType.DI)
                {
                    if (IoTypeList.Contains(con.ProName))
                    {
                        ConfigIO io = (ConfigIO)con;
                        if (!DIODeviceNameList.Contains(io.DeviceName) &&  io.GetIOAddr() >= 0)
                        {
                            DIODeviceNameList.Add(io.DeviceName);
                        }
                        this.StoreDIList.Add(con.ProName, io);
                    }
                    else
                    {
                        LOGGER.Error("配置文件:" + this.ConfigFilePath + ",属性名=" + con.ProName + "的属性未找到匹配字段!");
                    }
                }
                else if (con.ProType == ConfigItemType.DO)
                {
                    if (IO_Type.GetTypeList().Contains(con.ProName))
                    {
                        ConfigIO io = (ConfigIO)con;
                        if (!DIODeviceNameList.Contains(io.DeviceName) && io.GetIOAddr() >= 0)
                        {
                            DIODeviceNameList.Add(io.DeviceName);
                        }
                        this.StoreDOList.Add(con.ProName, io);
                    }
                    else
                    {
                        LOGGER.Error("配置文件:" + this.ConfigFilePath + ",属性名=" + con.ProName + "的属性未找到匹配字段!");
                    }
                }
            }

            if (checkProList.Count >= 0)
            {
                //常规属性检测
                foreach (string str in checkProList)
                {
                    PropertyInfo prop = props.First(c => c.Name == str);//获取同名属性
                    //判断是否必须要配置
                    object[] array = prop.GetCustomAttributes(false);
                    if (array.Length > 0)
                    {
                        ConfigProAttribute att = (ConfigProAttribute)array[0];
                        if (att != null)
                        {
                            if (att.IsMust)
                            {
                                throw new CVSFieldNotMatchingExection(this.ToString() + "的属性" + str + "必须配置值!");
                            }
                            else
                            {
                                if (prop.PropertyType.Equals(typeof(int)))
                                {
                                    prop.SetValue(this, Convert.ChangeType(0, prop.PropertyType), null);//赋值****在这里需要考虑类型问题
                                }
                                else
                                {
                                    prop.SetValue(this, Convert.ChangeType("", prop.PropertyType), null);//赋值****在这里需要考虑类型问题
                                }
                            }
                        }
                    }
                }

                //DI检测
                foreach (string di in MustHaveDIList)
                {
                    if (!StoreDIList.ContainsKey(di))
                    {
                        throw new CVSFieldNotMatchingExection(this.ToString() + "的DI属性" + di + "必须配置值!");
                    }
                }

                //DO检测
                foreach (string io in MustHaveDOList)
                {
                    if (!this.StoreDOList.ContainsKey(io))
                    {
                        throw new CVSFieldNotMatchingExection(this.ToString() + "的DO属性" + io + "必须配置值!");
                    }
                }

            }
        }
         
        public ConfigIO getWaitIO(string ioType)
        {
            if (StoreDIList.ContainsKey(ioType))
            {
                return StoreDIList[ioType];
            }
            else if (StoreDOList.ContainsKey(ioType))
            {
                return StoreDOList[ioType];
            }
            return null;
        } 
    } 
}