Merge remote-tracking branch 'origin/sonlt' into dangnv

This commit is contained in:
Đăng Nguyễn
2025-12-22 16:11:45 +07:00
9 changed files with 284 additions and 108 deletions

View File

@@ -25,9 +25,9 @@
<span>X: @MonitorData.RobotPosition.X.ToString("F2")m | Y: @MonitorData.RobotPosition.Y.ToString("F2")m | θ: @((MonitorData.RobotPosition.Theta * 180 / Math.PI).ToString("F1"))°</span>
</div>
}
<MudChip T="string" Color="@(IsConnected? Color.Success: Color.Error)" Size="Size.Small">
@* <MudChip T="string" Color="@(IsConnected? Color.Success: Color.Error)" Size="Size.Small">
@(IsConnected ? "Connected" : "Disconnected")
</MudChip>
</MudChip> *@
</div>
<div @ref="SvgContainerRef" class="svg-container">
<svg @ref="SvgRef"

View File

@@ -38,15 +38,18 @@
@if (CurrentState != null)
{
<MudChip T="string"
Icon="@(IsConnected ? Icons.Material.Filled.CheckCircle : Icons.Material.Filled.Error)"
Size="Size.Large"
Color="@(IsConnected ? Color.Success : Color.Error)"
Variant="@Variant.Filled"
Class="px-6 py-4 text-white"
Style="font-weight: bold; font-size: 1.1rem;">
@(IsConnected ? "ONLINE" : "OFFLINE")
</MudChip>
}
Icon="@(IsConnected
? Icons.Material.Filled.CheckCircle
: Icons.Material.Filled.Error)"
Size="Size.Large"
Color="@(IsConnected ? Color.Success : Color.Error)"
Variant="Variant.Filled"
Class="px-6 py-4 text-white"
Style="font-weight: bold; font-size: 1.1rem;">
@(IsConnected ? "ONLINE" : "OFFLINE")
</MudChip>
}
</MudPaper>
@{
@@ -133,7 +136,6 @@
</MudCard>
</MudItem>
<!-- BATTERY -->
<!-- BATTERY -->
<MudItem xs="12" md="6" lg="4">
<MudCard Elevation="6" Class="h-100 rounded-lg">
@@ -371,21 +373,35 @@
};
private StateMsg? CurrentState;
private bool IsConnected => RobotStateClient.LatestStates.ContainsKey(RobotSerial);
private bool IsConnected;
private readonly string RobotSerial = "T800-002";
private List<MessageRow> MessageRows = new();
protected override async Task OnInitializedAsync()
{
RobotStateClient.OnStateReceived += OnRobotStateReceived;
if (RobotStateClient.LatestStates.Count == 0)
RobotStateClient.OnRobotConnectionChanged += OnRobotConnectionChanged;
if (RobotStateClient.ConnectionState == RobotClientState.Disconnected)
{
await RobotStateClient.StartAsync();
}
await RobotStateClient.SubscribeRobotAsync(RobotSerial);
CurrentState = RobotStateClient.GetLatestState(RobotSerial);
IsConnected = RobotStateClient.IsRobotConnected;
UpdateMessageRows();
}
private void OnRobotConnectionChanged(bool connected)
{
InvokeAsync(() =>
{
IsConnected = connected;
StateHasChanged();
});
}
private void OnRobotStateReceived(string serialNumber, StateMsg state)
@@ -421,6 +437,7 @@
public void Dispose()
{
RobotStateClient.OnStateReceived -= OnRobotStateReceived;
RobotStateClient.OnRobotConnectionChanged -= OnRobotConnectionChanged;
}
private record MessageRow(string Type, string Level, string Description, bool IsError);

View File

