using Aitex.Core.RT.Event; using Aitex.Core.RT.SCCore; using Aitex.Core.Util; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Runtime.Serialization; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; namespace MECF.Framework.Common.Aitex.Core.RT.EMS { [DataContract] public class TimeObserver : IObserver { #region Variables protected DateTime _startDate = DateTime.Now; protected DateTime? _trigDate = null; protected double _value = 0; protected bool _isTimeOut = false; protected double _timeExceeded = 0; protected readonly R_TRIG _trigTimeOut = new(); protected double _threshold = 1000000; #endregion #region Properties [DataMember] public string Name { get; set; } [DataMember] public string Module { get; set; } [DataMember] public int Index { get; set; } [DataMember] public string Description { get; set; } [DataMember] public bool Enable { get; set; } [DataMember] public TimeUnit TimeDisplayUnit { get; set; } #region OutputOnly [Subscription("IsTrigger")] public bool IsTrigger => _isTimeOut; [Subscription("StartDate")] public DateTime StartDate => _startDate; [Subscription("Value")] /// /// 单位s /// public double Value => _value; [Subscription("Threshold")] /// /// 单位s /// public double Threshold => _threshold; [Subscription("TrigDate")] public DateTime? TrigDate => _trigDate; /// /// 超出时间,单位TimeDisplayUnit /// [Subscription("TimeExceeded")] public double TimeExceeded => _timeExceeded; [Subscription("ValueExceeded")] public virtual double ValueExceeded => _timeExceeded; [Subscription("IsProcessing")] public virtual bool IsProcessing => true; #endregion #endregion #region Constructors public TimeObserver() { } public TimeObserver(string name,string module ,int index,string description, bool enabled,DateTime startdate, double threshold, TimeUnit timedisplayunit) { Name = name; Module = module; Index = index; Description = description; Enable = enabled; TimeDisplayUnit = timedisplayunit; _threshold = threshold; _startDate = startdate; } #endregion #region Methods /// /// 计算剩余时间和超时时间,不同类算法不同,需要重载 /// public virtual void Monitor() { _trigTimeOut.CLK = _isTimeOut; if (_trigTimeOut.Q) { bool iswarning = true; if (SC.ContainsItem("System.IgnoreMaintainTimeOutWarning")) { iswarning = !SC.GetValue("System.IgnoreMaintainTimeOutWarning"); } if (iswarning) EV.PostWarningLog("Maintainance", $"Plan {Name} reach the deadline, please check as soon as possible."); else EV.PostInfoLog("Maintainance", $"Plan {Name} reach the deadline, please check as soon as possible."); } } /// /// 重新开始计时,未使能时或不满足条件无法复位 /// /// public virtual bool Reset() { if (!Enable) { EV.PostWarningLog("Maintainance", $"Plan {Name} is disable,can not be reset"); return false; } if (CanReset()) return true; else return false; } /// /// 阈值设置 /// /// 必须大于0 /// public virtual bool SetThreshold(double threshold) { if (threshold <= 0) { EV.PostWarningLog("Maintainance", $"threshold must be greater than zero"); return false; } return true; } public virtual bool SetStartDate(DateTime t) { return true; } public virtual bool CanReset() { return true; } public virtual bool SetMaintain(string parentname, string name, int itemid, DateTime date) { return true; } public virtual Dictionary> GetAllMaintainerItemsDic() { return new(); } public virtual bool SetMaintainerItemEnable(string parentname, string name, int itemid, bool enable) { return true; } public virtual void AddMaintainerItem(string parentname, MaintainerItem item) { } public virtual void RemoveMaintainerItem(string parentname, string itemname, int id) { } #endregion } }