123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- 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>>();
- 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<IDevice>();
- _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<IDevice>();
- _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<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();
- }
- }
- }
|