initial commit
This commit is contained in:
208
Lab505.Client/Pages/ControlPanel.razor
Normal file
208
Lab505.Client/Pages/ControlPanel.razor
Normal file
@@ -0,0 +1,208 @@
|
||||
@implements IAsyncDisposable
|
||||
@inject HttpClient Http
|
||||
|
||||
<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">CONTROL</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 → TAKE CONTROL.", "You don't hold control. Go to CONTROL ACCESS → TAKE CONTROL.")</MudText>
|
||||
</div>
|
||||
}
|
||||
|
||||
<div class="always-row">
|
||||
<MudButton Variant="Variant.Outlined" Color="Color.Error" Size="Size.Small" FullWidth="true"
|
||||
OnClick="Cancel" Disabled="@(!IsAnyRunning)">
|
||||
Cancel
|
||||
</MudButton>
|
||||
</div>
|
||||
|
||||
@if (_expanded)
|
||||
{
|
||||
<MudText Typo="Typo.caption" Class="sec-title mt-2">Params</MudText>
|
||||
<div class="op-grid-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" />
|
||||
</div>
|
||||
|
||||
<MudText Typo="Typo.caption" Class="sec-title mt-2">Test</MudText>
|
||||
<div class="op-grid">
|
||||
<MudButton Variant="Variant.Outlined" Color="Color.Tertiary" Size="Size.Small"
|
||||
Disabled="@(IsAnyRunning || IsLockBlocked)"
|
||||
OnClick='()=>RunOp("Test", ct => Cmd.WorkflowRunCycleAsync(_times ?? 1, _ng ?? 0, ct))'>No ASRS</MudButton>
|
||||
</div>
|
||||
|
||||
<MudText Typo="Typo.caption" Class="sec-title mt-2">X_A</MudText>
|
||||
<div class="op-grid">
|
||||
<MudButton Variant="Variant.Outlined" Color="Color.Warning" Size="Size.Small" Disabled="@(!CanA(1))"
|
||||
OnClick='()=>RunOp("X1A", ct => Cmd.WorkflowX1AAsync(_times ?? 1, _ng ?? 0, ct), GoodsGoneS34(1))'>X1A</MudButton>
|
||||
<MudButton Variant="Variant.Outlined" Color="Color.Warning" Size="Size.Small" Disabled="@(!CanA(2))"
|
||||
OnClick='()=>RunOp("X2A", ct => Cmd.WorkflowX2AAsync(_times ?? 1, _ng ?? 0, ct), GoodsGoneS34(2))'>X2A</MudButton>
|
||||
<MudButton Variant="Variant.Outlined" Color="Color.Warning" Size="Size.Small" Disabled="@(!CanA(3))"
|
||||
OnClick='()=>RunOp("X3A", ct => Cmd.WorkflowX3AAsync(_times ?? 1, _ng ?? 0, ct), GoodsGoneS34(3))'>X3A</MudButton>
|
||||
</div>
|
||||
|
||||
<MudText Typo="Typo.caption" Class="sec-title mt-2">X_B</MudText>
|
||||
<div class="op-grid">
|
||||
<MudButton Variant="Variant.Outlined" Color="Color.Info" Size="Size.Small" Disabled="@(!CanB(1))"
|
||||
OnClick='()=>RunOp("X1B", ct => Cmd.WorkflowX1BAsync(_times ?? 1, _ng ?? 0, ct), GoodsGoneS12(1))'>X1B</MudButton>
|
||||
<MudButton Variant="Variant.Outlined" Color="Color.Info" Size="Size.Small" Disabled="@(!CanB(2))"
|
||||
OnClick='()=>RunOp("X2B", ct => Cmd.WorkflowX2BAsync(_times ?? 1, _ng ?? 0, ct), GoodsGoneS12(2))'>X2B</MudButton>
|
||||
<MudButton Variant="Variant.Outlined" Color="Color.Info" Size="Size.Small" Disabled="@(!CanB(3))"
|
||||
OnClick='()=>RunOp("X3B", ct => Cmd.WorkflowX3BAsync(_times ?? 1, _ng ?? 0, ct), GoodsGoneS12(3))'>X3B</MudButton>
|
||||
</div>
|
||||
}
|
||||
</MudPaper>
|
||||
|
||||
@code {
|
||||
[Inject] StateService State { get; set; } = default!;
|
||||
[Inject] CommandService Cmd { get; set; } = default!;
|
||||
[Inject] LangService Lang { get; set; } = default!;
|
||||
|
||||
private bool _expanded = true;
|
||||
void ToggleExpand() => _expanded = !_expanded;
|
||||
|
||||
private int? _times;
|
||||
private int? _ng;
|
||||
private string _myUserId = "";
|
||||
|
||||
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 Dictionary<string, CancellationTokenSource> _opCts = new();
|
||||
private Func<bool>? _goodsTaken;
|
||||
|
||||
|
||||
private bool IsAnyRunning => _runningOps.Count > 0 || State.Workflow.IsRunning;
|
||||
|
||||
private bool CanA(int col) => !IsAnyRunning && State.CanControl && !IsLockBlocked && col switch
|
||||
{
|
||||
1 => State.Asrs.X31 == true || State.Asrs.X41 == true,
|
||||
2 => State.Asrs.X32 == true || State.Asrs.X42 == true,
|
||||
3 => State.Asrs.X33 == true || State.Asrs.X43 == true,
|
||||
_ => false
|
||||
};
|
||||
|
||||
private bool CanB(int col) => !IsAnyRunning && State.CanControl && !IsLockBlocked && col switch
|
||||
{
|
||||
1 => State.Asrs.X11 == true || State.Asrs.X21 == true,
|
||||
2 => State.Asrs.X12 == true || State.Asrs.X22 == true,
|
||||
3 => State.Asrs.X13 == true || State.Asrs.X23 == true,
|
||||
_ => false
|
||||
};
|
||||
|
||||
// "Goods taken" lambdas for SAFETY_MODE check — read from cached SignalR state
|
||||
private Func<bool> GoodsGoneS34(int col) => col switch
|
||||
{
|
||||
1 => () => State.Asrs.X31 != true && State.Asrs.X41 != true,
|
||||
2 => () => State.Asrs.X32 != true && State.Asrs.X42 != true,
|
||||
_ => () => State.Asrs.X33 != true && State.Asrs.X43 != true,
|
||||
};
|
||||
|
||||
private Func<bool> GoodsGoneS12(int col) => col switch
|
||||
{
|
||||
1 => () => State.Asrs.X11 != true && State.Asrs.X21 != true,
|
||||
2 => () => State.Asrs.X12 != true && State.Asrs.X22 != true,
|
||||
_ => () => State.Asrs.X13 != true && State.Asrs.X23 != true,
|
||||
};
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
await State.StartAsync();
|
||||
State.WorkflowChanged += OnWorkflowChanged;
|
||||
State.AsrsChanged += OnAsrsChanged;
|
||||
State.LockStateChanged += OnLockChanged;
|
||||
Lang.LangChanged += OnLockChanged;
|
||||
|
||||
try
|
||||
{
|
||||
var me = await Http.GetFromJsonAsync<MeCtrlDto>("/api/auth/me");
|
||||
_myUserId = me?.UserId ?? "";
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
public ValueTask DisposeAsync()
|
||||
{
|
||||
State.WorkflowChanged -= OnWorkflowChanged;
|
||||
State.AsrsChanged -= OnAsrsChanged;
|
||||
State.LockStateChanged -= OnLockChanged;
|
||||
Lang.LangChanged -= OnLockChanged;
|
||||
CancelAll();
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
|
||||
private void OnWorkflowChanged() => InvokeAsync(StateHasChanged);
|
||||
private void OnLockChanged() => InvokeAsync(StateHasChanged);
|
||||
|
||||
private void OnAsrsChanged()
|
||||
{
|
||||
// SAFETY_MODE went off during an active operation → cancel unless goods already taken
|
||||
if (State.Asrs.SAFETY_MODE == false && IsAnyRunning)
|
||||
{
|
||||
bool goodsDone = _goodsTaken != null && _goodsTaken();
|
||||
if (!goodsDone)
|
||||
InvokeAsync(CancelAll);
|
||||
}
|
||||
InvokeAsync(StateHasChanged);
|
||||
}
|
||||
|
||||
private void Cancel() => CancelAll();
|
||||
|
||||
record MeCtrlDto(string? UserId);
|
||||
|
||||
private void CancelAll()
|
||||
{
|
||||
foreach (var cts in _opCts.Values) cts.Cancel();
|
||||
_ = Cmd.WorkflowEmergencyStopAsync();
|
||||
}
|
||||
|
||||
private void RunOp(string name, Func<CancellationToken, Task> action, Func<bool>? goodsTaken = null)
|
||||
{
|
||||
if (_runningOps.Contains(name)) return;
|
||||
_goodsTaken = goodsTaken;
|
||||
var cts = new CancellationTokenSource();
|
||||
_opCts[name] = cts;
|
||||
_ = ExecOpAsync(name, action, cts.Token);
|
||||
}
|
||||
|
||||
private async Task ExecOpAsync(string name, Func<CancellationToken, Task> action, CancellationToken token)
|
||||
{
|
||||
await InvokeAsync(() => { _runningOps.Add(name); StateHasChanged(); });
|
||||
try
|
||||
{
|
||||
await action(token);
|
||||
await InvokeAsync(StateHasChanged);
|
||||
}
|
||||
catch (OperationCanceledException) { }
|
||||
catch (Exception)
|
||||
{
|
||||
_ = Cmd.WorkflowEmergencyStopAsync();
|
||||
}
|
||||
finally
|
||||
{
|
||||
await InvokeAsync(() =>
|
||||
{
|
||||
_goodsTaken = null;
|
||||
_runningOps.Remove(name);
|
||||
if (_opCts.TryGetValue(name, out var c)) { c.Dispose(); _opCts.Remove(name); }
|
||||
StateHasChanged();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
<style>
|
||||
.sec-title { display:block; color:#9CA3AF; margin-bottom:4px; }
|
||||
.op-grid { display:grid; grid-template-columns:repeat(3, 1fr); gap:5px; }
|
||||
.op-grid-2 { display:grid; grid-template-columns:1fr 1fr; gap:5px; }
|
||||
</style>
|
||||
Reference in New Issue
Block a user