101 lines
2.3 KiB
C#
101 lines
2.3 KiB
C#
using System;
|
|
|
|
namespace Models;
|
|
|
|
/// <summary>
|
|
/// 状态值 (StatusValue)
|
|
/// 维护当前状态值以及上限和下限阈值。
|
|
/// </summary>
|
|
public class StatusValue
|
|
{
|
|
private PropertyValue _current;
|
|
private PropertyValue _upperThreshold;
|
|
private PropertyValue _lowerThreshold;
|
|
|
|
/// <summary>
|
|
/// 当 Current >= UpperThreshold 时触发此事件
|
|
/// </summary>
|
|
public event Action OnUpperThresholdReached;
|
|
|
|
/// <summary>
|
|
/// 当 Current <= LowerThreshold 时触发此事件
|
|
/// </summary>
|
|
public event Action OnLowerThresholdReached;
|
|
|
|
/// <summary>
|
|
/// 当前状态值
|
|
/// 修改时会自动检测阈值。
|
|
/// </summary>
|
|
public PropertyValue Current
|
|
{
|
|
get => _current;
|
|
set
|
|
{
|
|
_current = value;
|
|
CheckThresholds();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 上限阈值
|
|
/// </summary>
|
|
public PropertyValue UpperThreshold
|
|
{
|
|
get => _upperThreshold;
|
|
set
|
|
{
|
|
_upperThreshold = value;
|
|
CheckThresholds();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 下限阈值
|
|
/// </summary>
|
|
public PropertyValue LowerThreshold
|
|
{
|
|
get => _lowerThreshold;
|
|
set
|
|
{
|
|
_lowerThreshold = value;
|
|
CheckThresholds();
|
|
}
|
|
}
|
|
|
|
public StatusValue(int current = 0, int upper = 100, int lower = 0)
|
|
{
|
|
_current = new PropertyValue(current);
|
|
_upperThreshold = new PropertyValue(upper);
|
|
_lowerThreshold = new PropertyValue(lower);
|
|
}
|
|
|
|
public StatusValue(PropertyValue current, PropertyValue upper, PropertyValue lower)
|
|
{
|
|
_current = current ?? new PropertyValue(0);
|
|
_upperThreshold = upper ?? new PropertyValue(100);
|
|
_lowerThreshold = lower ?? new PropertyValue(0);
|
|
}
|
|
|
|
private void CheckThresholds()
|
|
{
|
|
if ((int)_current >= (int)_upperThreshold)
|
|
{
|
|
OnUpperThresholdReached?.Invoke();
|
|
}
|
|
|
|
if ((int)_current <= (int)_lowerThreshold)
|
|
{
|
|
OnLowerThresholdReached?.Invoke();
|
|
}
|
|
}
|
|
|
|
public void Add(int value)
|
|
{
|
|
Current += value; // 触发 setter -> CheckThresholds
|
|
}
|
|
|
|
public void Subtract(int value)
|
|
{
|
|
Current -= value; // 触发 setter -> CheckThresholds
|
|
}
|
|
} |