DeviceManagerBase.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. using Aitex.Core.RT.DataCenter;
  2. using Aitex.Core.RT.Device;
  3. using Aitex.Core.Util;
  4. using MECF.Framework.Common.Equipment;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Reflection;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Xml;
  14. using DATA = Common.DataCenter.DATA;
  15. namespace athosRT.Devices
  16. {
  17. public class DeviceManagerBase : IDeviceManager
  18. {
  19. private Dictionary<string, IDevice> _nameDevice = new Dictionary<string, IDevice>();
  20. private Dictionary<Type, List<IDevice>> _typeDevice = new Dictionary<Type, List<IDevice>>();
  21. private DeviceTimer _peformanceTimer = new DeviceTimer();
  22. private R_TRIG _trigExceed500 = new R_TRIG();
  23. private List<IDevice> _optionDevice = new List<IDevice>();
  24. private object _lockerDevice = new object();
  25. private bool _isModularDevice;
  26. protected XmlElement DeviceModelNodes { get; private set; }
  27. public bool DisableAsyncInitialize { get; set; }
  28. public DeviceManagerBase()
  29. {
  30. DEVICE.Managers.Add(this);
  31. }
  32. public DeviceManagerBase(bool modularDevice)
  33. {
  34. _isModularDevice = modularDevice;
  35. DEVICE.Managers.Add(this);
  36. }
  37. public virtual void Terminate()
  38. {
  39. lock (_lockerDevice)
  40. {
  41. foreach (IDevice value in _nameDevice.Values)
  42. {
  43. value.Terminate();
  44. }
  45. }
  46. }
  47. public void Monitor()
  48. {
  49. lock (_lockerDevice)
  50. {
  51. foreach (IDevice value in _nameDevice.Values)
  52. {
  53. try
  54. {
  55. _peformanceTimer.Start(0.0);
  56. value.Monitor();
  57. _trigExceed500.CLK = _peformanceTimer.GetElapseTime() > 500.0;
  58. if (_trigExceed500.Q)
  59. {
  60. //LOG.Warning($"{value.Module}.{value.Name} monitor time {_peformanceTimer.GetElapseTime()} ms", 2, "D:\\sorter\\trunk\\Framework\\Common\\Device\\Common\\DeviceManagerBase.cs", "Monitor", 74);
  61. }
  62. }
  63. catch (Exception ex)
  64. {
  65. //LOG.Write(ex, $"Monitor {value.Name} Exception", 2, "D:\\sorter\\trunk\\Framework\\Common\\Device\\Common\\DeviceManagerBase.cs", "Monitor", 79);
  66. }
  67. }
  68. }
  69. }
  70. public void Reset()
  71. {
  72. lock (_lockerDevice)
  73. {
  74. foreach (IDevice value in _nameDevice.Values)
  75. {
  76. value.Reset();
  77. }
  78. _trigExceed500.RST = true;
  79. }
  80. }
  81. public void Initialize(string modelFile, string type, ModuleName mod = ModuleName.System, string ioModule = "", bool endCallInit = true)
  82. {
  83. if (!File.Exists(modelFile))
  84. {
  85. throw new ApplicationException($"did not find the device model file {modelFile} ");
  86. }
  87. XmlDocument xmlDocument = new XmlDocument();
  88. try
  89. {
  90. xmlDocument.Load(modelFile);
  91. XmlElement xmlElement = xmlDocument.SelectSingleNode("DeviceModelDefine") as XmlElement;
  92. if (xmlElement == null)
  93. {
  94. throw new ApplicationException($"device mode file {modelFile} is not valid");
  95. }
  96. string attribute = xmlElement.GetAttribute("type");
  97. if (attribute != type)
  98. {
  99. throw new ApplicationException($"the type {attribute} in device mode file {modelFile} is inaccordance with the system type {type}");
  100. }
  101. DeviceModelNodes = xmlElement;
  102. //这里会定义三个主要的内容 xmlElement
  103. foreach (XmlNode childNode in xmlElement.ChildNodes)
  104. {
  105. if (childNode.NodeType == XmlNodeType.Comment)
  106. {
  107. continue;
  108. }
  109. XmlElement xmlElement2 = childNode as XmlElement;
  110. if (xmlElement2 == null)
  111. {
  112. continue;
  113. }
  114. string text = xmlElement2.GetAttribute("assembly");
  115. if (string.IsNullOrEmpty(text))
  116. {
  117. text = "MECF.Framework.RT.EquipmentLibrary";
  118. }
  119. string text2 = xmlElement2.Name.Substring(0, xmlElement2.Name.Length - 1);
  120. string text3 = xmlElement2.GetAttribute("classType");
  121. if (string.IsNullOrEmpty(text3))
  122. {
  123. text3 = "Aitex.Core.RT.Device.Unit." + text2;
  124. }
  125. Assembly assembly = Assembly.Load(text);
  126. Type type2 = assembly.GetType(text3);
  127. if (type2 == null)
  128. {
  129. continue;
  130. }
  131. foreach (object childNode2 in xmlElement2.ChildNodes)
  132. {
  133. XmlElement xmlElement3 = childNode2 as XmlElement;
  134. if (xmlElement3 == null)
  135. {
  136. //LOG.Write("Device Model File contains non element node, " + (childNode2 as XmlNode).Value, 2, "D:\\sorter\\trunk\\Framework\\Common\\Device\\Common\\DeviceManagerBase.cs", "Initialize", 156);
  137. continue;
  138. }
  139. IDevice device = Activator.CreateInstance(type2, mod.ToString(), xmlElement3, ioModule) as IDevice;
  140. if (xmlElement3.HasAttribute("option") && Convert.ToBoolean(xmlElement3.Attributes["option"].Value))
  141. {
  142. _optionDevice.Add(device);
  143. continue;
  144. }
  145. QueueDevice(device);
  146. DATA.Subscribe(device, $"{device.Module}.{text2}.{device.Name}");
  147. if (!_typeDevice.ContainsKey(type2))
  148. {
  149. _typeDevice[type2] = new List<IDevice>();
  150. }
  151. _typeDevice[type2].Add(device);
  152. if (DisableAsyncInitialize)
  153. {
  154. InitDevice(device);
  155. continue;
  156. }
  157. Task task = Task.Run(delegate
  158. {
  159. InitDevice(device);
  160. });
  161. }
  162. }
  163. if (endCallInit)
  164. {
  165. Initialize();
  166. }
  167. }
  168. catch (Exception ex)
  169. {
  170. //LOG.Write(ex, 2, "D:\\sorter\\trunk\\Framework\\Common\\Device\\Common\\DeviceManagerBase.cs", "Initialize", 196);
  171. }
  172. }
  173. public void Initialize(string modelFile, string modelType, string moduleName, string ioPath, string scPath)
  174. {
  175. if (!File.Exists(modelFile))
  176. {
  177. throw new ApplicationException($"did not find the device model file {modelFile} ");
  178. }
  179. XmlDocument xmlDocument = new XmlDocument();
  180. try
  181. {
  182. xmlDocument.Load(modelFile);
  183. XmlElement xmlElement = xmlDocument.SelectSingleNode("DeviceModelDefine") as XmlElement;
  184. if (xmlElement == null)
  185. {
  186. throw new ApplicationException($"device mode file {modelFile} is not valid");
  187. }
  188. string attribute = xmlElement.GetAttribute("type");
  189. if (attribute != modelType)
  190. {
  191. throw new ApplicationException($"the type {attribute} in device mode file {modelFile} is different with the system type {modelType}");
  192. }
  193. DeviceModelNodes = xmlElement;
  194. foreach (XmlNode childNode in xmlElement.ChildNodes)
  195. {
  196. if (childNode.NodeType == XmlNodeType.Comment)
  197. {
  198. continue;
  199. }
  200. XmlElement xmlElement2 = childNode as XmlElement;
  201. if (xmlElement2 == null)
  202. {
  203. continue;
  204. }
  205. string text = xmlElement2.GetAttribute("assembly");
  206. if (string.IsNullOrEmpty(text))
  207. {
  208. text = "MECF.Framework.RT.EquipmentLibrary";
  209. }
  210. string text2 = xmlElement2.Name.Substring(0, xmlElement2.Name.Length - 1);
  211. string text3 = xmlElement2.GetAttribute("classType");
  212. if (string.IsNullOrEmpty(text3))
  213. {
  214. text3 = "Aitex.Core.RT.Device.Unit." + text2;
  215. }
  216. Assembly assembly = Assembly.Load(text);
  217. Type type = assembly.GetType(text3);
  218. if (type == null)
  219. {
  220. continue;
  221. }
  222. foreach (object childNode2 in xmlElement2.ChildNodes)
  223. {
  224. XmlElement xmlElement3 = childNode2 as XmlElement;
  225. if (xmlElement3 == null)
  226. {
  227. //LOG.Write("Device Model File contains non element node, " + (childNode2 as XmlNode).Value, 2, "D:\\sorter\\trunk\\Framework\\Common\\Device\\Common\\DeviceManagerBase.cs", "Initialize", 257);
  228. continue;
  229. }
  230. xmlElement3.SetAttribute("ioPath", ioPath);
  231. xmlElement3.SetAttribute("scPath", scPath);
  232. IDevice device = Activator.CreateInstance(type, moduleName, xmlElement3, ioPath) as IDevice;
  233. if (xmlElement3.HasAttribute("option") && Convert.ToBoolean(xmlElement3.Attributes["option"]))
  234. {
  235. _optionDevice.Add(device);
  236. continue;
  237. }
  238. DATA.Subscribe(device, $"{device.Module}.{text2}.{device.Name}");
  239. QueueDevice(device);
  240. if (!_typeDevice.ContainsKey(type))
  241. {
  242. _typeDevice[type] = new List<IDevice>();
  243. }
  244. _typeDevice[type].Add(device);
  245. if (DisableAsyncInitialize)
  246. {
  247. InitDevice(device);
  248. continue;
  249. }
  250. Task task = Task.Run(delegate
  251. {
  252. InitDevice(device);
  253. });
  254. }
  255. }
  256. Initialize();
  257. }
  258. catch (Exception ex)
  259. {
  260. //LOG.Write(ex, 2, "D:\\sorter\\trunk\\Framework\\Common\\Device\\Common\\DeviceManagerBase.cs", "Initialize", 295);
  261. }
  262. }
  263. private void InitDevice(IDevice device)
  264. {
  265. try
  266. {
  267. if (!device.Initialize())
  268. {
  269. //LOG.Write(device.Name + " initialize failed.", 2, "D:\\sorter\\trunk\\Framework\\Common\\Device\\Common\\DeviceManagerBase.cs", "InitDevice", 305);
  270. }
  271. }
  272. catch (Exception ex)
  273. {
  274. //LOG.Write(ex, 2, "D:\\sorter\\trunk\\Framework\\Common\\Device\\Common\\DeviceManagerBase.cs", "InitDevice", 310);
  275. }
  276. }
  277. protected virtual void QueueDevice(IDevice device)
  278. {
  279. if (_isModularDevice)
  280. {
  281. QueueDevice(device.Module + "." + device.Name, device);
  282. }
  283. else
  284. {
  285. QueueDevice(device.Name, device);
  286. }
  287. }
  288. protected void QueueDevice(string key, IDevice device)
  289. {
  290. lock (_lockerDevice)
  291. {
  292. foreach (IDeviceManager manager in DEVICE.Managers)
  293. {
  294. if (manager.ContainsDevice(key))
  295. {
  296. Debug.Assert(condition: false, "Failed add duplicated device, named with " + key);
  297. //LOG.Write("Failed add duplicated device, name " + key, 2, "D:\\sorter\\trunk\\Framework\\Common\\Device\\Common\\DeviceManagerBase.cs", "QueueDevice", 337);
  298. return;
  299. }
  300. }
  301. _nameDevice.Add(key, device);
  302. }
  303. }
  304. public IDevice AddCustomDevice(IDevice device, string groupType, Type deviceType)
  305. {
  306. lock (_lockerDevice)
  307. {
  308. DATA.Subscribe(device, $"{device.Module}.{groupType}.{device.Name}");
  309. if (!string.IsNullOrEmpty(device.Module) && device.Module != "System")
  310. {
  311. QueueDevice(device.Module + "." + device.Name, device);
  312. }
  313. else
  314. {
  315. QueueDevice(device.Name, device);
  316. }
  317. if (!_typeDevice.ContainsKey(deviceType))
  318. {
  319. _typeDevice[deviceType] = new List<IDevice>();
  320. }
  321. _typeDevice[deviceType].Add(device);
  322. device.Initialize();
  323. }
  324. return device;
  325. }
  326. public IDevice AddCustomDevice(IDevice device, Type deviceType)
  327. {
  328. lock (_lockerDevice)
  329. {
  330. try
  331. {
  332. DATA.Subscribe(device, $"{device.Module}.{device.Name}");
  333. QueueDevice(device.Name, device);
  334. if (!_typeDevice.ContainsKey(deviceType))
  335. {
  336. _typeDevice[deviceType] = new List<IDevice>();
  337. }
  338. _typeDevice[deviceType].Add(device);
  339. device.Initialize();
  340. }
  341. catch (Exception ex)
  342. {
  343. //LOG.Write(ex, 2, "D:\\sorter\\trunk\\Framework\\Common\\Device\\Common\\DeviceManagerBase.cs", "AddCustomDevice", 390);
  344. }
  345. }
  346. return device;
  347. }
  348. public IDevice AddCustomDevice(IDevice device)
  349. {
  350. lock (_lockerDevice)
  351. {
  352. try
  353. {
  354. DATA.Subscribe(device, $"{device.Module}.{device.Name}");
  355. QueueDevice(device.Name, device);
  356. if (!_typeDevice.ContainsKey(device.GetType()))
  357. {
  358. _typeDevice[device.GetType()] = new List<IDevice>();
  359. }
  360. _typeDevice[device.GetType()].Add(device);
  361. device.Initialize();
  362. }
  363. catch (Exception ex)
  364. {
  365. //LOG.Write(ex, 2, "D:\\sorter\\trunk\\Framework\\Common\\Device\\Common\\DeviceManagerBase.cs", "AddCustomDevice", 417);
  366. }
  367. }
  368. return device;
  369. }
  370. public IDevice AddCustomModuleDevice(IDevice device)
  371. {
  372. lock (_lockerDevice)
  373. {
  374. try
  375. {
  376. DATA.Subscribe(device, $"{device.Module}.{device.Name}");
  377. QueueDevice(device.Module + "." + device.Name, device);
  378. if (!_typeDevice.ContainsKey(device.GetType()))
  379. {
  380. _typeDevice[device.GetType()] = new List<IDevice>();
  381. }
  382. _typeDevice[device.GetType()].Add(device);
  383. device.Initialize();
  384. }
  385. catch (Exception ex)
  386. {
  387. }
  388. }
  389. return device;
  390. }
  391. public dynamic DynamicAddCustomDevice(string type, string module, string name, params object[] otherParam)
  392. {
  393. Type[] types = Assembly.Load(Process.GetCurrentProcess().ProcessName).GetTypes();
  394. Type type2 = types.FirstOrDefault((Type x) => x.IsClass & (x.Name == type));
  395. if (type2 != null)
  396. {
  397. IDevice device;
  398. try
  399. {
  400. object[] args = new object[2] { module, name }.Concat(otherParam).ToArray();
  401. device = Activator.CreateInstance(type2, args) as IDevice;
  402. if (device == null)
  403. {
  404. throw new InvalidCastException($"{type2} does not inherit IDevice interface");
  405. }
  406. }
  407. catch (Exception ex)
  408. {
  409. throw ex;
  410. }
  411. return AddCustomDevice(device);
  412. }
  413. Type[] types2 = Assembly.GetExecutingAssembly().GetTypes();
  414. Type type3 = types2.FirstOrDefault((Type x) => x.IsClass & (x.Name == type));
  415. if (type3 != null)
  416. {
  417. IDevice device2;
  418. try
  419. {
  420. object[] args2 = new object[2] { module, name }.Concat(otherParam).ToArray();
  421. device2 = Activator.CreateInstance(type3, args2) as IDevice;
  422. if (device2 == null)
  423. {
  424. throw new InvalidCastException($"{type3} does not inherit IDevice interface");
  425. }
  426. }
  427. catch (Exception ex2)
  428. {
  429. throw ex2;
  430. }
  431. return AddCustomDevice(device2);
  432. }
  433. throw new NotSupportedException(type + " Undefined in RT or Common Assembly");
  434. }
  435. public virtual bool Initialize()
  436. {
  437. return true;
  438. }
  439. public T GetDevice<T>(string name) where T : class, IDevice
  440. {
  441. if (!_nameDevice.ContainsKey(name))
  442. {
  443. return null;
  444. }
  445. return _nameDevice[name] as T;
  446. }
  447. public object GetDevice(string name)
  448. {
  449. if (!_nameDevice.ContainsKey(name))
  450. {
  451. return null;
  452. }
  453. return _nameDevice[name];
  454. }
  455. public bool ContainsDevice(string name)
  456. {
  457. return _nameDevice.ContainsKey(name);
  458. }
  459. public List<T> GetDevice<T>() where T : class, IDevice
  460. {
  461. if (!_typeDevice.ContainsKey(typeof(T)))
  462. {
  463. return null;
  464. }
  465. List<T> list = new List<T>();
  466. foreach (IDevice item in _typeDevice[typeof(T)])
  467. {
  468. list.Add(item as T);
  469. }
  470. return list;
  471. }
  472. public List<IDevice> GetAllDevice()
  473. {
  474. return _nameDevice.Values.ToList();
  475. }
  476. public object GetOptionDevice(string name, Type type)
  477. {
  478. foreach (IDevice item in _optionDevice)
  479. {
  480. if (item.Module + "." + item.Name == name && (type == null || item.GetType() == type))
  481. {
  482. return item;
  483. }
  484. }
  485. return null;
  486. }
  487. }
  488. }