@implements IDisposable
@using Microsoft.AspNetCore.SignalR.Client
@using RobotNet.WebApp.Scripts.Models
@inject IHttpClientFactory httpFactory
@inject IDialogService DialogService
Danh sách Script Missions
STT
Tên
Parameters
Script
@context.Index
@context.Name
@if (context.Parameters?.Any() == true)
{
}
else
{
Không có
}
Thông tin Parameters
@if (_selectedParameters != null)
{
Tên
Kiểu
Default
@context.Name
@context.Type
@context.Default
}
Đóng
Mã Script đầy đủ
@_selectedCode
Đóng
@code {
[CascadingParameter]
private ProcessorHubClient ProcessorHub { get; set; } = null!;
private string TableHeight = "0px";
private ElementReference divRef;
private ElementReference toolbarRef;
private HttpClient http = default!;
private List scriptMissions = [];
// Dialog state
private bool _parametersDialogOpen = false;
private IEnumerable? _selectedParameters;
private bool _codeDialogOpen = false;
private string? _selectedCode;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await base.OnAfterRenderAsync(firstRender);
if (firstRender)
{
var rect = await divRef.MudGetBoundingClientRectAsync();
var toolbarRect = await toolbarRef.MudGetBoundingClientRectAsync();
TableHeight = $"{rect.Height - 25 - Math.Max(toolbarRect.Height, 64)}px";
StateHasChanged();
}
}
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
http = httpFactory.CreateClient("ScriptManagerAPI");
await ReloadData();
if (ProcessorHub.IsConnected)
{
OnProcessorStateChanged(await ProcessorHub.GetState());
}
ProcessorHub.StateChanged += OnProcessorStateChanged;
ProcessorHub.ConnectionStateChanged += OnConnectionStateChanged;
}
private void OnConnectionStateChanged(HubConnectionState state)
{
if (state == HubConnectionState.Connected)
{
_ = InvokeAsync(async Task () =>
{
OnProcessorStateChanged(await ProcessorHub.GetState());
});
}
}
private bool isRebuild = false;
private bool CallDisabled = true;
private void OnProcessorStateChanged(ProcessorState state)
{
if (state == ProcessorState.Building)
{
isRebuild = true;
}
else if (state == ProcessorState.Ready)
{
if (isRebuild)
{
isRebuild = false;
_ = InvokeAsync(async Task () =>
{
await ReloadData();
});
}
}
if (CallDisabled)
{
if (state == ProcessorState.Running)
{
CallDisabled = false;
StateHasChanged();
}
}
else
{
if (state != ProcessorState.Running)
{
CallDisabled = true;
StateHasChanged();
}
}
}
private async Task ReloadData()
{
scriptMissions.Clear();
var missions = await http.GetFromJsonAsync>("/api/ScriptMissions") ?? [];
foreach (var item in missions.Select((value, index) => (value, index)))
{
scriptMissions.Add(new ScriptMissionModel(item.index + 1, item.value));
}
StateHasChanged();
}
private void ShowParametersDialog(ScriptMissionModel model)
{
_selectedParameters = model.Parameters;
_parametersDialogOpen = true;
StateHasChanged();
}
private void CloseParametersDialog()
{
_parametersDialogOpen = false;
_selectedParameters = null;
StateHasChanged();
}
private void ShowCodeDialog(string code)
{
_selectedCode = code;
_codeDialogOpen = true;
StateHasChanged();
}
private void CloseCodeDialog()
{
_codeDialogOpen = false;
_selectedCode = null;
StateHasChanged();
}
private async Task ShowInstantiateMissionDialog(ScriptMissionModel model)
{
if (CallDisabled) return;
foreach (var parameter in model.Parameters)
{
parameter.Reset();
}
;
var parameters = new DialogParameters()
{
{x => x.Model, model},
};
var dialog = await DialogService.ShowAsync("Instantiate new mission", parameters);
var resultDialog = await dialog.Result;
}
public void Dispose()
{
ProcessorHub.StateChanged -= OnProcessorStateChanged;
ProcessorHub.ConnectionStateChanged -= OnConnectionStateChanged;
}
}