323 lines
11 KiB
C#
323 lines
11 KiB
C#
using Microsoft.AspNetCore.Components;
|
|
using Microsoft.AspNetCore.SignalR.Client;
|
|
using RobotNet10.RobotApp.Client.Shared.Devices;
|
|
|
|
namespace RobotNet10.RobotApp.Client.Clients;
|
|
|
|
/// <summary>
|
|
/// SignalR client để kết nối với CiA402ServoHub
|
|
/// </summary>
|
|
public class CiA402ServoHubClient : IAsyncDisposable
|
|
{
|
|
private readonly HubConnection _hubConnection;
|
|
private bool _disposed;
|
|
|
|
public bool IsConnected => _hubConnection.State == HubConnectionState.Connected;
|
|
public HubConnectionState ConnectionState => _hubConnection.State;
|
|
|
|
public CiA402ServoHubClient(NavigationManager navigationManager)
|
|
{
|
|
var hubUrl = navigationManager.ToAbsoluteUri("/hubs/devices/cia402servo");
|
|
_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<CiA402ServoDataDto> GetServoDataAsync(string deviceId)
|
|
{
|
|
var result = await _hubConnection.InvokeAsync<CiA402ServoDataDto?>("GetServoData", deviceId);
|
|
return result ?? new CiA402ServoDataDto();
|
|
}
|
|
|
|
public async Task<DeviceInfoDto?> GetDeviceInfoAsync(string deviceId)
|
|
{
|
|
return await _hubConnection.InvokeAsync<DeviceInfoDto?>("GetDeviceInfo", deviceId);
|
|
}
|
|
|
|
// State Machine Control Methods
|
|
public async Task EnableOperationAsync(string deviceId)
|
|
{
|
|
await _hubConnection.InvokeAsync("EnableOperationAsync", deviceId);
|
|
}
|
|
|
|
public async Task DisableOperationAsync(string deviceId)
|
|
{
|
|
await _hubConnection.InvokeAsync("DisableOperationAsync", deviceId);
|
|
}
|
|
|
|
public async Task QuickStopAsync(string deviceId)
|
|
{
|
|
await _hubConnection.InvokeAsync("QuickStopAsync", deviceId);
|
|
}
|
|
|
|
public async Task FaultResetAsync(string deviceId)
|
|
{
|
|
await _hubConnection.InvokeAsync("FaultResetAsync", deviceId);
|
|
}
|
|
|
|
public async Task ShutdownAsync(string deviceId)
|
|
{
|
|
await _hubConnection.InvokeAsync("ShutdownAsync", deviceId);
|
|
}
|
|
|
|
public async Task SwitchOnAsync(string deviceId)
|
|
{
|
|
await _hubConnection.InvokeAsync("SwitchOnAsync", deviceId);
|
|
}
|
|
|
|
public async Task EnableAsync(string deviceId)
|
|
{
|
|
await _hubConnection.InvokeAsync("EnableAsync", deviceId);
|
|
}
|
|
|
|
public async Task DisableAsync(string deviceId)
|
|
{
|
|
await _hubConnection.InvokeAsync("DisableAsync", deviceId);
|
|
}
|
|
|
|
// Operation Mode
|
|
public async Task SetOperationModeAsync(string deviceId, string mode)
|
|
{
|
|
await _hubConnection.InvokeAsync("SetOperationModeAsync", deviceId, mode);
|
|
}
|
|
|
|
// Position Control
|
|
public async Task SetTargetPositionAsync(string deviceId, int position)
|
|
{
|
|
await _hubConnection.InvokeAsync("SetTargetPositionAsync", deviceId, position);
|
|
}
|
|
|
|
public async Task MoveToPositionAsync(string deviceId, int position, uint velocity = 1000, uint acceleration = 5000, uint deceleration = 5000)
|
|
{
|
|
await _hubConnection.InvokeAsync("MoveToPositionAsync", deviceId, position, velocity, acceleration, deceleration);
|
|
}
|
|
|
|
// Velocity Control
|
|
public async Task SetTargetVelocityAsync(string deviceId, int velocity)
|
|
{
|
|
await _hubConnection.InvokeAsync("SetTargetVelocityAsync", deviceId, velocity);
|
|
}
|
|
|
|
public async Task TargetVelocityAsync(string deviceId, int targetVelocity, uint acceleration = 5000, uint deceleration = 5000)
|
|
{
|
|
await _hubConnection.InvokeAsync("TargetVelocityAsync", deviceId, targetVelocity, acceleration, deceleration);
|
|
}
|
|
|
|
public async Task ProfileVelocityAsync(string deviceId, int targetVelocity, uint acceleration = 5000, uint deceleration = 5000)
|
|
{
|
|
await _hubConnection.InvokeAsync("ProfileVelocityAsync", deviceId, targetVelocity, acceleration, deceleration);
|
|
}
|
|
|
|
// Torque Control
|
|
public async Task SetTargetTorqueAsync(string deviceId, short torque)
|
|
{
|
|
await _hubConnection.InvokeAsync("SetTargetTorqueAsync", deviceId, torque);
|
|
}
|
|
|
|
public async Task RunTorqueAsync(string deviceId, short torque)
|
|
{
|
|
await _hubConnection.InvokeAsync("RunTorqueAsync", deviceId, torque);
|
|
}
|
|
|
|
// Profile Settings
|
|
public async Task SetProfileAccelerationAsync(string deviceId, uint acceleration)
|
|
{
|
|
await _hubConnection.InvokeAsync("SetProfileAccelerationAsync", deviceId, acceleration);
|
|
}
|
|
|
|
public async Task SetProfileDecelerationAsync(string deviceId, uint deceleration)
|
|
{
|
|
await _hubConnection.InvokeAsync("SetProfileDecelerationAsync", deviceId, deceleration);
|
|
}
|
|
|
|
public async Task SetProfileVelocityAsync(string deviceId, uint velocity)
|
|
{
|
|
await _hubConnection.InvokeAsync("SetProfileVelocityAsync", deviceId, velocity);
|
|
}
|
|
|
|
public async Task SetProfileSpeedAsync(string deviceId, uint velocity)
|
|
{
|
|
await _hubConnection.InvokeAsync("SetProfileSpeedAsync", deviceId, velocity);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ghi cùng lúc cả 3 profile (speed, acceleration, deceleration) xuống drive.
|
|
/// </summary>
|
|
public async Task SetProfileSettingsAsync(string deviceId, uint profileSpeed, uint profileAcceleration, uint profileDeceleration)
|
|
{
|
|
await _hubConnection.InvokeAsync("SetProfileSettingsAsync", deviceId, profileSpeed, profileAcceleration, profileDeceleration);
|
|
}
|
|
|
|
// Homing
|
|
public async Task SetHomingMethodAsync(string deviceId, byte method)
|
|
{
|
|
await _hubConnection.InvokeAsync("SetHomingMethodAsync", deviceId, method);
|
|
}
|
|
|
|
public async Task SetHomingSpeedAsync(string deviceId, int speed)
|
|
{
|
|
await _hubConnection.InvokeAsync("SetHomingSpeedAsync", deviceId, speed);
|
|
}
|
|
|
|
public async Task SetHomingOffsetAsync(string deviceId, int offset)
|
|
{
|
|
await _hubConnection.InvokeAsync("SetHomingOffsetAsync", deviceId, offset);
|
|
}
|
|
|
|
public async Task StartHomingAsync(string deviceId, byte method, int speed)
|
|
{
|
|
await _hubConnection.InvokeAsync("StartHomingAsync", deviceId, method, speed);
|
|
}
|
|
|
|
/// <summary>Đọc lại homing method từ drive để kiểm tra đã ghi xuống chưa.</summary>
|
|
public async Task<byte> GetHomingMethodAsync(string deviceId)
|
|
{
|
|
return await _hubConnection.InvokeAsync<byte>("GetHomingMethodAsync", deviceId);
|
|
}
|
|
|
|
/// <summary>Đọc lại homing speed từ drive để kiểm tra đã ghi xuống chưa.</summary>
|
|
public async Task<int> GetHomingSpeedAsync(string deviceId)
|
|
{
|
|
return await _hubConnection.InvokeAsync<int>("GetHomingSpeedAsync", deviceId);
|
|
}
|
|
|
|
/// <summary>Đọc lại homing offset từ drive để kiểm tra đã ghi xuống chưa.</summary>
|
|
public async Task<int> GetHomingOffsetAsync(string deviceId)
|
|
{
|
|
return await _hubConnection.InvokeAsync<int>("GetHomingOffsetAsync", deviceId);
|
|
}
|
|
|
|
// Position Control - Additional Methods
|
|
public async Task StartPositionMoveAsync(string deviceId)
|
|
{
|
|
await _hubConnection.InvokeAsync("StartPositionMoveAsync", deviceId);
|
|
}
|
|
|
|
public async Task WaitUntilAtTargetAsync(string deviceId, int tolerance = 100, bool useStatusword = true, int checkIntervalMs = 10)
|
|
{
|
|
await _hubConnection.InvokeAsync("WaitUntilAtTargetAsync", deviceId, tolerance, useStatusword, checkIntervalMs);
|
|
}
|
|
|
|
// Error and Status Information
|
|
public async Task<bool> IsInFaultStateAsync(string deviceId)
|
|
{
|
|
return await _hubConnection.InvokeAsync<bool>("IsInFaultStateAsync", deviceId);
|
|
}
|
|
|
|
public async Task<byte> GetErrorRegisterAsync(string deviceId)
|
|
{
|
|
return await _hubConnection.InvokeAsync<byte>("GetErrorRegisterAsync", deviceId);
|
|
}
|
|
|
|
public async Task<ushort[]> GetErrorHistoryAsync(string deviceId)
|
|
{
|
|
return await _hubConnection.InvokeAsync<ushort[]>("GetErrorHistoryAsync", deviceId);
|
|
}
|
|
|
|
public async Task<ushort> GetLatestErrorCodeAsync(string deviceId)
|
|
{
|
|
return await _hubConnection.InvokeAsync<ushort>("GetLatestErrorCodeAsync", deviceId);
|
|
}
|
|
|
|
public async Task<bool> TryFaultResetAsync(string deviceId)
|
|
{
|
|
return await _hubConnection.InvokeAsync<bool>("TryFaultResetAsync", deviceId);
|
|
}
|
|
|
|
public async Task<bool> IsEnabledAsync(string deviceId)
|
|
{
|
|
return await _hubConnection.InvokeAsync<bool>("IsEnabledAsync", deviceId);
|
|
}
|
|
|
|
public async Task<bool> IsReadyAsync(string deviceId)
|
|
{
|
|
return await _hubConnection.InvokeAsync<bool>("IsReadyAsync", deviceId);
|
|
}
|
|
|
|
// Statusword & Controlword
|
|
public async Task<ushort> GetStatuswordAsync(string deviceId)
|
|
{
|
|
return await _hubConnection.InvokeAsync<ushort>("GetStatuswordAsync", deviceId);
|
|
}
|
|
|
|
public async Task SetControlwordAsync(string deviceId, ushort controlwordValue)
|
|
{
|
|
await _hubConnection.InvokeAsync("SetControlwordAsync", deviceId, controlwordValue);
|
|
}
|
|
|
|
public async Task<string> GetStateAsync(string deviceId)
|
|
{
|
|
return await _hubConnection.InvokeAsync<string>("GetStateAsync", deviceId);
|
|
}
|
|
|
|
public async Task<string> GetOperationModeAsync(string deviceId)
|
|
{
|
|
return await _hubConnection.InvokeAsync<string>("GetOperationModeAsync", deviceId);
|
|
}
|
|
|
|
// Position, Velocity, Torque
|
|
public async Task<int> GetActualPositionAsync(string deviceId)
|
|
{
|
|
return await _hubConnection.InvokeAsync<int>("GetActualPositionAsync", deviceId);
|
|
}
|
|
|
|
public async Task<int> GetActualVelocityAsync(string deviceId)
|
|
{
|
|
return await _hubConnection.InvokeAsync<int>("GetActualVelocityAsync", deviceId);
|
|
}
|
|
|
|
public async Task<short> GetActualTorqueAsync(string deviceId)
|
|
{
|
|
return await _hubConnection.InvokeAsync<short>("GetActualTorqueAsync", deviceId);
|
|
}
|
|
|
|
// Target Position Checking
|
|
public async Task<bool> IsAtTargetAsync(string deviceId, int tolerance = 100, bool useStatusword = true)
|
|
{
|
|
return await _hubConnection.InvokeAsync<bool>("IsAtTarget", deviceId, tolerance, useStatusword);
|
|
}
|
|
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
if (_disposed)
|
|
return;
|
|
|
|
_disposed = true;
|
|
await StopAsync();
|
|
await _hubConnection.DisposeAsync();
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
}
|