diff --git a/.gitignore b/.gitignore
index 547a964..2728a62 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,4 @@ bin/
obj/
.vs/
*.user
+Lab505/certs/https.pfx
diff --git a/Lab505.Client/Pages/AsrsOpcPanel.razor b/Lab505.Client/Pages/AsrsOpcPanel.razor
index 7434bb5..4bcb990 100644
--- a/Lab505.Client/Pages/AsrsOpcPanel.razor
+++ b/Lab505.Client/Pages/AsrsOpcPanel.razor
@@ -21,6 +21,14 @@
+ @if (S.Available)
+ {
+
+ Cancel
+
+ }
+
@if (!string.IsNullOrEmpty(S.LastError))
{
@S.LastError
@@ -137,6 +145,9 @@
private bool CanCommand => State.CanControl && S.Available && S.Online
&& S.Fault != true && !S.DriverBusy;
+ // Cancel bấm được cả khi đang chạy / PLC Busy (lệnh có thể do client khác gửi)
+ private bool CanCancel => State.CanControl && S.Available && S.Online;
+
private bool CanRetrieve => CanCommand && _from is { } f && _to is null && Occ(f) == true;
private bool CanStore => CanCommand && _from is { } f && _to is null && Occ(f) == false;
private bool CanMove => CanCommand && _from is { } f && _to is { } t && Occ(f) == true && Occ(t) == false;
@@ -163,6 +174,7 @@
private Task StoreAny() => Cmd.AsrsOpcStoreAnyAsync(_conveyor);
private Task Home() => Cmd.AsrsOpcHomeAsync();
+ private Task Cancel() => Cmd.AsrsOpcCancelAsync();
private void ClearSel() { _from = null; _to = null; }
}
diff --git a/Lab505.Client/Services/CommandService.cs b/Lab505.Client/Services/CommandService.cs
index cf53161..ec4765d 100644
--- a/Lab505.Client/Services/CommandService.cs
+++ b/Lab505.Client/Services/CommandService.cs
@@ -170,6 +170,9 @@ public class CommandService
}
// ── ASRS qua OPC UA (gateway) ─────────────────────────────────────────
+ public Task AsrsOpcCancelAsync(CancellationToken ct = default)
+ => PostAsrsOpcAsync("Cancel", null, "/api/asrs/opc/cancel", null, ct);
+
public Task AsrsOpcHomeAsync(CancellationToken ct = default)
=> PostAsrsOpcAsync("Home", null, "/api/asrs/opc/home", null, ct);
diff --git a/Lab505/Infrastructure/Drivers/Asrs/AsrsOpcDriver.cs b/Lab505/Infrastructure/Drivers/Asrs/AsrsOpcDriver.cs
index 8fb3ed2..239ee1e 100644
--- a/Lab505/Infrastructure/Drivers/Asrs/AsrsOpcDriver.cs
+++ b/Lab505/Infrastructure/Drivers/Asrs/AsrsOpcDriver.cs
@@ -35,6 +35,36 @@ public sealed class AsrsOpcDriver(AsrsOpcState state, AsrsOpcOptions options, IN
public event Action? OnOperationStateChanged;
+ private CancellationTokenSource? _opCts;
+
+ ///
+ /// Huỷ lệnh: gọi method Cancel() của gateway (gateway ngừng chờ + hạ coil trigger),
+ /// rồi ngừng chờ phía Lab505. Gọi được cả khi lệnh do client khác gửi (vd RoboLab).
+ ///
+ public async Task CancelAsync(CancellationToken ct = default)
+ {
+ string? gatewayError = null;
+ if (state.Nodes?.Cancel is { } cancelNode)
+ {
+ try
+ {
+ var outputs = await _ua.CallCheckedAsync(AsrsNodeIds.MethodOwner, cancelNode, [], ct);
+ if (outputs.Count > 0 && ToStatus(outputs[0]) is { } code && !StatusCode.IsGood(code))
+ gatewayError = $"gateway báo lỗi khi huỷ: {StatusCode.LookupSymbolicId(code)}";
+ }
+ catch (Exception ex) { gatewayError = $"không gọi được Cancel trên gateway: {ex.Message}"; }
+ }
+ else
+ {
+ gatewayError = "gateway chưa có method Cancel — chỉ ngừng chờ phía Lab505";
+ }
+
+ _opCts?.Cancel();
+ notif.Notify("ASRS", "Cancel", gatewayError is null ? "warning" : "error", gatewayError ?? "đã huỷ lệnh");
+ if (gatewayError is not null && state.Nodes?.Cancel is not null)
+ throw new InvalidOperationException(gatewayError);
+ }
+
// ═══════════════════════════════════════════════════════════════
// Guards — public để API kiểm tra trước và trả lỗi ngay cho client
// ═══════════════════════════════════════════════════════════════
@@ -143,6 +173,9 @@ public sealed class AsrsOpcDriver(AsrsOpcState state, AsrsOpcOptions options, IN
if (!await _gate.WaitAsync(0, ct))
throw new InvalidOperationException($"ASRS đang thực hiện '{CurrentOp}' — chờ lệnh hiện tại xong");
+ using var opCts = CancellationTokenSource.CreateLinkedTokenSource(ct);
+ _opCts = opCts;
+ ct = opCts.Token;
try
{
validate(); // kiểm tra lại sau khi giữ gate (state có thể vừa đổi)
@@ -174,10 +207,11 @@ public sealed class AsrsOpcDriver(AsrsOpcState state, AsrsOpcOptions options, IN
notif.Notify("ASRS", CurrentOp ?? label, "success", "hoàn thành");
return outputs;
}
- catch (OperationCanceledException)
+ catch (Exception ex) when (ex is OperationCanceledException || ct.IsCancellationRequested)
{
- notif.Notify("ASRS", CurrentOp ?? label, "warning", "ngừng chờ (phần cứng không dừng — dùng E-Stop nếu cần)");
- throw;
+ // Cancel: client OPC UA bỏ lời gọi đang chờ (BadRequestInterrupted) — báo gọn là "đã huỷ"
+ LastError = $"{CurrentOp ?? label}: đã huỷ (Cancel)";
+ throw new OperationCanceledException(LastError, ex, ct);
}
catch (Exception ex)
{
@@ -187,6 +221,7 @@ public sealed class AsrsOpcDriver(AsrsOpcState state, AsrsOpcOptions options, IN
}
finally
{
+ _opCts = null;
SetCurrentOp(null);
_gate.Release();
}
@@ -211,6 +246,13 @@ public sealed class AsrsOpcDriver(AsrsOpcState state, AsrsOpcOptions options, IN
}
}
+ private static uint? ToStatus(object? v) => v switch
+ {
+ int i => unchecked((uint)i),
+ uint u => u,
+ _ => null
+ };
+
private static void EnsureGood(string label, IList