61 lines
1.9 KiB
C#
61 lines
1.9 KiB
C#
using Microsoft.Extensions.Logging;
|
||
using SSPCTester.Devices.Interfaces;
|
||
|
||
namespace SSPCTester.Devices.Drivers.Mock;
|
||
|
||
/// <summary>
|
||
/// 模拟可编程负载。
|
||
/// </summary>
|
||
public sealed class MockLoad : MockDeviceBase, ILoad
|
||
{
|
||
private readonly Random _rng = new(4096);
|
||
private LoadMode _mode = LoadMode.CC;
|
||
private double _value = 1.0;
|
||
private bool _input = true;
|
||
|
||
public MockLoad(ILogger<MockLoad> logger)
|
||
: base(logger, "负载:IT8702P+IT8733P (Mock)")
|
||
{
|
||
}
|
||
|
||
public async Task SetModeAsync(LoadMode mode, CancellationToken ct = default)
|
||
{
|
||
await Task.Delay(10, ct).ConfigureAwait(false);
|
||
_mode = mode;
|
||
}
|
||
|
||
public async Task SetValueAsync(double value, CancellationToken ct = default)
|
||
{
|
||
await Task.Delay(10, ct).ConfigureAwait(false);
|
||
_value = value;
|
||
}
|
||
|
||
public async Task InputAsync(bool on, CancellationToken ct = default)
|
||
{
|
||
await Task.Delay(10, ct).ConfigureAwait(false);
|
||
_input = on;
|
||
}
|
||
|
||
public Task<(double volts, double amps)> ReadAsync(CancellationToken ct = default)
|
||
{
|
||
if (!_input) return Task.FromResult((0.0, 0.0));
|
||
// CC 模式下电流接近设定值;其他模式简化处理
|
||
double i = _mode == LoadMode.CC ? _value : 1.0;
|
||
double v = 27.4 + (_rng.NextDouble() - 0.5) * 0.1;
|
||
i *= 1 + (_rng.NextDouble() - 0.5) * 0.01;
|
||
return Task.FromResult((v, i));
|
||
}
|
||
|
||
public Task<PowerReading> ReadPowerAsync(CancellationToken ct = default)
|
||
{
|
||
if (!_input) return Task.FromResult(new PowerReading(0.0, 0.0, 0.0));
|
||
double i = _mode == LoadMode.CC ? _value : 1.0;
|
||
double v = 27.4 + (_rng.NextDouble() - 0.5) * 0.1;
|
||
i *= 1 + (_rng.NextDouble() - 0.5) * 0.01;
|
||
return Task.FromResult(new PowerReading(v, i, v * i));
|
||
}
|
||
|
||
public Task<string> QueryIdentityAsync(CancellationToken ct = default) =>
|
||
Task.FromResult("ITECH,IT8702P,MOCK,1.0");
|
||
}
|