提交代码
This commit is contained in:
@@ -213,6 +213,52 @@ namespace HardwareControl.Core.HardwareServiceImpl
|
||||
public Task StartSingleFocusAsync(CancellationToken ct = default) =>
|
||||
ExecuteAsync(FocusSystemNative.focus_single_focus, "单次对焦", ct);
|
||||
|
||||
public async Task<FocusSingleResult> FocusSingleAndWaitAsync(
|
||||
TimeSpan timeout,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
var limit = timeout > TimeSpan.Zero ? timeout : TimeSpan.FromSeconds(10);
|
||||
var startedAt = DateTimeOffset.UtcNow;
|
||||
|
||||
// 动态库内部完成测距光源开关与对焦动作,并阻塞到运行结果返回;
|
||||
// 这里只在外部加一个等待上限,避免界面长时间无响应。
|
||||
var command = ExecuteRawAsync(
|
||||
FocusSystemNative.focus_single_focus,
|
||||
"单次对焦",
|
||||
ct);
|
||||
|
||||
int code;
|
||||
try
|
||||
{
|
||||
code = await command.WaitAsync(limit, ct).ConfigureAwait(false);
|
||||
}
|
||||
catch (TimeoutException)
|
||||
{
|
||||
_logger.LogWarning(
|
||||
"单次对焦在 {Timeout}s 内未返回结果,判定为失败",
|
||||
limit.TotalSeconds);
|
||||
return new FocusSingleResult(
|
||||
FocusSingleOutcome.WaitTimeout,
|
||||
null,
|
||||
DateTimeOffset.UtcNow - startedAt);
|
||||
}
|
||||
|
||||
var outcome = code switch
|
||||
{
|
||||
FocusSystemNative.Ok => FocusSingleOutcome.Completed,
|
||||
FocusSystemNative.ErrNullPtr => FocusSingleOutcome.NullHandle,
|
||||
FocusSystemNative.ErrComNotOpen => FocusSingleOutcome.ComNotOpen,
|
||||
FocusSystemNative.ErrComSendFailed => FocusSingleOutcome.DeviceFailed,
|
||||
FocusSystemNative.ErrTimeout => FocusSingleOutcome.DeviceTimeout,
|
||||
_ => FocusSingleOutcome.Unknown,
|
||||
};
|
||||
|
||||
return new FocusSingleResult(
|
||||
outcome,
|
||||
code,
|
||||
DateTimeOffset.UtcNow - startedAt);
|
||||
}
|
||||
|
||||
public Task StartContinuousFocusAsync(CancellationToken ct = default) =>
|
||||
ExecuteAsync(FocusSystemNative.focus_continuous_focus, "连续对焦", ct);
|
||||
|
||||
@@ -338,7 +384,53 @@ namespace HardwareControl.Core.HardwareServiceImpl
|
||||
.ConfigureAwait(false);
|
||||
|
||||
EnsureSuccess(handle, result, operation);
|
||||
_logger.LogInformation("对焦系统:{Operation}完成", operation);
|
||||
_logger.LogInformation("对焦系统:{Operation}命令已下发", operation);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_gate.Release();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行原生命令并返回动态库原始结果码,非 0 时不抛异常,
|
||||
/// 由调用方按业务语义处理。
|
||||
/// </summary>
|
||||
private async Task<int> ExecuteRawAsync(
|
||||
Func<IntPtr, int> command,
|
||||
string operation,
|
||||
CancellationToken ct)
|
||||
{
|
||||
await _gate.WaitAsync(ct).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
var handle = RequireHandle();
|
||||
ct.ThrowIfCancellationRequested();
|
||||
|
||||
var result = await Task.Run(
|
||||
() => command(handle),
|
||||
CancellationToken.None)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (result >= 0)
|
||||
{
|
||||
_logger.LogInformation(
|
||||
"对焦系统:{Operation}命令已返回,结果码 {Code}",
|
||||
operation,
|
||||
result);
|
||||
}
|
||||
else
|
||||
{
|
||||
var detail = FocusSystemNative.GetError(handle);
|
||||
_logger.LogWarning(
|
||||
"对焦系统:{Operation}命令返回错误码 {Code}{Detail}",
|
||||
operation,
|
||||
result,
|
||||
string.IsNullOrWhiteSpace(detail) ? string.Empty : $":{detail}");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -459,17 +551,20 @@ namespace HardwareControl.Core.HardwareServiceImpl
|
||||
return MapResult(response);
|
||||
}
|
||||
|
||||
// 非阻塞查询,未找到结果帧时继续轮询
|
||||
if (result != FocusSystemNative.ErrTimeout)
|
||||
// 未找到完整结果帧时继续轮询:底层在无数据时返回 ErrTimeout,
|
||||
// 缓冲区只有半帧时返回 ErrParseFailed,两者都视为“结果尚未就绪”。
|
||||
if (result is not (FocusSystemNative.ErrTimeout
|
||||
or FocusSystemNative.ErrParseFailed))
|
||||
{
|
||||
throw MapNativeError(handle, result, "查询对焦系统结果");
|
||||
}
|
||||
|
||||
if (Environment.TickCount64 >= deadline)
|
||||
{
|
||||
throw new HardwareException(
|
||||
return MapResult(response);
|
||||
/*throw new HardwareException(
|
||||
HardwareErrorKind.Timeout,
|
||||
$"查询对焦系统结果超时({timeoutMs}ms):未找到结果帧");
|
||||
$"查询对焦系统结果超时({timeoutMs}ms):未找到结果帧");*/
|
||||
}
|
||||
|
||||
Thread.Sleep(retryInterval);
|
||||
|
||||
Reference in New Issue
Block a user