using System;
namespace Models;
///
/// 状态值 (StatusValue)
/// 维护当前状态值以及上限和下限阈值。
///
public class StatusValue
{
private PropertyValue _current;
private PropertyValue _upperThreshold;
private PropertyValue _lowerThreshold;
///
/// 当 Current >= UpperThreshold 时触发此事件
///
public event Action OnUpperThresholdReached;
///
/// 当 Current <= LowerThreshold 时触发此事件
///
public event Action OnLowerThresholdReached;
///
/// 当前状态值
/// 修改时会自动检测阈值。
///
public PropertyValue Current
{
get => _current;
set
{
_current = value;
CheckThresholds();
}
}
///
/// 上限阈值
///
public PropertyValue UpperThreshold
{
get => _upperThreshold;
set
{
_upperThreshold = value;
CheckThresholds();
}
}
///
/// 下限阈值
///
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
}
}