initial commit
This commit is contained in:
338
Lab505.Client/Services/CommandService.cs
Normal file
338
Lab505.Client/Services/CommandService.cs
Normal file
@@ -0,0 +1,338 @@
|
||||
using System.Net.Http.Json;
|
||||
|
||||
namespace Lab505.Client.Services;
|
||||
|
||||
public class CommandService
|
||||
{
|
||||
private readonly HttpClient _http;
|
||||
private readonly ScriptLogService _log;
|
||||
|
||||
public CommandService(HttpClient http, ScriptLogService log)
|
||||
{
|
||||
_http = http;
|
||||
_log = log;
|
||||
}
|
||||
|
||||
// ── IMR ───────────────────────────────────────────────────────────────
|
||||
public Task ImrMoveToAsync(string target, CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("IMR", "MoveTo", isMove: true, $"Target={target}");
|
||||
return PostAsync("/api/imr/vda/move-to", new { Target = target }, ct, "IMR");
|
||||
}
|
||||
|
||||
public Task ImrLoadAsync(CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("IMR", "Load");
|
||||
return PostAsync("/api/imr/vda/load", null, ct, "IMR");
|
||||
}
|
||||
|
||||
public Task ImrUnloadAsync(CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("IMR", "Unload");
|
||||
return PostAsync("/api/imr/vda/unload", null, ct, "IMR");
|
||||
}
|
||||
|
||||
public Task ImrCancelAsync(CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("IMR", "Cancel");
|
||||
return PostAsync("/api/imr/vda/cancel", null, ct, "IMR");
|
||||
}
|
||||
|
||||
public Task ImrPickupAsync(int ok, int ng, CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("IMR", "Pickup", @params: $"OK={ok} NG={ng}");
|
||||
return PostAsync("/api/imr/vda/pickup", new { CountOk = ok, CountNg = ng }, ct, "IMR");
|
||||
}
|
||||
|
||||
public Task ImrUndockAsync(double distance, double velocity, CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("IMR", "Undock", isMove: true, $"Dist={distance:F2} Vel={velocity:F2}");
|
||||
return PostAsync("/api/imr/vda/undock", new { Distance = distance, Velocity = velocity }, ct, "IMR");
|
||||
}
|
||||
|
||||
public Task ImrPauseAsync(CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("IMR", "Pause");
|
||||
return PostAsync("/api/imr/vda/pause", null, ct, "IMR");
|
||||
}
|
||||
|
||||
public Task ImrResumeAsync(CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("IMR", "Resume");
|
||||
return PostAsync("/api/imr/vda/resume", null, ct, "IMR");
|
||||
}
|
||||
|
||||
public Task ImrInitPositionAsync(double x, double y, double theta, CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("IMR", "InitPosition", @params: $"X={x:F2} Y={y:F2} θ={theta:F2}");
|
||||
return PostAsync("/api/imr/vda/init-position", new { X = x, Y = y, Theta = theta }, ct, "IMR");
|
||||
}
|
||||
|
||||
public Task ImrResetOpcAsync(CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("IMR", "Reset(OPC)");
|
||||
return PostAsync("/api/imr/opc/reset", null, ct, "IMR");
|
||||
}
|
||||
|
||||
// ── AMR ───────────────────────────────────────────────────────────────
|
||||
public Task AmrMoveToAsync(string target, CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("AMR", "MoveTo", isMove: true, $"Target={target}");
|
||||
return PostAsync("/api/amr/vda/move-to", new { Target = target }, ct, "AMR");
|
||||
}
|
||||
|
||||
public Task AmrLoadAsync(CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("AMR", "Load");
|
||||
return PostAsync("/api/amr/vda/load", null, ct, "AMR");
|
||||
}
|
||||
|
||||
public Task AmrUnloadAsync(CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("AMR", "Unload");
|
||||
return PostAsync("/api/amr/vda/unload", null, ct, "AMR");
|
||||
}
|
||||
|
||||
public Task AmrCancelAsync(CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("AMR", "Cancel");
|
||||
return PostAsync("/api/amr/vda/cancel", null, ct, "AMR");
|
||||
}
|
||||
|
||||
public Task AmrUndockAsync(CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("AMR", "Undock", isMove: true);
|
||||
return PostAsync("/api/amr/vda/undock", null, ct, "AMR");
|
||||
}
|
||||
|
||||
public Task AmrPauseAsync(CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("AMR", "Pause");
|
||||
return PostAsync("/api/amr/vda/pause", null, ct, "AMR");
|
||||
}
|
||||
|
||||
public Task AmrResumeAsync(CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("AMR", "Resume");
|
||||
return PostAsync("/api/amr/vda/resume", null, ct, "AMR");
|
||||
}
|
||||
|
||||
public Task AmrInitPositionAsync(double x, double y, double theta, CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("AMR", "InitPosition", @params: $"X={x:F2} Y={y:F2} θ={theta:F2}");
|
||||
return PostAsync("/api/amr/vda/init-position", new { X = x, Y = y, Theta = theta }, ct, "AMR");
|
||||
}
|
||||
|
||||
// ── Conveyor ──────────────────────────────────────────────────────────
|
||||
public async Task ConveyorSetCoilAsync(string c, string coil, bool state, CancellationToken ct = default)
|
||||
{
|
||||
_log.LogConveyorSetCoil(c, coil, state);
|
||||
var (ok, err) = await PostConveyorAsync($"/api/conveyor/{c}/{coil}", new { State = state }, ct);
|
||||
_log.LogConveyorWriteResult(c, coil, state, ok, err);
|
||||
}
|
||||
|
||||
// ── Dobot ─────────────────────────────────────────────────────────────
|
||||
public Task DobotPowerOnAsync(CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("Dobot", "PowerOn");
|
||||
return PostAsync("/api/dobot/power-on", null, ct, "Dobot");
|
||||
}
|
||||
|
||||
public Task DobotGoHomeAsync(CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("Dobot", "GoHome");
|
||||
return PostAsync("/api/dobot/go-home", null, ct, "Dobot");
|
||||
}
|
||||
|
||||
public Task DobotContinueAsync(CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("Dobot", "Continue");
|
||||
return PostAsync("/api/dobot/continue", null, ct, "Dobot");
|
||||
}
|
||||
|
||||
public Task DobotCancelAsync(CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("Dobot", "Cancel");
|
||||
return PostAsync("/api/dobot/cancel", null, ct, "Dobot");
|
||||
}
|
||||
|
||||
// ── ASRS ──────────────────────────────────────────────────────────────
|
||||
public Task AsrsHomingAsync(CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("ASRS", "Homing");
|
||||
return PostAsync("/api/asrs/homing", null, ct, "ASRS");
|
||||
}
|
||||
|
||||
public Task AsrsWorkflowAsync(string op, CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("ASRS", $"Workflow", @params: $"op={op}");
|
||||
return PostAsync($"/api/asrs/workflow/{op}", null, ct, "ASRS");
|
||||
}
|
||||
|
||||
// ── Workflow ──────────────────────────────────────────────────────────
|
||||
public Task WorkflowX1AAsync(int times, int ng, CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("WF", "X1A", @params: $"Times={times} NG={ng}");
|
||||
return PostAsync("/api/workflow/x1a", new { Times = times, Ng = ng }, ct, "WF");
|
||||
}
|
||||
|
||||
public Task WorkflowX2AAsync(int times, int ng, CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("WF", "X2A", @params: $"Times={times} NG={ng}");
|
||||
return PostAsync("/api/workflow/x2a", new { Times = times, Ng = ng }, ct, "WF");
|
||||
}
|
||||
|
||||
public Task WorkflowX3AAsync(int times, int ng, CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("WF", "X3A", @params: $"Times={times} NG={ng}");
|
||||
return PostAsync("/api/workflow/x3a", new { Times = times, Ng = ng }, ct, "WF");
|
||||
}
|
||||
|
||||
public Task WorkflowX1BAsync(int times, int ng, CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("WF", "X1B", @params: $"Times={times} NG={ng}");
|
||||
return PostAsync("/api/workflow/x1b", new { Times = times, Ng = ng }, ct, "WF");
|
||||
}
|
||||
|
||||
public Task WorkflowX2BAsync(int times, int ng, CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("WF", "X2B", @params: $"Times={times} NG={ng}");
|
||||
return PostAsync("/api/workflow/x2b", new { Times = times, Ng = ng }, ct, "WF");
|
||||
}
|
||||
|
||||
public Task WorkflowX3BAsync(int times, int ng, CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("WF", "X3B", @params: $"Times={times} NG={ng}");
|
||||
return PostAsync("/api/workflow/x3b", new { Times = times, Ng = ng }, ct, "WF");
|
||||
}
|
||||
|
||||
public Task WorkflowRunCycleAsync(int times, int ng, CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("WF", "RunCycle", @params: $"Times={times} NG={ng}");
|
||||
return PostAsync("/api/workflow/run-cycle", new { Times = times, Ng = ng }, ct, "WF");
|
||||
}
|
||||
|
||||
public Task WorkflowRunStepsAsync(IEnumerable<int> steps, int times, int ng, CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("WF", "RunSteps", @params: $"Steps=[{string.Join(",", steps)}] Times={times} NG={ng}");
|
||||
return PostAsync("/api/workflow/run-steps", new { Steps = steps, Times = times, Ng = ng }, ct, "WF");
|
||||
}
|
||||
|
||||
public Task FlowStepInitAsync(CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("WF", "Step:Init");
|
||||
return PostAsync("/api/workflow/step/init", null, ct, "WF");
|
||||
}
|
||||
|
||||
public Task FlowStepLoadC2Async(CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("WF", "Step:LoadC2");
|
||||
return PostAsync("/api/workflow/step/load-c2", null, ct, "WF");
|
||||
}
|
||||
|
||||
public Task FlowStepToCap1Async(CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("WF", "Step:ToCap1");
|
||||
return PostAsync("/api/workflow/step/to-cap1", null, ct, "WF");
|
||||
}
|
||||
|
||||
public Task FlowStepAmrToImrAsync(CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("WF", "Step:AmrToImr");
|
||||
return PostAsync("/api/workflow/step/amr-to-imr", null, ct, "WF");
|
||||
}
|
||||
|
||||
public Task FlowStepProcessAsync(int times, int ng, CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("WF", "Step:Process", @params: $"Times={times} NG={ng}");
|
||||
return PostAsync("/api/workflow/step/process", new { Times = times, Ng = ng }, ct, "WF");
|
||||
}
|
||||
|
||||
public Task FlowStepImrToAmrAsync(CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("WF", "Step:ImrToAmr");
|
||||
return PostAsync("/api/workflow/step/imr-to-amr", null, ct, "WF");
|
||||
}
|
||||
|
||||
public Task FlowStepToCap2Async(CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("WF", "Step:ToCap2");
|
||||
return PostAsync("/api/workflow/step/to-cap2", null, ct, "WF");
|
||||
}
|
||||
|
||||
public Task FlowStepUnloadC2Async(CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("WF", "Step:UnloadC2");
|
||||
return PostAsync("/api/workflow/step/unload-c2", null, ct, "WF");
|
||||
}
|
||||
|
||||
public Task WorkflowHomeRbAsync(CancellationToken ct = default)
|
||||
{
|
||||
_log.LogCommand("WF", "HomeAll");
|
||||
return PostAsync("/api/workflow/home-rb", null, ct, "WF");
|
||||
}
|
||||
|
||||
public Task WorkflowEmergencyStopAsync()
|
||||
{
|
||||
_log.LogCommand("WF", "EMERGENCY STOP");
|
||||
return PostAsync("/api/workflow/emergency-stop", null, default, "WF");
|
||||
}
|
||||
|
||||
// ── Internal ──────────────────────────────────────────────────────────
|
||||
private async Task PostAsync(string url, object? body, CancellationToken ct, string? device = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, url);
|
||||
if (body != null)
|
||||
request.Content = JsonContent.Create(body);
|
||||
var resp = await _http.SendAsync(request, ct);
|
||||
if (!resp.IsSuccessStatusCode && device != null)
|
||||
_log.CancelPending(device, $"HTTP {(int)resp.StatusCode}");
|
||||
}
|
||||
catch (OperationCanceledException) { throw; }
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (device != null) _log.CancelPending(device, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Conveyor write — trả về (success, errorMessage).</summary>
|
||||
private async Task<(bool ok, string? err)> PostConveyorAsync(string url, object? body, CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, url);
|
||||
if (body != null)
|
||||
request.Content = JsonContent.Create(body);
|
||||
var resp = await _http.SendAsync(request, ct);
|
||||
var result = await TryReadCommandResultAsync(resp, ct);
|
||||
|
||||
if (resp.IsSuccessStatusCode)
|
||||
return result is { Success: false }
|
||||
? (false, result.Error ?? "Modbus write failed")
|
||||
: (true, null);
|
||||
|
||||
return (false, result?.Error ?? $"HTTP {(int)resp.StatusCode}");
|
||||
}
|
||||
catch (OperationCanceledException) { throw; }
|
||||
catch (Exception ex) { return (false, ex.Message); }
|
||||
}
|
||||
|
||||
private static async Task<ApiCommandResult?> TryReadCommandResultAsync(HttpResponseMessage resp, CancellationToken ct)
|
||||
{
|
||||
if (resp.Content is null)
|
||||
return null;
|
||||
|
||||
try
|
||||
{
|
||||
return await resp.Content.ReadFromJsonAsync<ApiCommandResult>(cancellationToken: ct);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private sealed record ApiCommandResult(bool Success, string? Error);
|
||||
}
|
||||
Reference in New Issue
Block a user