DeviceManagerBase.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. protected XmlElement DeviceModelNodes { get; private set; }
  19. DeviceTimer _peformanceTimer = new DeviceTimer();
  20. R_TRIG _trigExceed500 = new R_TRIG();
  21. public DeviceManagerBase()
  22. {
  23. DEVICE.Manager = this;
  24. }
  25. public virtual void Terminate()
  26. {
  27. foreach (var device in _nameDevice.Values)
  28. {
  29. device.Terminate();
  30. }
  31. }
  32. public void Monitor()
  33. {
  34. foreach (var device in _nameDevice.Values)
  35. {
  36. try
  37. {
  38. _peformanceTimer.Start(0);
  39. device.Monitor();
  40. _trigExceed500.CLK = _peformanceTimer.GetElapseTime() > 500;
  41. if (_trigExceed500.Q)
  42. {
  43. //LOG.Warning($"{device.Module}.{device.Name} monitor time {_peformanceTimer.GetElapseTime()} ms");
  44. }
  45. }
  46. catch (Exception ex)
  47. {
  48. //LOG.Write(ex, string.Format("Monitor {0} Exception", device.Name));
  49. throw (ex);
  50. }
  51. }
  52. }
  53. public void Reset()
  54. {
  55. foreach (var device in _nameDevice.Values)
  56. {
  57. device.Reset();
  58. }
  59. _trigExceed500.RST = true;
  60. }
  61. public void Initialize(string modelFile, string type, ModuleName mod, string ioModule="")
  62. {
  63. if (!File.Exists(modelFile))
  64. {
  65. throw new ApplicationException(string.Format("did not find the device model file {0} ", modelFile));
  66. }
  67. XmlDocument _dom = new XmlDocument();
  68. try
  69. {
  70. _dom.Load(modelFile);
  71. XmlElement node = _dom.SelectSingleNode("DeviceModelDefine") as XmlElement;
  72. if (node == null)
  73. {
  74. throw new ApplicationException(string.Format("device mode file {0} is not valid", modelFile));
  75. }
  76. string fileType = node.GetAttribute("type");
  77. if (fileType != type)
  78. {
  79. throw new ApplicationException(string.Format("the type {0} in device mode file {1} is in accordance with the system type {2}", fileType, modelFile, type));
  80. }
  81. DeviceModelNodes = node;
  82. foreach (XmlNode devicesNode1 in node.ChildNodes)
  83. {
  84. if (devicesNode1.NodeType == XmlNodeType.Comment)
  85. continue;
  86. XmlElement nodeDevices = devicesNode1 as XmlElement;
  87. if (null == nodeDevices) continue;
  88. string assName = nodeDevices.GetAttribute("assembly");
  89. if (string.IsNullOrEmpty(assName))
  90. assName = "MECF.Framework.Common";
  91. string typeName = nodeDevices.GetAttribute("classType");
  92. if (string.IsNullOrEmpty(typeName))
  93. typeName = "Aitex.Core.RT.Device.Unit." + nodeDevices.Name.Substring(0, nodeDevices.Name.Length - 1);
  94. string isEnabled = nodeDevices.GetAttribute("IsEnabled");
  95. if (isEnabled == "False" || isEnabled == "false") continue;
  96. Assembly assembly = Assembly.Load(assName);
  97. Type deviceType = assembly.GetType(typeName);
  98. if (deviceType == null)
  99. {
  100. //throw new ApplicationException(string.Format("can not find the device type {0}", "Aitex.Core.RT.Device.Unit." + typeName));
  101. continue;
  102. }
  103. foreach (var nodeDeviceChild in nodeDevices.ChildNodes)
  104. {
  105. XmlElement nodeDevice = nodeDeviceChild as XmlElement;
  106. if (nodeDevice == null)
  107. {
  108. //LOG.Write("Device Model File contains non element node, " + (nodeDeviceChild as XmlNode).Value);
  109. continue;
  110. }
  111. IDevice device = Activator.CreateInstance(deviceType, mod.ToString(), nodeDevice, ioModule) as IDevice;
  112. System.Diagnostics.Debug.Assert(!_nameDevice.ContainsKey(device.Name), $"DeviceModel config file contains duplicated name {device.Name}");
  113. DATA.Subscribe(device, string.Format("{0}.{1}.{2}", mod, nodeDevices.Name.Substring(0, nodeDevices.Name.Length - 1), device.Name));
  114. _nameDevice.Add($"{device.Module}.{device.Name}", device);
  115. if (!_typeDevice.ContainsKey(deviceType))
  116. _typeDevice[deviceType] = new List<IDevice>();
  117. _typeDevice[deviceType].Add(device);
  118. Task task = Task.Run(()=> InitDevice(device));
  119. }
  120. }
  121. //Initialize();
  122. }
  123. catch (Exception ex)
  124. {
  125. LOG.WriteExeption(ex);
  126. throw;
  127. }
  128. }
  129. public void Initialize(string modelFile, string modelType, string moduleName, string ioPath, string scPath)
  130. {
  131. if (!File.Exists(modelFile))
  132. {
  133. throw new ApplicationException(string.Format("did not find the device model file {0} ", modelFile));
  134. }
  135. XmlDocument _dom = new XmlDocument();
  136. try
  137. {
  138. _dom.Load(modelFile);
  139. XmlElement node = _dom.SelectSingleNode("DeviceModelDefine") as XmlElement;
  140. if (node == null)
  141. {
  142. throw new ApplicationException(string.Format("device mode file {0} is not valid", modelFile));
  143. }
  144. string fileType = node.GetAttribute("type");
  145. if (fileType != modelType)
  146. {
  147. throw new ApplicationException(string.Format("the type {0} in device mode file {1} is different with the system type {2}", fileType, modelFile, modelType));
  148. }
  149. DeviceModelNodes = node;
  150. foreach (XmlNode nodeModelChild in node.ChildNodes)
  151. {
  152. if (nodeModelChild.NodeType == XmlNodeType.Comment)
  153. continue;
  154. XmlElement nodeDevices = nodeModelChild as XmlElement;
  155. if (null == nodeDevices) continue;
  156. string assName = nodeDevices.GetAttribute("assembly");
  157. if (string.IsNullOrEmpty(assName))
  158. assName = "MECF.Framework.RT.EquipmentLibrary";
  159. string className = nodeDevices.Name.Substring(0, nodeDevices.Name.Length - 1);
  160. string typeName = nodeDevices.GetAttribute("classType");
  161. if (string.IsNullOrEmpty(typeName))
  162. typeName = "Aitex.Core.RT.Device.Unit." + className;
  163. Assembly assembly = Assembly.Load(assName);
  164. Type deviceType = assembly.GetType(typeName);
  165. if (deviceType == null)
  166. {
  167. //throw new ApplicationException(string.Format("can not find the device type {0}", "Aitex.Core.RT.Device.Unit." + typeName));
  168. continue;
  169. }
  170. foreach (var nodeDeviceChild in nodeDevices.ChildNodes)
  171. {
  172. XmlElement nodeDevice = nodeDeviceChild as XmlElement;
  173. if (nodeDevice == null)
  174. {
  175. //LOG.Write("Device Model File contains non element node, " + (nodeDeviceChild as XmlNode).Value);
  176. continue;
  177. }
  178. nodeDevice.SetAttribute("ioPath", ioPath);
  179. nodeDevice.SetAttribute("scPath", scPath);
  180. IDevice device = Activator.CreateInstance(deviceType, moduleName, nodeDevice, ioPath) as IDevice;
  181. System.Diagnostics.Debug.Assert(!_nameDevice.ContainsKey(device.Name), $"DeviceModel config file contains duplicated name {device.Name}");
  182. DATA.Subscribe(device, string.Format("{0}.{1}.{2}", device.Module, className, device.Name));
  183. _nameDevice.Add(device.Name, device);
  184. if (!_typeDevice.ContainsKey(deviceType))
  185. _typeDevice[deviceType] = new List<IDevice>();
  186. _typeDevice[deviceType].Add(device);
  187. Task task = Task.Run(() => InitDevice(device));
  188. }
  189. }
  190. Initialize();
  191. }
  192. catch (Exception ex)
  193. {
  194. LOG.WriteExeption(ex);
  195. throw;
  196. }
  197. }
  198. private void InitDevice(IDevice device)
  199. {
  200. try
  201. {
  202. if (!device.Initialize())
  203. {
  204. //LOG.Write($"{device.Name} initialize failed.");
  205. }
  206. }
  207. catch (Exception e)
  208. {
  209. LOG.WriteExeption(e);
  210. }
  211. }
  212. public IDevice AddCustomDevice(IDevice device, string groupType, Type deviceType)
  213. {
  214. DATA.Subscribe(device, string.Format("{0}.{1}.{2}", device.Module, groupType, device.Name));
  215. if (!string.IsNullOrEmpty(device.Module) && device.Module != "System")
  216. {
  217. _nameDevice.Add(device.Module + "."+ device.Name, device);
  218. }
  219. else
  220. {
  221. _nameDevice.Add( device.Name, device);
  222. }
  223. if (!_typeDevice.ContainsKey(deviceType))
  224. _typeDevice[deviceType] = new List<IDevice>();
  225. _typeDevice[deviceType].Add(device);
  226. device.Initialize();
  227. //Task task = Task.Run(()=>device.Initialize());
  228. return device;
  229. }
  230. public IDevice AddCustomDevice(IDevice device, Type deviceType)
  231. {
  232. try
  233. {
  234. DATA.Subscribe(device, string.Format("{0}.{1}", device.Module, device.Name));
  235. _nameDevice.Add(device.Name, device);
  236. if (!_typeDevice.ContainsKey(deviceType))
  237. _typeDevice[deviceType] = new List<IDevice>();
  238. _typeDevice[deviceType].Add(device);
  239. device.Initialize();
  240. }
  241. catch (Exception ex)
  242. {
  243. LOG.WriteExeption(ex);
  244. throw;
  245. }
  246. //Task task = Task.Run(()=>device.Initialize());
  247. return device;
  248. }
  249. public IDevice AddCustomDevice(IDevice device)
  250. {
  251. try
  252. {
  253. DATA.Subscribe(device, string.Format("{0}.{1}", device.Module, device.Name));
  254. _nameDevice.Add(device.Name, device);
  255. if (!_typeDevice.ContainsKey(device.GetType()))
  256. _typeDevice[device.GetType()] = new List<IDevice>();
  257. _typeDevice[device.GetType()].Add(device);
  258. device.Initialize();
  259. }
  260. catch (Exception ex)
  261. {
  262. LOG.WriteExeption(ex);
  263. throw;
  264. }
  265. //Task task = Task.Run(()=>device.Initialize());
  266. return device;
  267. }
  268. public IDevice AddCustomModuleDevice(IDevice device)
  269. {
  270. try
  271. {
  272. DATA.Subscribe(device, string.Format("{0}.{1}", device.Module, device.Name));
  273. _nameDevice.Add($"{device.Module}.{device.Name}", device);
  274. if (!_typeDevice.ContainsKey(device.GetType()))
  275. _typeDevice[device.GetType()] = new List<IDevice>();
  276. _typeDevice[device.GetType()].Add(device);
  277. device.Initialize();
  278. }
  279. catch (Exception ex)
  280. {
  281. LOG.WriteExeption(ex);
  282. throw;
  283. }
  284. //Task task = Task.Run(()=>device.Initialize());
  285. return device;
  286. }
  287. public virtual bool Initialize()
  288. {
  289. return true;
  290. }
  291. public T GetDevice<T>(string name) where T : class,IDevice
  292. {
  293. if (!_nameDevice.ContainsKey(name))
  294. return null;
  295. return _nameDevice[name] as T;
  296. }
  297. public object GetDevice(string name)
  298. {
  299. if (!_nameDevice.ContainsKey(name))
  300. return null;
  301. return _nameDevice[name];
  302. }
  303. public List<T> GetDevice<T>() where T : class, IDevice
  304. {
  305. if (!_typeDevice.ContainsKey(typeof(T)))
  306. return null;
  307. List<T> result = new List<T>();
  308. foreach (var d in _typeDevice[typeof(T)])
  309. {
  310. result.Add(d as T);
  311. }
  312. return result;
  313. }
  314. public List<IDevice> GetAllDevice()
  315. {
  316. return _nameDevice.Values.ToList();
  317. }
  318. }
  319. }