first commit

This commit is contained in:
2026-10-22 14:31:58 +07:00
parent 55228b3772
commit 62d33e8ef0
24 changed files with 1073 additions and 25 deletions

View File

@@ -0,0 +1,184 @@
@implements IAsyncDisposable
<MudPaper Elevation="0" Class="pa-2" Style="background:transparent;">
<MudStack Row="true" Justify="Justify.SpaceBetween" AlignItems="AlignItems.Center"
Style="cursor:pointer" @onclick="Toggle">
<MudText Typo="Typo.subtitle1">ASRS · OPC UA</MudText>
<MudIcon Icon="@(_expanded ? 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="@StatusColor" />
<div>
<MudText Typo="Typo.caption">@StatusText</MudText>
@if (S.Available && S.Online)
{
<MudText Typo="Typo.caption" Style="color:#9a9a9a;">
Homed: @Fmt(S.Homed) &nbsp;|&nbsp; Busy: @Fmt(S.Busy) &nbsp;|&nbsp; Fault: @Fmt(S.Fault)
</MudText>
}
</div>
</div>
@if (!string.IsNullOrEmpty(S.LastError))
{
<MudText Typo="Typo.caption" Style="color:#e74c3c;display:block;">@S.LastError</MudText>
}
@if (_expanded && S.Available)
{
<MudText Typo="Typo.caption" Class="sec-title mt-2">
Kho (cột × tầng) — chọn ô@(_from is null ? "" : $": {Name(_from.Value)}")@(_to is null ? "" : $" → {Name(_to.Value)}")
</MudText>
<div class="slot-grid">
@for (int shelf = AsrsOpcLayout.Shelves; shelf >= 1; shelf--)
{
<span class="slot-row-label">T@(shelf)</span>
@for (int col = 1; col <= AsrsOpcLayout.Columns; col++)
{
var cell = (col, shelf);
<button class="@CellClass(cell)" disabled="@(!S.Online)" @onclick="() => Pick(cell)">
@col.@shelf
</button>
}
}
</div>
<MudText Typo="Typo.caption" Class="sec-title mt-2">Băng tải</MudText>
<div class="op-grid-2">
<MudButton Variant="@(_conveyor == 1 ? Variant.Filled : Variant.Outlined)" Color="Color.Primary" Size="Size.Small"
OnClick="() => _conveyor = 1">BT1 · Trái</MudButton>
<MudButton Variant="@(_conveyor == 2 ? Variant.Filled : Variant.Outlined)" Color="Color.Primary" Size="Size.Small"
OnClick="() => _conveyor = 2">BT2 · Phải</MudButton>
</div>
<MudText Typo="Typo.caption" Class="sec-title mt-2">Lệnh</MudText>
<div class="op-grid">
<MudButton Variant="Variant.Outlined" Size="Size.Small" Disabled="@(!CanRetrieve)" OnClick="Retrieve">Retrieve</MudButton>
<MudButton Variant="Variant.Outlined" Size="Size.Small" Disabled="@(!CanStore)" OnClick="Store">Store</MudButton>
<MudButton Variant="Variant.Outlined" Size="Size.Small" Disabled="@(!CanMove)" OnClick="Move">Move</MudButton>
<MudButton Variant="Variant.Outlined" Size="Size.Small" Disabled="@(!CanStoreAny)" OnClick="StoreAny">StoreAny</MudButton>
<MudButton Variant="Variant.Outlined" Size="Size.Small" Disabled="@(!CanCommand)" OnClick="Home"
Color="Color.Primary" Style="grid-column:span 2;">Home</MudButton>
</div>
}
</MudPaper>
@code {
[Inject] StateService State { get; set; } = default!;
[Inject] CommandService Cmd { get; set; } = default!;
private bool _expanded = true;
private void Toggle() => _expanded = !_expanded;
private int _conveyor = 1;
private (int Col, int Shelf)? _from;
private (int Col, int Shelf)? _to;
private AsrsOpcPushDto S => State.AsrsOpc;
protected override async Task OnInitializedAsync()
{
await State.StartAsync();
State.AsrsOpcChanged += Refresh;
}
public ValueTask DisposeAsync()
{
State.AsrsOpcChanged -= Refresh;
return ValueTask.CompletedTask;
}
private void Refresh() => InvokeAsync(StateHasChanged);
// ── Status ──────────────────────────────────────────────────────────
// PLC giữ Busy=1 thêm ~3s sau mỗi lệnh để chờ lệnh kế tiếp chạy nối luôn → vẫn cho gửi lệnh.
private string StatusText =>
!S.Available ? "CHỈ CÓ TRÊN REAL STACK" :
!S.Online ? "OFFLINE" :
S.Fault == true ? "FAULT (E-STOP / SAFETY)" :
S.DriverBusy ? $"ĐANG CHẠY: {S.CurrentOp}" :
S.Busy == true ? "SẴN SÀNG · PLC CHỜ LỆNH KẾ TIẾP" :
"SẴN SÀNG";
private Color StatusColor =>
!S.Available || !S.Online ? Color.Default :
S.Fault == true ? Color.Error :
S.DriverBusy ? Color.Info :
Color.Success;
private static string Fmt(bool? v) => v switch { true => "1", false => "0", _ => "-" };
// ── Slots ───────────────────────────────────────────────────────────
private bool? Occ((int Col, int Shelf) c) => S.Occupied.ElementAtOrDefault(AsrsOpcLayout.SlotIndex(c.Col, c.Shelf));
private static string Name((int Col, int Shelf) c) => $"{c.Col}.{c.Shelf}";
private string CellClass((int Col, int Shelf) c)
{
var cls = Occ(c) switch { true => "slot full", false => "slot", _ => "slot unknown" };
if (_from == c) cls += " sel-from";
if (_to == c) cls += " sel-to";
return cls;
}
/// <summary>Click 1 = ô chính (Retrieve/Store/nguồn Move). Click 2 ô khác = ô đích Move. Click lại = bỏ chọn.</summary>
private void Pick((int Col, int Shelf) c)
{
if (_from == c) { _from = _to; _to = null; }
else if (_to == c) { _to = null; }
else if (_from is null) _from = c;
else _to = c;
}
// ── Enable rules (server vẫn kiểm tra lại) ─────────────────────────
private bool CanCommand => State.CanControl && S.Available && S.Online
&& S.Fault != true && !S.DriverBusy;
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;
private bool CanStoreAny => CanCommand && S.Occupied.Any(o => o == false);
// ── Commands ───────────────────────────────────────────────────────
private async Task Retrieve()
{
var f = _from!.Value; ClearSel();
await Cmd.AsrsOpcRetrieveAsync(f.Col, f.Shelf, _conveyor);
}
private async Task Store()
{
var f = _from!.Value; ClearSel();
await Cmd.AsrsOpcStoreAsync(f.Col, f.Shelf, _conveyor);
}
private async Task Move()
{
var f = _from!.Value; var t = _to!.Value; ClearSel();
await Cmd.AsrsOpcMoveAsync(f.Col, f.Shelf, t.Col, t.Shelf);
}
private Task StoreAny() => Cmd.AsrsOpcStoreAnyAsync(_conveyor);
private Task Home() => Cmd.AsrsOpcHomeAsync();
private void ClearSel() { _from = null; _to = null; }
}
<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:repeat(2, 1fr); gap:5px; }
.slot-grid { display:grid; grid-template-columns:28px repeat(3, 1fr); gap:4px; align-items:center; }
.slot-row-label { color:#9CA3AF; font-size:11px; text-align:center; }
.slot { height:26px; border-radius:4px; border:1px solid rgba(255,255,255,0.18);
background:transparent; color:#9a9a9a; font-size:11px; cursor:pointer; }
.slot.full { background:rgba(46,204,113,0.25); border-color:rgba(46,204,113,0.6); color:#e6e6e6; }
.slot.unknown { opacity:0.4; }
.slot:disabled { cursor:default; }
.slot.sel-from { outline:2px solid #4a9eff; outline-offset:1px; }
.slot.sel-to { outline:2px dashed #f39c12; outline-offset:1px; }
</style>

View File

@@ -92,7 +92,7 @@
<div id="p-imr" class="panel-card"><ImrPanel /></div>
<div id="p-amr" class="panel-card"><AmrPanel /></div>
<div id="p-dobot" class="panel-card"><DobotPanel /></div>
<div id="p-asrs" class="panel-card"><AsrsPanel /></div>
<div id="p-asrs" class="panel-card"><AsrsOpcPanel /></div>
</div>
@code {

View File

@@ -4,12 +4,12 @@
@RobotRow("IMR", State.Imr)
@RobotRow("AMR", State.Amr)
<div class="ias-row">
<MudChip T="string" Size="Size.Small" Color="@(!State.Asrs.IsOffline ? Color.Success : Color.Default)"
<MudChip T="string" Size="Size.Small" Color="@(State.AsrsOpc.Online ? Color.Success : Color.Default)"
Variant="Variant.Text" Style="height:22px;font-size:11px;margin:0;flex-shrink:0;">ASRS</MudChip>
<MudChip T="string" Size="Size.Small"
Color="@(State.Asrs.SAFETY_MODE == true ? Color.Success : Color.Default)"
Variant="@(State.Asrs.SAFETY_MODE == true ? Variant.Filled : Variant.Outlined)"
Style="height:22px;font-size:11px;margin:0;flex-shrink:0;">Safety</MudChip>
Color="@(State.AsrsOpc.Fault == true ? Color.Error : State.AsrsOpc.Fault == false ? Color.Success : Color.Default)"
Variant="@(State.AsrsOpc.Fault is null ? Variant.Outlined : Variant.Filled)"
Style="height:22px;font-size:11px;margin:0;flex-shrink:0;">@(State.AsrsOpc.Fault == true ? "Fault" : "Safety")</MudChip>
</div>
</div>
@@ -21,7 +21,7 @@
await State.StartAsync();
State.ImrChanged += Refresh;
State.AmrChanged += Refresh;
State.AsrsChanged += Refresh;
State.AsrsOpcChanged += Refresh;
State.WorkflowChanged += Refresh;
}
@@ -29,7 +29,7 @@
{
State.ImrChanged -= Refresh;
State.AmrChanged -= Refresh;
State.AsrsChanged -= Refresh;
State.AsrsOpcChanged -= Refresh;
State.WorkflowChanged -= Refresh;
return ValueTask.CompletedTask;
}

View File

@@ -26,13 +26,13 @@
<MudDivider Class="mt-2 mb-1" />
@* ── ASRS grid ── *@
@* ── ASRS grid — Slot.{cột}.{tầng}.Occupied từ gateway OPC UA, tầng cao nhất ở trên ── *@
<div style="display:grid;grid-template-columns:repeat(3,1fr);gap:4px;">
@foreach (var row in AsrsRows)
{
@foreach (var cell in row.Cells)
{
bool on = cell.Val == true && !State.Asrs.IsOffline;
bool on = cell.Val == true && State.AsrsOpc.Online;
<MudChip T="string" Size="Size.Small"
Color="@(on ? Color.Info : Color.Default)"
Variant="@(on ? Variant.Filled : Variant.Outlined)"
@@ -63,13 +63,13 @@
protected override async Task OnInitializedAsync()
{
await State.StartAsync();
State.AsrsChanged += Refresh;
State.AsrsOpcChanged += Refresh;
State.WorkflowChanged += Refresh;
}
public ValueTask DisposeAsync()
{
State.AsrsChanged -= Refresh;
State.AsrsOpcChanged -= Refresh;
State.WorkflowChanged -= Refresh;
return ValueTask.CompletedTask;
}
@@ -94,12 +94,13 @@
!string.IsNullOrEmpty(State.Workflow.LastError) ? "color:#e74c3c;" :
State.Workflow.IsRunning ? "color:#4a9eff;" : "color:#9CA3AF;";
private (string RowLabel, (string Label, bool? Val)[] Cells)[] AsrsRows => new[]
{
("L4", new (string, bool?)[] { ("41", State.Asrs.X41), ("42", State.Asrs.X42), ("43", State.Asrs.X43) }),
("L3", new (string, bool?)[] { ("31", State.Asrs.X31), ("32", State.Asrs.X32), ("33", State.Asrs.X33) }),
("L2", new (string, bool?)[] { ("21", State.Asrs.X21), ("22", State.Asrs.X22), ("23", State.Asrs.X23) }),
("L1", new (string, bool?)[] { ("11", State.Asrs.X11), ("12", State.Asrs.X12), ("13", State.Asrs.X13) }),
};
private (string RowLabel, (string Label, bool? Val)[] Cells)[] AsrsRows =>
Enumerable.Range(1, AsrsOpcLayout.Shelves).Reverse()
.Select(shelf => ($"T{shelf}",
Enumerable.Range(1, AsrsOpcLayout.Columns)
.Select(col => ($"{col}.{shelf}",
State.AsrsOpc.Occupied.ElementAtOrDefault(AsrsOpcLayout.SlotIndex(col, shelf))))
.ToArray()))
.ToArray();
}

View File

@@ -169,6 +169,48 @@ public class CommandService
return PostAsync($"/api/asrs/workflow/{op}", null, ct, "ASRS");
}
// ── ASRS qua OPC UA (gateway) ─────────────────────────────────────────
public Task AsrsOpcHomeAsync(CancellationToken ct = default)
=> PostAsrsOpcAsync("Home", null, "/api/asrs/opc/home", null, ct);
public Task AsrsOpcRetrieveAsync(int column, int shelf, int conveyor, CancellationToken ct = default)
=> PostAsrsOpcAsync("Retrieve", $"{column}.{shelf} → BT{conveyor}", "/api/asrs/opc/retrieve",
new { Column = column, Shelf = shelf, Conveyor = conveyor }, ct);
public Task AsrsOpcStoreAsync(int column, int shelf, int conveyor, CancellationToken ct = default)
=> PostAsrsOpcAsync("Store", $"BT{conveyor} → {column}.{shelf}", "/api/asrs/opc/store",
new { Column = column, Shelf = shelf, Conveyor = conveyor }, ct);
public Task AsrsOpcStoreAnyAsync(int conveyor, CancellationToken ct = default)
=> PostAsrsOpcAsync("StoreAny", $"BT{conveyor}", "/api/asrs/opc/store-any",
new { Conveyor = conveyor }, ct);
public Task AsrsOpcMoveAsync(int fromColumn, int fromShelf, int toColumn, int toShelf, CancellationToken ct = default)
=> PostAsrsOpcAsync("Move", $"{fromColumn}.{fromShelf} → {toColumn}.{toShelf}", "/api/asrs/opc/move",
new { FromColumn = fromColumn, FromShelf = fromShelf, ToColumn = toColumn, ToShelf = toShelf }, ct);
private async Task PostAsrsOpcAsync(string command, string? @params, string url, object? body, CancellationToken ct)
{
_log.LogAsrsOpcCommand(command, @params);
try
{
var request = new HttpRequestMessage(HttpMethod.Post, url);
if (body != null)
request.Content = JsonContent.Create(body);
var resp = await _http.SendAsync(request, ct);
if (resp.IsSuccessStatusCode) return;
string? error = null;
try { error = (await resp.Content.ReadFromJsonAsync<ApiErrorBody>(cancellationToken: ct))?.Error; }
catch { }
_log.LogAsrsOpcRejected(command, error ?? $"HTTP {(int)resp.StatusCode}");
}
catch (OperationCanceledException) { throw; }
catch (Exception ex) { _log.LogAsrsOpcRejected(command, ex.Message); }
}
private sealed record ApiErrorBody(string? Error);
// ── Workflow ──────────────────────────────────────────────────────────
public Task WorkflowX1AAsync(int times, int ng, CancellationToken ct = default)
{

View File

@@ -279,6 +279,16 @@ public class ScriptLogService : IDisposable
if (!ok) _conveyorPending = null;
}
/// <summary>
/// ASRS qua OPC UA: chỉ log lệnh gửi và lỗi bị từ chối ngay (HTTP).
/// Kết quả thực thi (hoàn thành / lỗi / timeout) đến qua notification "ASRS" từ server.
/// </summary>
public void LogAsrsOpcCommand(string command, string? @params = null)
=> Push(Line(CInfo, "INFO", "ASRS", command, _lang.T("→ gửi", "→ sent"), @params));
public void LogAsrsOpcRejected(string command, string? error)
=> Push(Line(CError, "ERR", "ASRS", command, _lang.T("✗ từ chối", "✗ rejected"), error));
// ── State handlers ────────────────────────────────────────────────────
private void OnImrChanged()

View File

@@ -12,6 +12,7 @@ public class StateService : IAsyncDisposable
public RobotStateDto Amr { get; private set; } = EmptyRobot;
public ConveyorPushDto Conveyor { get; private set; } = EmptyConveyor;
public AsrsPushDto Asrs { get; private set; } = EmptyAsrs;
public AsrsOpcPushDto AsrsOpc { get; private set; } = EmptyAsrsOpc;
public WorkflowStateDto Workflow { get; private set; } = EmptyWorkflow;
public SystemStateDto System { get; private set; } = new(false, false);
@@ -24,6 +25,7 @@ public class StateService : IAsyncDisposable
public event Action? AmrChanged;
public event Action? ConveyorChanged;
public event Action? AsrsChanged;
public event Action? AsrsOpcChanged;
public event Action? WorkflowChanged;
public event Action? SystemChanged;
public event Action? LockStateChanged;
@@ -87,6 +89,12 @@ public class StateService : IAsyncDisposable
AsrsChanged?.Invoke();
});
_hub.On<AsrsOpcPushDto>(HubContracts.AsrsOpcStateEvent, dto =>
{
AsrsOpc = dto;
AsrsOpcChanged?.Invoke();
});
_hub.On<WorkflowStateDto>(HubContracts.WorkflowStateEvent, dto =>
{
Workflow = dto;
@@ -142,6 +150,9 @@ public class StateService : IAsyncDisposable
private static readonly AsrsPushDto EmptyAsrs =
new(null, null, null, null, null, null, null, null, null, null, null, null, null, true, false, Array.Empty<string>(), null);
private static readonly AsrsOpcPushDto EmptyAsrsOpc =
new(false, false, null, null, null, null, new bool?[AsrsOpcLayout.SlotCount], false, null, null);
private static readonly WorkflowStateDto EmptyWorkflow =
new(false, null, 0, "", "", false, null, null, false, null, null, null, null, false, null, null, null);
}