Sic.Framework-Nanjing-Baishi/MECF.Framework.RT.Core/Managers/PDS/ProcessDataStatCounter.cs

120 lines
3.1 KiB
C#

using System;
using Aitex.Core.RT.DataCenter;
using Aitex.Core.RT.Log;
using Aitex.Core.Util;
using Sicentury.Core;
namespace MECF.Framework.RT.Core.Managers.PDS
{
/// <summary>
/// 气体流量累加器。
/// </summary>
public class ProcessDataStatCounter : BindableBase
{
#region Variables
private DateTime _lastPollingTime = DateTime.MinValue;
private readonly R_TRIG _trigDataPollingError = new();
#endregion
#region Constructors
public ProcessDataStatCounter(string name, string module)
{
Name = name;
Module = module;
DataPath = new[]
{
$"{module}.GasRealTimeFlow.{name}_Run.FeedBack",
$"{module}.GasRealTimeFlow.{name}_Vent.FeedBack",
};
}
public ProcessDataStatCounter(string name, string module, params string[] dataPath) : this(name, module)
{
DataPath = dataPath;
Total = 0;
}
public ProcessDataStatCounter(string name, string module, double initValue) : this(name, module)
{
Total = initValue;
}
public ProcessDataStatCounter(string name, string module, double initValue, params string[] dataPath) : this(name, module, dataPath)
{
Total = initValue;
}
#endregion
#region Properties
/// <summary>
/// 返回当前统计数据的名称。
/// </summary>
public string Name { get; }
/// <summary>
/// 返回当前统计数据所属的模块。
/// </summary>
public string Module { get; }
/// <summary>
/// 返回当前数据统计时所使用的数据拉取路径。
/// </summary>
public string[] DataPath { get; }
/// <summary>
/// 返回当前统计数据的累加值。
/// </summary>
public double Total { get; internal set; }
#endregion
#region Methods
/// <summary>
/// 累加气体流量。
/// </summary>
public void Accumulate()
{
var ts = DateTime.Now - _lastPollingTime;
if(_lastPollingTime == DateTime.MinValue)
ts = TimeSpan.FromSeconds(1);
foreach (var pdata in DataPath)
{
var sFlow = DATA.Poll(pdata).ToString();
if (double.TryParse(sFlow, out var dFlow))
{
Total += dFlow * ts.TotalSeconds;
_trigDataPollingError.RST = true;
}
else
{
_trigDataPollingError.CLK = true;
if (_trigDataPollingError.Q)
{
LOG.Error($"Unable to polling data {DataPath} or the data({sFlow}) is not double value.");
}
break;
}
}
_lastPollingTime = DateTime.Now;
}
public override string ToString()
{
return $"{Name}: {Total:F1}";
}
#endregion
}
}