@page "/robots/detail/{RobotId}"
@attribute [Authorize]
@implements IAsyncDisposable
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using RobotNet.RobotShares.Dtos
@using RobotNet.Shares
@using RobotNet.WebApp.Clients
@using RobotNet.WebApp.Robots.Components.Robot
@inject RobotHubClient RobotHub
@inject IHttpClientFactory HttpFactory
Robot Detail
@code {
[Parameter]
public string RobotId { get; set; } = "";
private RobotDto Robot = new();
private RobotInfomation RobotInfomationRef = default!;
private RobotAction RobotActionRef = default!;
private RobotTestRandom RobotTestRandomRef = default!;
private bool IsDeactive;
private string DeactiveStr = "Robot Empty";
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await base.OnAfterRenderAsync(firstRender);
if (!firstRender) return;
RobotHub.VDA5050InfoChanged += VDA5050InfoChanged;
RobotHub.RobotDetailDeactive += RobotDetailDeactive;
await RobotHub.StartAsync();
await LoadRobot();
}
private async Task LoadRobot()
{
try
{
IsDeactive = true;
StateHasChanged();
using var Http = HttpFactory.CreateClient("RobotManagerAPI");
var result = await Http.GetFromJsonAsync>($"api/Robots/{RobotId}");
if (result is null || result.Data is null || !result.IsSuccess) return;
Robot = result.Data;
await RobotHub.RobotDetailActive(RobotId);
IsDeactive = false;
StateHasChanged();
await RobotActionRef.LoadNodes();
await RobotActionRef.LoadActionAsync();
await RobotTestRandomRef.LoadNodes();
}
catch (AccessTokenNotAvailableException ex)
{
ex.Redirect();
}
}
private void VDA5050InfoChanged(RobotVDA5050StateDto state)
{
if (state.RobotId == RobotId)
{
Robot.Online = state.Online;
RobotInfomationRef.UpdateState(state);
RobotActionRef.UpdateState(state.Online, state.IsWorking);
RobotTestRandomRef.UpdateState(state.Online, state.IsWorking);
}
StateHasChanged();
}
private void RobotDetailDeactive()
{
DeactiveStr = "Deactive";
IsDeactive = true;
StateHasChanged();
RobotHub.VDA5050InfoChanged -= VDA5050InfoChanged;
RobotHub.RobotDetailDeactive -= RobotDetailDeactive;
_ = RobotHub.StopAsync();
}
public async ValueTask DisposeAsync()
{
RobotHub.VDA5050InfoChanged -= VDA5050InfoChanged;
RobotHub.RobotDetailDeactive -= RobotDetailDeactive;
await RobotHub.StopAsync();
}
}