@@ -1,11 +1,21 @@
@using System.Text.Json
@using MudBlazor
@using Microsoft.AspNetCore.Components.Forms
<MudDialog>
<!-- ================= TITLE ================= -->
<TitleContent>
<MudText Typo="Typo.h6">Import Order JSON</MudText>
<MudStack Row AlignItems="AlignItems.Center" Spacing="2">
<MudIcon Icon="@Icons.Material.Filled.UploadFile"
Color="Color.Primary" />
<MudText Typo="Typo.h6">
Import Order JSON
</MudText>
</MudStack>
</TitleContent>
<!-- ================= CONTENT ================= -->
<DialogContent>
@if (ShowWarning)
@@ -18,6 +28,21 @@
</MudAlert>
}
<!-- ===== FILE INPUT (PURE BLAZOR) ===== -->
<div class="mb-4">
<label class="mud-button-root mud-button mud-button-outlined mud-button-outlined-primary"
style="cursor:pointer;">
<MudIcon Icon="@Icons.Material.Filled.AttachFile" Class="mr-2" />
Choose JSON file
<InputFile OnChange="OnFileSelected"
accept=".json"
style="display:none" />
</label>
</div>
<MudDivider Class="my-3" />
<!-- ===== PASTE JSON ===== -->
<MudTextField @bind-Value="JsonText"
Label="Paste Order JSON"
Lines="20"
@@ -34,29 +59,77 @@
</DialogContent>
<!-- ================= ACTIONS ================= -->
<DialogActions>
<MudButton OnClick="Cancel">Cancel</MudButton>
<MudButton OnClick="Cancel">
Cancel
</MudButton>
<MudButton Variant="Variant.Filled"
Color="Color.Primary"
OnClick="ValidateAndImport">
Import
</MudButton>
</DialogActions>
</MudDialog>
@code {
[CascadingParameter] public IMudDialogInstance MudDialog { get; set; } = default!;
[CascadingParameter]
public IMudDialogInstance MudDialog { get; set; } = default!;
public string JsonText { get; set; } = "";
public string? ErrorMessage;
public bool ShowWarning { get; set; } = false;
public bool ShowWarning { get; set; }
private void Cancel() => MudDialog.Cancel();
// ================= FILE HANDLER =================
private async Task OnFileSelected(InputFileChangeEventArgs e)
{
ErrorMessage = null;
ShowWarning = false;
var file = e.File;
if (file == null)
return;
if (!file.Name.EndsWith(".json", StringComparison.OrdinalIgnoreCase) &&
!file.Name.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
{
ShowWarning = true;
ErrorMessage = "Only .json or .txt files are supported.";
return;
}
try
{
using var stream = file.OpenReadStream(maxAllowedSize: 1_048_576);
using var reader = new StreamReader(stream);
JsonText = await reader.ReadToEndAsync();
StateHasChanged();
}
catch (Exception ex)
{
ShowWarning = true;
ErrorMessage = $"Failed to read file: {ex.Message}";
}
}
// ================= VALIDATE & IMPORT =================
private void ValidateAndImport()
{
ErrorMessage = null;
ShowWarning = false;
if (string.IsNullOrWhiteSpace(JsonText))
{
ShowWarning = true;
ErrorMessage = "JSON content is empty.";
return;
}
try
{
using var doc = JsonDocument.Parse(JsonText);
@@ -65,10 +138,11 @@
// ===== BASIC STRUCTURE CHECK =====
if (!root.TryGetProperty("nodes", out _) ||
!root.TryGetProperty("edges", out _))
{
throw new Exception("Missing 'nodes' or 'edges' field.");
}
var order = OrderMessage.FromSchemaObject(root);
ValidateOrder(order);
MudDialog.Close(DialogResult.Ok(order));
@@ -85,19 +159,40 @@
}
}
// ================= DOMAIN VALIDATION =================
private void ValidateOrder(OrderMessage order)
{
if (order.Nodes.Count == 0)
throw new Exception("Order must contain at least one node.");
var nodeIds = order.Nodes.Select(n => n.NodeId).ToHashSet();
if (order.Nodes.Count != order.Edges.Count + 1)
throw new Exception(
$"Invalid path structure: Nodes count ({order.Nodes.Count}) " +
$"must equal Edges count + 1 ({order.Edges.Count + 1})."
);
var nodeIds = order.Nodes
.Select(n => n.NodeId)
.ToHashSet(StringComparer.Ordinal);
foreach (var e in order.Edges)
{
if (!nodeIds.Contains(e.StartNodeId) ||
!nodeIds.Contains(e.EndNodeId))
if (string.IsNullOrWhiteSpace(e.StartNodeId) ||
string.IsNullOrWhiteSpace(e.EndNodeId))
{
throw new Exception(
$"Edge '{e.EdgeId}' references unknown node."
$"Edge '{e.EdgeId}' must define both StartNodeId and EndNodeId."
);
}
if (!nodeIds.Contains(e.StartNodeId))
throw new Exception(
$"Edge '{e.EdgeId}' references unknown StartNodeId '{e.StartNodeId}'."
);
if (!nodeIds.Contains(e.EndNodeId))
throw new Exception(
$"Edge '{e.EdgeId}' references unknown EndNodeId '{e.EndNodeId}'."
);
}
}

View File

@@ -1,4 +1,4 @@
<MudPaper Class="pa-4 h-100 d-flex flex-column" Elevation="2">
<MudPaper Class="pa-4 h-100 d-flex flex-column overflow-hidden" Elevation="2">
<MudStack Row AlignItems="AlignItems.Center" Justify="Justify.SpaceBetween"
Class="mb-4 flex-shrink-0">
<MudText Typo="Typo.h6">📄 JSON Output (/order)</MudText>
@@ -27,7 +27,6 @@
<MudTooltip Text="@(Copied ? "Copied!" : "Copy to clipboard")">
<MudButton Variant="Variant.Filled"
Color="@(Copied ? Color.Success : Color.Primary)"
Size="Size.Small"
StartIcon="@(Copied ? Icons.Material.Filled.Check : Icons.Material.Filled.ContentCopy)"
OnClick="OnCopy">
@(Copied ? "Copied!" : "Copy")
@@ -36,12 +35,11 @@
</div>
</MudStack>
<div class="flex-grow-1" style="overflow:auto;">
<div class="flex-grow-1">
<MudTextField Value="@OrderJson"
ReadOnly
Variant="Variant.Filled"
Lines="70"
Class="h-100"
Lines="50"
Style="font-family: 'Roboto Mono', Consolas, monospace;
font-size: 0.85rem;
background:#1e1e1e;

View File

@@ -44,21 +44,22 @@
</MudItem>
<!-- Sequence -->
<MudItem xs="12">
@* <MudItem xs="12">
<MudNumericField T="int"
Value="@node.SequenceId"
ValueChanged="@((int v) => SetValue(() => node.SequenceId = v))"
Immediate="true"
Label="Sequence ID" />
</MudItem>
</MudItem> *@
<!-- Released -->
<MudItem xs="12">
@* <MudItem xs="12">
<MudSwitch T="bool"
Checked="@node.Released"
CheckedChanged="@((bool v) => SetValue(() => node.Released = v))"
Label="Released" />
</MudItem>
</MudItem> *@
<!-- Position -->
<MudItem xs="6">