using System; using System.Xml; using Aitex.Core.Common.DeviceData; using Aitex.Core.RT.DataCenter; using Aitex.Core.RT.Event; using Aitex.Core.RT.IOCore; using Aitex.Core.RT.SCCore; using Aitex.Core.Util; namespace Aitex.Core.RT.Device.Unit { public class IoStatistics : BaseDevice, IDevice { public string LastPMTime { get { return _scLastPMTime != null ? _scLastPMTime.Value : ""; } } public double DaysFromLastPM { get { return _scFromLastPMTime==null ? 0 : _scFromLastPMTime.Value; } set { if (_scFromLastPMTime != null) _scFromLastPMTime.Value = value; } } public double TotalDays { get { return _scTotalTime != null ? _scTotalTime.Value : 0; } set { if (_scTotalTime != null) _scTotalTime.Value = value; } } public double PMIntervalDays { get { return _scPMInterval != null ? _scPMInterval.Value : 0; } } public bool IsPMNeeded { get { return DaysFromLastPM > PMIntervalDays; } } public bool EnableAlarm { get { return _scEnableAlarm == null || _scEnableAlarm.Value; } } private DIAccessor _diMonitor = null; private SCString _scLastPMTime = null; private SCItem _scFromLastPMTime = null; private SCItem _scTotalTime = null; private SCItem _scPMInterval = null; private SCItem _scEnableAlarm = null; private double _total; private double _fromLast; private RD_TRIG _trigOnOff = new RD_TRIG(); private DeviceTimer _timerTotal =new DeviceTimer(); private DeviceTimer _timerFromLast = new DeviceTimer(); private R_TRIG _trigPMNeeded = new R_TRIG(); public IoStatistics(string module, XmlElement node) { base.Module = module; base.Name = node.GetAttribute("id"); base.Display = node.GetAttribute("display"); base.DeviceID = node.GetAttribute("schematicId"); _diMonitor = ParseDiNode("diMonitorSignal", node); _scLastPMTime = ParseScNodeString("scLastPMTime", node); _scFromLastPMTime = ParseScNodeDouble("scTimeFromLastPM", node); _scTotalTime = ParseScNodeDouble("scTimeTotal", node); _scPMInterval = ParseScNodeDouble("scPMInterval", node); _scEnableAlarm = ParseScNodeBool("scEnableAlarm", node); } public bool Initialize() { DATA.Subscribe(string.Format("Device.{0}.{1}", Module , Name), () => { AITStatisticsData data = new AITStatisticsData () { DeviceName = Name, DeviceSchematicId = DeviceID, DisplayName = Display, LastPMTime = LastPMTime, TimeFromLastPM = DaysFromLastPM, TimeTotal = TotalDays, PMInterval = PMIntervalDays, }; return data; }, SubscriptionAttribute.FLAG.IgnoreSaveDB); DEVICE.Register(String.Format("{0}.{1}", Name, AITStatisticsOperation.Reset), (out string reason, int time, object[] param) => { if (_scLastPMTime == null || _scFromLastPMTime==null) { reason = string.Format("Reset statistics of {0} failed, not define config entry", Display); return false; } reason = string.Format("Reset statistics of {0}", Display); _scLastPMTime.Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); DaysFromLastPM = 0; _fromLast = 0; _timerFromLast.Start(0); return true; }); return true; } public void Terminate() { } public void Monitor() { _trigOnOff.CLK = _diMonitor.RawData; //第一次检测到打开了,开始计时 if (_trigOnOff.R) { _total = TotalDays; _fromLast = DaysFromLastPM; _timerTotal.Start(0); _timerFromLast.Start(0); } //第一次检测到从打开到关闭状态 if (_trigOnOff.T) { } //如果开着,就更新SC if (_trigOnOff.M) { TotalDays = _total + _timerTotal.GetElapseTime()/1000/60/60/24; DaysFromLastPM = _fromLast + _timerFromLast.GetElapseTime() / 1000/60/60/24; } if (PMIntervalDays > 0) { _trigPMNeeded.CLK = IsPMNeeded; if (_trigPMNeeded.Q) { if (EnableAlarm) { EV.PostMessage(Module, EventEnum.PMNeededWarning, Display, PMIntervalDays); } else { EV.PostMessage(Module, EventEnum.PMNeededInformation, Display, PMIntervalDays); } } } } public void Reset() { _trigPMNeeded.RST = true; } } }