212 lines
9.0 KiB
Plaintext
212 lines
9.0 KiB
Plaintext
@implements IAsyncDisposable
|
|
|
|
<MudPaper Elevation="0" Class="pa-3" Style="background:transparent;">
|
|
|
|
<MudStack Row="true" Justify="Justify.SpaceBetween" AlignItems="AlignItems.Center"
|
|
Style="cursor:pointer" @onclick="ToggleAMR">
|
|
<MudText Typo="Typo.subtitle1">AMR</MudText>
|
|
<MudIcon Icon="@(IsAmrExpanded ? Icons.Material.Filled.ExpandMore : Icons.Material.Filled.ExpandLess)" Size="Size.Small" />
|
|
</MudStack>
|
|
|
|
<div class="status-row mt-2">
|
|
<MudIcon Icon="@Icons.Material.Filled.Circle" Size="Size.Small" Color="@AmrMudColor" />
|
|
<div>
|
|
<MudText Typo="Typo.caption">@AmrStatus.ToUpper()</MudText>
|
|
<MudText Typo="Typo.caption" Style="color:#9a9a9a;">
|
|
Errors: @(State.Amr.Errors is { Count: > 0 } errs ? string.Join(", ", errs) : "none")
|
|
</MudText>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="mt-1 mb-1">
|
|
<MudButton FullWidth="true" Variant="Variant.Outlined" Color="Color.Error" Size="Size.Small" OnClick="Cancel_Vda" Disabled="@(!State.CanControl)">Cancel</MudButton>
|
|
</div>
|
|
|
|
@if (IsAmrExpanded)
|
|
{
|
|
<MudText Typo="Typo.caption" Class="sec-title mt-2">Move</MudText>
|
|
<div class="op-grid">
|
|
<MudButton Variant="Variant.Outlined" Size="Size.Small" OnClick="MovetoCap1" Disabled="@AmrLocked("MoveTo Cap1")">Cap1</MudButton>
|
|
<MudButton Variant="Variant.Outlined" Size="Size.Small" OnClick="MovetoCap2" Disabled="@AmrLocked("MoveTo Cap2")">Cap2</MudButton>
|
|
<MudButton Variant="Variant.Outlined" Size="Size.Small" OnClick="MovetoHome" Disabled="@AmrLocked("MoveTo Home")">Home</MudButton>
|
|
</div>
|
|
<MudText Typo="Typo.caption" Class="sec-title mt-2">Action</MudText>
|
|
<div class="op-grid">
|
|
<MudButton Variant="Variant.Outlined" Size="Size.Small" OnClick="AMR_Load" Disabled="@AmrLocked("Load")">Load</MudButton>
|
|
<MudButton Variant="Variant.Outlined" Size="Size.Small" OnClick="AMR_Unload" Disabled="@AmrLocked("Unload")">Unload</MudButton>
|
|
<MudButton Variant="Variant.Outlined" Size="Size.Small" OnClick="UndockFromStation" Disabled="@UndockBlocked()">Undock</MudButton>
|
|
</div>
|
|
<MudText Typo="Typo.caption" Class="sec-title mt-2">NAV</MudText>
|
|
<div class="op-grid-2">
|
|
<MudButton Variant="Variant.Outlined" Size="Size.Small" OnClick="Resume" Disabled="@(!State.CanControl)">Resume</MudButton>
|
|
<MudButton Variant="Variant.Outlined" Size="Size.Small" OnClick="Pause" Disabled="@(!State.CanControl)">Pause</MudButton>
|
|
</div>
|
|
<div class="op-grid-4 mt-2">
|
|
<MudNumericField T="double?" @bind-Value="_poseX" Placeholder="X" Variant="Variant.Outlined"
|
|
Format="F3" Step="0.01" Culture="@System.Globalization.CultureInfo.InvariantCulture"
|
|
HideSpinButtons="true" Class="pnl-num" />
|
|
<MudNumericField T="double?" @bind-Value="_poseY" Placeholder="Y" Variant="Variant.Outlined"
|
|
Format="F3" Step="0.01" Culture="@System.Globalization.CultureInfo.InvariantCulture"
|
|
HideSpinButtons="true" Class="pnl-num" />
|
|
<MudNumericField T="double?" @bind-Value="_poseTheta" Placeholder="θ" Variant="Variant.Outlined"
|
|
Format="F3" Step="0.01" Culture="@System.Globalization.CultureInfo.InvariantCulture"
|
|
HideSpinButtons="true" Class="pnl-num" />
|
|
<MudButton Variant="Variant.Outlined" Size="Size.Small" Color="Color.Warning"
|
|
StartIcon="@(_settingPose ? Icons.Material.Filled.HourglassBottom : null)"
|
|
OnClick="RunInitPosition" Disabled="@(_settingPose || !State.CanControl)">
|
|
@(_settingPose ? "..." : "InitPos")
|
|
</MudButton>
|
|
</div>
|
|
}
|
|
|
|
</MudPaper>
|
|
|
|
@code {
|
|
[Inject] StateService State { get; set; } = default!;
|
|
[Inject] CommandService Cmd { get; set; } = default!;
|
|
[Inject] ISnackbar Snackbar { get; set; } = default!;
|
|
[Inject] LangService Lang { get; set; } = default!;
|
|
|
|
bool IsAmrExpanded = true;
|
|
void ToggleAMR() => IsAmrExpanded = !IsAmrExpanded;
|
|
|
|
private double? _poseX;
|
|
private double? _poseY;
|
|
private double? _poseTheta;
|
|
private bool _settingPose;
|
|
private CancellationTokenSource? _initPoseCts;
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
await State.StartAsync();
|
|
State.AmrChanged += Refresh;
|
|
State.WorkflowChanged += Refresh;
|
|
Lang.LangChanged += Refresh;
|
|
}
|
|
|
|
public ValueTask DisposeAsync()
|
|
{
|
|
State.AmrChanged -= Refresh;
|
|
State.WorkflowChanged -= Refresh;
|
|
Lang.LangChanged -= Refresh;
|
|
try { _initPoseCts?.Cancel(); } catch { }
|
|
_initPoseCts?.Dispose();
|
|
_initPoseCts = null;
|
|
return ValueTask.CompletedTask;
|
|
}
|
|
|
|
private void Refresh() => InvokeAsync(StateHasChanged);
|
|
|
|
private string AmrStatus
|
|
{
|
|
get
|
|
{
|
|
var s = State.Amr;
|
|
if (!s.IsOnline) return "off";
|
|
if (s.Errors.Count > 0) return "error";
|
|
if (s.NewBaseRequest == false) return "moving";
|
|
return "ok";
|
|
}
|
|
}
|
|
|
|
private Color AmrMudColor => AmrStatus switch
|
|
{
|
|
"ok" => Color.Success,
|
|
"moving" => Color.Info,
|
|
"error" => Color.Error,
|
|
_ => Color.Default
|
|
};
|
|
|
|
private Task Cancel_Vda() => Cmd.AmrCancelAsync();
|
|
|
|
// Nút bị khóa nếu là lệnh đang chạy HOẶC lệnh đang chờ trong hàng đợi, hoặc không có quyền điều khiển
|
|
private bool AmrLocked(string label)
|
|
=> !State.CanControl || State.Workflow.AmrBusyLabel == label || State.Workflow.AmrNextLabel == label;
|
|
|
|
// Undock bị khóa khi không ở trạm dock hoặc đang bận
|
|
private bool UndockBlocked() => AmrLocked("Undock") || !AmrAtDock();
|
|
|
|
// Cảnh báo khi robot online+idle tại Home (không phải trạm dock)
|
|
private bool AmrIdleAtHome()
|
|
{
|
|
var s = State.Amr;
|
|
return s.IsOnline && s.NewBaseRequest == true
|
|
&& Near(s.X, s.Y, RobotPositions.AMR_HOME_X, RobotPositions.AMR_HOME_Y);
|
|
}
|
|
|
|
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 AmrAtDock()
|
|
{
|
|
var s = State.Amr;
|
|
return Near(s.X, s.Y, RobotPositions.AMR_CAP1_X, RobotPositions.AMR_CAP1_Y)
|
|
|| Near(s.X, s.Y, RobotPositions.AMR_CAP2_X, RobotPositions.AMR_CAP2_Y);
|
|
}
|
|
|
|
private Task MovetoCap1()
|
|
{
|
|
if (AmrAtDock() && State.Workflow.AmrCmd != "undocking")
|
|
{ Snackbar.Add(Lang.T("AMR đang docked — lùi trước khi di chuyển!", "AMR is docked — undock before moving!"), Severity.Warning); return Task.CompletedTask; }
|
|
return Cmd.AmrMoveToAsync("Cap1");
|
|
}
|
|
|
|
private Task MovetoCap2()
|
|
{
|
|
if (AmrAtDock() && State.Workflow.AmrCmd != "undocking")
|
|
{ Snackbar.Add(Lang.T("AMR đang docked — lùi trước khi di chuyển!", "AMR is docked — undock before moving!"), Severity.Warning); return Task.CompletedTask; }
|
|
return Cmd.AmrMoveToAsync("Cap2");
|
|
}
|
|
private Task MovetoHome() => Cmd.AmrMoveToAsync("Home");
|
|
private Task AMR_Load() => Cmd.AmrLoadAsync();
|
|
private Task AMR_Unload() => Cmd.AmrUnloadAsync();
|
|
private Task UndockFromStation()=> Cmd.AmrUndockAsync();
|
|
|
|
private Task Pause() => Cmd.AmrPauseAsync();
|
|
private Task Resume() => Cmd.AmrResumeAsync();
|
|
|
|
private async Task RunInitPosition()
|
|
{
|
|
_initPoseCts?.Dispose();
|
|
_initPoseCts = new CancellationTokenSource();
|
|
var ct = _initPoseCts.Token;
|
|
|
|
_settingPose = true;
|
|
await InvokeAsync(StateHasChanged);
|
|
|
|
try
|
|
{
|
|
var x = _poseX ?? 0;
|
|
var y = _poseY ?? 0;
|
|
var theta = _poseTheta ?? 0;
|
|
|
|
// VDA initPosition instant action
|
|
await Cmd.AmrInitPositionAsync(x, y, theta, ct);
|
|
Snackbar.Add(
|
|
Lang.T(
|
|
$"Đã gửi InitPosition ({x:F2}, {y:F2}, {theta:F2})",
|
|
$"InitPosition sent ({x:F2}, {y:F2}, {theta:F2})"),
|
|
Severity.Success);
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
Snackbar.Add(Lang.T("InitPosition đã huỷ", "InitPosition cancelled"), Severity.Info);
|
|
}
|
|
finally
|
|
{
|
|
_settingPose = false;
|
|
_initPoseCts?.Dispose();
|
|
_initPoseCts = null;
|
|
await InvokeAsync(StateHasChanged);
|
|
}
|
|
}
|
|
}
|
|
|
|
<style>
|
|
.status-row { display:flex; align-items:center; gap:8px; margin-bottom:4px; }
|
|
.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; }
|
|
.op-grid-4 { display:grid; grid-template-columns:repeat(4, 1fr); gap:5px; }
|
|
</style>
|