| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 | using System;using System.Collections.Generic;using System.IO;using System.Reflection;using System.Xml;using Aitex.Core.RT.Log;using Aitex.Core.RT.SCCore;using Aitex.Core.Util;using MECF.Framework.Common.IOCore;using CyberX8_Core;namespace MECF.Framework.RT.Core.IoProviders{    public class IoProviderManager : Singleton<IoProviderManager>    {        private List<IIoProvider> _providers = new List<IIoProvider>();        public List<IIoProvider> Providers => _providers;        /// <summary>        ///         /// </summary>        /// <param name="xmlConfigFile">配置文件名,绝对路径</param>        /// <param name="ioMappingPathFile">需要激活的IO Provider名字</param>        public void Initialize(string xmlConfigFile, Dictionary<string, Dictionary<int, string>> ioMappingPathFile)        {            XmlDocument xml = new XmlDocument();            try            {                xml.Load(xmlConfigFile);                XmlNodeList nodeProviders = xml.SelectNodes("IoProviders/IoProvider");                foreach (var nodeProvider in nodeProviders)                {                    XmlElement element = nodeProvider as XmlElement;                    if (element == null)                        continue;                    XmlElement nodeParameter = element.SelectSingleNode("Parameter") as XmlElement;                    if (nodeParameter == null)                        continue;                    string module = element.GetAttribute("module").Trim();                    string name = element.GetAttribute("name").Trim();                    string className = element.GetAttribute("class").Trim();                    string assemblyName = element.GetAttribute("assembly").Trim();                    string loadCondition = element.GetAttribute("load_condition").Trim();                    bool isSimulator = SC.GetConfigItem("System.IsSimulatorMode").BoolValue;                    //0 : Simualtor, 1:real 2: both                    if (isSimulator && loadCondition!="0" && loadCondition != "2")                    {                        continue;                    }                    if (!isSimulator && loadCondition != "1" && loadCondition != "2")                    {                        continue;                    }                    string source = module + "." + name;                    Type t = Assembly.Load(assemblyName).GetType(className);                    if (t == null)                    {                        throw new Exception(string.Format("ioProvider config file class and assembly not valid,"+ source));                    }                    IIoProvider provider;                    try                    {                        provider = (IIoProvider)Activator.CreateInstance(t);                        _providers.Add(provider);                    }                    catch (Exception ex)                    {                        LOG.WriteExeption(ex);                        throw new Exception(string.Format("ioProvider can not be created," + source));                    }                    List<IoBlockItem> lstBuffers = new List<IoBlockItem>();                    XmlNodeList nodeBlocks = element.SelectNodes("Blocks/Block");                    foreach (var nodeBlock in nodeBlocks)                    {                        XmlElement blockElement = nodeBlock as XmlElement;                        if (blockElement == null)                            continue;                        IoBlockItem section = new IoBlockItem();                        string type = blockElement.GetAttribute("type");                        string offset = blockElement.GetAttribute("offset");                        string size = blockElement.GetAttribute("size");                        string value_type = blockElement.GetAttribute("value_type");                        int result;                        if (!int.TryParse(offset, out result))                        {                            continue;                        }                        section.Offset = result;                        if (!int.TryParse(size, out result))                        {                            continue;                        }                        section.Size = result;                        switch (type.ToLower())                        {                            case "ai":                                section.Type = IoType.AI;                                break;                            case "ao":                                section.Type = IoType.AO;                                break;                            case "di":                                section.Type = IoType.DI;                                break;                            case "do":                                section.Type = IoType.DO;                                break;                            default:                                continue;                        }                                                lstBuffers.Add(section);                    }                    if (ioMappingPathFile.ContainsKey(source))                    {                        provider.Initialize(module, name, lstBuffers, IoManager.Instance, nodeParameter, ioMappingPathFile[source]);                    }                    else                    {                        throw new Exception(string.Format("can not find io map config files," + source));                    }                     provider.Start();                }            }            catch (Exception ex)            {                throw  new ApplicationException("IoProvider configuration not valid," + ex.Message);            }                     }        /// <summary>        ///         /// </summary>        /// <param name="xmlConfigFile">配置文件名,绝对路径</param>        public void Initialize(string xmlConfigFile)        {            XmlDocument xml = new XmlDocument();            try            {                xml.Load(xmlConfigFile);                XmlNodeList nodeProviders = xml.SelectNodes("IoProviders/IoProvider");                foreach (var nodeProvider in nodeProviders)                {                    XmlElement element = nodeProvider as XmlElement;                    if (element == null)                        continue;                    XmlElement nodeParameter = element.SelectSingleNode("Parameter") as XmlElement;                    if (nodeParameter == null)                        continue;                    string module = element.GetAttribute("module").Trim();                    string name = element.GetAttribute("name").Trim();                    string className = element.GetAttribute("class").Trim();                    string assemblyName = element.GetAttribute("assembly").Trim();                    string loadCondition = element.GetAttribute("load_condition").Trim();                    bool isSimulator = SC.GetConfigItem("System.IsSimulatorMode").BoolValue;                    //0 : Simualtor, 1:real 2: both                    if (isSimulator && loadCondition != "0" && loadCondition != "2")                    {                        continue;                    }                    if (!isSimulator && loadCondition != "1" && loadCondition != "2")                    {                        continue;                    }                    string source = module + "." + name;                    Type t = Assembly.Load(assemblyName).GetType(className);                    if (t == null)                    {                        throw new Exception(string.Format("ioProvider config file class and assembly not valid," + source));                    }                    IIoProvider provider;                    try                    {                        provider = (IIoProvider)Activator.CreateInstance(t);                        _providers.Add(provider);                    }                    catch (Exception ex)                    {                        LOG.WriteExeption(ex);                        throw new Exception(string.Format("ioProvider can not be created," + source));                    }                    List<IoBlockItem> lstBuffers = new List<IoBlockItem>();                    XmlNodeList nodeBlocks = element.SelectNodes("Blocks/Block");                    foreach (var nodeBlock in nodeBlocks)                    {                        XmlElement blockElement = nodeBlock as XmlElement;                        if (blockElement == null)                            continue;                        IoBlockItem section = new IoBlockItem();                        string type = blockElement.GetAttribute("type");                        string offset = blockElement.GetAttribute("offset");                        string size = blockElement.GetAttribute("size");                        string value_type = blockElement.GetAttribute("value_type");                        int result;                        if (!int.TryParse(offset, out result))                        {                            continue;                        }                        section.Offset = result;                        if (!int.TryParse(size, out result))                        {                            continue;                        }                        section.Size = result;                        switch (type.ToLower())                        {                            case "ai":                                section.Type = IoType.AI;                                break;                            case "ao":                                section.Type = IoType.AO;                                break;                            case "di":                                section.Type = IoType.DI;                                break;                            case "do":                                section.Type = IoType.DO;                                break;                            default:                                continue;                        }                        lstBuffers.Add(section);                    }                    string mapModule = element.GetAttribute("map_module").Trim();                    string mapFile="";                    if (mapModule == "TM"|| mapModule == "SETM")                    {                        mapFile ="TM"+"\\"+ element.GetAttribute("map_file").Trim();                    }                    else                    {                        // JetChamber jetChamber = (JetChamber)Enum.Parse(typeof(JetChamber), SC.GetStringValue($"{mapModule}.ChamberType"));                        JetChamber jetChamber = (JetChamber)SC.GetValue<int>($"{mapModule}.ChamberType");                        switch (jetChamber)                        {                            case JetChamber.Venus:                                mapFile= "PM" + "\\" + "Venus" + "\\" + "VenusIoDefine.xml";                                break;                            case JetChamber.Kepler2300:                                mapFile = "PM" + "\\" + "Kepler2300" + "\\" + "Kepler2300IoDefine.xml";                                break;                            case JetChamber.Kepler2200A:                                mapFile = "PM" + "\\" + "Kepler2200A" + "\\" + "Kepler2200AIoDefine.xml";                                break;                            case JetChamber.Kepler2200B:                                //mapFile = jetChamber.ToString() + "\\" + "_ioDefineKepler.xml";                                mapFile= "PM" + "\\" + "Kepler2200B" + "\\" + "Kepler2200BIoDefine.xml";                                break;                            case JetChamber.VenusSE:                                mapFile = "PM" + "\\" + "VenusSE" + "\\" + "VenusSEIoDefine.xml";                                break;                            case JetChamber.VenusDE:                                mapFile = "PM" + "\\" + "VenusDE" + "\\" + "VenusDEIoDefine.xml";                                break;                        }                    }                    FileInfo file = new FileInfo(xmlConfigFile);                    string fullPathName = file.Directory.FullName + "\\" + mapFile;                     provider.Initialize(module, name, lstBuffers, IoManager.Instance, nodeParameter, fullPathName, mapModule);                    provider.Start();                }            }            catch (Exception ex)            {                throw new ApplicationException("IoProvider configuration not valid," + ex.Message);            }        }        public void Terminate()        {            try            {                foreach (var ioProvider in _providers)                {                    ioProvider.Stop();                }            }            catch (Exception ex)            {                LOG.WriteExeption(ex);            }        }        public void Reset()        {            try            {                foreach (var ioProvider in _providers)                {                    ioProvider.Reset();                }            }            catch (Exception ex)            {                LOG.WriteExeption(ex);            }        }    }}
 |