@implements IAsyncDisposable OPERATIONS @if (IsLockBlocked) {
@Lang.T("Bạn chưa giữ quyền điều khiển. Vào CONTROL ACCESS → ACQUIRE CONTROL.", "You don't hold control. Go to CONTROL ACCESS → ACQUIRE CONTROL.")
} Cancel @(_opQueue.Count > 0 ? $"+{_opQueue.Count}" : "") @if (_expanded) { Params
[@_selectedSteps.Count]
Steps
Home All @OpCard(1, "Init") @OpCard(2, "C2 AMR") @OpCard(3, "→ Cap1") @OpCard(4, "C:AMR→IMR") @OpCard(5, "→ABB:PINKUP") @OpCard(6, "C:IMR→AMR") @OpCard(7, "→ Cap2") @OpCard(8, "AMR C2")
}
@code { [Inject] StateService State { get; set; } = default!; [Inject] CommandService Cmd { get; set; } = default!; [Inject] ISnackbar Snackbar { get; set; } = default!; [Inject] HttpClient Http { get; set; } = default!; [Inject] LangService Lang { get; set; } = default!; [Inject] ScriptLogService Log { get; set; } = default!; private bool _expanded = true; private void ToggleExpand() => _expanded = !_expanded; private int? _times, _ng; private int _prevCycles = -1; private string _myUserId = ""; // True when using real stack and lock state shows we don't hold workflow lock private bool IsLockBlocked => !State.System.IsSimulation && State.LockState is not null && (State.LockState.Workflow is null || State.LockState.Workflow.UserId != _myUserId); private readonly HashSet _runningOps = new(); private readonly Queue<(string Name, Func Action)> _opQueue = new(); private CancellationTokenSource? _cts; private bool _queueRunning; private readonly List _selectedSteps = new(); private bool IsAnyRunning => _queueRunning || State.Workflow.IsRunning; private bool IsRunning(string k) => _runningOps.Contains(k); private bool IsQueued(string k) => _opQueue.Any(q => q.Name == k); private void ToggleStep(int n) { if (!_selectedSteps.Remove(n)) _selectedSteps.Add(n); } private Task ExecStep(int n, CancellationToken ct) => n switch { 1 => Cmd.FlowStepInitAsync(ct), 2 => Cmd.FlowStepLoadC2Async(ct), 3 => Cmd.FlowStepToCap1Async(ct), 4 => Cmd.FlowStepAmrToImrAsync(ct), 5 => Cmd.FlowStepProcessAsync(_times ?? 1, _ng ?? 0, ct), 6 => Cmd.FlowStepImrToAmrAsync(ct), 7 => Cmd.FlowStepToCap2Async(ct), 8 => Cmd.FlowStepUnloadC2Async(ct), _ => Task.CompletedTask }; private RenderFragment OpCard(int num, string name) => __builder => { var key = $"step{num}"; bool running = IsRunning(key); bool queued = IsQueued(key); int selIdx = _selectedSteps.IndexOf(num); bool sel = selIdx >= 0; var color = running ? Color.Primary : sel ? Color.Success : Color.Default; var variant = (running || sel) ? Variant.Filled : Variant.Outlined; @if (sel && !running) { @(selIdx + 1) } @(running ? "…" : name) }; protected override async Task OnInitializedAsync() { await State.StartAsync(); State.WorkflowChanged += OnWorkflowChanged; State.LockStateChanged += OnLockChanged; Lang.LangChanged += OnLockChanged; try { var me = await Http.GetFromJsonAsync("/api/auth/me"); _myUserId = me?.UserId ?? ""; } catch { } } public ValueTask DisposeAsync() { State.WorkflowChanged -= OnWorkflowChanged; State.LockStateChanged -= OnLockChanged; Lang.LangChanged -= OnLockChanged; HardCancel(); return ValueTask.CompletedTask; } private void OnWorkflowChanged() { var cycles = State.Workflow.CompletedCycles; if (_prevCycles >= 0 && cycles > _prevCycles) Snackbar.Add(Lang.T($"Hoàn thành chu kỳ {cycles}", $"Completed cycle {cycles}"), Severity.Success); _prevCycles = cycles; InvokeAsync(StateHasChanged); } private void OnLockChanged() => InvokeAsync(StateHasChanged); record MeWfDto(string? UserId); private void RunAll() { _selectedSteps.Clear(); WarnStep(1); // online check EnqueueOp("Run All", ct => Cmd.WorkflowRunCycleAsync(_times ?? 1, _ng ?? 0, ct)); } private void RunSelected() { if (_selectedSteps.Count == 0 || IsAnyRunning) return; var steps = _selectedSteps.ToList(); _selectedSteps.Clear(); foreach (var n in steps) WarnStep(n); EnqueueOp("Run Sel", async ct => { foreach (var n in steps) { ct.ThrowIfCancellationRequested(); await ExecStep(n, ct); } }); } private void RunOp(string n, Func a) => EnqueueOp(n, a); // ── Position detection (mirrors SystemCondition server-side logic) ─────────── private static bool Near(double x, double y, double tx, double ty) => Math.Abs(x - tx) < RobotPositions.POS_TOL && Math.Abs(y - ty) < RobotPositions.POS_TOL; private bool ImrAtC1 => Near(State.Imr.X, State.Imr.Y, RobotPositions.IMR_C1_X, RobotPositions.IMR_C1_Y); private bool AmrAtCap1 => Near(State.Amr.X, State.Amr.Y, RobotPositions.AMR_CAP1_X, RobotPositions.AMR_CAP1_Y); private bool AmrAtCap2 => Near(State.Amr.X, State.Amr.Y, RobotPositions.AMR_CAP2_X, RobotPositions.AMR_CAP2_Y); /// Ghi cảnh báo ra log + snackbar nếu pre-condition của step chưa đạt. private void WarnStep(int n) { if (!State.Imr.IsOnline) PosWarn("IMR", "offline", Lang.T("Kiểm tra kết nối IMR", "Check IMR connection")); if (!State.Amr.IsOnline) PosWarn("AMR", "offline", Lang.T("Kiểm tra kết nối AMR", "Check AMR connection")); switch (n) { case 2: // C2 AMR — AMR cần ở Cap2 if (!AmrAtCap2) PosWarn("AMR", Lang.T("chưa ở Cap2", "not at Cap2"), Lang.T("→ chạy Step 1 Init trước", "→ run Step 1 Init first")); break; case 3: // → Cap1 — AMR cần ở Cap2 để undock if (!AmrAtCap2) PosWarn("AMR", Lang.T("chưa ở Cap2", "not at Cap2"), Lang.T("→ chạy Step 1 Init hoặc Step 8 trước", "→ run Step 1 Init or Step 8 first")); break; case 4: // C:AMR→IMR — IMR@C1 + AMR@Cap1 if (!ImrAtC1) PosWarn("IMR", Lang.T("chưa ở C1", "not at C1"), Lang.T("→ chạy Step 1 Init trước", "→ run Step 1 Init first")); if (!AmrAtCap1) PosWarn("AMR", Lang.T("chưa ở Cap1", "not at Cap1"), Lang.T("→ chạy Step 3 → Cap1 trước", "→ run Step 3 → Cap1 first")); break; case 5: // ABB:PICKUP — IMR cần ở C1 trước khi undock if (!ImrAtC1) PosWarn("IMR", Lang.T("chưa ở C1", "not at C1"), Lang.T("→ chạy Step 1 Init trước", "→ run Step 1 Init first")); break; case 6: // C:IMR→AMR — IMR@C1 + AMR@Cap1 if (!ImrAtC1) PosWarn("IMR", Lang.T("chưa ở C1", "not at C1"), Lang.T("→ chạy Step 1 Init trước", "→ run Step 1 Init first")); if (!AmrAtCap1) PosWarn("AMR", Lang.T("chưa ở Cap1", "not at Cap1"), Lang.T("→ chạy Step 4 C:AMR→IMR hoặc Step 3 trước", "→ run Step 4 or Step 3 first")); break; case 7: // → Cap2 — AMR cần ở Cap1 để undock if (!AmrAtCap1) PosWarn("AMR", Lang.T("chưa ở Cap1", "not at Cap1"), Lang.T("→ chạy Step 3 → Cap1 trước", "→ run Step 3 → Cap1 first")); break; case 8: // AMR C2 — AMR cần ở Cap2 if (!AmrAtCap2) PosWarn("AMR", Lang.T("chưa ở Cap2", "not at Cap2"), Lang.T("→ chạy Step 7 → Cap2 trước", "→ run Step 7 → Cap2 first")); break; } } private void PosWarn(string device, string condition, string guide) => Log.Log(LogFormat.Line(LogFormat.CWarn, "WARN", device, condition, guide)); private void EnqueueOp(string name, Func action) { if (IsAnyRunning || IsQueued(name)) return; _opQueue.Enqueue((name, action)); if (!_queueRunning) { _cts?.Dispose(); _cts = new CancellationTokenSource(); _ = RunQueueAsync(_cts.Token); } } private void HardCancel() { _opQueue.Clear(); _cts?.Cancel(); _ = Cmd.WorkflowEmergencyStopAsync(); } private async Task RunQueueAsync(CancellationToken token) { _queueRunning = true; try { while (_opQueue.Count > 0 && !token.IsCancellationRequested) { (string name, Func action)? item = null; await InvokeAsync(() => { if (_opQueue.Count > 0) item = _opQueue.Dequeue(); }); if (item == null) break; await ExecAsync(item.Value.name, item.Value.action, token); } } finally { _queueRunning = false; await InvokeAsync(StateHasChanged); } } private async Task ExecAsync(string name, Func action, CancellationToken token) { await InvokeAsync(() => { _runningOps.Add(name); StateHasChanged(); }); try { await action(token); await InvokeAsync(StateHasChanged); } catch (OperationCanceledException) { } catch { _ = Cmd.WorkflowEmergencyStopAsync(); } finally { await InvokeAsync(() => { _runningOps.Remove(name); StateHasChanged(); }); } } }