@page "/traffic-manager" @attribute [Authorize] @using Microsoft.AspNetCore.Components.WebAssembly.Authentication @using RobotNet.MapShares.Dtos @using RobotNet.RobotShares.Dtos @using RobotNet.RobotShares.Models @using RobotNet.Shares @using RobotNet.WebApp.Clients @using RobotNet.WebApp.Robots.Components @using RobotNet.WebApp.Robots.Components.Traffic @inject ISnackbar Snackbar @inject TrafficHubClient TrafficHub @inject IDialogService Dialog @inject IHttpClientFactory HttpFactory Traffic Manager
@foreach (var map in Maps) { @map.MapName } @* Add Lock *@
Nr RobotId Traffic State Target Node Release Node In Node Locked Node @(Table?.CurrentPage * Table?.RowsPerPage + AgentShow.IndexOf(context) + 1) @context.RobotId @context.State @(context.Nodes.Count > 0 ? (string.IsNullOrEmpty(context.Nodes[^1].Name) ? context.Nodes[^1].Id.ToString("N").Substring(0, 4) : context.Nodes[^1].Name) : "") @(context.ReleaseNode is null ? "" : string.IsNullOrEmpty(context.ReleaseNode.Name) ? context.ReleaseNode.Id.ToString("N").Substring(0, 4) : context.ReleaseNode.Name) @(context.InNode is null ? "" : string.IsNullOrEmpty(context.InNode.Name) ? context.InNode.Id.ToString("N").Substring(0, 4) : context.InNode.Name) @(context.LockedNodes.Count > 0 ? (string.IsNullOrEmpty(context.LockedNodes[^1].Name) ? context.LockedNodes[^1].Id.ToString("N").Substring(0, 4) : context.LockedNodes[^1].Name) : "")
Edit Delete
@DeactiveStr
Edit Agent @AgentUpdate.AgentId
No items found Add
@foreach (var node in AgentUpdate.LockedNodes) {
@(string.IsNullOrEmpty(node.Name) ? node.Id.ToString("N").Substring(0, 4) : node.Name)
}
Cancel Update
@code { private string? txtSearch { get; set; } private bool IsLoading { get; set; } private bool IsDeactive; private string DeactiveStr = "Robot Empty"; private MudTable? Table; private readonly List Agents = []; private List AgentShow = []; private int selectedRowNumber = -1; private TrafficAgentDto? AgentSelected; private List Maps = []; private TrafficMapDto? MapSelected; private TrafficAgentReview TrafficAgentReviewRef = default!; private bool IsEditAgentVisible; private readonly UpdateAgentLockerModel AgentUpdate = new(); private List Nodes = []; private NodeDto? SelectedNode; protected override async Task OnAfterRenderAsync(bool firstRender) { await base.OnAfterRenderAsync(firstRender); if (!firstRender) return; TrafficHub.TrafficAgentUpdated += TrafficAgentUpdated; TrafficHub.MapDeactive += MapDeactive; await TrafficHub.StartAsync(); await LoadTrafficMaps(); } private async Task LoadTrafficMaps() { IsLoading = true; StateHasChanged(); var trafficMaps = await TrafficHub.LoadTrafficMaps(); if (!trafficMaps.IsSuccess) Snackbar.Add(trafficMaps.Message, Severity.Warning); else if (trafficMaps.Data is null || !trafficMaps.Data.Any()) { Snackbar.Add("Không có bản đồ nào được tải lên", Severity.Warning); IsLoading = false; return; } else { Maps = [.. trafficMaps.Data]; if (Maps.Count > 0) { MapSelected = Maps.First(); Agents.Clear(); Agents.AddRange(MapSelected.Agents); Table?.ReloadServerData(); await TrafficHub.TrafficActive(MapSelected.MapId); } } IsLoading = false; StateHasChanged(); } private void TextSearchChanged(string text) { txtSearch = text; Table?.ReloadServerData(); } private bool FilterFunc(TrafficAgentDto robot) { if (string.IsNullOrWhiteSpace(txtSearch)) return true; if (robot.RobotId.Contains(txtSearch, StringComparison.OrdinalIgnoreCase)) return true; return false; } private Task> ReloadData(TableState state, CancellationToken _) { AgentShow.Clear(); var agents = new List(); Agents.ForEach(agent => { if (FilterFunc(agent)) agents.Add(agent); }); AgentShow = agents.Skip(state.Page * state.PageSize).Take(state.PageSize).ToList(); return Task.FromResult(new TableData() { TotalItems = agents.Count(), Items = AgentShow }); } private void RowClickEvent(TableRowClickEventArgs tableRowClickEventArgs) { } private string SelectedRowClassFunc(TrafficAgentDto element, int rowNumber) { if (selectedRowNumber == rowNumber && Table?.SelectedItem != null && !Table.SelectedItem.Equals(element)) { return string.Empty; } else if (selectedRowNumber == rowNumber && Table?.SelectedItem != null && Table.SelectedItem.Equals(element)) { return "selected"; } else if (Table?.SelectedItem != null && Table.SelectedItem.Equals(element)) { selectedRowNumber = rowNumber; AgentSelected = element; TrafficAgentReviewRef.UpdateState(AgentSelected); return "selected"; } else { return string.Empty; } } private async Task MapChanged() { if (MapSelected is not null) { var trafficMap = await TrafficHub.LoadTrafficMap(MapSelected.MapId); if (!trafficMap.IsSuccess) Snackbar.Add(trafficMap.Message, Severity.Warning); else if (trafficMap.Data is null) { Snackbar.Add("Không có agent nào trong bản đồ này", Severity.Warning); Agents.Clear(); AgentShow.Clear(); if (Table is not null) await Table.ReloadServerData(); } else { Agents.Clear(); Agents.AddRange(trafficMap.Data.Agents); if (Table is not null) await Table.ReloadServerData(); await TrafficHub.TrafficActive(MapSelected.MapId); } } } private void TrafficAgentUpdated(IEnumerable agents) { bool isNewUpdate = false; foreach (var newagent in agents) { var agent = Agents.FirstOrDefault(a => a.RobotId == newagent.RobotId); if (agent is null) { isNewUpdate = true; Agents.Add(newagent); } else { if ((agent.ReleaseNode is null && newagent.ReleaseNode is not null) || (agent.ReleaseNode is not null && newagent.ReleaseNode is null) || (agent.ReleaseNode is not null && newagent.ReleaseNode is not null && agent.ReleaseNode.Id != newagent.ReleaseNode.Id) || (agent.InNode is null && newagent.InNode is not null) || (agent.InNode is not null && newagent.InNode is null) || (agent.InNode is not null && newagent.InNode is not null && agent.InNode.Id != newagent.InNode.Id) || agent.State != newagent.State || (agent.Nodes.Count > 0 && newagent.Nodes.Count == 0) || (agent.Nodes.Count == 0 && newagent.Nodes.Count > 0) || (agent.Nodes.Count > 0 && newagent.Nodes.Count > 0 && agent.Nodes[^1].Id != newagent.Nodes[^1].Id) || (agent.LockedNodes.Count > 0 && newagent.LockedNodes.Count == 0) || (agent.LockedNodes.Count == 0 && newagent.LockedNodes.Count > 0) || (agent.LockedNodes.Count > 0 && newagent.LockedNodes.Count > 0 && agent.LockedNodes[^1].Id != newagent.LockedNodes[^1].Id) ) isNewUpdate = true; agent.Nodes = [.. newagent.Nodes]; agent.ReleaseNode = newagent.ReleaseNode; agent.LockedNodes = [.. newagent.LockedNodes]; agent.InNode = newagent.InNode; agent.State = newagent.State; agent.SubNodes = [.. newagent.SubNodes]; agent.GiveWayNodes = [.. newagent.GiveWayNodes]; agent.ConflictNode = newagent.ConflictNode; agent.ConflictAgentId = newagent.ConflictAgentId; } if (AgentSelected is not null && newagent.RobotId == AgentSelected.RobotId) TrafficAgentReviewRef.UpdateState(AgentSelected); } if (isNewUpdate) Table?.ReloadServerData(); } private void MapDeactive() { DeactiveStr = "Deactive"; IsDeactive = true; StateHasChanged(); TrafficHub.TrafficAgentUpdated -= TrafficAgentUpdated; TrafficHub.MapDeactive -= MapDeactive; _ = TrafficHub.StopAsync(); } private async Task DeleteAgent(TrafficAgentDto agent) { var parameters = new DialogParameters { { x => x.Content, "Bạn có chắc chắn muốn xóa agent đi không?" }, { x => x.ConfirmText, "Delete" }, { x => x.Color, Color.Secondary } }; var ConfirmDelete = await Dialog.ShowAsync("Xoá Agent", parameters); var result = await ConfirmDelete.Result; if (result is not null && result.Data is not null && bool.TryParse(result.Data.ToString(), out bool data) && data) { using var Http = HttpFactory.CreateClient("RobotManagerAPI"); var delete = await Http.DeleteFromJsonAsync($"api/TrafficManager/{MapSelected?.MapId}/{agent.RobotId}"); if (delete is null) Snackbar.Add("Lỗi giao tiếp với hệ thống", Severity.Error); else if (!delete.IsSuccess) Snackbar.Add($"Có lỗi xảy ra: {delete.Message}", Severity.Error); else { Agents.Remove(agent); Snackbar.Add("Xóa robot thành công!", Severity.Success); Table?.ReloadServerData(); } StateHasChanged(); } } private async Task OpenUpdateAgent(TrafficAgentDto agent) { AgentUpdate.AgentId = agent.RobotId; AgentUpdate.LockedNodes = agent.LockedNodes; AgentUpdate.MapId = MapSelected is null ? Guid.Empty : MapSelected.MapId; await LoadNodes(); IsEditAgentVisible = true; StateHasChanged(); } private async Task UpdateAgent() { using var Http = HttpFactory.CreateClient("RobotManagerAPI"); var result = await (await Http.PutAsJsonAsync($"api/TrafficManager", AgentUpdate)).Content.ReadFromJsonAsync(); if (result == null) Snackbar.Add("Lỗi giao tiếp với hệ thống", Severity.Error); else if (!result.IsSuccess) Snackbar.Add(result.Message, Severity.Error); else Snackbar.Add("Cập nhật thành công", Severity.Success); IsEditAgentVisible = false; StateHasChanged(); } public async Task LoadNodes() { try { using var Http = HttpFactory.CreateClient("MapManagerAPI"); var result = await Http.GetFromJsonAsync($"api/Nodes/{MapSelected?.MapId}"); if (result is not null) Nodes = result.Where(n => !string.IsNullOrEmpty(n.Name)).ToList(); } catch (AccessTokenNotAvailableException ex) { ex.Redirect(); } } private async Task> 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(NodeDto value) { if (SelectedNode is null || value.Id != SelectedNode.Id) SelectedNode = value; } private void AddNode() { if (SelectedNode is not null && !AgentUpdate.LockedNodes.Any(n => n.Id == SelectedNode.Id)) AgentUpdate.LockedNodes.Add(new() { Id = SelectedNode.Id, X = SelectedNode.X, Y = SelectedNode.Y, Name = SelectedNode.Name, }); StateHasChanged(); } public async ValueTask DisposeAsync() { TrafficHub.TrafficAgentUpdated -= TrafficAgentUpdated; TrafficHub.MapDeactive -= MapDeactive; await TrafficHub.StopAsync(); } }