using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.SignalR.Client;
using RobotNet10.RobotApp.Client.Shared.Devices;
using RobotNet10.Shared.Sensor;
namespace RobotNet10.RobotApp.Client.Clients;
///
/// SignalR client để kết nối với InertialMeasurementUnitHub
///
public class InertialMeasurementUnitHubClient : IAsyncDisposable
{
private readonly HubConnection _hubConnection;
private bool _disposed;
public bool IsConnected => _hubConnection.State == HubConnectionState.Connected;
public HubConnectionState ConnectionState => _hubConnection.State;
public InertialMeasurementUnitHubClient(NavigationManager navigationManager)
{
var hubUrl = navigationManager.ToAbsoluteUri("/hubs/devices/inertialmeasurementunit");
_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 GetImuDataAsync(string deviceId)
{
var result = await _hubConnection.InvokeAsync("GetImuData", deviceId);
return result ?? new Imu();
}
public async Task ReadAllDataAsync(string deviceId)
{
var result = await _hubConnection.InvokeAsync("ReadAllData", deviceId);
return result ?? new Imu();
}
public async Task GetDeviceInfoAsync(string deviceId)
{
return await _hubConnection.InvokeAsync("GetDeviceInfo", deviceId);
}
public async Task?> GetDevicePropertiesAsync(string deviceId)
{
return await _hubConnection.InvokeAsync?>("GetDeviceProperties", deviceId);
}
public async Task?> GetDevicePropertyDescriptionsAsync(string deviceId)
{
return await _hubConnection.InvokeAsync?>("GetDevicePropertyDescriptions", deviceId);
}
public async ValueTask DisposeAsync()
{
if (_disposed)
return;
_disposed = true;
await StopAsync();
await _hubConnection.DisposeAsync();
GC.SuppressFinalize(this);
}
}