FaEvent.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using Aitex.Core.Common;
  2. using Aitex.Core.RT.Event;
  3. using Aitex.Core.RT.Log;
  4. using Aitex.Core.RT.OperationCenter;
  5. using Aitex.Core.Util;
  6. using FabConnect.SecsGemInterface.Common.ToolModel;
  7. using log4net.Core;
  8. using MECF.Framework.Common.Equipment;
  9. using MECF.Framework.Common.SubstrateTrackings;
  10. using Mono.Security.Authenticode;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Runtime.Serialization;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using Venus_RT.HostWrapper;
  19. using static Aitex.Core.RT.Log.LOG;
  20. using static Mono.Security.X509.X520;
  21. namespace Venus_RT.FAs
  22. {
  23. public class FaEvent
  24. {
  25. //public event Action<PostFAItem> PostEvent;
  26. static Dictionary<string, PostFAItem> _eventDic = new Dictionary<string, PostFAItem>();
  27. static FixSizeQueue<PostFAItem> _eventQueue;
  28. PeriodicJob _eventJob;
  29. private const string INFORMATION_EVENT = "INFORMATION_EVENT";
  30. private const string WARNING_EVENT = "WARNING_EVENT";
  31. private const string ALARM_EVENT = "ALARM_EVENT";
  32. public void Initialize(string commonEventListXmlFile)
  33. {
  34. _eventJob = new PeriodicJob(100, this.PeriodicRun, "FaEventPeriodicJob", true);
  35. _eventQueue = new FixSizeQueue<PostFAItem>(1000);
  36. AddFaEvent(new PostFAItem(INFORMATION_EVENT, EventLevel.Information));
  37. AddFaEvent(new PostFAItem(WARNING_EVENT, EventLevel.Warning));
  38. AddFaEvent(new PostFAItem(ALARM_EVENT, EventLevel.Alarm));
  39. try
  40. {
  41. EventDefine eventList = CustomXmlSerializer.Deserialize<EventDefine>(new FileInfo(commonEventListXmlFile));
  42. foreach (var item in eventList.Items)
  43. {
  44. _eventDic[item.EventEnum] = new PostFAItem();
  45. _eventDic[item.EventEnum].FaEventEnum = item.EventEnum;
  46. _eventDic[item.EventEnum].Description = item.Description;
  47. _eventDic[item.EventEnum].Level = item.Level;
  48. _eventDic[item.EventEnum].Source = item.Source;
  49. _eventDic[item.EventEnum].FaEventEnum = item.EventEnum;
  50. }
  51. }
  52. catch (ArgumentNullException)
  53. {
  54. throw new ApplicationException("初始化EventManager没有设置Event列表文件");
  55. }
  56. catch (FileNotFoundException ex)
  57. {
  58. throw new ApplicationException("没有找到Event列表文件," + ex.Message);
  59. }
  60. catch (Exception ex)
  61. {
  62. throw new ApplicationException("EventDefine文件格式不对," + commonEventListXmlFile + ",\r\n" + ex.Message);
  63. }
  64. }
  65. public void Terminate()
  66. {
  67. if (_eventJob != null)
  68. {
  69. _eventJob.Stop();
  70. _eventJob = null;
  71. }
  72. }
  73. public static void AddFaEvent(PostFAItem item)
  74. {
  75. if (_eventDic.ContainsKey(item.FaEventEnum))
  76. {
  77. return;
  78. }
  79. _eventDic[item.FaEventEnum] = item;
  80. }
  81. bool PeriodicRun()
  82. {
  83. PostFAItem FaEv;
  84. while (_eventQueue.TryDequeue(out FaEv))
  85. {
  86. try
  87. {
  88. InstanceOnOnEvent(FaEv);
  89. }
  90. catch (Exception ex)
  91. {
  92. }
  93. }
  94. return true;
  95. }
  96. private void InstanceOnOnEvent(PostFAItem obj)
  97. {
  98. if (obj.Level != EventLevel.Alarm)
  99. {
  100. Singleton<FaManager>.Instance.NotifyEvent(obj.FaEventEnum, obj.DVID, obj.ObjDVID);
  101. }
  102. else
  103. {
  104. Singleton<FaManager>.Instance.NotifyAlarm(obj.FaEventEnum, obj.DVID, obj.ObjDVID, obj.Description);
  105. }
  106. }
  107. public static void SendHost(string module, string eventName, params object[] args)
  108. {
  109. if (!_eventDic.ContainsKey(eventName))
  110. {
  111. //LOG.Write(eEvent.INFO_FA, "Event name not registered, " + eventName);
  112. return;
  113. }
  114. PostFAItem item = _eventDic[eventName].Clone();
  115. item.Source = module;
  116. item.Description = string.Format(_eventDic[eventName].Description, args);
  117. if (!string.IsNullOrWhiteSpace(item.Source))
  118. {
  119. item.Description = item.Source + " " + item.Description;
  120. }
  121. _eventQueue.Enqueue(item);
  122. }
  123. public static void SendHost(string module, string eventName, string description)
  124. {
  125. if (!_eventDic.ContainsKey(eventName))
  126. {
  127. //LOG.Write(eEvent.INFO_FA, "Event name not registered, " + eventName);
  128. return;
  129. }
  130. PostFAItem item = _eventDic[eventName].Clone();
  131. item.Source = module;
  132. item.Description = description;
  133. if (!string.IsNullOrWhiteSpace(item.Source))
  134. {
  135. item.Description = item.Source + " " + item.Description;
  136. }
  137. _eventQueue.Enqueue(item);
  138. }
  139. public static void SendHost(string eventName, SerializableDictionary<string, string> dvid)
  140. {
  141. if (!_eventDic.ContainsKey(eventName))
  142. {
  143. return;
  144. }
  145. PostFAItem item = _eventDic[eventName].Clone();
  146. item.Description = string.Format(_eventDic[eventName].Description);
  147. item.DVID = dvid;
  148. item.Source = _eventDic[eventName].Source;
  149. _eventQueue.Enqueue(item);
  150. }
  151. public static void SendHost(string eventName, SerializableDictionary<string, object> dvid)
  152. {
  153. if (!_eventDic.ContainsKey(eventName))
  154. {
  155. return;
  156. }
  157. PostFAItem item = _eventDic[eventName].Clone(dvid);
  158. item.Description = string.Format(_eventDic[eventName].Description);
  159. item.ObjDVID = dvid;
  160. item.Source = _eventDic[eventName].Source;
  161. _eventQueue.Enqueue(item);
  162. }
  163. public static void FaPostAlarm(string module, string message)
  164. {
  165. SendHost(module, WARNING_EVENT, message);
  166. }
  167. public static void FaPostWarning(string module, string message)
  168. {
  169. SendHost(module, ALARM_EVENT, message);
  170. }
  171. public static void FaPostInfo(string module, string message)
  172. {
  173. SendHost(module, INFORMATION_EVENT, message);
  174. }
  175. public static void FaPostMessage(string module, string message, params object[] args)
  176. {
  177. SendHost(module, message,args);
  178. }
  179. public static void FaNotify(string eventName, SerializableDictionary<string, string> dvid)
  180. {
  181. SendHost(eventName, dvid);
  182. }
  183. public static void FaNotify(string eventName, SerializableDictionary<string, object> dvid)
  184. {
  185. SendHost(eventName, dvid);
  186. }
  187. }
  188. public class PostFAItem
  189. {
  190. public string Source { get; set; }
  191. public string FaEventEnum { get; set; }
  192. public int Id { get; set; }
  193. public string Explaination { get; set; }
  194. public EventLevel Level { get; set; }
  195. public string Description { get; set; }
  196. public SerializableDictionary<string, string> DVID
  197. {
  198. get;
  199. set;
  200. }
  201. public SerializableDictionary<string, object> ObjDVID
  202. {
  203. get;
  204. set;
  205. }
  206. public PostFAItem Clone()
  207. {
  208. PostFAItem result = new PostFAItem();
  209. result.Description = Description;
  210. result.FaEventEnum = FaEventEnum;
  211. result.Explaination = Explaination;
  212. result.Id = Id;
  213. result.Level = Level;
  214. result.Source = Source;
  215. result.DVID = DVID;
  216. result.ObjDVID = ObjDVID;
  217. return result;
  218. }
  219. public PostFAItem Clone(SerializableDictionary<string, object> objDvid)
  220. {
  221. PostFAItem result = new PostFAItem();
  222. result.Description = Description;
  223. result.FaEventEnum = FaEventEnum;
  224. result.Explaination = Explaination;
  225. result.Id = Id;
  226. result.Level = Level;
  227. result.Source = Source;
  228. result.DVID = DVID;
  229. result.ObjDVID = objDvid;
  230. return result;
  231. }
  232. public PostFAItem()
  233. {
  234. }
  235. public PostFAItem(string name, EventLevel level)
  236. {
  237. FaEventEnum = name;
  238. Level = level;
  239. }
  240. public PostFAItem(string source, string name, string description, EventLevel level)
  241. {
  242. Source = source;
  243. FaEventEnum = name;
  244. Description = description;
  245. Level = level;
  246. }
  247. public PostFAItem(string source, string name, string description)
  248. {
  249. Source = source;
  250. FaEventEnum = name;
  251. Description = description;
  252. }
  253. }
  254. }