123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Threading.Tasks;
- using System.Xml;
- using Aitex.Core.RT.DataCenter;
- using Aitex.Core.RT.Log;
- using Aitex.Core.Util;
- using MECF.Framework.Common.Equipment;
- namespace Aitex.Core.RT.Device
- {
- public class DeviceManagerBase : IDeviceManager
- {
- private Dictionary<string, IDevice> _nameDevice = new Dictionary<string, IDevice>();
- private Dictionary<Type, List<IDevice>> _typeDevice = new Dictionary<Type, List<IDevice>>();
- protected XmlElement DeviceModelNodes { get; private set; }
- DeviceTimer _peformanceTimer = new DeviceTimer();
- R_TRIG _trigExceed500 = new R_TRIG();
- public bool DisableAsyncInitialize { get; set; }
- private List<IDevice> _optionDevice = new List<IDevice>();
- private object _lockerDevice = new object();
- public DeviceManagerBase()
- {
- DEVICE.Manager = this;
- }
- public virtual void Terminate()
- {
- lock (_lockerDevice)
- {
- foreach (var device in _nameDevice.Values)
- {
- device.Terminate();
- }
- }
- }
- public void Monitor()
- {
- lock (_lockerDevice)
- {
- foreach (var device in _nameDevice.Values)
- {
- try
- {
- _peformanceTimer.Start(0);
- device.Monitor();
- _trigExceed500.CLK = _peformanceTimer.GetElapseTime() > 500;
- if (_trigExceed500.Q)
- {
- LOG.Warning($"{device.Module}.{device.Name} monitor time {_peformanceTimer.GetElapseTime()} ms");
- }
- }
- catch (Exception ex)
- {
- LOG.Write(ex, string.Format("Monitor {0} Exception", device.Name));
- }
- }
- }
- }
- public void Reset()
- {
- lock (_lockerDevice)
- {
- foreach (var device in _nameDevice.Values)
- {
- device.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(string.Format("did not find the device model file {0} ", modelFile));
- }
- XmlDocument _dom = new XmlDocument();
- try
- {
- _dom.Load(modelFile);
- XmlElement node = _dom.SelectSingleNode("DeviceModelDefine") as XmlElement;
- if (node == null)
- {
- throw new ApplicationException(string.Format("device mode file {0} is not valid", modelFile));
- }
- string fileType = node.GetAttribute("type");
- if (fileType != type)
- {
- throw new ApplicationException(string.Format("the type {0} in device mode file {1} is inaccordance with the system type {2}", fileType, modelFile, type));
- }
- DeviceModelNodes = node;
- foreach (XmlNode nodeModelChild in node.ChildNodes)
- {
- if (nodeModelChild.NodeType == XmlNodeType.Comment)
- continue;
- XmlElement nodeDevices = nodeModelChild as XmlElement;
- if (null == nodeDevices) continue;
- string assName = nodeDevices.GetAttribute("assembly");
- if (string.IsNullOrEmpty(assName))
- assName = "MECF.Framework.RT.EquipmentLibrary";
- string className = nodeDevices.Name.Substring(0, nodeDevices.Name.Length - 1);
- string typeName = nodeDevices.GetAttribute("classType");
- if (string.IsNullOrEmpty(typeName))
- typeName = "Aitex.Core.RT.Device.Unit." + className;
- Assembly assembly = Assembly.Load(assName);
- Type deviceType = assembly.GetType(typeName);
- if (deviceType == null)
- {
- //throw new ApplicationException(string.Format("can not find the device type {0}", "Aitex.Core.RT.Device.Unit." + typeName));
- continue;
- }
- foreach (var nodeDeviceChild in nodeDevices.ChildNodes)
- {
- XmlElement nodeDevice = nodeDeviceChild as XmlElement;
- if (nodeDevice == null)
- {
- LOG.Write("Device Model File contains non element node, " + (nodeDeviceChild as XmlNode).Value);
- continue;
- }
- IDevice device = Activator.CreateInstance(deviceType, mod.ToString(), nodeDevice, ioModule) as IDevice;
- if (nodeDevice.HasAttribute("option") && Convert.ToBoolean(nodeDevice.Attributes["option"].Value))
- {
- _optionDevice.Add( device);
- continue;
- }
- QueueDevice(device);
- DATA.Subscribe(device, string.Format("{0}.{1}.{2}", device.Module, className, device.Name));
- if (!_typeDevice.ContainsKey(deviceType))
- _typeDevice[deviceType] = new List<IDevice>();
- _typeDevice[deviceType].Add(device);
- if (DisableAsyncInitialize)
- {
- InitDevice(device);
- }
- else
- {
- Task task = Task.Run(() => InitDevice(device));
- }
- }
- }
- if (endCallInit)
- {
- Initialize();
- }
- }
- catch (Exception ex)
- {
- LOG.Write(ex);
- }
- }
- public void Initialize(string modelFile, string modelType, string moduleName, string ioPath, string scPath)
- {
- if (!File.Exists(modelFile))
- {
- throw new ApplicationException(string.Format("did not find the device model file {0} ", modelFile));
- }
- XmlDocument _dom = new XmlDocument();
- try
- {
- _dom.Load(modelFile);
- XmlElement node = _dom.SelectSingleNode("DeviceModelDefine") as XmlElement;
- if (node == null)
- {
- throw new ApplicationException(string.Format("device mode file {0} is not valid", modelFile));
- }
- string fileType = node.GetAttribute("type");
- if (fileType != modelType)
- {
- throw new ApplicationException(string.Format("the type {0} in device mode file {1} is different with the system type {2}", fileType, modelFile, modelType));
- }
- DeviceModelNodes = node;
- foreach (XmlNode nodeModelChild in node.ChildNodes)
- {
- if (nodeModelChild.NodeType == XmlNodeType.Comment)
- continue;
- XmlElement nodeDevices = nodeModelChild as XmlElement;
- if (null == nodeDevices) continue;
- string assName = nodeDevices.GetAttribute("assembly");
- if (string.IsNullOrEmpty(assName))
- assName = "MECF.Framework.RT.EquipmentLibrary";
- string className = nodeDevices.Name.Substring(0, nodeDevices.Name.Length - 1);
- string typeName = nodeDevices.GetAttribute("classType");
- if (string.IsNullOrEmpty(typeName))
- typeName = "Aitex.Core.RT.Device.Unit." + className;
- Assembly assembly = Assembly.Load(assName);
- Type deviceType = assembly.GetType(typeName);
- if (deviceType == null)
- {
- //throw new ApplicationException(string.Format("can not find the device type {0}", "Aitex.Core.RT.Device.Unit." + typeName));
- continue;
- }
- foreach (var nodeDeviceChild in nodeDevices.ChildNodes)
- {
- XmlElement nodeDevice = nodeDeviceChild as XmlElement;
- if (nodeDevice == null)
- {
- LOG.Write("Device Model File contains non element node, " + (nodeDeviceChild as XmlNode).Value);
- continue;
- }
- nodeDevice.SetAttribute("ioPath", ioPath);
- nodeDevice.SetAttribute("scPath", scPath);
- IDevice device = Activator.CreateInstance(deviceType, moduleName, nodeDevice, ioPath) as IDevice;
- if (nodeDevice.HasAttribute("option") && Convert.ToBoolean(nodeDevice.Attributes["option"]))
- {
- _optionDevice.Add(device);
- continue;
- }
- DATA.Subscribe(device, string.Format("{0}.{1}.{2}", device.Module, className, device.Name));
- QueueDevice(device);
- if (!_typeDevice.ContainsKey(deviceType))
- _typeDevice[deviceType] = new List<IDevice>();
- _typeDevice[deviceType].Add(device);
- if (DisableAsyncInitialize)
- {
- InitDevice(device);
- }
- else
- {
- Task task = Task.Run(() => InitDevice(device));
- }
- }
- }
- Initialize();
- }
- catch (Exception ex)
- {
- LOG.Write(ex);
- }
- }
- private void InitDevice(IDevice device)
- {
- try
- {
- if (!device.Initialize())
- {
- LOG.Write($"{device.Name} initialize failed.");
- }
- }
- catch (Exception e)
- {
- LOG.Write(e);
- }
- }
- protected virtual void QueueDevice(IDevice device)
- {
- QueueDevice(device.Name, device);
- }
- protected void QueueDevice(string key, IDevice device)
- {
- lock (_lockerDevice)
- {
- //LOG.Info(key);
- System.Diagnostics.Debug.Assert(!_nameDevice.ContainsKey(key),
- $"DeviceModel config file contains duplicated name {key}");
- _nameDevice.Add(key, device);
- }
- }
- public IDevice AddCustomDevice(IDevice device, string groupType, Type deviceType)
- {
- lock (_lockerDevice)
- {
- DATA.Subscribe(device, string.Format("{0}.{1}.{2}", device.Module, groupType, device.Name));
- if (!string.IsNullOrEmpty(device.Module) && device.Module != "System")
- {
- _nameDevice.Add(device.Module + "." + device.Name, device);
- }
- else
- {
- _nameDevice.Add(device.Name, device);
- }
- if (!_typeDevice.ContainsKey(deviceType))
- _typeDevice[deviceType] = new List<IDevice>();
- _typeDevice[deviceType].Add(device);
- device.Initialize();
- //Task task = Task.Run(()=>device.Initialize());
- }
- return device;
- }
- public IDevice AddCustomDevice(IDevice device, Type deviceType)
- {
- lock (_lockerDevice)
- {
- try
- {
- DATA.Subscribe(device, string.Format("{0}.{1}", device.Module, device.Name));
- _nameDevice.Add(device.Name, device);
- if (!_typeDevice.ContainsKey(deviceType))
- _typeDevice[deviceType] = new List<IDevice>();
- _typeDevice[deviceType].Add(device);
- device.Initialize();
- }
- catch (Exception ex)
- {
- LOG.Write(ex);
- }
- }
- //Task task = Task.Run(()=>device.Initialize());
- return device;
- }
- public IDevice AddCustomDevice(IDevice device)
- {
- lock (_lockerDevice)
- {
- try
- {
- DATA.Subscribe(device, string.Format("{0}.{1}", device.Module, device.Name));
- _nameDevice.Add(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);
- }
- }
- //Task task = Task.Run(()=>device.Initialize());
- return device;
- }
- public IDevice AddCustomModuleDevice(IDevice device)
- {
- lock (_lockerDevice)
- {
- try
- {
- DATA.Subscribe(device, string.Format("{0}.{1}", device.Module, device.Name));
- _nameDevice.Add($"{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)
- {
- LOG.Write(ex);
- }
- //Task task = Task.Run(()=>device.Initialize());
- }
- return device;
- }
- 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 List<T> GetDevice<T>() where T : class, IDevice
- {
- if (!_typeDevice.ContainsKey(typeof(T)))
- return null;
- List<T> result = new List<T>();
- foreach (var d in _typeDevice[typeof(T)])
- {
- result.Add(d as T);
- }
- return result;
- }
- public List<IDevice> GetAllDevice()
- {
- return _nameDevice.Values.ToList();
- }
-
- public object GetOptionDevice(string name, Type type)
- {
- foreach (var device in _optionDevice)
- {
- if ($"{device.Module}.{device.Name}" == name)
- {
- if (type == null || device.GetType() == type)
- {
- return device;
- }
- }
- }
- return null;
- }
- }
- }
|