using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.SignalR.Client; using RobotNet10.RobotApp.Client.Shared.Devices; namespace RobotNet10.RobotApp.Client.Clients; /// /// SignalR client để kết nối với CiA402ServoHub /// 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 GetServoDataAsync(string deviceId) { var result = await _hubConnection.InvokeAsync("GetServoData", deviceId); return result ?? new CiA402ServoDataDto(); } public async Task GetDeviceInfoAsync(string deviceId) { return await _hubConnection.InvokeAsync("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); } /// /// Ghi cùng lúc cả 3 profile (speed, acceleration, deceleration) xuống drive. /// 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); } /// Đọc lại homing method từ drive để kiểm tra đã ghi xuống chưa. public async Task GetHomingMethodAsync(string deviceId) { return await _hubConnection.InvokeAsync("GetHomingMethodAsync", deviceId); } /// Đọc lại homing speed từ drive để kiểm tra đã ghi xuống chưa. public async Task GetHomingSpeedAsync(string deviceId) { return await _hubConnection.InvokeAsync("GetHomingSpeedAsync", deviceId); } /// Đọc lại homing offset từ drive để kiểm tra đã ghi xuống chưa. public async Task GetHomingOffsetAsync(string deviceId) { return await _hubConnection.InvokeAsync("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 IsInFaultStateAsync(string deviceId) { return await _hubConnection.InvokeAsync("IsInFaultStateAsync", deviceId); } public async Task GetErrorRegisterAsync(string deviceId) { return await _hubConnection.InvokeAsync("GetErrorRegisterAsync", deviceId); } public async Task GetErrorHistoryAsync(string deviceId) { return await _hubConnection.InvokeAsync("GetErrorHistoryAsync", deviceId); } public async Task GetLatestErrorCodeAsync(string deviceId) { return await _hubConnection.InvokeAsync("GetLatestErrorCodeAsync", deviceId); } public async Task TryFaultResetAsync(string deviceId) { return await _hubConnection.InvokeAsync("TryFaultResetAsync", deviceId); } public async Task IsEnabledAsync(string deviceId) { return await _hubConnection.InvokeAsync("IsEnabledAsync", deviceId); } public async Task IsReadyAsync(string deviceId) { return await _hubConnection.InvokeAsync("IsReadyAsync", deviceId); } // Statusword & Controlword public async Task GetStatuswordAsync(string deviceId) { return await _hubConnection.InvokeAsync("GetStatuswordAsync", deviceId); } public async Task SetControlwordAsync(string deviceId, ushort controlwordValue) { await _hubConnection.InvokeAsync("SetControlwordAsync", deviceId, controlwordValue); } public async Task GetStateAsync(string deviceId) { return await _hubConnection.InvokeAsync("GetStateAsync", deviceId); } public async Task GetOperationModeAsync(string deviceId) { return await _hubConnection.InvokeAsync("GetOperationModeAsync", deviceId); } // Position, Velocity, Torque public async Task GetActualPositionAsync(string deviceId) { return await _hubConnection.InvokeAsync("GetActualPositionAsync", deviceId); } public async Task GetActualVelocityAsync(string deviceId) { return await _hubConnection.InvokeAsync("GetActualVelocityAsync", deviceId); } public async Task GetActualTorqueAsync(string deviceId) { return await _hubConnection.InvokeAsync("GetActualTorqueAsync", deviceId); } // Target Position Checking public async Task IsAtTargetAsync(string deviceId, int tolerance = 100, bool useStatusword = true) { return await _hubConnection.InvokeAsync("IsAtTarget", deviceId, tolerance, useStatusword); } public async ValueTask DisposeAsync() { if (_disposed) return; _disposed = true; await StopAsync(); await _hubConnection.DisposeAsync(); GC.SuppressFinalize(this); } }