first commit -push

This commit is contained in:
dungtt
2025-10-15 15:15:53 +07:00
parent 674ae395be
commit a9577c5756
885 changed files with 74595 additions and 0 deletions

View File

@@ -0,0 +1,310 @@
@implements IAsyncDisposable
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using RobotNet.WebApp.Clients
@inject RobotHubClient RobotHub
@inject ISnackbar Snackbar
@inject IHttpClientFactory HttpFactory
<MudCard Class="w-100 me-2 position-relative" Elevation="0" Outlined Style="height: 450px">
<MudCardHeader>
<MudText Typo="Typo.h5">Setting</MudText>
<MudSpacer />
<MudText Typo="Typo.subtitle1">@Robot.Name</MudText>
</MudCardHeader>
<MudCardContent>
<div class="d-flex flex-column">
<div class="d-flex flex-row my-2">
<div class="d-flex flex-row" style="width: 350px">
<MudAutocomplete T="MapInfoDto" Variant="Variant.Outlined" Label="Map" ShrinkLabel Disabled="!Robot.Online" Modal SearchFunc="MapSearch"
ToStringFunc="@(e=> e == null ? null : $"{e.Name}")" MaxItems="Maps.Count" ValueChanged="MapChanged" ShowProgressIndicator="true">
<NoItemsTemplate>
<MudText Align="Align.Center" Class="px-4 py-1">
No items found
</MudText>
</NoItemsTemplate>
</MudAutocomplete>
</div>
<MudButton Class="d-flex flex-grow-1 mt-2 mb-1 ms-2" Variant="Variant.Filled" Color="Color.Info" Disabled="!Robot.Online" OnClick="SetMap">SetMap</MudButton>
</div>
<div class="d-flex flex-row my-2">
<div class="d-flex flex-row" style="width: 350px">
<MudAutocomplete T="string" Variant="Variant.Outlined" Label="Node" ShrinkLabel Disabled="!Robot.Online" Modal SearchFunc="NodeSearch" ShowProgressIndicator="true"
MaxItems="@Nodes.Count" ValueChanged="NodeChanged">
<NoItemsTemplate>
<MudText Align="Align.Center" Class="px-4 py-1">
No items found
</MudText>
</NoItemsTemplate>
</MudAutocomplete>
<MudButton Class="ms-2 mt-2 mb-1" Variant="Variant.Filled" Color="Color.Info" Disabled="@(!Robot.Online || IsWorking)" OnClick="MoveToNode">Move</MudButton>
</div>
<MudButton Class="d-flex flex-grow-1 mt-2 mb-1 ms-2" Variant="Variant.Filled" Color="Color.Secondary" Disabled="@(!Robot.Online)" OnClick="CancelOrder">CancelOrder</MudButton>
</div>
<div class="d-flex flex-row my-2">
<div class="d-flex flex-row" style="width: 350px">
<MudAutocomplete T="ActionDto" Variant="Variant.Outlined" Label="Action" ShrinkLabel Disabled="!Robot.Online" Modal SearchFunc="ActionSearch" ShowProgressIndicator="true"
MaxItems="@Actions.Count" ValueChanged="ActionChanged" ToStringFunc="@(e=> e == null ? null : $"{e.Name}")">
<NoItemsTemplate>
<MudText Align="Align.Center" Class="px-4 py-1">
No items found
</MudText>
</NoItemsTemplate>
</MudAutocomplete>
<MudButton Class="ms-2 mt-2 mb-1" Variant="Variant.Filled" Color="Color.Info" Disabled="@(!Robot.Online || IsWorking)" OnClick="SendAction">Send</MudButton>
</div>
<MudButton Class="d-flex flex-grow-1 mt-2 mb-1 ms-2" Variant="Variant.Filled" Color="Color.Secondary" Disabled="@(!Robot.Online)" OnClick="CancelAction">CancelAction</MudButton>
</div>
<div class="d-flex flex-row my-2">
<div class="d-flex flex-row" style="width: 350px">
<MudNumericField T="double" Label="X(m)" Class="me-2" @bind-Value="InitPosition.X" Disabled="!Robot.Online" Variant="Variant.Outlined"></MudNumericField>
<MudNumericField T="double" Label="Y(m)" Class="me-2" @bind-Value="InitPosition.Y" Disabled="!Robot.Online" Variant="Variant.Outlined"></MudNumericField>
<MudNumericField T="double" Label="Theta(degree)" @bind-Value="InitPosition.Theta" Disabled="!Robot.Online" Variant="Variant.Outlined"></MudNumericField>
</div>
<MudButton Class="d-flex flex-grow-1 ms-2" Variant="Variant.Filled" Color="Color.Info" Disabled="!Robot.Online" OnClick="InitPose">InitPose</MudButton>
</div>
</div>
</MudCardContent>
<MudOverlay Visible="OverlayIsVisible" DarkBackground="true" Absolute="true">
<MudProgressCircular Color="Color.Secondary" Indeterminate="true" />
</MudOverlay>
</MudCard>
@code {
[Parameter, EditorRequired]
public RobotDto Robot { get; set; } = new();
private MapInfoDto MapSelected = default!;
private string SelectedNode = "";
private List<NodeDto> Nodes = [];
private List<MapInfoDto> Maps = [];
private (double X, double Y, double Theta) InitPosition = new();
private bool IsWorking = false;
private bool OverlayIsVisible;
private List<ActionDto> Actions = [];
private ActionDto? ActionSelected = null;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await base.OnAfterRenderAsync(firstRender);
if (!firstRender) return;
await RobotHub.StartAsync();
await LoadMaps();
}
public void UpdateState(bool isOnline, bool isWorking)
{
Robot.Online = isOnline;
IsWorking = isWorking;
StateHasChanged();
}
private async Task MoveToNode()
{
if (string.IsNullOrEmpty(SelectedNode))
{
Snackbar.Add("Vui lòng chọn node", Severity.Warning);
}
else
{
OverlayIsVisible = true;
StateHasChanged();
var result = await RobotHub.MoveToNode(Robot.RobotId, SelectedNode);
if (result.IsSuccess) Snackbar.Add("Ra lệnh thành công", Severity.Success);
else Snackbar.Add($"Ra lệnh không thành công: {result.Message}", Severity.Warning);
OverlayIsVisible = false;
}
StateHasChanged();
}
private async Task CancelOrder()
{
OverlayIsVisible = true;
StateHasChanged();
var result = await RobotHub.CancelNavigation(Robot.RobotId);
if (result.IsSuccess) Snackbar.Add("Ra lệnh thành công", Severity.Success);
else Snackbar.Add($"Ra lệnh không thành công: {result.Message}", Severity.Warning);
OverlayIsVisible = false;
StateHasChanged();
}
private async Task SetMap()
{
OverlayIsVisible = true;
StateHasChanged();
var result = await RobotHub.SetMap(Robot.RobotId, MapSelected.Id);
if (result.IsSuccess)
{
Robot.MapId = MapSelected.Id;
await LoadNodes();
await LoadActionAsync();
Snackbar.Add("Ra lệnh thành công", Severity.Success);
}
else Snackbar.Add($"Ra lệnh không thành công: {result.Message}", Severity.Warning);
OverlayIsVisible = false;
StateHasChanged();
}
private async Task InitPose()
{
OverlayIsVisible = true;
StateHasChanged();
var result = await RobotHub.SetInitialPose(Robot.RobotId, InitPosition.X, InitPosition.Y, InitPosition.Theta * Math.PI / 180);
if (result.IsSuccess) Snackbar.Add("Ra lệnh thành công", Severity.Success);
else Snackbar.Add($"Ra lệnh không thành công: {result.Message}", Severity.Warning);
OverlayIsVisible = false;
StateHasChanged();
}
private async Task SendAction()
{
if (ActionSelected is null)
{
Snackbar.Add("Vui lòng chọn action", Severity.Warning);
}
else
{
OverlayIsVisible = true;
StateHasChanged();
var result = await RobotHub.SendAction(Robot.RobotId, ActionSelected);
if (result.IsSuccess) Snackbar.Add("Ra lệnh thành công", Severity.Success);
else Snackbar.Add($"Ra lệnh không thành công: {result.Message}", Severity.Warning);
OverlayIsVisible = false;
}
StateHasChanged();
}
private async Task CancelAction()
{
OverlayIsVisible = true;
StateHasChanged();
var result = await RobotHub.CancelAction(Robot.RobotId);
if (result.IsSuccess) Snackbar.Add("Ra lệnh thành công", Severity.Success);
else Snackbar.Add($"Ra lệnh không thành công: {result.Message}", Severity.Warning);
OverlayIsVisible = false;
StateHasChanged();
}
public async Task LoadActionAsync()
{
Actions.Clear();
using var Http = HttpFactory.CreateClient("MapManagerAPI");
var result = await Http.GetFromJsonAsync<IEnumerable<ActionDto>>($"api/Actions/{Robot.MapId}");
if (result is not null && result.Any())
{
Actions.AddRange(result);
}
if (Actions.Any()) ActionSelected = Actions.First();
StateHasChanged();
}
public async Task LoadNodes()
{
try
{
using var Http = HttpFactory.CreateClient("MapManagerAPI");
var result = await Http.GetFromJsonAsync<NodeDto[]>($"api/Nodes/{Robot.MapId}");
if (result is not null) Nodes = result.Where(n => !string.IsNullOrEmpty(n.Name)).ToList();
}
catch (AccessTokenNotAvailableException ex)
{
ex.Redirect();
}
}
private async Task LoadMaps()
{
try
{
Maps.Clear();
using var Http = HttpFactory.CreateClient("MapManagerAPI");
var maps = await Http.GetFromJsonAsync<IEnumerable<MapInfoDto>>($"api/MapsManager?txtSearch=");
Maps.AddRange(maps ?? []);
StateHasChanged();
}
catch (AccessTokenNotAvailableException ex)
{
ex.Redirect();
return;
}
}
private async Task<IEnumerable<MapInfoDto>> MapSearch(string value, CancellationToken token)
{
await Task.Delay(5, token);
if (string.IsNullOrEmpty(value))
{
return Maps;
}
return Maps.Where(x => x.Name.Contains(value, StringComparison.InvariantCultureIgnoreCase));
}
private async Task<IEnumerable<string>> NodeSearch(string value, CancellationToken token)
{
await Task.Delay(5, token);
if (string.IsNullOrEmpty(value))
{
return Nodes.Select(n => n.Name);
}
return Nodes.Where(x => x.Name.Contains(value, StringComparison.InvariantCultureIgnoreCase)).Select(n => n.Name);
}
private async Task<IEnumerable<ActionDto>> ActionSearch(string value, CancellationToken token)
{
await Task.Delay(5, token);
if (string.IsNullOrEmpty(value))
{
return Actions;
}
return Actions.Where(x => x.Name.Contains(value, StringComparison.InvariantCultureIgnoreCase));
}
private void NodeChanged(string value)
{
if (string.IsNullOrEmpty(SelectedNode) || value != SelectedNode) SelectedNode = value;
}
private void ActionChanged(ActionDto value)
{
if (ActionSelected is null || value.Id != ActionSelected.Id) ActionSelected = value;
}
private void MapChanged(MapInfoDto value)
{
if (MapSelected is null || value.Id != MapSelected.Id) MapSelected = value;
}
public async ValueTask DisposeAsync()
{
await RobotHub.StopAsync();
}
}

