DeviceManagerBase.cs 16 KB

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