DeviceConfig.cs 14.1 KB
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
 
namespace OnlineStore.LoadCSVLibrary
{
    public class DeviceConfig
    {
        public static  Dictionary<int, Dictionary<string, ConfigIO>> SubDIList { get; set; }
        public static Dictionary<int, Dictionary<string, ConfigIO>> SubDOList { get; set; }
        public static readonly ILog LOGGER = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public static  Dictionary<string, string> ProIpMap = null;
        /// <summary>
        /// ID
        /// </summary>
        public int Id { get; set; }
        /// <summary>
        /// 编号,与服务器通信的唯一标识
        /// </summary>
        public string CID { get; set; }
        /// <summary>
        /// 设备类型
        /// </summary>
        public string DType { 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; }





        /// <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 DeviceConfig()
        {
            initMustHavePro();
        }

        public DeviceConfig(int id, string cid, string type, string filepath)
        {
            initMustHavePro();
            MustHaveDIList = new List<string>();
            MustHaveDOList = new List<string>();
            this.Id = id;
            this.CID = cid;
            this.DType = type;
            this.ConfigFilePath = filepath;
        }
        private int LineSubType = 1000;
        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 (!ProIpMap.ContainsKey(con.ProName) && con.ProName.ToUpper().Contains("IP"))
                    {
                        ProIpMap.Add(con.ProName, con.ProVale);
                    }
                }

                else if (con.ProType == ConfigItemType.DI)
                {
                    // if (IO_Type.GetTypeList().Contains(con.ProName))
                    {
                        ConfigIO io = (ConfigIO)con;
                        if (!IOIPList.Contains(io.IO_IP) && io.GetIOAddr() >= 0)
                        {
                            IOIPList.Add(io.IO_IP);
                        }
                        if (io.SubType > 0)
                        {
                            if (io.SubType.Equals(LineSubType))
                            {
                                string newType = io.ProName.Replace("SW1_", "SW_").Replace("SW2_", "SW_").Replace("SW3_", "SW_").Replace("SW4_", "SW_");
                                if (DIList.ContainsKey(newType))
                                {
                                    LOGGER.Error("配置DI:[" + io.ToString() + "] ProName重复 ");
                                }
                                else
                                {
                                    this.DIList.Add(newType, io);
                                }
                            }
                            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.IO_IP) && io.GetIOAddr() >= 0)
                        {
                            IOIPList.Add(io.IO_IP);
                        }
                        if (io.SubType > 0)
                        {
                            if (io.SubType.Equals(LineSubType))
                            {
                                string newType = io.ProName.Replace("SW1_", "SW_").Replace("SW2_", "SW_").Replace("SW3_", "SW_").Replace("SW4_", "SW_");
                                if (DOList.ContainsKey(newType))
                                {
                                    LOGGER.Error("配置DO:[" + io.ToString() + "] ProName重复 ");
                                }
                                else
                                {
                                    this.DOList.Add(newType, io);
                                }
                            }
                            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(  int subType)
        {
            Dictionary<string, ConfigIO> doList = new Dictionary<string, ConfigIO>();
            Dictionary<string, ConfigIO> diList = new Dictionary<string, ConfigIO>();

            if (DeviceConfig.SubDIList.ContainsKey(subType))
            {
                diList = DeviceConfig.SubDIList[subType];
            }
            if (DeviceConfig.SubDOList.ContainsKey(subType))
            {
                doList = DeviceConfig.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;
        }
    } 
    public class DeviceType
    {
        /// <summary>
        /// 流水线线体
        /// </summary>
        public static string Line = "Line";
        /// <summary>
        /// 出入库移栽
        /// </summary>
        public static string MoveEquip = "MoveEquip";
        /// <summary>
        /// 入料模块
        /// </summary>
        public static string FeedingEquip = "FeedingEquip";
        /// <summary>
        /// 出料模块
        /// </summary>
        public static string ProvidingEquip = "ProvidingEquip";
        /// <summary>
        /// 出料流水线
        /// </summary>
        public static string DischargeLine = "DischargeLine";

    }
   
}