View File

@@ -0,0 +1,129 @@
@using System.Text.Json
<div class="data-preview">
<div class="d-flex" style="height: fit-content;">
<MudToggleGroup Class="w-100" T="DataPreviewType" SelectionMode="SelectionMode.SingleSelection" @bind-Value="DataType" Color="Color.Default"
Outlined="false" SelectedClass="custom-background" @bind-Value:after="DataTypeChanged" Disabled="!RobotState.Online">
<MudToggleItem Value="@(DataPreviewType.Visualization)" Style="text-transform:none" />
<MudToggleItem Value="@(DataPreviewType.Node)" Style="text-transform:none" />
<MudToggleItem Value="@(DataPreviewType.Edge)" Style="text-transform:none" />
<MudToggleItem Value="@(DataPreviewType.Order)" Style="text-transform:none" />
<MudToggleItem Value="@(DataPreviewType.Action)" Style="text-transform:none" />
<MudToggleItem Value="@(DataPreviewType.Infomation)" Style="text-transform:none" />
<MudToggleItem Value="@(DataPreviewType.Battery)" Style="text-transform:none" />
<MudToggleItem Value="@(DataPreviewType.Error)" Style="text-transform:none" />
</MudToggleGroup>
</div>
<div class="content">
@DataPreview
</div>
</div>
<style>
.custom-background {
background-color: #283593;
color: white !important;
}
.custom-background:hover {
background-color: #283593;
color: white !important;
}
</style>
@code {
private DataPreviewType DataType;
private string DataPreview = "";
private RobotVDA5050StateDto RobotState = new();
public void UpdateState(RobotVDA5050StateDto robotstate)
{
RobotState = robotstate;
DataTypeChanged();
StateHasChanged();
}
private void DataTypeChanged()
{
switch (DataType)
{
case DataPreviewType.Visualization:
DataPreview = JsonSerializer.Serialize(RobotState.Visualization, JsonOptionExtends.Read);
break;
case DataPreviewType.Node:
DataPreview = JsonSerializer.Serialize(RobotState.State.NodeStates, JsonOptionExtends.Read);
break;
case DataPreviewType.Edge:
DataPreview = JsonSerializer.Serialize(RobotState.State.EdgeStates, JsonOptionExtends.Read);
break;
case DataPreviewType.Order:
DataPreview = JsonSerializer.Serialize(GetOrderState(RobotState), JsonOptionExtends.Read);
break;
case DataPreviewType.Action:
DataPreview = JsonSerializer.Serialize(RobotState.State.ActionStates, JsonOptionExtends.Read);
break;
case DataPreviewType.Infomation:
DataPreview = JsonSerializer.Serialize(RobotState.State.Information, JsonOptionExtends.Read);
break;
case DataPreviewType.Battery:
DataPreview = JsonSerializer.Serialize(RobotState.State.BatteryState, JsonOptionExtends.Read);
break;
case DataPreviewType.Error:
DataPreview = JsonSerializer.Serialize(RobotState.State.Errors, JsonOptionExtends.Read);
break;
};
StateHasChanged();
}
private OrderState GetOrderState(RobotVDA5050StateDto state)
{
return new()
{
OrderId = state.State.OrderId,
OrderUpdateId = state.State.OrderUpdateId,
ZoneSetId = state.State.ZoneSetId,
LastNodeId = state.State.LastNodeId,
LastNodeSequenceId = state.State.LastNodeSequenceId,
Driving = state.State.Driving,
Paused = state.State.Paused,
NewBaseRequest = state.State.NewBaseRequest,
DistanceSinceLastNode = state.State.DistanceSinceLastNode,
OperatingMode = state.State.OperatingMode,
IsCanceled = state.OrderState.IsCanceled,
IsCompleted = state.OrderState.IsCompleted,
IsError = state.OrderState.IsError,
IsProcessing = state.OrderState.IsProcessing,
};
}
public enum DataPreviewType
{
Visualization,
Node,
Edge,
Order,
Action,
Infomation,
Battery,
Error
}
public class OrderState
{
public string OrderId { get; set; } = "";
public int OrderUpdateId { get; set; }
public string ZoneSetId { get; set; } = "";
public string LastNodeId { get; set; } = "";
public int LastNodeSequenceId { get; set; }
public bool Driving { get; set; }
public bool Paused { get; set; }
public bool NewBaseRequest { get; set; }
public double DistanceSinceLastNode { get; set; }
public string OperatingMode { get; set; } = "";
public bool IsError { get; set; }
public bool IsCompleted { get; set; }
public bool IsProcessing { get; set; }
public bool IsCanceled { get; set; }
}
}

