SSPC-Tester/SSPCTester.Devices/Drivers/Mock/MockLoad.cs
2026-07-12 12:07:41 +08:00

75 lines
2.4 KiB
C#
Raw Permalink 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 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<bool> GetInputStateAsync(CancellationToken ct = default) =>
Task.FromResult(_input);
public async Task ResetAsync(CancellationToken ct = default)
{
await Task.Delay(10, ct).ConfigureAwait(false);
_mode = LoadMode.CC;
_value = 0;
_input = false;
}
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");
public Task<string> QueryErrorAsync(CancellationToken ct = default) =>
Task.FromResult("0,\"No error\"");
}