save
This commit is contained in:
@@ -7,7 +7,7 @@ using System.Text.Json;
|
||||
|
||||
namespace RobotApp.Client.Services;
|
||||
|
||||
// ================= CONNECTION STATE =================
|
||||
// ================= SIGNALR CONNECTION STATE =================
|
||||
public enum RobotClientState
|
||||
{
|
||||
Disconnected,
|
||||
@@ -16,7 +16,7 @@ public enum RobotClientState
|
||||
Reconnecting
|
||||
}
|
||||
|
||||
// ================= CLIENT =================
|
||||
// ================= ROBOT STATE CLIENT =================
|
||||
public sealed class RobotStateClient : IAsyncDisposable
|
||||
{
|
||||
private readonly NavigationManager _nav;
|
||||
@@ -25,11 +25,17 @@ public sealed class RobotStateClient : IAsyncDisposable
|
||||
private readonly object _lock = new();
|
||||
private bool _started;
|
||||
|
||||
// ================= STATE CACHE =================
|
||||
public ConcurrentDictionary<string, StateMsg> LatestStates { get; } = new();
|
||||
|
||||
// ================= ROBOT CONNECTION =================
|
||||
private bool _isRobotConnected;
|
||||
public bool IsRobotConnected => _isRobotConnected;
|
||||
|
||||
// ================= EVENTS =================
|
||||
public event Action<string, StateMsg>? OnStateReceived;
|
||||
public event Action<StateMsg>? OnStateReceivedAny;
|
||||
public event Action<bool>? OnRobotConnectionChanged;
|
||||
public event Action<RobotClientState>? OnConnectionStateChanged;
|
||||
|
||||
public RobotClientState ConnectionState { get; private set; } = RobotClientState.Disconnected;
|
||||
@@ -43,7 +49,8 @@ public sealed class RobotStateClient : IAsyncDisposable
|
||||
// ================= STATE HELPER =================
|
||||
private void SetState(RobotClientState state)
|
||||
{
|
||||
if (ConnectionState == state) return;
|
||||
if (ConnectionState == state)
|
||||
return;
|
||||
|
||||
ConnectionState = state;
|
||||
OnConnectionStateChanged?.Invoke(state);
|
||||
@@ -82,9 +89,10 @@ public sealed class RobotStateClient : IAsyncDisposable
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
_connection.Closed += async error =>
|
||||
_connection.Closed += async _ =>
|
||||
{
|
||||
_started = false;
|
||||
SetState(RobotClientState.Disconnected);
|
||||
|
||||
if (_connection != null)
|
||||
{
|
||||
@@ -96,8 +104,14 @@ public sealed class RobotStateClient : IAsyncDisposable
|
||||
}
|
||||
};
|
||||
|
||||
// ================= SIGNALR HANDLERS =================
|
||||
|
||||
// VDA5050 State
|
||||
_connection.On<string>("ReceiveState", HandleState);
|
||||
|
||||
// Robot connection (bool only)
|
||||
_connection.On<bool>("ReceiveRobotConnection", HandleRobotConnection);
|
||||
|
||||
try
|
||||
{
|
||||
await _connection.StartAsync();
|
||||
@@ -136,6 +150,13 @@ public sealed class RobotStateClient : IAsyncDisposable
|
||||
OnStateReceivedAny?.Invoke(state);
|
||||
}
|
||||
|
||||
// ================= HANDLE ROBOT CONNECTION =================
|
||||
private void HandleRobotConnection(bool isConnected)
|
||||
{
|
||||
_isRobotConnected = isConnected;
|
||||
OnRobotConnectionChanged?.Invoke(isConnected);
|
||||
}
|
||||
|
||||
// ================= SUBSCRIBE =================
|
||||
public async Task SubscribeRobotAsync(string serialNumber)
|
||||
{
|
||||
@@ -164,6 +185,7 @@ public sealed class RobotStateClient : IAsyncDisposable
|
||||
}
|
||||
|
||||
LatestStates.TryRemove(serialNumber, out _);
|
||||
_isRobotConnected = false;
|
||||
}
|
||||
|
||||
// ================= GET CACHE =================
|
||||
@@ -177,6 +199,7 @@ public sealed class RobotStateClient : IAsyncDisposable
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
_started = false;
|
||||
_isRobotConnected = false;
|
||||
SetState(RobotClientState.Disconnected);
|
||||
|
||||
if (_connection != null)
|
||||
|
||||
Reference in New Issue
Block a user