View File

@@ -0,0 +1,19 @@
.data-preview {
display: flex;
height: 100%;
width: 100%;
flex-direction: column;
border: 1px solid silver;
border-radius: 5px;
}
.data-preview .content {
display: flex;
white-space: pre;
overflow-y: auto;
overflow-x: hidden;
font-size: 15px;
flex-grow: 1;
border-top: 1px solid silver;
padding: 5px;
}

View File

@@ -0,0 +1,160 @@
@using System.Net.Http.Headers
@inject IHttpClientFactory HttpFactory
@inject IJSRuntime JSRuntime
@inject ISnackbar Snackbar
<div class="d-flex flex-column">
<span Class="title">@RobotModelName</span>
<div class="d-flex flex-grow-1 w-100 flex-column">
<div class="robot-item">
<img src="@imageSrc" alt="robot model image" onerror="this.onerror=null; this.src='/images/Image-not-found.png';" />
</div>
<div class="d-flex flex-row m-3 align-items-center justify-content-between">
<MudText Typo="Typo.h5">Image</MudText>
<div class="d-flex flex-row">
<MudIconButton Class="me-3" Size="Size.Small" Variant="Variant.Filled" Icon="@Icons.Material.Filled.Download" Color="Color.Primary" Disabled="@Disable" OnClick="@(async () => await DownloadImage())" />
<MudIconButton Class="me-3" Size="Size.Small" Variant="Variant.Filled" Icon="@Icons.Material.Filled.Upload" Color="Color.Primary" Disabled="@Disable" OnClick="OpenUpdateRobotImage" />
</div>
</div>
</div>
</div>
<MudDialog @bind-Visible="@IsUpdateRobotImageVisable">
<TitleContent>
<MudText Typo="Typo.h6">
Update RobotModel's Image
</MudText>
</TitleContent>
<DialogContent>
<div class="d-flex flex-column w-100 h-100 px-3">
<div class="d-flex flex-row mb-2">
<InputFile id="fileRobotImageCreate" OnChange="RobotImageChanged" accept=".png" hidden />
<MudButton HtmlTag="label"
Variant="Variant.Filled"
Color="Color.Primary"
StartIcon="@Icons.Material.Filled.CloudUpload"
for="fileRobotImageCreate">
Robot Model Image
</MudButton>
</div>
<div class="d-flex mb-2">
<div class="robot-update-item">
<img alt="Robot Model Image" src="@ImagePreview" style="image-rendering: pixelated;" />
</div>
</div>
</div>
</DialogContent>
<DialogActions>
<MudButton OnClick="@(() => IsUpdateRobotImageVisable = false)" Variant="Variant.Filled">Cancel</MudButton>
<MudButton Color="Color.Primary" OnClick="UpdateRobotModelImage" Variant="Variant.Filled">Update</MudButton>
</DialogActions>
</MudDialog>
@code {
private bool Disable = true;
private string imageSrc = "/images/Image-not-found.png";
private string RobotModelName = "Robot Model preview";
private Guid RobotModelId = Guid.Empty;
private string ImagePreview = "";
private IBrowserFile? RobotImageChange;
private bool IsUpdateRobotImageVisable;
public void SetRobotPreview(RobotModelDto? robotmodel)
{
if (robotmodel is null)
{
Disable = true;
RobotModelName = "Robot Model preview";
imageSrc = "/images/Image-not-found.png";
RobotModelId = Guid.Empty;
}
else
{
using var Http = HttpFactory.CreateClient("RobotManagerAPI");
imageSrc = $"{Http.BaseAddress}api/RobotModels/image/{robotmodel.Id}?t={DateTime.Now}";
RobotModelName = robotmodel.ModelName;
RobotModelId = robotmodel.Id;
Disable = false;
}
StateHasChanged();
}
private async Task DownloadImage()
{
try
{
using var Http = HttpFactory.CreateClient("RobotManagerAPI");
var response = await Http.GetAsync(imageSrc);
if (response.IsSuccessStatusCode)
{
var fileBytes = await response.Content.ReadAsByteArrayAsync();
var base64Data = Convert.ToBase64String(fileBytes);
var mimeType = "image/png";
var url = $"data:{mimeType};base64,{base64Data}";
await JSRuntime.InvokeVoidAsync("DownloadImage", url, $"{RobotModelName}.png");
}
else Snackbar.Add("Không thể tải ảnh robot", Severity.Warning);
}
catch (Exception ex)
{
Snackbar.Add($"Không thể tải ảnh robot: {ex.Message}", Severity.Warning);
}
}
private void OpenUpdateRobotImage()
{
ImagePreview = imageSrc;
RobotImageChange = null;
IsUpdateRobotImageVisable = true;
StateHasChanged();
}
public async Task<string> UploadMedia(IBrowserFile file)
{
var path = Path.Combine(Path.GetTempPath(), RobotModelName);
await using var fs = new FileStream(path, FileMode.Create);
await file.OpenReadStream(file.Size).CopyToAsync(fs);
var bytes = new byte[file.Size];
fs.Position = 0;
await fs.ReadAsync(bytes);
fs.Close();
File.Delete(path);
return $"data:{file.ContentType};base64,{Convert.ToBase64String(bytes)}";
}
private async Task RobotImageChanged(InputFileChangeEventArgs e)
{
RobotImageChange = e.File;
if (RobotImageChange is not null)
{
ImagePreview = await UploadMedia(RobotImageChange);
StateHasChanged();
}
}
private async Task UpdateRobotModelImage()
{
if (RobotImageChange is null) return;
using var content = new MultipartFormDataContent();
var fileContent = new StreamContent(RobotImageChange.OpenReadStream(maxAllowedSize: 20480000));
fileContent.Headers.ContentType = new MediaTypeHeaderValue(RobotImageChange.ContentType);
content.Add(fileContent, "image", RobotImageChange.Name);
using var Http = HttpFactory.CreateClient("RobotManagerAPI");
var result = await (await Http.PutAsync($"api/RobotModels/image/{RobotModelId}", content)).Content.ReadFromJsonAsync<MessageResult>();
if (result is null) Snackbar.Add("Lỗi giao tiếp với hệ thống", Severity.Error);
else if (!result.IsSuccess) Snackbar.Add(result.Message ?? "Lỗi chưa xác định.", Severity.Error);
else
{
imageSrc = ImagePreview;
IsUpdateRobotImageVisable = false;
Snackbar.Add("Thay đổi thành công", Severity.Success);
}
StateHasChanged();
}
}

