123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567 |
- using Aitex.Core.RT.DataCenter;
- using Aitex.Core.RT.Device;
- using Aitex.Core.Util;
- using MECF.Framework.Common.Equipment;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using System.Xml;
- using DATA = Common.DataCenter.DATA;
- namespace athosRT.Devices
- {
- public class DeviceManagerBase : IDeviceManager
- {
- private Dictionary<string, IDevice> _nameDevice = new Dictionary<string, IDevice>();
- private Dictionary<Type, List<IDevice>> _typeDevice = new Dictionary<Type, List<IDevice>>();
- private DeviceTimer _peformanceTimer = new DeviceTimer();
- private R_TRIG _trigExceed500 = new R_TRIG();
- private List<IDevice> _optionDevice = new List<IDevice>();
- private object _lockerDevice = new object();
- private bool _isModularDevice;
- protected XmlElement DeviceModelNodes { get; private set; }
- public bool DisableAsyncInitialize { get; set; }
- public DeviceManagerBase()
- {
- DEVICE.Managers.Add(this);
- }
- public DeviceManagerBase(bool modularDevice)
- {
- _isModularDevice = modularDevice;
- DEVICE.Managers.Add(this);
- }
- public virtual void Terminate()
- {
- lock (_lockerDevice)
- {
- foreach (IDevice value in _nameDevice.Values)
- {
- value.Terminate();
- }
- }
- }
- public void Monitor()
- {
- lock (_lockerDevice)
- {
- foreach (IDevice value in _nameDevice.Values)
- {
- try
- {
- _peformanceTimer.Start(0.0);
- value.Monitor();
- _trigExceed500.CLK = _peformanceTimer.GetElapseTime() > 500.0;
- if (_trigExceed500.Q)
- {
- //LOG.Warning($"{value.Module}.{value.Name} monitor time {_peformanceTimer.GetElapseTime()} ms", 2, "D:\\sorter\\trunk\\Framework\\Common\\Device\\Common\\DeviceManagerBase.cs", "Monitor", 74);
- }
- }
- catch (Exception ex)
- {
- //LOG.Write(ex, $"Monitor {value.Name} Exception", 2, "D:\\sorter\\trunk\\Framework\\Common\\Device\\Common\\DeviceManagerBase.cs", "Monitor", 79);
- }
- }
- }
- }
- public void Reset()
- {
- lock (_lockerDevice)
- {
- foreach (IDevice value in _nameDevice.Values)
- {
- value.Reset();
- }
- _trigExceed500.RST = true;
- }
- }
- public void Initialize(string modelFile, string type, ModuleName mod = ModuleName.System, string ioModule = "", bool endCallInit = true)
- {
- if (!File.Exists(modelFile))
- {
- throw new ApplicationException($"did not find the device model file {modelFile} ");
- }
- XmlDocument xmlDocument = new XmlDocument();
- try
- {
- xmlDocument.Load(modelFile);
- XmlElement xmlElement = xmlDocument.SelectSingleNode("DeviceModelDefine") as XmlElement;
- if (xmlElement == null)
- {
- throw new ApplicationException($"device mode file {modelFile} is not valid");
- }
- string attribute = xmlElement.GetAttribute("type");
- if (attribute != type)
- {
- throw new ApplicationException($"the type {attribute} in device mode file {modelFile} is inaccordance with the system type {type}");
- }
- DeviceModelNodes = xmlElement;
- //这里会定义三个主要的内容 xmlElement
- foreach (XmlNode childNode in xmlElement.ChildNodes)
- {
- if (childNode.NodeType == XmlNodeType.Comment)
- {
- continue;
- }
- XmlElement xmlElement2 = childNode as XmlElement;
- if (xmlElement2 == null)
- {
- continue;
- }
- string text = xmlElement2.GetAttribute("assembly");
- if (string.IsNullOrEmpty(text))
- {
- text = "MECF.Framework.RT.EquipmentLibrary";
- }
- string text2 = xmlElement2.Name.Substring(0, xmlElement2.Name.Length - 1);
- string text3 = xmlElement2.GetAttribute("classType");
- if (string.IsNullOrEmpty(text3))
- {
- text3 = "Aitex.Core.RT.Device.Unit." + text2;
- }
- Assembly assembly = Assembly.Load(text);
- Type type2 = assembly.GetType(text3);
- if (type2 == null)
- {
- continue;
- }
- foreach (object childNode2 in xmlElement2.ChildNodes)
- {
- XmlElement xmlElement3 = childNode2 as XmlElement;
- if (xmlElement3 == null)
- {
- //LOG.Write("Device Model File contains non element node, " + (childNode2 as XmlNode).Value, 2, "D:\\sorter\\trunk\\Framework\\Common\\Device\\Common\\DeviceManagerBase.cs", "Initialize", 156);
- continue;
- }
- IDevice device = Activator.CreateInstance(type2, mod.ToString(), xmlElement3, ioModule) as IDevice;
- if (xmlElement3.HasAttribute("option") && Convert.ToBoolean(xmlElement3.Attributes["option"].Value))
- {
- _optionDevice.Add(device);
- continue;
- }
- QueueDevice(device);
- DATA.Subscribe(device, $"{device.Module}.{text2}.{device.Name}");
- if (!_typeDevice.ContainsKey(type2))
- {
- _typeDevice[type2] = new List<IDevice>();
- }
- _typeDevice[type2].Add(device);
- if (DisableAsyncInitialize)
- {
- InitDevice(device);
- continue;
- }
- Task task = Task.Run(delegate
- {
- InitDevice(device);
- });
- }
- }
- if (endCallInit)
- {
- Initialize();
- }
- }
- catch (Exception ex)
- {
- //LOG.Write(ex, 2, "D:\\sorter\\trunk\\Framework\\Common\\Device\\Common\\DeviceManagerBase.cs", "Initialize", 196);
- }
- }
- public void Initialize(string modelFile, string modelType, string moduleName, string ioPath, string scPath)
- {
- if (!File.Exists(modelFile))
- {
- throw new ApplicationException($"did not find the device model file {modelFile} ");
- }
- XmlDocument xmlDocument = new XmlDocument();
- try
- {
- xmlDocument.Load(modelFile);
- XmlElement xmlElement = xmlDocument.SelectSingleNode("DeviceModelDefine") as XmlElement;
- if (xmlElement == null)
- {
- throw new ApplicationException($"device mode file {modelFile} is not valid");
- }
- string attribute = xmlElement.GetAttribute("type");
- if (attribute != modelType)
- {
- throw new ApplicationException($"the type {attribute} in device mode file {modelFile} is different with the system type {modelType}");
- }
- DeviceModelNodes = xmlElement;
- foreach (XmlNode childNode in xmlElement.ChildNodes)
- {
- if (childNode.NodeType == XmlNodeType.Comment)
- {
- continue;
- }
- XmlElement xmlElement2 = childNode as XmlElement;
- if (xmlElement2 == null)
- {
- continue;
- }
- string text = xmlElement2.GetAttribute("assembly");
- if (string.IsNullOrEmpty(text))
- {
- text = "MECF.Framework.RT.EquipmentLibrary";
- }
- string text2 = xmlElement2.Name.Substring(0, xmlElement2.Name.Length - 1);
- string text3 = xmlElement2.GetAttribute("classType");
- if (string.IsNullOrEmpty(text3))
- {
- text3 = "Aitex.Core.RT.Device.Unit." + text2;
- }
- Assembly assembly = Assembly.Load(text);
- Type type = assembly.GetType(text3);
- if (type == null)
- {
- continue;
- }
- foreach (object childNode2 in xmlElement2.ChildNodes)
- {
- XmlElement xmlElement3 = childNode2 as XmlElement;
- if (xmlElement3 == null)
- {
- //LOG.Write("Device Model File contains non element node, " + (childNode2 as XmlNode).Value, 2, "D:\\sorter\\trunk\\Framework\\Common\\Device\\Common\\DeviceManagerBase.cs", "Initialize", 257);
- continue;
- }
- xmlElement3.SetAttribute("ioPath", ioPath);
- xmlElement3.SetAttribute("scPath", scPath);
- IDevice device = Activator.CreateInstance(type, moduleName, xmlElement3, ioPath) as IDevice;
- if (xmlElement3.HasAttribute("option") && Convert.ToBoolean(xmlElement3.Attributes["option"]))
- {
- _optionDevice.Add(device);
- continue;
- }
- DATA.Subscribe(device, $"{device.Module}.{text2}.{device.Name}");
- QueueDevice(device);
- if (!_typeDevice.ContainsKey(type))
- {
- _typeDevice[type] = new List<IDevice>();
- }
- _typeDevice[type].Add(device);
- if (DisableAsyncInitialize)
- {
- InitDevice(device);
- continue;
- }
- Task task = Task.Run(delegate
- {
- InitDevice(device);
- });
- }
- }
- Initialize();
- }
- catch (Exception ex)
- {
- //LOG.Write(ex, 2, "D:\\sorter\\trunk\\Framework\\Common\\Device\\Common\\DeviceManagerBase.cs", "Initialize", 295);
- }
- }
- private void InitDevice(IDevice device)
- {
- try
- {
- if (!device.Initialize())
- {
- //LOG.Write(device.Name + " initialize failed.", 2, "D:\\sorter\\trunk\\Framework\\Common\\Device\\Common\\DeviceManagerBase.cs", "InitDevice", 305);
- }
- }
- catch (Exception ex)
- {
- //LOG.Write(ex, 2, "D:\\sorter\\trunk\\Framework\\Common\\Device\\Common\\DeviceManagerBase.cs", "InitDevice", 310);
- }
- }
- protected virtual void QueueDevice(IDevice device)
- {
- if (_isModularDevice)
- {
- QueueDevice(device.Module + "." + device.Name, device);
- }
- else
- {
- QueueDevice(device.Name, device);
- }
- }
- protected void QueueDevice(string key, IDevice device)
- {
- lock (_lockerDevice)
- {
- foreach (IDeviceManager manager in DEVICE.Managers)
- {
- if (manager.ContainsDevice(key))
- {
- Debug.Assert(condition: false, "Failed add duplicated device, named with " + key);
- //LOG.Write("Failed add duplicated device, name " + key, 2, "D:\\sorter\\trunk\\Framework\\Common\\Device\\Common\\DeviceManagerBase.cs", "QueueDevice", 337);
- return;
- }
- }
- _nameDevice.Add(key, device);
- }
- }
- public IDevice AddCustomDevice(IDevice device, string groupType, Type deviceType)
- {
- lock (_lockerDevice)
- {
- DATA.Subscribe(device, $"{device.Module}.{groupType}.{device.Name}");
- if (!string.IsNullOrEmpty(device.Module) && device.Module != "System")
- {
- QueueDevice(device.Module + "." + device.Name, device);
- }
- else
- {
- QueueDevice(device.Name, device);
- }
- if (!_typeDevice.ContainsKey(deviceType))
- {
- _typeDevice[deviceType] = new List<IDevice>();
- }
- _typeDevice[deviceType].Add(device);
- device.Initialize();
- }
- return device;
- }
- public IDevice AddCustomDevice(IDevice device, Type deviceType)
- {
- lock (_lockerDevice)
- {
- try
- {
- DATA.Subscribe(device, $"{device.Module}.{device.Name}");
- QueueDevice(device.Name, device);
- if (!_typeDevice.ContainsKey(deviceType))
- {
- _typeDevice[deviceType] = new List<IDevice>();
- }
- _typeDevice[deviceType].Add(device);
- device.Initialize();
- }
- catch (Exception ex)
- {
- //LOG.Write(ex, 2, "D:\\sorter\\trunk\\Framework\\Common\\Device\\Common\\DeviceManagerBase.cs", "AddCustomDevice", 390);
- }
- }
- return device;
- }
- public IDevice AddCustomDevice(IDevice device)
- {
- lock (_lockerDevice)
- {
- try
- {
- DATA.Subscribe(device, $"{device.Module}.{device.Name}");
- QueueDevice(device.Name, device);
- if (!_typeDevice.ContainsKey(device.GetType()))
- {
- _typeDevice[device.GetType()] = new List<IDevice>();
- }
- _typeDevice[device.GetType()].Add(device);
- device.Initialize();
- }
- catch (Exception ex)
- {
- //LOG.Write(ex, 2, "D:\\sorter\\trunk\\Framework\\Common\\Device\\Common\\DeviceManagerBase.cs", "AddCustomDevice", 417);
- }
- }
- return device;
- }
- public IDevice AddCustomModuleDevice(IDevice device)
- {
- lock (_lockerDevice)
- {
- try
- {
- DATA.Subscribe(device, $"{device.Module}.{device.Name}");
- QueueDevice(device.Module + "." + device.Name, device);
- if (!_typeDevice.ContainsKey(device.GetType()))
- {
- _typeDevice[device.GetType()] = new List<IDevice>();
- }
- _typeDevice[device.GetType()].Add(device);
- device.Initialize();
- }
- catch (Exception ex)
- {
- }
- }
- return device;
- }
- public dynamic DynamicAddCustomDevice(string type, string module, string name, params object[] otherParam)
- {
- Type[] types = Assembly.Load(Process.GetCurrentProcess().ProcessName).GetTypes();
- Type type2 = types.FirstOrDefault((Type x) => x.IsClass & (x.Name == type));
- if (type2 != null)
- {
- IDevice device;
- try
- {
- object[] args = new object[2] { module, name }.Concat(otherParam).ToArray();
- device = Activator.CreateInstance(type2, args) as IDevice;
- if (device == null)
- {
- throw new InvalidCastException($"{type2} does not inherit IDevice interface");
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- return AddCustomDevice(device);
- }
- Type[] types2 = Assembly.GetExecutingAssembly().GetTypes();
- Type type3 = types2.FirstOrDefault((Type x) => x.IsClass & (x.Name == type));
- if (type3 != null)
- {
- IDevice device2;
- try
- {
- object[] args2 = new object[2] { module, name }.Concat(otherParam).ToArray();
- device2 = Activator.CreateInstance(type3, args2) as IDevice;
- if (device2 == null)
- {
- throw new InvalidCastException($"{type3} does not inherit IDevice interface");
- }
- }
- catch (Exception ex2)
- {
- throw ex2;
- }
- return AddCustomDevice(device2);
- }
- throw new NotSupportedException(type + " Undefined in RT or Common Assembly");
- }
- public virtual bool Initialize()
- {
- return true;
- }
- public T GetDevice<T>(string name) where T : class, IDevice
- {
- if (!_nameDevice.ContainsKey(name))
- {
- return null;
- }
- return _nameDevice[name] as T;
- }
- public object GetDevice(string name)
- {
- if (!_nameDevice.ContainsKey(name))
- {
- return null;
- }
- return _nameDevice[name];
- }
- public bool ContainsDevice(string name)
- {
- return _nameDevice.ContainsKey(name);
- }
- public List<T> GetDevice<T>() where T : class, IDevice
- {
- if (!_typeDevice.ContainsKey(typeof(T)))
- {
- return null;
- }
- List<T> list = new List<T>();
- foreach (IDevice item in _typeDevice[typeof(T)])
- {
- list.Add(item as T);
- }
- return list;
- }
- public List<IDevice> GetAllDevice()
- {
- return _nameDevice.Values.ToList();
- }
- public object GetOptionDevice(string name, Type type)
- {
- foreach (IDevice item in _optionDevice)
- {
- if (item.Module + "." + item.Name == name && (type == null || item.GetType() == type))
- {
- return item;
- }
- }
- return null;
- }
- }
- }
|