StoreConfig.cs 11.7 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> DIList { get; set; }
        /// <summary>
        /// 输出IO配置
        /// 手动料仓输出Io列表,key=对应的坐标位置的positionNum
        /// </summary>
        public Dictionary<String, ConfigIO> DOList { get; set; }



        public Dictionary<int,Dictionary<string,ConfigIO>> SubDIList { get; set; }
        public Dictionary<int, Dictionary<string, ConfigIO>> SubDOList { get; set; }


        /// <summary>
        /// 料仓所有用到的IO卡名称
        /// </summary>
        public List<string> IOIPList { 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)
        {
            DIList = new Dictionary<string, ConfigIO>();
            DOList = new Dictionary<string, ConfigIO>();
            SubDIList = new Dictionary<int, Dictionary<string, ConfigIO>>();
            SubDOList = new Dictionary<int, Dictionary<string, ConfigIO>>();
            IOIPList = new List<string>(); 
            //取得属性集合
            PropertyInfo[] props = GetType().GetProperties();
            Dictionary<string, string> proMap = CSVReaderBase.getConfigProAttributeMap(GetType());

            List<string> checkProList = new List<string>(proMap.Keys);//用来检测attribute属性都应该要配置值
            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;
                                
                            }
                            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 (IO_Type.GetTypeList().Contains(con.ProName))
                    {
                        ConfigIO io = (ConfigIO)con;
                        if (!IOIPList.Contains(io.DeviceName) &&  io.GetIOAddr() >= 0) 
                        {
                            IOIPList.Add(io.DeviceName);
                        }
                        if (io.SubType > 0)
                        {
                            if (!SubDIList.ContainsKey(io.SubType))
                            {
                                SubDIList.Add(io.SubType, new Dictionary<string, ConfigIO>());
                            }
                            if (SubDIList[io.SubType].ContainsKey(io.ProName))
                            {
                                LOGGER.Error("配置DI:[" + io.ToString() + "] ProName重复 ");
                            }
                            else { 
                            SubDIList[io.SubType].Add(io.ProName, io);
                            }
                        }
                        else
                        {
                            if (DIList.ContainsKey(io.ProName))
                            {
                                LOGGER.Error("配置DI:[" + io.ToString() + "] ProName重复 ");
                            }
                            else
                            {
                                this.DIList.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 (!IOIPList.Contains(io.DeviceName) && io.GetIOAddr() >= 0)
                        {
                            IOIPList.Add(io.DeviceName);
                        }
                        if (io.SubType > 0)
                        {
                            if (!SubDOList.ContainsKey(io.SubType))
                            {
                                SubDOList.Add(io.SubType, new Dictionary<string, ConfigIO>());
                            }
                            if (SubDOList[io.SubType].ContainsKey(io.ProName))
                            {
                                LOGGER.Error("配置DO:[" + io.ToString() + "] ProName重复 ");
                            }
                            else
                            {
                                SubDOList[io.SubType].Add(io.ProName, io);
                            }
                        }
                        else
                        {
                            if (DOList.ContainsKey(io.ProName))
                            {
                                LOGGER.Error("配置DO:[" + io.ToString() + "] ProName重复 ");
                            }
                            else
                            {
                                this.DOList.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 (!DIList.ContainsKey(di))
                    {
                        throw new CVSFieldNotMatchingExection(this.ToString() + "的DI属性" + di + "必须配置值!");
                    }
                }

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

            }
        }

        public void SetIO(Line_Config Config, int subType)
        {
            Dictionary<string, ConfigIO> doList = new Dictionary<string, ConfigIO>();
            Dictionary<string, ConfigIO> diList = new Dictionary<string, ConfigIO>();

            if (Config.SubDIList.ContainsKey(subType))
            {
                diList = Config.SubDIList[subType];
            }
            if (Config.SubDOList.ContainsKey(subType))
            {
                doList = Config.SubDOList[subType];
            }
            foreach (string key in diList.Keys)
            {
                DIList.Add(key, diList[key]);
            }
            foreach (string key in doList.Keys)
            {
                DOList.Add(key, doList[key]);
            }
        }
        public ConfigIO getWaitIO(string ioType)
        {
            if (DIList.ContainsKey(ioType))
            {
                return DIList[ioType];
            }
            else if (DOList.ContainsKey(ioType))
            {
                return DOList[ioType];
            }
            return null;
        }
        public string GetDisplayName(string ioType)
        {
            ConfigIO io = getWaitIO(ioType);
            if (io == null)
            {
                return ioType;
            }
            return io.DisplayStr;
        }
    } 
}