initial commit
This commit is contained in:
147
Lab505.Client/Services/StateService.cs
Normal file
147
Lab505.Client/Services/StateService.cs
Normal file
@@ -0,0 +1,147 @@
|
||||
using Lab505.Shared;
|
||||
using Microsoft.AspNetCore.SignalR.Client;
|
||||
|
||||
namespace Lab505.Client.Services;
|
||||
|
||||
public class StateService : IAsyncDisposable
|
||||
{
|
||||
private HubConnection? _hub;
|
||||
private readonly HttpClient _http;
|
||||
|
||||
public RobotStateDto Imr { get; private set; } = EmptyRobot;
|
||||
public RobotStateDto Amr { get; private set; } = EmptyRobot;
|
||||
public ConveyorPushDto Conveyor { get; private set; } = EmptyConveyor;
|
||||
public AsrsPushDto Asrs { get; private set; } = EmptyAsrs;
|
||||
public WorkflowStateDto Workflow { get; private set; } = EmptyWorkflow;
|
||||
public SystemStateDto System { get; private set; } = new(false, false);
|
||||
|
||||
public LockStateDto? LockState { get; private set; }
|
||||
|
||||
public bool CanControl => true;
|
||||
public bool IsConnected => _hub?.State == HubConnectionState.Connected;
|
||||
|
||||
public event Action? ImrChanged;
|
||||
public event Action? AmrChanged;
|
||||
public event Action? ConveyorChanged;
|
||||
public event Action? AsrsChanged;
|
||||
public event Action? WorkflowChanged;
|
||||
public event Action? SystemChanged;
|
||||
public event Action? LockStateChanged;
|
||||
public event Action<NotificationDto>? NotificationReceived;
|
||||
|
||||
/// <summary>Fires khi stack thay đổi do T3 push ModeChangedEvent — UI dùng để update nút SIM/REAL.</summary>
|
||||
public event Action<bool>? StackChanged;
|
||||
|
||||
public StateService(HttpClient http) => _http = http;
|
||||
|
||||
/// <summary>
|
||||
/// Chuyển client sang đúng SignalR group sau khi user thay đổi stack (real ↔ sim).
|
||||
/// Gọi sau khi POST /api/mode/real hoặc /api/mode/simulation thành công.
|
||||
/// </summary>
|
||||
public async Task SwitchStackAsync(bool useReal)
|
||||
{
|
||||
if (_hub?.State == HubConnectionState.Connected)
|
||||
await _hub.InvokeAsync("JoinStack", useReal);
|
||||
}
|
||||
|
||||
public async Task StartAsync()
|
||||
{
|
||||
if (_hub != null) return;
|
||||
if (!OperatingSystem.IsBrowser()) return;
|
||||
|
||||
_hub = new HubConnectionBuilder()
|
||||
.WithUrl(new Uri(_http.BaseAddress!, HubContracts.StateHubRoute))
|
||||
.WithAutomaticReconnect()
|
||||
.Build();
|
||||
|
||||
RegisterHubHandlers();
|
||||
await _hub.StartAsync();
|
||||
}
|
||||
|
||||
private void RegisterHubHandlers()
|
||||
{
|
||||
if (_hub is null)
|
||||
return;
|
||||
|
||||
_hub.On<RobotStateDto>(HubContracts.ImrStateEvent, dto =>
|
||||
{
|
||||
Imr = dto;
|
||||
ImrChanged?.Invoke();
|
||||
});
|
||||
|
||||
_hub.On<RobotStateDto>(HubContracts.AmrStateEvent, dto =>
|
||||
{
|
||||
Amr = dto;
|
||||
AmrChanged?.Invoke();
|
||||
});
|
||||
|
||||
_hub.On<ConveyorPushDto>(HubContracts.ConveyorStateEvent, dto =>
|
||||
{
|
||||
Conveyor = dto;
|
||||
ConveyorChanged?.Invoke();
|
||||
});
|
||||
|
||||
_hub.On<AsrsPushDto>(HubContracts.AsrsStateEvent, dto =>
|
||||
{
|
||||
Asrs = dto;
|
||||
AsrsChanged?.Invoke();
|
||||
});
|
||||
|
||||
_hub.On<WorkflowStateDto>(HubContracts.WorkflowStateEvent, dto =>
|
||||
{
|
||||
Workflow = dto;
|
||||
WorkflowChanged?.Invoke();
|
||||
});
|
||||
|
||||
_hub.On<SystemStateDto>(HubContracts.SystemStateEvent, dto =>
|
||||
{
|
||||
System = dto;
|
||||
SystemChanged?.Invoke();
|
||||
});
|
||||
|
||||
_hub.On<NotificationDto>(HubContracts.NotificationEvent, dto =>
|
||||
{
|
||||
NotificationReceived?.Invoke(dto);
|
||||
});
|
||||
|
||||
_hub.On<LockStateDto>(HubContracts.LockStateEvent, dto =>
|
||||
{
|
||||
LockState = dto;
|
||||
LockStateChanged?.Invoke();
|
||||
});
|
||||
|
||||
// T3 đổi mode server-side → server push event → client tự JoinStack đúng group + notify UI
|
||||
_hub.On<bool>(HubContracts.ModeChangedEvent, async useReal =>
|
||||
{
|
||||
await SwitchStackAsync(useReal);
|
||||
StackChanged?.Invoke(useReal);
|
||||
});
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_hub != null)
|
||||
{
|
||||
await _hub.DisposeAsync();
|
||||
_hub = null;
|
||||
}
|
||||
}
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────
|
||||
|
||||
public bool AsrsIsLocked(string key) => Asrs.LockedOps.Contains(key);
|
||||
|
||||
// ── Defaults ─────────────────────────────────────────────────
|
||||
|
||||
private static readonly RobotStateDto EmptyRobot =
|
||||
new(0, 0, false, null, new(), Array.Empty<ActionStateDto>(), "", 9999, Array.Empty<RobotErrorDto>());
|
||||
|
||||
private static readonly ConveyorPushDto EmptyConveyor =
|
||||
new(null, null, null, null, null, null, null, null, null, null, null, null, null);
|
||||
|
||||
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 WorkflowStateDto EmptyWorkflow =
|
||||
new(false, null, 0, "", "", false, null, null, false, null, null, null, null, false, null, null, null);
|
||||
}
|
||||
Reference in New Issue
Block a user