View File

@@ -0,0 +1,43 @@
.robot-item {
display: flex;
justify-content: center;
width: 100%;
height: fit-content;
}
.robot-item img {
justify-content: center;
max-width: 300px;
width: 95%;
height: 400px;
object-fit: contain;
border-radius: 10px;
image-rendering: pixelated;
}
.title {
display: flex;
height: 77px;
width: 100%;
justify-content: center;
align-content: center;
/*border-bottom: 0.5px solid gray;*/
font-size: 30px;
flex-wrap: wrap;
padding-bottom: .5rem;
}
.robot-update-item {
display: flex;
justify-content: center;
width: 100%;
height: fit-content;
}
.robot-update-item img {
width: 400px;
height: 300px;
object-fit: contain;
border-radius: 10px;
image-rendering: pixelated;
}

View File

@@ -0,0 +1,147 @@
@implements IAsyncDisposable
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using RobotNet.WebApp.Clients
@inject RobotHubClient RobotHub
@inject ISnackbar Snackbar
@inject IHttpClientFactory HttpFactory
<MudCard Class="w-100 me-2" Elevation="0" Outlined Style="height: 450px">
<MudCardHeader>
<MudText Typo="Typo.h5">Test</MudText>
</MudCardHeader>
<MudCardContent>
<div class="d-flex flex-column">
<div class="d-flex flex-row my-2">
<div class="d-flex flex-row" style="width: 415px">
@* <MudAutocomplete T="NodeDto" Variant="Variant.Outlined" Label="Node" Disabled="!Robot.Online" SearchFunc="NodeSearch"
MaxItems="@Nodes.Count" ToStringFunc="@(e=> e == null ? null : $"{e.Name}")" ValueChanged="NodeChanged" ShowProgressIndicator="true">
<NoItemsTemplate>
<MudText Align="Align.Center" Class="px-4 py-1">
No items found
</MudText>
</NoItemsTemplate>
</MudAutocomplete> *@
<MudSelect T="NodeDto" Label="Node" Variant="Variant.Outlined" MultiSelection="true" SelectAll="true" SelectAllText="Select all nodes" Disabled="!Robot.Online"
@bind-SelectedValues="PreSelectedNodes" HelperText="@($"{PreSelectedNodes.Count()} node{(PreSelectedNodes.Count() > 1 ? "s have" : " has")} been selected")">
@foreach (var node in Nodes)
{
<MudSelectItem T="NodeDto" Value="@node">@node.Name</MudSelectItem>
}
</MudSelect>
</div>
<div class="d-flex flex-grow-1 mt-2 mb-7 ms-2">
<MudButton Class="w-100" Variant="Variant.Filled" Color="Color.Info" Disabled="@(!Robot.Online)" OnClick="AddNode">Add</MudButton>
</div>
</div>
<div class="paper-nodes">
@foreach (var node in SelectedNodes)
{
<div class="m-1" style="height: fit-content">
<MudButton Variant="Variant.Filled" Color="Color.Info" Style="text-transform:none" OnClick="@(() => SelectedNodes.Remove(node))">
@(node.Name)
</MudButton>
</div>
}
</div>
<div class="d-flex w-100 flex-row my-2">
<MudButton Class="d-flex mt-2 mb-1" Variant="Variant.Filled" Color="Color.Info" Disabled="@(!Robot.Online || IsWorking)" OnClick="Run">Run</MudButton>
<MudButton Class="d-flex mt-2 mb-1 ms-2" Variant="Variant.Filled" Color="Color.Secondary" Disabled="@(!Robot.Online)" OnClick="Cancel">Cancel</MudButton>
</div>
</div>
</MudCardContent>
</MudCard>
@code {
[Parameter, EditorRequired]
public RobotDto Robot { get; set; } = new();
// private NodeDto? SelectedNode;
private List<NodeDto> Nodes = [];
private bool IsWorking = false;
private List<NodeDto> SelectedNodes = [];
private IEnumerable<NodeDto> PreSelectedNodes = new HashSet<NodeDto>();
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await base.OnAfterRenderAsync(firstRender);
if (!firstRender) return;
await RobotHub.StartAsync();
}
public void UpdateState(bool isOnline, bool isWorking)
{
Robot.Online = isOnline;
IsWorking = isWorking;
StateHasChanged();
}
public async Task LoadNodes()
{
try
{
using var Http = HttpFactory.CreateClient("MapManagerAPI");
var result = await Http.GetFromJsonAsync<NodeDto[]>($"api/Nodes/{Robot.MapId}");
if (result is not null) Nodes = result.Where(n => !string.IsNullOrEmpty(n.Name)).ToList();
}
catch (AccessTokenNotAvailableException ex)
{
ex.Redirect();
}
}
private async Task<IEnumerable<NodeDto>> NodeSearch(string value, CancellationToken token)
{
await Task.Delay(5, token);
if (string.IsNullOrEmpty(value))
{
return Nodes.Select(n => n);
}
return Nodes.Where(x => x.Name.Contains(value, StringComparison.InvariantCultureIgnoreCase)).Select(n => n);
}
// private void NodeChanged(List<NodeDto> value)
// {
// if (SelectedNode is null || value.Id != SelectedNode.Id) SelectedNode = value;
// }
private void AddNode()
{
// if (SelectedNode is not null && !SelectedNodes.Contains(SelectedNode))
// {
// SelectedNodes.Add(SelectedNode);
// StateHasChanged();
// }
if (PreSelectedNodes.Any())
{
SelectedNodes.Clear();
SelectedNodes.AddRange(PreSelectedNodes);
StateHasChanged();
}
}
private async Task Cancel()
{
var result = await RobotHub.CancelNavigation(Robot.RobotId);
if (result.IsSuccess) Snackbar.Add("Ra lệnh thành công", Severity.Success);
else Snackbar.Add($"Ra lệnh không thành công: {result.Message}", Severity.Warning);
StateHasChanged();
}
private async Task Run()
{
var result = await RobotHub.MoveRandom(Robot.RobotId, [.. SelectedNodes.Select(n => n.Name)]);
if (result.IsSuccess) Snackbar.Add("Ra lệnh thành công", Severity.Success);
else Snackbar.Add($"Ra lệnh không thành công: {result.Message}", Severity.Warning);
StateHasChanged();
}
public async ValueTask DisposeAsync()
{
await RobotHub.StopAsync();
}
}

View File

@@ -0,0 +1,13 @@
.paper-nodes {
padding: 5px;
border: 1px solid silver;
border-radius: 10px;
display: flex;
flex-wrap: wrap;
align-content: flex-start;
overflow-x: hidden;
overflow-y: auto;
width: 100%;
max-width: 530px;
height: 200px;
}