103 lines
3.2 KiB
C#
103 lines
3.2 KiB
C#
using Microsoft.AspNetCore.Components;
|
|
using Microsoft.AspNetCore.SignalR.Client;
|
|
using RobotNet10.RobotApp.Client.Shared.Devices;
|
|
|
|
namespace RobotNet10.RobotApp.Client.Services;
|
|
|
|
/// <summary>
|
|
/// SignalR client để kết nối với DeviceHub
|
|
/// </summary>
|
|
public class DeviceHubClient : IAsyncDisposable
|
|
{
|
|
private readonly HubConnection _hubConnection;
|
|
private bool _disposed;
|
|
|
|
public event Action<DeviceUpdateDto>? DeviceUpdated;
|
|
public event Action<string, DeviceStatus>? DeviceStatusChanged;
|
|
public event Action<HubConnectionState>? ConnectionStateChanged;
|
|
|
|
public bool IsConnected => _hubConnection.State == HubConnectionState.Connected;
|
|
public HubConnectionState ConnectionState => _hubConnection.State;
|
|
|
|
public DeviceHubClient(NavigationManager navigationManager)
|
|
{
|
|
var hubUrl = navigationManager.ToAbsoluteUri("/hubs/devices");
|
|
_hubConnection = new HubConnectionBuilder()
|
|
.WithUrl(hubUrl)
|
|
.WithAutomaticReconnect()
|
|
.Build();
|
|
|
|
_hubConnection.On<DeviceUpdateDto>("DeviceUpdated", update => DeviceUpdated?.Invoke(update));
|
|
_hubConnection.On<string, DeviceStatus>("DeviceStatusChanged", (deviceId, status) =>
|
|
DeviceStatusChanged?.Invoke(deviceId, status));
|
|
|
|
_hubConnection.Reconnecting += error =>
|
|
{
|
|
ConnectionStateChanged?.Invoke(_hubConnection.State);
|
|
return Task.CompletedTask;
|
|
};
|
|
|
|
_hubConnection.Reconnected += connectionId =>
|
|
{
|
|
ConnectionStateChanged?.Invoke(_hubConnection.State);
|
|
return Task.CompletedTask;
|
|
};
|
|
|
|
_hubConnection.Closed += error =>
|
|
{
|
|
ConnectionStateChanged?.Invoke(_hubConnection.State);
|
|
return Task.CompletedTask;
|
|
};
|
|
}
|
|
|
|
public async Task StartAsync()
|
|
{
|
|
if (_hubConnection.State == HubConnectionState.Disconnected)
|
|
{
|
|
await _hubConnection.StartAsync();
|
|
ConnectionStateChanged?.Invoke(_hubConnection.State);
|
|
}
|
|
}
|
|
|
|
public async Task StopAsync()
|
|
{
|
|
if (_hubConnection.State != HubConnectionState.Disconnected)
|
|
{
|
|
await _hubConnection.StopAsync();
|
|
ConnectionStateChanged?.Invoke(_hubConnection.State);
|
|
}
|
|
}
|
|
|
|
public async Task<DeviceDto[]> GetAllDevicesAsync()
|
|
{
|
|
return await _hubConnection.InvokeAsync<DeviceDto[]>("GetAllDevices");
|
|
}
|
|
|
|
public async Task<DeviceDto?> GetDeviceAsync(string deviceId)
|
|
{
|
|
return await _hubConnection.InvokeAsync<DeviceDto?>("GetDevice", deviceId);
|
|
}
|
|
|
|
public async Task<DeviceDto[]> GetDevicesByTypeAsync(DeviceType deviceType)
|
|
{
|
|
return await _hubConnection.InvokeAsync<DeviceDto[]>("GetDevicesByType", deviceType);
|
|
}
|
|
|
|
public async Task<int> GetDeviceCountAsync()
|
|
{
|
|
return await _hubConnection.InvokeAsync<int>("GetDeviceCount");
|
|
}
|
|
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
if (_disposed)
|
|
return;
|
|
|
|
_disposed = true;
|
|
await StopAsync();
|
|
await _hubConnection.DisposeAsync();
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
}
|
|
|