using System; using System.Collections.Generic; using System.Linq; using System.Text; using Aitex.Core.Util; using System.IO; using System.ServiceModel; using Aitex.Core.WCF; using Aitex.Core.RT.Log; namespace Aitex.Core.RT.Event { public class EventManager : ICommonEvent { public event Action FireEvent; public event Action OnAlarmEvent; FixSizeQueue _eventQueue; FixSizeQueue _alarmQueue; PeriodicJob _eventJob; EventDBWriter _eventDB; EventLogWriter _writerToLog; EventMailWriter _writerToMail; EventService _eventService; ServiceHost _eventServiceHost; Dictionary _eventDic = new Dictionary(); private const string INFORMATION_EVENT = "INFORMATION_EVENT"; private const string WARNING_EVENT = "WARNING_EVENT"; private const string ALARM_EVENT = "ALARM_EVENT"; public EventManager() { } public void Initialize(string commonEventListXmlFile, bool needCreateService = true, bool needSaveDB = true, bool needMailOut = false, string localEventListXmlFile=null) { if (needSaveDB) { _eventDB = new EventDBWriter(); try { _eventDB.Initialize(); } catch (Exception ex) { LOG.Write(ex); } } _writerToLog = new EventLogWriter(); if (needMailOut) { _writerToMail = new EventMailWriter(); } if (needCreateService) { try { _eventService = new EventService(); _eventServiceHost = new ServiceHost(_eventService); _eventServiceHost.Open(); } catch (Exception ex) { throw new ApplicationException("创建Event服务失败," + ex.Message); } } _eventQueue = new FixSizeQueue(1000); _alarmQueue = new FixSizeQueue(1000); _eventJob = new PeriodicJob(100, this.PeriodicRun, "EventPeriodicJob", true); try { EventDefine eventList = CustomXmlSerializer.Deserialize(new FileInfo(commonEventListXmlFile)); foreach (var item in eventList.Items) _eventDic[item.EventEnum] = item; } catch (ArgumentNullException) { throw new ApplicationException("初始化EventManager没有设置Event列表文件"); } catch (FileNotFoundException ex) { throw new ApplicationException("没有找到Event列表文件,"+ex.Message); } catch (Exception ex) { throw new ApplicationException("EventDefine文件格式不对," + commonEventListXmlFile + ",\r\n" + ex.Message); } try { if (!string.IsNullOrEmpty(localEventListXmlFile)) { EventDefine evList = CustomXmlSerializer.Deserialize(new FileInfo(localEventListXmlFile)); foreach (var item in evList.Items) _eventDic[item.EventEnum] = item; } } catch (ArgumentNullException) { throw new ApplicationException("初始化EventManager没有设置Event列表文件"); } catch (FileNotFoundException ex) { throw new ApplicationException("没有找到Event列表文件," + ex.Message); } catch (Exception ex) { throw new ApplicationException("EventDefine文件格式不对," + localEventListXmlFile + ",\r\n" + ex.Message); } Subscribe(new EventItem(INFORMATION_EVENT, EventType.EventUI_Notify, EventLevel.Information)); Subscribe(new EventItem(WARNING_EVENT, EventType.EventUI_Notify, EventLevel.Warning)); Subscribe(new EventItem(ALARM_EVENT, EventType.EventUI_Notify, EventLevel.Alarm)); EV.InnerEventManager = this; } public void Terminate() { if (_eventJob != null) { _eventJob.Stop(); _eventJob = null; } if (_eventServiceHost != null) { _eventServiceHost.Close(); _eventServiceHost = null; } } public void WriteEvent(string eventName) { if (!_eventDic.ContainsKey(eventName)) { LOG.Write("Event name not registered, " + eventName); return; } WriteEvent(_eventDic[eventName].Source, eventName); } public void WriteEvent(string module, string eventName, string message) { if (!_eventDic.ContainsKey(eventName)) { LOG.Write("Event name not registered, " + eventName); return; } EventItem item = _eventDic[eventName].Clone(); item.Source = module; item.Description = message; item.OccuringTime = DateTime.Now; _eventQueue.Enqueue(item); if (item.Level == EventLevel.Alarm || item.Level == EventLevel.Warning) { _alarmQueue.Enqueue(item); if (OnAlarmEvent != null) OnAlarmEvent(item); } _writerToLog.WriteEvent(item); //WriteEvent(eventName); } public void WriteEvent(string eventName, SerializableDictionary dvid) { if (!_eventDic.ContainsKey(eventName)) { LOG.Write("Event name not registered, " + eventName); return; } WriteEvent(_eventDic[eventName].Source, eventName, dvid); } public void WriteEvent(string module, string eventName, params object[] args) { EventItem item = _eventDic[eventName].Clone(); item.Source = module; if(_eventDic[eventName].Description == null) { LOG.Error($"{module}.{eventName}, found empty description event."); return; } item.Description = string.Format(_eventDic[eventName].Description, args); if (!string.IsNullOrEmpty(_eventDic[eventName].GlobalDescription_en)) item.GlobalDescription_en = string.Format(_eventDic[eventName].GlobalDescription_en, args); if (!string.IsNullOrEmpty(_eventDic[eventName].GlobalDescription_zh)) item.GlobalDescription_zh = string.Format(_eventDic[eventName].GlobalDescription_zh, args); item.OccuringTime = DateTime.Now; _eventQueue.Enqueue(item); if (item.Level == EventLevel.Alarm || item.Level == EventLevel.Warning) { _alarmQueue.Enqueue(item); if (OnAlarmEvent != null) OnAlarmEvent(item); } _writerToLog.WriteEvent(item); } public void WriteEvent(string module, string eventName, SerializableDictionary dvid, params object[] args) { EventItem item = _eventDic[eventName].Clone(); item.Source = module; item.Description = string.Format(_eventDic[eventName].Description, args); if (!string.IsNullOrEmpty(_eventDic[eventName].GlobalDescription_en)) item.GlobalDescription_en = string.Format(_eventDic[eventName].GlobalDescription_en, args); if (!string.IsNullOrEmpty(_eventDic[eventName].GlobalDescription_zh)) item.GlobalDescription_zh = string.Format(_eventDic[eventName].GlobalDescription_zh, args); item.OccuringTime = DateTime.Now; item.dvid = dvid; _eventQueue.Enqueue(item); if (item.Level == EventLevel.Alarm || item.Level == EventLevel.Warning) { _alarmQueue.Enqueue(item); if (OnAlarmEvent != null) OnAlarmEvent(item); } _writerToLog.WriteEvent(item); } public void PostNotificationMessage(string message) { var eventItem = new EventItem() { Type = EventType.UIMessage_Notify, Description = message, OccuringTime = DateTime.Now, }; _eventQueue.Enqueue(eventItem); _writerToLog.WriteEvent(eventItem); } public void PostPopDialogMessage(EventLevel level, string title, string message) { var eventItem = new EventItem() { Type = EventType.Dialog_Nofity, Description = title, Explaination = message, OccuringTime = DateTime.Now, Level = level, }; _eventQueue.Enqueue(eventItem); _writerToLog.WriteEvent(eventItem); } public void PostKickoutMessage(string message) { var eventItem = new EventItem() { Type = EventType.KickOut_Notify, Description = message, OccuringTime = DateTime.Now, }; _eventQueue.Enqueue(eventItem); _writerToLog.WriteEvent(eventItem); } public void PostSoundMessage(string message) { var eventItem = new EventItem() { Type = EventType.Sound_Notify, Description = message, OccuringTime = DateTime.Now, }; _eventQueue.Enqueue(eventItem); _writerToLog.WriteEvent(eventItem); } bool PeriodicRun() { EventItem ev; while (_eventQueue.TryDequeue(out ev)) { try { if (_eventDB!= null) _eventDB.WriteEvent(ev); //_writerToLog.WriteEvent(ev); if (_writerToMail != null) _writerToMail.WriteEvent(ev); if (_eventService != null) _eventService.FireEvent(ev); if (FireEvent != null) FireEvent(ev); } catch (Exception ex) { LOG.Error("事件输出失败", ex); } } return true; } public List GetAlarmEvent() { return _alarmQueue.ToList(); } public void ClearAlarmEvent() { _alarmQueue.Clear(); } public List QueryDBEvent(string sql) { return _eventDB.QueryDBEvent(sql); } public void Subscribe(EventItem item) { if (_eventDic.ContainsKey(item.EventEnum)) { return; } _eventDic[item.EventEnum] = item; } public void PostInfoLog(string module, string message) { WriteEvent(module, INFORMATION_EVENT, message); } public void PostWarningLog(string module, string message) { WriteEvent(module, WARNING_EVENT , message); } public void PostAlarmLog(string module, string message) { WriteEvent(module, ALARM_EVENT, message); } } }