This commit is contained in:
2025-12-22 14:35:55 +07:00
parent 92c01004f5
commit 7a6f813825
9 changed files with 284 additions and 108 deletions

View File

@@ -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)

View File

@@ -270,31 +270,17 @@ public class OrderMessage
}
public object ToSchemaObject()
{
int seq = 0;
// ================= SORT NODES BY UI SEQUENCE =================
var orderedNodes = Nodes
.OrderBy(n => n.SequenceId)
.ToList();
return new
{
headerId = HeaderId++,
timestamp = string.IsNullOrWhiteSpace(Timestamp)
? DateTime.UtcNow.ToString("O")
: Timestamp,
version = Version,
manufacturer = Manufacturer,
serialNumber = SerialNumber,
orderId = OrderId,
orderUpdateId = OrderUpdateId,
zoneSetId = string.IsNullOrWhiteSpace(ZoneSetId)
? null
: ZoneSetId,
// ================= NODES =================
nodes = Nodes.Select(n => new
// ================= BUILD NODE OBJECTS =================
var nodeObjects = orderedNodes
.Select((n, index) => new
{
nodeId = n.NodeId,
sequenceId = seq++,
sequenceId = index * 2, // ✅ NODE = EVEN
released = n.Released,
nodePosition = new
@@ -311,36 +297,55 @@ public class OrderMessage
: n.NodePosition.MapId
},
actions = n.Actions.Select(a => new
{
actionId = a.ActionId,
actionType = a.ActionType,
blockingType = a.BlockingType,
actions = n.Actions?
.Select(a => new
{
actionId = a.ActionId,
actionType = a.ActionType,
blockingType = a.BlockingType,
actionParameters = a.ActionParameters != null
? a.ActionParameters
.Select(p => new { key = p.Key, value = p.Value })
actionParameters = a.ActionParameters?
.Select(p => new
{
key = p.Key,
value = p.Value
})
.ToArray()
: Array.Empty<object>()
}).ToArray()
}).ToArray(),
?? Array.Empty<object>()
})
.ToArray()
?? Array.Empty<object>()
})
.ToArray();
// ================= EDGES =================
edges = Edges.Select<UiEdge, object>(e =>
// ================= BUILD EDGE OBJECTS =================
var edgeObjects = Edges
.Select<UiEdge, object>((e, index) =>
{
int sequenceId = index * 2 + 1; // ✅ EDGE = ODD
// ---------- BASE ----------
var baseEdge = new
{
edgeId = e.EdgeId,
sequenceId,
released = true,
startNodeId = e.StartNodeId,
endNodeId = e.EndNodeId
};
// =================================================
// 1️⃣ IMPORTED TRAJECTORY (ƯU TIÊN CAO NHẤT)
// 1️⃣ IMPORTED TRAJECTORY
// =================================================
if (e.HasTrajectory && e.Trajectory != null)
{
return new
{
edgeId = e.EdgeId,
sequenceId = seq++,
released = true,
startNodeId = e.StartNodeId,
endNodeId = e.EndNodeId,
baseEdge.edgeId,
baseEdge.sequenceId,
baseEdge.released,
baseEdge.startNodeId,
baseEdge.endNodeId,
trajectory = new
{
@@ -356,27 +361,25 @@ public class OrderMessage
}
// =================================================
// 2️⃣ STRAIGHT EDGE (KHÔNG CÓ CURVE)
// 2️⃣ STRAIGHT EDGE
// =================================================
if (e.Radius <= 0)
{
return new
{
edgeId = e.EdgeId,
sequenceId = seq++,
released = true,
startNodeId = e.StartNodeId,
endNodeId = e.EndNodeId,
baseEdge.edgeId,
baseEdge.sequenceId,
baseEdge.released,
baseEdge.startNodeId,
baseEdge.endNodeId,
actions = Array.Empty<object>()
};
}
// =================================================
// 3️⃣ EDITOR GENERATED CURVE (RADIUS + QUADRANT)
// 3️⃣ GENERATED CURVE (EDITOR)
// =================================================
var startNode = Nodes.First(n => n.NodeId == e.StartNodeId);
var startNode = orderedNodes.First(n => n.NodeId == e.StartNodeId);
var A = new Point(
startNode.NodePosition.X,
@@ -391,17 +394,40 @@ public class OrderMessage
return new
{
edgeId = e.EdgeId,
sequenceId = seq++,
released = true,
startNodeId = e.StartNodeId,
endNodeId = e.EndNodeId,
baseEdge.edgeId,
baseEdge.sequenceId,
baseEdge.released,
baseEdge.startNodeId,
baseEdge.endNodeId,
trajectory = result.Trajectory,
actions = Array.Empty<object>()
};
}).ToArray()
})
.ToArray();
// ================= FINAL SCHEMA OBJECT =================
return new
{
headerId = HeaderId++,
timestamp = string.IsNullOrWhiteSpace(Timestamp)
? DateTime.UtcNow.ToString("O")
: Timestamp,
version = Version,
manufacturer = Manufacturer,
serialNumber = SerialNumber,
orderId = OrderId,
orderUpdateId = OrderUpdateId,
zoneSetId = string.IsNullOrWhiteSpace(ZoneSetId)
? null
: ZoneSetId,
nodes = nodeObjects,
edges = edgeObjects
};
}
}