EventManager.cs 12 KB

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