51 lines
1.6 KiB
C#
51 lines
1.6 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using SSPCTester.Devices.Interfaces;
|
|
|
|
namespace SSPCTester.Devices.Drivers.Mock;
|
|
|
|
/// <summary>
|
|
/// 模拟程控电源:保存设定值,读取时返回带噪声的设定值。
|
|
/// </summary>
|
|
public sealed class MockPowerSupply : MockDeviceBase, IPowerSupply
|
|
{
|
|
private readonly Random _rng = new(2026);
|
|
private double _setVolts = 28.2;
|
|
private double _setAmps = 5.0;
|
|
private bool _output = true;
|
|
|
|
public MockPowerSupply(ILogger<MockPowerSupply> logger)
|
|
: base(logger, "电源 UDP5080 (Mock)")
|
|
{
|
|
}
|
|
|
|
public async Task SetVoltageAsync(double volts, CancellationToken ct = default)
|
|
{
|
|
await Task.Delay(10, ct).ConfigureAwait(false);
|
|
_setVolts = volts;
|
|
}
|
|
|
|
public async Task SetCurrentLimitAsync(double amps, CancellationToken ct = default)
|
|
{
|
|
await Task.Delay(10, ct).ConfigureAwait(false);
|
|
_setAmps = amps;
|
|
}
|
|
|
|
public async Task OutputAsync(bool on, CancellationToken ct = default)
|
|
{
|
|
await Task.Delay(10, ct).ConfigureAwait(false);
|
|
_output = on;
|
|
}
|
|
|
|
public Task<(double volts, double amps)> ReadAsync(CancellationToken ct = default)
|
|
{
|
|
if (!_output) return Task.FromResult((0.0, 0.0));
|
|
double v = _setVolts * (1 + (_rng.NextDouble() - 0.5) * 0.002);
|
|
double nominalCurrent = Math.Min(Math.Max(_setAmps, 0.0), 1.05);
|
|
double i = nominalCurrent * (1 + (_rng.NextDouble() - 0.5) * 0.006);
|
|
return Task.FromResult((v, i));
|
|
}
|
|
|
|
public Task<string> QueryIdentityAsync(CancellationToken ct = default) =>
|
|
Task.FromResult("UNI-T,UDP5080-100,MOCK,1.0");
|
|
}
|