Files
BQP/srcs/RobotNet10/RobotApp/RobotNet10.RobotApp.Client/Clients/MarkerDetectorHubClient.cs
2026-07-13 09:25:40 +07:00

78 lines
2.2 KiB
C#

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.SignalR.Client;
using RobotNet10.Shared;
using RobotNet10.Shared.Detection;
using RobotNet10.Shared.Geometry;
namespace RobotNet10.RobotApp.Client.Clients;
public class MarkerDetectorHubClient : IAsyncDisposable
{
private readonly HubConnection _hubConnection;
private bool _disposed;
public bool IsConnected => _hubConnection.State == HubConnectionState.Connected;
public HubConnectionState ConnectionState => _hubConnection.State;
public MarkerDetectorHubClient(NavigationManager navigationManager)
{
var hubUrl = navigationManager.ToAbsoluteUri("hubs/marker-detect");
_hubConnection = new HubConnectionBuilder()
.WithUrl(hubUrl)
.WithAutomaticReconnect()
.Build();
_hubConnection.Reconnecting += error =>
{
return Task.CompletedTask;
};
_hubConnection.Reconnected += connectionId =>
{
return Task.CompletedTask;
};
_hubConnection.Closed += error =>
{
return Task.CompletedTask;
};
}
public async Task StartAsync()
{
if (_hubConnection.State == HubConnectionState.Disconnected)
{
await _hubConnection.StartAsync();
}
}
public async Task StopAsync()
{
if (_hubConnection.State != HubConnectionState.Disconnected)
{
await _hubConnection.StopAsync();
}
}
public async Task<MessageResult<Guid>> CreateSessionAsync(MarkersSearchRequest request)
{
return await _hubConnection.InvokeAsync<MessageResult<Guid>>("CreateSession", request);
}
public async Task<MessageResult<Pose>> GetGoalAsync(Guid sessionId)
{
return await _hubConnection.InvokeAsync<MessageResult<Pose>>("GetGoal", sessionId);
}
public async ValueTask DisposeAsync()
{
if (_disposed)
return;
_disposed = true;
await StopAsync();
await _hubConnection.DisposeAsync();
GC.SuppressFinalize(this);
}
}