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 _nameDevice = new Dictionary(); private Dictionary> _typeDevice = new Dictionary>(); DeviceTimer _peformanceTimer = new DeviceTimer(); R_TRIG _trigExceed500 = new R_TRIG(); public DeviceManagerBase() { DEVICE.Manager = this; } public virtual void Terminate() { foreach (var device in _nameDevice.Values) { device.Terminate(); } } public void Monitor() { 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)); throw (ex); } } } public void Reset() { foreach (var device in _nameDevice.Values) { device.Reset(); } _trigExceed500.RST = true; } public IDevice AddCustomDevice(IDevice device) { 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(); _typeDevice[device.GetType()].Add(device); device.Initialize(); } catch (Exception ex) { LOG.WriteExeption(ex); throw; } //Task task = Task.Run(()=>device.Initialize()); return device; } public IDevice AddCustomModuleDevice(IDevice device) { 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(); _typeDevice[device.GetType()].Add(device); device.Initialize(); } catch (Exception ex) { LOG.WriteExeption(ex); throw; } //Task task = Task.Run(()=>device.Initialize()); return device; } public virtual bool Initialize() { return true; } public T GetDevice(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 GetDevice() where T : class, IDevice { if (!_typeDevice.ContainsKey(typeof(T))) return null; List result = new List(); foreach (var d in _typeDevice[typeof(T)]) { result.Add(d as T); } return result; } public List GetAllDevice() { return _nameDevice.Values.ToList(); } } }