EventManager.cs 15 KB

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