Files
XplorePlane/XP.Hardware.RaySource/Services/OperationProgressCalculator.cs
T

214 lines
8.0 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 XP.Hardware.RaySource.Abstractions.Events;
using XP.Hardware.RaySource.Config;
namespace XP.Hardware.RaySource.Services
{
/// <summary>
/// 操作类型枚举 | Operation type enum
/// </summary>
public enum OperationType
{
WarmUp,
Training,
FilamentCalibration,
AutoCenter
}
/// <summary>
/// 操作进度计算结果 | Operation progress calculation result
/// </summary>
public class ProgressResult
{
/// <summary>
/// 进度百分比(0-100| Progress percentage (0-100)
/// </summary>
public int Progress { get; set; }
/// <summary>
/// 操作是否已完成 | Whether operation is completed
/// </summary>
public bool IsCompleted { get; set; }
}
/// <summary>
/// 操作进度计算器 | Operation Progress Calculator
/// 根据实时电压/电流/状态字符串估算暖机、训机、灯丝校准、自动定心的进度
/// Estimates progress for warm-up, training, filament calibration, auto-center based on real-time voltage/current/status strings
/// </summary>
public class OperationProgressCalculator
{
private readonly OperationType _operationType;
private readonly RaySourceConfig _config;
private double _maxRatio;
private int _trainingPhase = 1;
public OperationProgressCalculator(OperationType operationType, RaySourceConfig config)
{
_operationType = operationType;
_config = config ?? throw new ArgumentNullException(nameof(config));
_maxRatio = 0;
_trainingPhase = 1;
}
/// <summary>
/// 根据最新状态数据计算进度 | Calculate progress based on latest status data
/// </summary>
public ProgressResult Calculate(SystemStatusData data)
{
if (data == null) return new ProgressResult { Progress = (int)_maxRatio, IsCompleted = false };
return _operationType switch
{
OperationType.WarmUp => CalculateWarmUp(data),
OperationType.Training => CalculateTraining(data),
OperationType.FilamentCalibration => CalculateFilamentCalibration(data),
OperationType.AutoCenter => CalculateAutoCenter(data),
_ => new ProgressResult { Progress = 0, IsCompleted = false }
};
}
/// <summary>
/// 暖机进度计算 | Warm-up progress calculation
/// 参照老项目:根据电压/电流阶段性变化估算,完成条件为 WarmUpStatus 包含"完成"
/// </summary>
private ProgressResult CalculateWarmUp(SystemStatusData data)
{
double ratio = 0;
float voltage = data.ActualVoltage;
float current = data.ActualCurrent;
float maxVoltage = _config.MaxVoltage; // 225
if (current < 10 && voltage <= 50)
{
// 第一阶段:电压升到 50kV,占 25%
ratio = voltage / 50.0 * 25.0;
}
else if (current >= 10 && current <= 100 && voltage >= 49)
{
// 第二阶段:电流升到 100μA,占 25%-75%
ratio = current / 100.0 * 50.0 + 25.0;
}
else if (current >= 98 && voltage >= 55)
{
// 第三阶段:电压升到最大值,占 75%-100%
ratio = voltage / maxVoltage * 100.0;
}
if (ratio > _maxRatio) _maxRatio = ratio;
// 完成条件:code = "2"(暖机完成)且电压超过最大值的 90% | Complete: code "2" and voltage > 90% max
bool isCompleted = data.WarmUpStatus == "2" && voltage > maxVoltage * 0.9f;
if (isCompleted) _maxRatio = 100;
return new ProgressResult
{
Progress = Math.Min((int)_maxRatio, 100),
IsCompleted = isCompleted
};
}
/// <summary>
/// 训机进度计算 | Training progress calculation
/// 参照老项目:分 6 个阶段,根据电压/电流和阶段号计算进度
/// </summary>
private ProgressResult CalculateTraining(SystemStatusData data)
{
double ratio = 0;
float voltage = data.ActualVoltage;
float current = data.ActualCurrent;
float maxVoltage = _config.MaxVoltage;
float maxCurrent = _config.MaxCurrent;
switch (_trainingPhase)
{
case 1:
if (current < 10 && voltage <= 50)
ratio = voltage / 50.0 * 20.0;
if (ratio > 17) _trainingPhase = 2;
break;
case 2:
if (current >= 10 && current <= 100 && voltage >= 45)
ratio = current / 100.0 * 30.0;
if (ratio > 27) _trainingPhase = 3;
break;
case 3:
if (current >= 98 && voltage >= 60)
ratio = voltage / maxVoltage * 40.0;
if (ratio > 37) _trainingPhase = 4;
break;
case 4:
if (current >= 98 && voltage >= 98)
ratio = current / maxCurrent * 60.0;
if (ratio > 57) _trainingPhase = 5;
break;
case 5:
if (current >= 45 && voltage >= 30)
ratio = voltage / maxVoltage * 80.0;
if (ratio > 77) _trainingPhase = 6;
break;
case 6:
if (current >= 45 && voltage >= 30)
ratio = voltage / maxVoltage * 100.0;
break;
}
if (ratio > _maxRatio) _maxRatio = ratio;
// 完成条件:code = "2"(训机完成)且处于最后阶段 | Complete: code "2" and final phase
bool isCompleted = data.StartUpStatus == "2" && _trainingPhase >= 6;
if (isCompleted) _maxRatio = 100;
return new ProgressResult
{
Progress = Math.Min((int)_maxRatio, 100),
IsCompleted = isCompleted
};
}
/// <summary>
/// 灯丝校准进度计算 | Filament calibration progress calculation
/// 参照老项目:电流 / 880 * 100,完成条件为状态包含"完成"且电流 > 800
/// </summary>
private ProgressResult CalculateFilamentCalibration(SystemStatusData data)
{
double ratio = data.ActualCurrent / 880.0 * 100.0;
if (ratio > _maxRatio) _maxRatio = ratio;
// 完成条件:code = "2"(灯丝校准完成)且电流 > 800 | Complete: code "2" and current > 800
bool isCompleted = data.FilamentAdjustStatus == "2" && data.ActualCurrent > 800;
if (isCompleted) _maxRatio = 100;
return new ProgressResult
{
Progress = Math.Min((int)_maxRatio, 100),
IsCompleted = isCompleted
};
}
/// <summary>
/// 自动定心进度计算 | Auto-center progress calculation
/// 参照老项目:电压 / 220 * 100,完成条件为状态包含"完成"且电压 > 200
/// </summary>
private ProgressResult CalculateAutoCenter(SystemStatusData data)
{
double ratio = data.ActualVoltage / 220.0 * 100.0;
if (ratio > _maxRatio) _maxRatio = ratio;
// 完成条件:code = "2"(自动定心完成)且电压 > 200 | Complete: code "2" and voltage > 200
bool isCompleted = data.AutoCenterStatus == "2" && data.ActualVoltage > 200;
if (isCompleted) _maxRatio = 100;
return new ProgressResult
{
Progress = Math.Min((int)_maxRatio, 100),
IsCompleted = isCompleted
};
}
}
}