| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 | using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.Text;using System.Threading.Tasks;using Aitex.Core.RT.Event;using Aitex.Core.RT.Log;namespace MECF.Framework.Common.Event{    public interface IAlarmHandler    {        void AlarmStateChanged(AlarmEventItem item);    }    [DataContract]    [Serializable]    public class AlarmEventItem : EventItem    {        public Func<bool> ResetChecker { get; set; }        private IAlarmHandler _alarmHandler;        private bool _ignoreAlarm;        public bool CanDoReset { get; set; }        public AlarmEventItem(string source, string name, string description, Func<bool> resetChecker, IAlarmHandler handler) : base(source, name, description,            EventLevel.Alarm, EventType.EventUI_Notify)        {            ResetChecker = resetChecker;            IsAcknowledged = true;            _alarmHandler = handler;        }        public AlarmEventItem(string source, EventItem item, Func<bool> resetChecker, IAlarmHandler handler)        {            _alarmHandler = handler;            ResetChecker = resetChecker;            item.IsAcknowledged = true;            item.Source = source;            this.Clone(item);        }        public AlarmEventItem()        {        }        public void SetIgnoreError(bool ignore)        {            if (_ignoreAlarm == ignore)                return;            _ignoreAlarm = ignore;            if (ignore)            {                EV.PostWarningLog(Source, $"{Source} {EventEnum} error will be ignored");                if (IsTriggered)                {                    IsAcknowledged = true;                    if (_alarmHandler != null)                        _alarmHandler.AlarmStateChanged(this);                }            }            else            {                Reset();            }        }        public void Reset()        {            if (_ignoreAlarm)                return;            if (!IsTriggered)                return;            if (ResetChecker == null || ResetChecker())            {                EV.PostInfoLog(Source, $"{Source} {EventEnum} is recovered");                IsAcknowledged = true;                if (_alarmHandler != null)                    _alarmHandler.AlarmStateChanged(this);            }        }        public void Set()        {            Set(null);        }        public void Set(string error)        {            if (_ignoreAlarm)                return;            if (IsTriggered)            {                //相同alarm只触发一次                if (EV.GetAlarmEvent().FirstOrDefault(x => x.EventEnum == this.EventEnum) != null)                {                    EV.ClearAlarmEvent(this.EventEnum);                }            }            if (this.EventEnum != null)            {                Description = error;                AdditionalDescription = error;            }            IsAcknowledged = false;            //OccuringTime = DateTime.Now;            Count++;            if (Count >= int.MaxValue)                Count = 0;            if (_alarmHandler != null)                _alarmHandler.AlarmStateChanged(this);        }    }}
 |