DeviceManagerBase.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Threading.Tasks;
  7. using System.Xml;
  8. using Aitex.Core.RT.DataCenter;
  9. using Aitex.Core.RT.Log;
  10. using Aitex.Core.Util;
  11. using MECF.Framework.Common.Equipment;
  12. namespace Aitex.Core.RT.Device
  13. {
  14. public class DeviceManagerBase : IDeviceManager
  15. {
  16. private Dictionary<string, IDevice> _nameDevice = new Dictionary<string, IDevice>();
  17. private Dictionary<Type, List<IDevice>> _typeDevice = new Dictionary<Type, List<IDevice>>();
  18. DeviceTimer _peformanceTimer = new DeviceTimer();
  19. R_TRIG _trigExceed500 = new R_TRIG();
  20. public DeviceManagerBase()
  21. {
  22. DEVICE.Manager = this;
  23. }
  24. public virtual void Terminate()
  25. {
  26. foreach (var device in _nameDevice.Values)
  27. {
  28. device.Terminate();
  29. }
  30. }
  31. public void Monitor()
  32. {
  33. foreach (var device in _nameDevice.Values)
  34. {
  35. try
  36. {
  37. _peformanceTimer.Start(0);
  38. device.Monitor();
  39. _trigExceed500.CLK = _peformanceTimer.GetElapseTime() > 500;
  40. if (_trigExceed500.Q)
  41. {
  42. //LOG.Warning($"{device.Module}.{device.Name} monitor time {_peformanceTimer.GetElapseTime()} ms");
  43. }
  44. }
  45. catch (Exception ex)
  46. {
  47. //LOG.Write(ex, string.Format("Monitor {0} Exception", device.Name));
  48. throw (ex);
  49. }
  50. }
  51. }
  52. public void Reset()
  53. {
  54. foreach (var device in _nameDevice.Values)
  55. {
  56. device.Reset();
  57. }
  58. _trigExceed500.RST = true;
  59. }
  60. public IDevice AddCustomDevice(IDevice device)
  61. {
  62. try
  63. {
  64. DATA.Subscribe(device, string.Format("{0}.{1}", device.Module, device.Name));
  65. _nameDevice.Add(device.Name, device);
  66. if (!_typeDevice.ContainsKey(device.GetType()))
  67. _typeDevice[device.GetType()] = new List<IDevice>();
  68. _typeDevice[device.GetType()].Add(device);
  69. device.Initialize();
  70. }
  71. catch (Exception ex)
  72. {
  73. LOG.WriteExeption(ex);
  74. throw;
  75. }
  76. //Task task = Task.Run(()=>device.Initialize());
  77. return device;
  78. }
  79. public IDevice AddCustomModuleDevice(IDevice device)
  80. {
  81. try
  82. {
  83. DATA.Subscribe(device, string.Format("{0}.{1}", device.Module, device.Name));
  84. _nameDevice.Add($"{device.Module}.{device.Name}", device);
  85. if (!_typeDevice.ContainsKey(device.GetType()))
  86. _typeDevice[device.GetType()] = new List<IDevice>();
  87. _typeDevice[device.GetType()].Add(device);
  88. device.Initialize();
  89. }
  90. catch (Exception ex)
  91. {
  92. LOG.WriteExeption(ex);
  93. throw;
  94. }
  95. //Task task = Task.Run(()=>device.Initialize());
  96. return device;
  97. }
  98. public virtual bool Initialize()
  99. {
  100. return true;
  101. }
  102. public T GetDevice<T>(string name) where T : class,IDevice
  103. {
  104. if (!_nameDevice.ContainsKey(name))
  105. return null;
  106. return _nameDevice[name] as T;
  107. }
  108. public object GetDevice(string name)
  109. {
  110. if (!_nameDevice.ContainsKey(name))
  111. return null;
  112. return _nameDevice[name];
  113. }
  114. public List<T> GetDevice<T>() where T : class, IDevice
  115. {
  116. if (!_typeDevice.ContainsKey(typeof(T)))
  117. return null;
  118. List<T> result = new List<T>();
  119. foreach (var d in _typeDevice[typeof(T)])
  120. {
  121. result.Add(d as T);
  122. }
  123. return result;
  124. }
  125. public List<IDevice> GetAllDevice()
  126. {
  127. return _nameDevice.Values.ToList();
  128. }
  129. }
  130. }