152 lines
4.6 KiB
C#
152 lines
4.6 KiB
C#
using System.Net.Http.Json;
|
|
|
|
namespace Lab505.Client.Services;
|
|
|
|
public sealed class ImrInitialPoseService
|
|
{
|
|
private const int OperationPollAttempts = 15;
|
|
private const int PositionPollAttempts = 20;
|
|
private const int PollDelayMs = 300;
|
|
private const double PositionTolerance = 0.1;
|
|
|
|
private readonly HttpClient _http;
|
|
private readonly ScriptLogService _log;
|
|
|
|
public ImrInitialPoseService(HttpClient http, ScriptLogService log)
|
|
{
|
|
_http = http;
|
|
_log = log;
|
|
}
|
|
|
|
public async Task<ImrInitialPoseResult> RunAsync(double x, double y, double yaw, CancellationToken ct = default)
|
|
{
|
|
_log.LogCommand("IMR", "InitPose(OPC)", isMove: true, $"x={x:F2} y={y:F2} yaw={yaw:F2}");
|
|
|
|
var operationId = await StartAsync(x, y, yaw, ct);
|
|
if (operationId is null)
|
|
return new(ImrInitialPoseOutcome.SendFailed);
|
|
|
|
var operation = await WaitForOperationAsync(operationId, ct);
|
|
if (operation.Status == "Failed")
|
|
return new(ImrInitialPoseOutcome.OperationFailed, operation.Error);
|
|
|
|
var position = await WaitForPositionAsync(x, y, ct);
|
|
return position.IsMatched
|
|
? new(ImrInitialPoseOutcome.Success, ActualX: position.ActualX, ActualY: position.ActualY)
|
|
: new(ImrInitialPoseOutcome.PositionMismatch,
|
|
ActualX: position.ActualX,
|
|
ActualY: position.ActualY,
|
|
WaitSeconds: PositionPollAttempts * PollDelayMs / 1000);
|
|
}
|
|
|
|
private async Task<string?> StartAsync(double x, double y, double yaw, CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
var response = await _http.PostAsJsonAsync("/api/imr/opc/init-pose", new { X = x, Y = y, Yaw = yaw }, ct);
|
|
if (!response.IsSuccessStatusCode)
|
|
return null;
|
|
|
|
var body = await response.Content.ReadFromJsonAsync<OperationAcceptedDto>(cancellationToken: ct);
|
|
return body?.OperationId;
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
throw;
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private async Task<OperationStatusDto> WaitForOperationAsync(string operationId, CancellationToken ct)
|
|
{
|
|
for (var i = 0; i < OperationPollAttempts; i++)
|
|
{
|
|
await Task.Delay(PollDelayMs, ct);
|
|
|
|
try
|
|
{
|
|
var operation = await _http.GetFromJsonAsync<OperationStatusDto>($"/api/operations/{operationId}", ct);
|
|
if (operation?.Status is "Completed" or "Failed")
|
|
return operation;
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
throw;
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
|
|
return new(operationId, "Completed", null);
|
|
}
|
|
|
|
private async Task<PositionCheckResult> WaitForPositionAsync(double expectedX, double expectedY, CancellationToken ct)
|
|
{
|
|
double? actualX = null;
|
|
double? actualY = null;
|
|
|
|
for (var i = 0; i < PositionPollAttempts; i++)
|
|
{
|
|
await Task.Delay(PollDelayMs, ct);
|
|
|
|
try
|
|
{
|
|
var status = await _http.GetFromJsonAsync<ImrStatusDto>("/api/imr/status", ct);
|
|
if (status is null)
|
|
continue;
|
|
|
|
actualX = status.X_Opc;
|
|
actualY = status.Y_Opc;
|
|
|
|
if (Math.Abs(status.X_Opc - expectedX) <= PositionTolerance &&
|
|
Math.Abs(status.Y_Opc - expectedY) <= PositionTolerance)
|
|
return new(true, actualX, actualY);
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
throw;
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
|
|
return new(false, actualX, actualY);
|
|
}
|
|
|
|
private sealed record OperationAcceptedDto(string OperationId);
|
|
private sealed record OperationStatusDto(string Id, string Status, string? Error);
|
|
private sealed record ImrStatusDto(
|
|
double X_Opc,
|
|
double Y_Opc,
|
|
double Theta_Opc,
|
|
string NavStatus,
|
|
string DobotStatus,
|
|
string ConveyorStatus,
|
|
double X_Vda,
|
|
double Y_Vda,
|
|
bool IsOnline,
|
|
string[] Errors);
|
|
|
|
private sealed record PositionCheckResult(bool IsMatched, double? ActualX, double? ActualY);
|
|
}
|
|
|
|
public enum ImrInitialPoseOutcome
|
|
{
|
|
Success,
|
|
SendFailed,
|
|
OperationFailed,
|
|
PositionMismatch
|
|
}
|
|
|
|
public sealed record ImrInitialPoseResult(
|
|
ImrInitialPoseOutcome Outcome,
|
|
string? Error = null,
|
|
double? ActualX = null,
|
|
double? ActualY = null,
|
|
int WaitSeconds = 0);
|