initial commit
This commit is contained in:
320
Lab505.Client/Pages/FlowPhasePanel.razor
Normal file
320
Lab505.Client/Pages/FlowPhasePanel.razor
Normal file
@@ -0,0 +1,320 @@
|
||||
@implements IAsyncDisposable
|
||||
|
||||
<MudPaper Elevation="0" Class="pa-2" Style="background:transparent;">
|
||||
|
||||
<MudStack Row="true" Justify="Justify.SpaceBetween" AlignItems="AlignItems.Center"
|
||||
Style="cursor:pointer" @onclick="ToggleExpand">
|
||||
<MudText Typo="Typo.subtitle1">OPERATIONS</MudText>
|
||||
<MudIcon Icon="@(_expanded ? Icons.Material.Filled.ExpandMore : Icons.Material.Filled.ExpandLess)" Size="Size.Small" />
|
||||
</MudStack>
|
||||
|
||||
@if (IsLockBlocked)
|
||||
{
|
||||
<div style="background:rgba(255,100,0,0.08);border:1px solid rgba(255,100,0,0.25);border-radius:5px;padding:4px 8px;margin-bottom:4px;">
|
||||
<MudText Typo="Typo.caption" Style="color:#f90;">@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.")</MudText>
|
||||
</div>
|
||||
}
|
||||
|
||||
<MudButton Variant="Variant.Outlined" Color="Color.Error" Size="Size.Small"
|
||||
FullWidth="true" Class="my-1"
|
||||
OnClick="HardCancel"
|
||||
Disabled="@(!IsAnyRunning && _opQueue.Count == 0)">
|
||||
<MudIcon Icon="@Icons.Material.Filled.Stop" Size="Size.Small" /> Cancel @(_opQueue.Count > 0 ? $"+{_opQueue.Count}" : "")
|
||||
</MudButton>
|
||||
|
||||
@if (_expanded)
|
||||
{
|
||||
<MudText Typo="Typo.caption" Class="sec-title mt-2">Params</MudText>
|
||||
<div class="op-grid-4 mb-2">
|
||||
<MudNumericField T="int?" @bind-Value="_times" Placeholder="OK" Variant="Variant.Outlined" Class="pnl-num" Min="1" Max="99" />
|
||||
<MudNumericField T="int?" @bind-Value="_ng" Placeholder="NG" Variant="Variant.Outlined" Class="pnl-num" Min="0" Max="99" />
|
||||
<MudButton Variant="Variant.Filled" Color="Color.Success" Size="Size.Small"
|
||||
StartIcon="@Icons.Material.Filled.PlayArrow" OnClick="RunAll"
|
||||
Disabled="@(IsAnyRunning || IsLockBlocked)"></MudButton>
|
||||
<MudButton Variant="Variant.Outlined" Color="Color.Success" Size="Size.Small"
|
||||
OnClick="RunSelected"
|
||||
Disabled="@(_selectedSteps.Count == 0 || IsAnyRunning || IsLockBlocked)">[@_selectedSteps.Count]</MudButton>
|
||||
</div>
|
||||
|
||||
<MudText Typo="Typo.caption" Class="sec-title">Steps</MudText>
|
||||
<div class="op-grid-3">
|
||||
<MudButton Variant="Variant.Outlined" Color="Color.Warning" Size="Size.Small"
|
||||
OnClick='() => RunOp("HomeRb", ct => Cmd.WorkflowHomeRbAsync(ct))'
|
||||
Disabled="@(IsAnyRunning || IsLockBlocked)">Home All</MudButton>
|
||||
@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")
|
||||
</div>
|
||||
}
|
||||
</MudPaper>
|
||||
|
||||
@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<string> _runningOps = new();
|
||||
private readonly Queue<(string Name, Func<CancellationToken, Task> Action)> _opQueue = new();
|
||||
private CancellationTokenSource? _cts;
|
||||
private bool _queueRunning;
|
||||
private readonly List<int> _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;
|
||||
|
||||
<MudButton Variant="@variant" Color="@color" Size="Size.Small"
|
||||
Disabled="@(IsAnyRunning)"
|
||||
Style="@(queued ? "opacity:0.55;border-style:dashed;" : "")"
|
||||
OnClick="() => { if (!IsAnyRunning) ToggleStep(num); }">
|
||||
@if (sel && !running)
|
||||
{
|
||||
<span class="sel-n">@(selIdx + 1)</span>
|
||||
}
|
||||
@(running ? "…" : name)
|
||||
</MudButton>
|
||||
};
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
await State.StartAsync();
|
||||
State.WorkflowChanged += OnWorkflowChanged;
|
||||
State.LockStateChanged += OnLockChanged;
|
||||
Lang.LangChanged += OnLockChanged;
|
||||
|
||||
try
|
||||
{
|
||||
var me = await Http.GetFromJsonAsync<MeWfDto>("/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<CancellationToken, Task> 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);
|
||||
|
||||
/// <summary>Ghi cảnh báo ra log + snackbar nếu pre-condition của step chưa đạt.</summary>
|
||||
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<CancellationToken, Task> 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<CancellationToken, Task> 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<CancellationToken, Task> 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(); }); }
|
||||
}
|
||||
}
|
||||
|
||||
<style>
|
||||
.sec-title { display:block; color:#9CA3AF; margin-bottom:4px; }
|
||||
.op-grid-4 { display:grid; grid-template-columns:repeat(4, 1fr); gap:5px; }
|
||||
.op-grid-3 { display:grid; grid-template-columns:repeat(3, 1fr); gap:5px; }
|
||||
.op-grid-2 { display:grid; grid-template-columns:1fr 1fr; gap:5px; }
|
||||
.mt-1 { margin-top:5px; }
|
||||
.sel-n { display:inline-flex; align-items:center; justify-content:center;
|
||||
width:14px; height:14px; border-radius:50%;
|
||||
background:rgba(0,0,0,0.25); font-size:9px; font-weight:900;
|
||||
margin-right:4px; flex-shrink:0; }
|
||||
</style>
|
||||
Reference in New Issue
Block a user