EventManager.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Aitex.Core.Util;
  6. using System.IO;
  7. using System.ServiceModel;
  8. using Aitex.Core.RT.DataCenter;
  9. using Aitex.Core.WCF;
  10. using Aitex.Core.RT.Log;
  11. using Aitex.Core.RT.OperationCenter;
  12. namespace Aitex.Core.RT.Event
  13. {
  14. public class EventManager : ICommonEvent
  15. {
  16. public EventService Service
  17. {
  18. get { return _eventService; }
  19. }
  20. public event Action<EventItem> FireEvent;
  21. public event Action<EventItem> OnAlarmEvent;
  22. public event Action<EventItem> OnEvent;
  23. FixSizeQueue<EventItem> _eventQueue;
  24. FixSizeQueue<EventItem> _alarmQueue;
  25. PeriodicJob _eventJob;
  26. EventDBWriter _eventDB;
  27. EventLogWriter _writerToLog;
  28. EventMailWriter _writerToMail;
  29. EventService _eventService;
  30. ServiceHost _eventServiceHost;
  31. Dictionary<string, EventItem> _eventDic = new Dictionary<string, EventItem>();
  32. private const string INFORMATION_EVENT = "INFORMATION_EVENT";
  33. private const string WARNING_EVENT = "WARNING_EVENT";
  34. private const string ALARM_EVENT = "ALARM_EVENT";
  35. public EventManager()
  36. {
  37. }
  38. public void Initialize(string commonEventListXmlFile, bool needCreateService = true, bool needSaveDB = true, bool needMailOut = false, string localEventListXmlFile=null)
  39. {
  40. if (needSaveDB)
  41. {
  42. _eventDB = new EventDBWriter();
  43. try
  44. {
  45. _eventDB.Initialize();
  46. }
  47. catch (Exception ex)
  48. {
  49. LOG.Write(ex);
  50. }
  51. }
  52. _writerToLog = new EventLogWriter();
  53. if (needMailOut)
  54. {
  55. _writerToMail = new EventMailWriter();
  56. }
  57. _eventService = new EventService();
  58. if (needCreateService)
  59. {
  60. try
  61. {
  62. _eventServiceHost = new ServiceHost(_eventService);
  63. _eventServiceHost.Open();
  64. }
  65. catch (Exception ex)
  66. {
  67. throw new ApplicationException("创建Event服务失败," + ex.Message);
  68. }
  69. }
  70. _eventQueue = new FixSizeQueue<EventItem>(1000);
  71. _alarmQueue = new FixSizeQueue<EventItem>(1000);
  72. _eventJob = new PeriodicJob(100, this.PeriodicRun, "EventPeriodicJob", true);
  73. try
  74. {
  75. EventDefine eventList = CustomXmlSerializer.Deserialize<EventDefine>(new FileInfo(commonEventListXmlFile));
  76. foreach (var item in eventList.Items)
  77. _eventDic[item.EventEnum] = item;
  78. }
  79. catch (ArgumentNullException)
  80. {
  81. throw new ApplicationException("初始化EventManager没有设置Event列表文件");
  82. }
  83. catch (FileNotFoundException ex)
  84. {
  85. throw new ApplicationException("没有找到Event列表文件,"+ex.Message);
  86. }
  87. catch (Exception ex)
  88. {
  89. throw new ApplicationException("EventDefine文件格式不对," + commonEventListXmlFile + ",\r\n" + ex.Message);
  90. }
  91. try
  92. {
  93. if (!string.IsNullOrEmpty(localEventListXmlFile))
  94. {
  95. EventDefine evList = CustomXmlSerializer.Deserialize<EventDefine>(new FileInfo(localEventListXmlFile));
  96. foreach (var item in evList.Items)
  97. _eventDic[item.EventEnum] = item;
  98. }
  99. }
  100. catch (ArgumentNullException)
  101. {
  102. throw new ApplicationException("初始化EventManager没有设置Event列表文件");
  103. }
  104. catch (FileNotFoundException ex)
  105. {
  106. throw new ApplicationException("没有找到Event列表文件," + ex.Message);
  107. }
  108. catch (Exception ex)
  109. {
  110. throw new ApplicationException("EventDefine文件格式不对," + localEventListXmlFile + ",\r\n" + ex.Message);
  111. }
  112. Subscribe(new EventItem(INFORMATION_EVENT, EventType.EventUI_Notify, EventLevel.Information));
  113. Subscribe(new EventItem(WARNING_EVENT, EventType.EventUI_Notify, EventLevel.Warning));
  114. Subscribe(new EventItem(ALARM_EVENT, EventType.EventUI_Notify, EventLevel.Alarm));
  115. OP.Subscribe("System.ResetAlarm", InvokeResetAlarm);
  116. EV.InnerEventManager = this;
  117. }
  118. private bool InvokeResetAlarm(string arg1, object[] arg2)
  119. {
  120. ClearAlarmEvent();
  121. return true;
  122. }
  123. public void Terminate()
  124. {
  125. if (_eventJob != null)
  126. {
  127. _eventJob.Stop();
  128. _eventJob = null;
  129. }
  130. if (_eventServiceHost != null)
  131. {
  132. _eventServiceHost.Close();
  133. _eventServiceHost = null;
  134. }
  135. }
  136. public void WriteEvent(string eventName)
  137. {
  138. if (!_eventDic.ContainsKey(eventName))
  139. {
  140. LOG.Write("Event name not registered, " + eventName);
  141. return;
  142. }
  143. WriteEvent(_eventDic[eventName].Source, eventName);
  144. }
  145. public void WriteEvent(string module, string eventName, string message)
  146. {
  147. if (!_eventDic.ContainsKey(eventName))
  148. {
  149. LOG.Write("Event name not registered, " + eventName);
  150. return;
  151. }
  152. EventItem item = _eventDic[eventName].Clone();
  153. item.Source = module;
  154. item.Description = message;
  155. item.OccuringTime = DateTime.Now;
  156. _eventQueue.Enqueue(item);
  157. if (item.Level == EventLevel.Alarm || item.Level == EventLevel.Warning)
  158. {
  159. _alarmQueue.Enqueue(item);
  160. if (OnAlarmEvent != null)
  161. OnAlarmEvent(item);
  162. }
  163. if (OnEvent != null)
  164. {
  165. OnEvent(item);
  166. }
  167. _writerToLog.WriteEvent(item);
  168. //WriteEvent(eventName);
  169. }
  170. public void WriteEvent(string eventName, SerializableDictionary<string, string> dvid)
  171. {
  172. if (!_eventDic.ContainsKey(eventName))
  173. {
  174. LOG.Write("Event name not registered, " + eventName);
  175. return;
  176. }
  177. WriteEvent(_eventDic[eventName].Source, eventName, dvid);
  178. }
  179. public void WriteEvent(string eventName, SerializableDictionary<string, object> dvid)
  180. {
  181. if (!_eventDic.ContainsKey(eventName))
  182. {
  183. LOG.Error("Event name not registered, " + eventName);
  184. return;
  185. }
  186. EventItem item = _eventDic[eventName].Clone(dvid);
  187. item.OccuringTime = DateTime.Now;
  188. ProceedReceivedEvent(item);
  189. }
  190. public void WriteEvent(string module, string eventName, params object[] args)
  191. {
  192. EventItem item = _eventDic[eventName].Clone();
  193. item.Source = module;
  194. if(_eventDic[eventName].Description == null)
  195. {
  196. System.Diagnostics.Trace.WriteLine("?? confirm with Liming");
  197. return;
  198. }
  199. item.Description = string.Format(_eventDic[eventName].Description, args);
  200. if (!string.IsNullOrEmpty(_eventDic[eventName].GlobalDescription_en))
  201. item.GlobalDescription_en = string.Format(_eventDic[eventName].GlobalDescription_en, args);
  202. if (!string.IsNullOrEmpty(_eventDic[eventName].GlobalDescription_zh))
  203. item.GlobalDescription_zh = string.Format(_eventDic[eventName].GlobalDescription_zh, args);
  204. item.OccuringTime = DateTime.Now;
  205. _eventQueue.Enqueue(item);
  206. if (item.Level == EventLevel.Alarm || item.Level == EventLevel.Warning)
  207. {
  208. _alarmQueue.Enqueue(item);
  209. if (OnAlarmEvent != null)
  210. OnAlarmEvent(item);
  211. }
  212. if (OnEvent != null)
  213. {
  214. OnEvent(item);
  215. }
  216. _writerToLog.WriteEvent(item);
  217. }
  218. public void WriteEvent(string module, string eventName, SerializableDictionary<string, string> dvid, params object[] args)
  219. {
  220. EventItem item = _eventDic[eventName].Clone();
  221. item.Source = module;
  222. item.Description = string.Format(_eventDic[eventName].Description, args);
  223. if (!string.IsNullOrEmpty(_eventDic[eventName].GlobalDescription_en))
  224. item.GlobalDescription_en = string.Format(_eventDic[eventName].GlobalDescription_en, args);
  225. if (!string.IsNullOrEmpty(_eventDic[eventName].GlobalDescription_zh))
  226. item.GlobalDescription_zh = string.Format(_eventDic[eventName].GlobalDescription_zh, args);
  227. item.OccuringTime = DateTime.Now;
  228. item.DVID = dvid;
  229. _eventQueue.Enqueue(item);
  230. if (item.Level == EventLevel.Alarm || item.Level == EventLevel.Warning)
  231. {
  232. _alarmQueue.Enqueue(item);
  233. if (OnAlarmEvent != null)
  234. OnAlarmEvent(item);
  235. }
  236. if (OnEvent != null)
  237. {
  238. OnEvent(item);
  239. }
  240. _writerToLog.WriteEvent(item);
  241. }
  242. private void ProceedReceivedEvent(EventItem item)
  243. {
  244. _eventQueue.Enqueue(item);
  245. if (item.Level == EventLevel.Alarm || item.Level == EventLevel.Warning)
  246. {
  247. _alarmQueue.Enqueue(item);
  248. if (OnAlarmEvent != null)
  249. OnAlarmEvent(item);
  250. }
  251. if (OnEvent != null)
  252. {
  253. OnEvent(item);
  254. }
  255. _writerToLog.WriteEvent(item);
  256. }
  257. public void PostNotificationMessage(string message)
  258. {
  259. var eventItem = new EventItem()
  260. {
  261. Type = EventType.UIMessage_Notify,
  262. Description = message,
  263. OccuringTime = DateTime.Now,
  264. };
  265. _eventQueue.Enqueue(eventItem);
  266. _writerToLog.WriteEvent(eventItem);
  267. }
  268. public void PostPopDialogMessage(EventLevel level, string title, string message)
  269. {
  270. var eventItem = new EventItem()
  271. {
  272. Type = EventType.Dialog_Nofity,
  273. Description = title,
  274. Explaination = message,
  275. OccuringTime = DateTime.Now,
  276. Level = level,
  277. };
  278. _eventQueue.Enqueue(eventItem);
  279. _writerToLog.WriteEvent(eventItem);
  280. }
  281. public void PostKickoutMessage(string message)
  282. {
  283. var eventItem = new EventItem()
  284. {
  285. Type = EventType.KickOut_Notify,
  286. Description = message,
  287. OccuringTime = DateTime.Now,
  288. };
  289. _eventQueue.Enqueue(eventItem);
  290. _writerToLog.WriteEvent(eventItem);
  291. }
  292. public void PostSoundMessage(string message)
  293. {
  294. var eventItem = new EventItem()
  295. {
  296. Type = EventType.Sound_Notify,
  297. Description = message,
  298. OccuringTime = DateTime.Now,
  299. };
  300. _eventQueue.Enqueue(eventItem);
  301. _writerToLog.WriteEvent(eventItem);
  302. }
  303. bool PeriodicRun()
  304. {
  305. EventItem ev;
  306. while (_eventQueue.TryDequeue(out ev))
  307. {
  308. try
  309. {
  310. if (_eventDB!= null)
  311. _eventDB.WriteEvent(ev);
  312. //_writerToLog.WriteEvent(ev);
  313. if (_writerToMail != null)
  314. _writerToMail.WriteEvent(ev);
  315. if (_eventService != null)
  316. _eventService.FireEvent(ev);
  317. if (FireEvent != null)
  318. FireEvent(ev);
  319. }
  320. catch (Exception ex)
  321. {
  322. LOG.Error("事件输出失败", ex);
  323. }
  324. }
  325. return true;
  326. }
  327. public List<EventItem> GetAlarmEvent()
  328. {
  329. return _alarmQueue.ToList();
  330. }
  331. public void ClearAlarmEvent()
  332. {
  333. _alarmQueue.Clear();
  334. }
  335. public List<EventItem> QueryDBEvent(string sql)
  336. {
  337. return _eventDB.QueryDBEvent(sql);
  338. }
  339. public void Subscribe(EventItem item)
  340. {
  341. if (_eventDic.ContainsKey(item.EventEnum))
  342. {
  343. return;
  344. }
  345. _eventDic[item.EventEnum] = item;
  346. }
  347. public void PostInfoLog(string module, string message)
  348. {
  349. WriteEvent(module, INFORMATION_EVENT, message);
  350. }
  351. public void PostWarningLog(string module, string message)
  352. {
  353. WriteEvent(module, WARNING_EVENT , message);
  354. }
  355. public void PostAlarmLog(string module, string message)
  356. {
  357. WriteEvent(module, ALARM_EVENT, message);
  358. }
  359. }
  360. }