123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Controls;
- using System.Xml;
- using Aitex.Core.RT.Log;
- using Aitex.Core.RT.SCCore;
- using Aitex.Core.Util;
- using MECF.Framework.Common.IOCore;
- namespace MECF.Framework.RT.Core.IoProviders
- {
- public class IoProviderManager : Singleton<IoProviderManager>
- {
- private List<IIoProvider> _providers = new List<IIoProvider>();
- private Dictionary<string, IIoProvider> _dicProviders = new Dictionary<string, IIoProvider>();
- public List<IIoProvider> Providers => _providers;
- public IIoProvider GetProvider(string name)
- {
- if (_dicProviders.ContainsKey(name))
- return _dicProviders[name];
- return null;
- }
- /// <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);
- _dicProviders[source] = provider;
- }
- catch (Exception ex)
- {
- LOG.Write(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 = (ushort)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;
- }
- if (section.Type == IoType.AI || section.Type == IoType.AO)
- {
- section.AIOType = (string.IsNullOrEmpty(value_type) || value_type.ToLower()!="float") ? typeof(short) : typeof(float);
- }
-
- 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);
- _dicProviders[source] = provider;
- }
- catch (Exception ex)
- {
- LOG.Write(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 = (ushort)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;
- }
- if (section.Type == IoType.AI || section.Type == IoType.AO)
- {
- section.AIOType = (string.IsNullOrEmpty(value_type) || value_type.ToLower() != "float") ? typeof(short) : typeof(float);
- }
- lstBuffers.Add(section);
- }
- string mapModule = element.GetAttribute("map_module").Trim();
- string mapFile = element.GetAttribute("map_file").Trim();
- 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);
- }
- }
- /// <summary>
- /// 模块化的版本
- /// </summary>
- /// <param name="xmlConfigFile"></param>
- /// <param name="module"></param>
- /// <param name="name"></param>
- public void Initialize(string xmlConfigFile, string module, string name, bool filterModule=false)
- {
- 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 className = element.GetAttribute("class").Trim();
- string assemblyName = element.GetAttribute("assembly").Trim();
- string loadCondition = element.GetAttribute("load_condition").Trim();
- string configModule = element.GetAttribute("module").Trim();
- if (filterModule &&!string.IsNullOrEmpty(configModule) && configModule!=module)
- continue;
- 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);
- _dicProviders[source] = provider;
- }
- catch (Exception ex)
- {
- LOG.Write(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 = (ushort)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;
- }
- if (section.Type == IoType.AI || section.Type == IoType.AO)
- {
- section.AIOType = (string.IsNullOrEmpty(value_type) || value_type.ToLower() != "float") ? typeof(short) : typeof(float);
- }
- lstBuffers.Add(section);
- }
- string mapFile = element.GetAttribute("map_file").Trim();
- FileInfo file = new FileInfo(xmlConfigFile);
- string fullPathName = file.Directory.FullName + "\\" + mapFile;
- provider.Initialize(module, name, lstBuffers, IoManager.Instance, nodeParameter, fullPathName, module);
- provider.Start();
- }
- }
- catch (Exception ex)
- {
- throw new ApplicationException("IoProvider configuration not valid," + ex.Message);
- }
- }
- public void Terminate()
- {
- try
- {
- _dicProviders.Clear();
- foreach (var ioProvider in _providers)
- {
- if(ioProvider.IsOpened)
- ioProvider.Stop();
- }
- }
- catch (Exception ex)
- {
- LOG.Write(ex);
- }
- }
- public void Reset()
- {
- try
- {
- foreach (var ioProvider in _providers)
- {
- ioProvider.Reset();
- }
- }
- catch (Exception ex)
- {
- LOG.Write(ex);
- }
- }
- }
- }
|