| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528 | 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.RT.DataCenter;using Aitex.Core.WCF;using Aitex.Core.RT.Log;using Aitex.Core.RT.OperationCenter;namespace Aitex.Core.RT.Event{    public class EventManager : ICommonEvent    {        public EventService Service        {            get { return _eventService; }        }        public event Action<EventItem> FireEvent;        public event Action<EventItem> OnAlarmEvent;        public event Action<EventItem> OnEvent;        FixSizeQueue<EventItem> _eventQueue;        FixSizeQueue<EventItem> _alarmQueue;        PeriodicJob _eventJob;        EventDBWriter _eventDB;        EventLogWriter _writerToLog;        EventMailWriter _writerToMail;        EventService _eventService;        ServiceHost _eventServiceHost;        Dictionary<string, EventItem> _eventDic = new Dictionary<string, EventItem>();        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.WriteExeption(ex);                }            }            _writerToLog = new EventLogWriter();            if (needMailOut)            {                _writerToMail = new EventMailWriter();            }            _eventService = new EventService();            if (needCreateService)            {                try                {                    _eventServiceHost = new ServiceHost(_eventService);                    _eventServiceHost.Open();                }                catch (Exception ex)                {                    throw new ApplicationException("创建Event服务失败," + ex.Message);                }            }            _eventQueue = new FixSizeQueue<EventItem>(1000);            _alarmQueue = new FixSizeQueue<EventItem>(1000);            _eventJob = new PeriodicJob(100, this.PeriodicRun, "EventPeriodicJob", true);            try            {                EventDefine eventList = CustomXmlSerializer.Deserialize<EventDefine>(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<EventDefine>(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));            OP.Subscribe("System.ResetAlarm", InvokeResetAlarm);            EV.InnerEventManager = this;        }        private bool InvokeResetAlarm(string arg1, object[] arg2)        {            ClearAlarmEvent();            return true;        }        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;                        if (!string.IsNullOrWhiteSpace(item.Source))            {                //item.Description = item.Source + "  " + item.Description;                item.Description = item.Description;            }            _eventQueue.Enqueue(item);            if (item.Level == EventLevel.Alarm || item.Level == EventLevel.Warning)            {                _alarmQueue.Enqueue(item);                if (OnAlarmEvent != null)                    OnAlarmEvent(item);            }            if (OnEvent != null)            {                OnEvent(item);            }            _writerToLog.WriteEvent(item);            //WriteEvent(eventName);        }        public void WriteEvent(string module, eEvent id, string eventName, string message)        {            if (!_eventDic.ContainsKey(eventName))            {                //LOG.Write("Event name not registered, " + eventName);                return;            }            EventItem item = _eventDic[eventName].Clone();            item.Id = (int)id;            item.Source = module;            item.Description = message;            item.OccuringTime = DateTime.Now;            if (!string.IsNullOrWhiteSpace(item.Source))            {                //item.Description = item.Source + "  " + item.Description;                item.Description = item.Description;            }            _eventQueue.Enqueue(item);            if (item.Level == EventLevel.Alarm || item.Level == EventLevel.Warning)            {                _alarmQueue.Enqueue(item);                if (OnAlarmEvent != null)                    OnAlarmEvent(item);            }            if (OnEvent != null)            {                OnEvent(item);            }            _writerToLog.WriteEvent(item);            //WriteEvent(eventName);        }        public void WriteEvent(string eventName, SerializableDictionary<string, string> dvid)        {            if (!_eventDic.ContainsKey(eventName))            {                //LOG.Write("Event name not registered, " + eventName);                return;            }            WriteEvent(_eventDic[eventName].Source, eventName, dvid);        }        public void WriteEvent(string eventName, SerializableDictionary<string, object> dvid)        {            if (!_eventDic.ContainsKey(eventName))            {                //LOG.Error("Event name not registered, " + eventName);                return;            }            EventItem item = _eventDic[eventName].Clone(dvid);            item.OccuringTime = DateTime.Now;            ProceedReceivedEvent(item);        }        public void WriteEvent(string module, string eventName, params object[] args)        {            EventItem item = _eventDic[eventName].Clone();            item.Source = module;            if(_eventDic[eventName].Description == null)            {                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;            if (!string.IsNullOrWhiteSpace(item.Source))            {                item.Description = item.Source + "  " + item.Description;            }            _eventQueue.Enqueue(item);            if (item.Level == EventLevel.Alarm || item.Level == EventLevel.Warning)            {                _alarmQueue.Enqueue(item);                if (OnAlarmEvent != null)                    OnAlarmEvent(item);            }            if (OnEvent != null)            {                OnEvent(item);            }            _writerToLog.WriteEvent(item);        }        public void WriteEvent(string module, string eventName, SerializableDictionary<string, string> 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;            if (!string.IsNullOrWhiteSpace(item.Source))            {                item.Description = item.Source + "  " + item.Description;            }            _eventQueue.Enqueue(item);            if (item.Level == EventLevel.Alarm || item.Level == EventLevel.Warning)            {                _alarmQueue.Enqueue(item);                if (OnAlarmEvent != null)                    OnAlarmEvent(item);            }            if (OnEvent != null)            {                OnEvent(item);            }            _writerToLog.WriteEvent(item);        }        private void ProceedReceivedEvent(EventItem item)        {            if (!string.IsNullOrWhiteSpace(item.Source))            {                item.Description = item.Source + "  " + item.Description;            }            _eventQueue.Enqueue(item);            if (item.Level == EventLevel.Alarm || item.Level == EventLevel.Warning)            {                _alarmQueue.Enqueue(item);                if (OnAlarmEvent != null)                    OnAlarmEvent(item);            }            if (OnEvent != null)            {                OnEvent(item);            }            _writerToLog.WriteEvent(item);        }        public void PostNotificationMessage(string message)        {            var eventItem = new EventItem()            {                Type = EventType.UIMessage_Notify,                Description = message,                OccuringTime = DateTime.Now,            };            if (!string.IsNullOrWhiteSpace(eventItem.Source))            {                eventItem.Description = eventItem.Source + "  " + eventItem.Description;            }            _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,            };            if (!string.IsNullOrWhiteSpace(eventItem.Source))            {                eventItem.Description = eventItem.Source + "  " + eventItem.Description;            }            _eventQueue.Enqueue(eventItem);            _writerToLog.WriteEvent(eventItem);        }        public void PostKickoutMessage(string message)        {            var eventItem = new EventItem()            {                Type = EventType.KickOut_Notify,                Description = message,                OccuringTime = DateTime.Now,            };            if (!string.IsNullOrWhiteSpace(eventItem.Source))            {                eventItem.Description = eventItem.Source + "  " + eventItem.Description;            }            _eventQueue.Enqueue(eventItem);            _writerToLog.WriteEvent(eventItem);        }        public void PostSoundMessage(string message)        {            var eventItem = new EventItem()            {                Type = EventType.Sound_Notify,                Description = message,                OccuringTime = DateTime.Now,            };            if (!string.IsNullOrWhiteSpace(eventItem.Source))            {                eventItem.Description = eventItem.Source + "  " + eventItem.Description;            }            _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.WriteExeption("事件输出失败", ex);                }            }            return true;        }        public List<EventItem> GetAlarmEvent()        {            return _alarmQueue.ToList();        }        public void ClearAlarmEvent()        {            _alarmQueue.Clear();        }        public List<EventItem> 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 PostInfoLog(string module, eEvent id, string message)        {            WriteEvent(module,id, INFORMATION_EVENT, message);        }        public void PostWarningLog(string module, string message)        {            WriteEvent(module, WARNING_EVENT , message);        }        public void PostWarningLog(string module, eEvent id, string message)        {            WriteEvent(module,id, WARNING_EVENT, message);        }        public void PostAlarmLog(string module, string message)        {            WriteEvent(module, ALARM_EVENT, message);        }        public void PostAlarmLog(string module, eEvent id, string message)        {            WriteEvent(module,id, ALARM_EVENT, message);        }    }}
 |