123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- using DocumentFormat.OpenXml.Bibliography;
- using MECF.Framework.Common.Beckhoff.IOAxis;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MECF.Framework.Common.TwinCat
- {
- public class BeckhoffCounterValue
- {
- #region 常量
- private const string COUNTER_VALUE = "CounterValue";
- private const double MAX_COUNTS = 4294967296.0;
- #endregion
- #region 内部变量
- /// <summary>
- /// counter对象
- /// </summary>
- private BeckhoffCounter _counter;
- /// <summary>
- /// 数值
- /// </summary>
- private double _value;
- /// <summary>
- /// 启动时间
- /// </summary>
- private DateTime _startTime = DateTime.Now;
- /// <summary>
- /// 时长
- /// </summary>
- private TimeSpan _elapsedTime = TimeSpan.Zero;
- /// <summary>
- /// 上一次计数
- /// </summary>
- private int _previousCounts { get; set; }
- /// <summary>
- /// 首次
- /// </summary>
- private bool first = true;
- private bool _resetCounter = false;
- /// <summary>
- /// 上一次数值
- /// </summary>
- private DateTime _lastTime= DateTime.Now;
- /// <summary>
- /// 定时器
- /// </summary>
- private System.Timers.Timer _timer;
- /// <summary>
- /// 定时器记录的数值
- /// </summary>
- private int _timerCounter = 0;
- #endregion
- #region 事件
- public event BeckhoffDelegate.OnUpdateVariableCounterValue OnUpdateVariableCounterValue;
- #endregion
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="counter"></param>
- public BeckhoffCounterValue(BeckhoffCounter counter)
- {
- _counter = counter;
- if (counter.Mode != 0)
- {
- _timer = new System.Timers.Timer();
- _timer.Interval = 5000;
- _timer.Elapsed += Timer_Elapsed;
- _timer.Start();
- }
- }
- /// <summary>
- /// 定时器
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
- {
- if (_timerCounter == _previousCounts)
- {
- if (OnUpdateVariableCounterValue != null)
- {
- string key = _counter.Name;
- string inputName = $"{key}.{COUNTER_VALUE}";
- OnUpdateVariableCounterValue(inputName, 0);
- }
- }
- else
- {
- _timerCounter = _previousCounts;
- }
- }
- /// <summary>
- /// 启动
- /// </summary>
- public void Start()
- {
- }
- /// <summary>
- /// 停止
- /// </summary>
- public void Stop()
- {
- }
- /// <summary>
- /// 重置
- /// </summary>
- public void Reset()
- {
- _resetCounter = true;
- _value = 0;
- _previousCounts = 0;
- BeckhoffCounterManager.Instance.SetCounterValue($"{_counter.Name}.{COUNTER_VALUE}", 0);
- }
- /// <summary>
- /// 设置数值
- /// </summary>
- /// <param name="value"></param>
- public void SetValue(int value)
- {
- if (_counter.Mode == 0)
- {
- SetNormalValue(value);
- }
- else if (_counter.Mode == 1)
- {
- SetRateValue(value);
- }
- else if(_counter.Mode==2)
- {
- SetTotalValue(value);
- }
- }
- /// <summary>
- /// 设置常规数值
- /// </summary>
- /// <param name="value"></param>
- private void SetNormalValue(int value)
- {
- if (!string.IsNullOrEmpty(_counter.Scaling))
- {
- var result = ScalingManager.Instance.CalculateValueByTwincatVariable($"{_counter.Name}.{COUNTER_VALUE}", value);
- if (result.Item1)
- {
- _value = result.Item2;
- }
- else
- {
- _value = value;
- }
- }
- else
- {
- _value = value;
- }
- }
- /// <summary>
- /// 设置比例数值
- /// </summary>
- /// <param name="value"></param>
- private void SetRateValue(int value)
- {
- _lastTime = DateTime.Now;
- double scaledValue = 0.0;
- double currentCounts = 0.0;
- double tDelta = ElapsedTime();
- double currentCounter = _value;
- if(value==0)
- {
- _value = 0;
- _previousCounts = 0;
- return;
- }
- if (_previousCounts == 0)
- {
- _previousCounts = value;
- _value = 0;
- StartTimer();
- return;
- }
- if (tDelta*1000>_counter.Period)
- {
- if (value < _previousCounts)
- currentCounts = (MAX_COUNTS - _previousCounts) + value;
- else
- currentCounts = value - _previousCounts;
- if (!string.IsNullOrEmpty(_counter.Scaling))
- {
- var result = ScalingManager.Instance.CalculateValueByTwincatVariable($"{_counter.Name}.{COUNTER_VALUE}", (currentCounts / tDelta));
- if (result.Item1)
- {
- double rate = result.Item2;
- if(first||rate!=scaledValue)
- {
- first = false;
- scaledValue = rate;
- }
- }
- else
- {
- scaledValue=currentCounter/tDelta;
- }
- }
- else
- {
- scaledValue = currentCounter / tDelta;
- }
- _previousCounts = value;
- StartTimer();
- }
- else
- {
- scaledValue = currentCounter;
- }
- _value = scaledValue;
- }
- /// <summary>
- /// 设置统计数值
- /// </summary>
- /// <param name="value"></param>
- private void SetTotalValue(int value)
- {
- double scaledValue = this._value;
- double deltaValue;
- if (!_resetCounter && !first)
- {
- int deltaCount = unchecked(value - _previousCounts);
- var result = ScalingManager.Instance.CalculateValueByTwincatVariable($"{_counter.Name}.{COUNTER_VALUE}", deltaCount);
- if (result.Item1)
- {
- deltaValue= result.Item2;
- }
- else
- {
- deltaValue = deltaCount;
- }
- }
- else
- {
- _resetCounter = false;
- first = false;
- deltaValue = 0.0;
- }
- _previousCounts = value;
- scaledValue += deltaValue;
- _value = scaledValue;
- }
- /// <summary>
- /// 获取数值
- /// </summary>
- /// <returns></returns>
- public double GetValue()
- {
- if(_counter.Mode==1)
- {
- if (DateTime.Now.Subtract(_lastTime).TotalMilliseconds > _counter.Period)
- {
- _value = 0;
- }
- }
- return _value;
- }
- /// <summary>
- /// 启动计时
- /// </summary>
- private void StartTimer()
- {
- _startTime = DateTime.Now;
- _elapsedTime = TimeSpan.Zero;
- }
- /// <summary>
- /// 计算时长
- /// </summary>
- /// <returns></returns>
- private double ElapsedTime()
- {
- _elapsedTime = DateTime.Now - _startTime;
- return _elapsedTime.TotalSeconds;
- }
- }
- }
|