using Aitex.Core.RT.Event; using Aitex.Core.RT.SCCore; using Aitex.Core.Util; using DocumentFormat.OpenXml.EMMA; using DocumentFormat.OpenXml.VariantTypes; using DocumentFormat.OpenXml.Wordprocessing; using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel.PeerResolvers; using System.Text; using System.Threading.Tasks; namespace MECF.Framework.Common.Aitex.Core.RT.EMS { public class Maintainer : IMaintainer { #region Variables /// /// 触发保养等级是否为Warning /// protected bool _isWarning = false; /// /// 确认监控部件消耗中 /// protected Func _getProcessing = ()=> true; /// /// 保养触发器相关信息 /// protected MaintainerInfo _info = new(); /// /// 开始时间 /// protected DateTime _startDate = DateTime.Now; /// /// 阈值到达时间 /// protected DateTime? _trigDate = null; /// /// 记录值 /// protected double _value = 0; /// /// 记录剩余值 /// protected double _valueElapsed = 0; /// /// 记录超出值 /// protected double _valueExceeded = 0; /// /// 触发标志 /// protected bool _isTimeOut = false; /// /// 触发后超出时间 /// protected double _timeExceeded = 0; /// /// 触发器上升沿 /// protected readonly R_TRIG _trigTimeOut = new(); /// /// 记录值门槛 /// protected double _threshold = 1000000; /// /// 保养项集合 /// protected MaintainerItemCollection _itemCollection = new(); /// /// 未点检保养项个数 /// protected int _unMaintainedCount => _itemCollection.UnMaintainCount; protected int _totalCount => _itemCollection.Count; protected bool _isProcessing = false; #endregion public delegate bool Save(); public event Save OnSave; #region Constructors public Maintainer() { SC.RegisterValueChangedCallback("System.IgnoreMaintainTimeOutWarning", (newValue) => { bool.TryParse(newValue.ToString(), out _isWarning); }); _itemCollection = new MaintainerItemCollection(); } public Maintainer(MaintainerItemCollection item, string name, string module, int index, string description, bool enabled, DateTime startdate, double threshold, TimeUnit timedisplayunit) { SC.RegisterValueChangedCallback("System.IgnoreMaintainTimeOutWarning", (newValue) => { bool.TryParse(newValue.ToString(), out _isWarning); }); _itemCollection = item; _threshold = threshold; _startDate = startdate; _info = new MaintainerInfo(_itemCollection.GetAllItemInfos(), name, module, index, description, enabled, startdate, threshold, timedisplayunit); } public Maintainer(MaintainerItemCollection item, MaintainerInfo info, DateTime startdate, double threshold) { SC.RegisterValueChangedCallback("System.IgnoreMaintainTimeOutWarning", (newValue) => { bool.TryParse(newValue.ToString(), out _isWarning); }); _itemCollection = item; _threshold = threshold; _startDate = startdate; _info = new MaintainerInfo(_itemCollection.GetAllItemInfos(), info, startdate, threshold); } #endregion #region Function /// /// 触发OnSave委托 /// /// public virtual bool Reset() { OnSave?.Invoke(); return true; } /// /// 阈值设置 /// /// /// /// public virtual bool SetThreshold(double threshold ,params object[] args) { if (threshold <= 0) { EV.PostWarningLog("Maintenance", $"threshold must be greater than zero"); return false; } return true; } /// /// 计算剩余时间和超时时间,不同类算法不同,需要重载 /// public virtual void Monitor() { _isProcessing = _getProcessing?.Invoke() ?? false; _trigTimeOut.CLK = _isTimeOut; if (_trigTimeOut.Q) { if (_isWarning) EV.PostWarningLog("Maintenance", $"Plan {_info.Name} reach the deadline, please check as soon as possible."); else EV.PostInfoLog("Maintenance", $"Plan {_info.Name} reach the deadline, please check as soon as possible."); } } /// /// 更新Info的数据 /// public virtual void UpdateInfo() { _info.StartDate = _startDate; _info.IsTrigger = _isTimeOut; _info.IsProcessing = _isProcessing; _info.TrigDate = _trigDate; _info.UnMaintainedCount = _unMaintainedCount; _info.TotalCount = _totalCount; _info.TimeExceeded = DateTimeHelper.SecondsTransform(_timeExceeded, _info.TimeDisplayUnit); } /// /// 更新Info的数据 /// public void SortItemInfo() { _itemCollection.SortInfo(); _info.ReShapeItemHandShake = DateTime.Now; } public virtual bool SetStartDate(DateTime t) { return true; } public virtual bool CanReset() { return _unMaintainedCount == 0; } public List GetAllMaintainerItems() { return _itemCollection.GetAllItems(); } public bool SetMaintain(string itemuid, DateTime date) { if (_itemCollection.SetMaintain(itemuid, date)) { return true; } return false; } public bool SetMaintainerItemEnable(string itemuid, bool enable) { if (_itemCollection.SetMaintainerItemEnable(itemuid, enable)) { _info.ReShapeItemHandShake = DateTime.Now; return true; } return false; } public void AddMaintainerItem(MaintainerItem item) { _itemCollection.AddMaintainerItem(item); } public void RemoveMaintainerItem(string uid) { if (_itemCollection.RemoveMaintainerItem(uid)) { _info.ReShapeItemHandShake = DateTime.Now; } } public MaintainerInfo GetInfo() { return _info; } public bool EqualsNameModule(string type, string name,string module) { return _info.EqualsTypeNameModule(type, name, module); } #endregion } }