Files
YuPu-OrpaonEMS/OrpaonEMS.Core/Model/TranceCmdValue.cs
2025-02-28 22:23:13 +08:00

82 lines
2.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrpaonEMS.Core.Model
{
/// <summary>
/// 追踪指令值的模型
/// </summary>
public class TranceCmdValue
{
/// <summary>
/// 实例化函数
/// </summary>
/// <param name="changeThreshold"></param>
public TranceCmdValue(double changeThreshold)
{
ChangeThreshold = changeThreshold;
}
/// <summary>
/// 变化的阀值
/// </summary>
public double ChangeThreshold { get; set; }
/// <summary>
/// 事件
/// </summary>
public event EventHandler<double> CmdValueChanged;
private double _CmdValue;
/// <summary>
/// 指令值
/// 可实时赋值
/// </summary>
public double CmdValue
{
get { return _CmdValue; }
set
{
//发送0代表是待机此时不需要看变化了防止上一个值在0附近没有触发变化还是输出一个靠近0的值出去
if (value == 0 && value != _CmdValue)//value != _CmdValue 防止一直0值导致不停是Invoke事件
{
_CmdValue = value;
//超过变化的阀值,可以触发动作 BeginInvoke 换 Invoke 可能导致问题
CmdValueChanged.Invoke(this, 0);
return;
}
if (value != _CmdValue && GetChange(value, _CmdValue))
{
_CmdValue = value;
//超过变化的阀值,可以触发动作
CmdValueChanged.Invoke(this, value);
}
}
}
/// <summary>
/// 判断是否超过某个阀值数据
/// </summary>
/// <param name="newValue"></param>
/// <param name="oldValue"></param>
/// <returns></returns>
private bool GetChange(double newValue, double oldValue)
{
if (Math.Abs(newValue - oldValue) >= ChangeThreshold)
{
return true;
}
return false;
}
}
}