@implements IDisposable
@using Microsoft.AspNetCore.SignalR.Client
@using RobotNet.Clients
@using RobotNet.Shares
@inject IHttpClientFactory httpFactory
@inject ISnackbar Snackbar
@inject IDialogService DialogService
Name
Type
Value
@context.Index
@context.Name
@context.TypeName
@context.Value
@code {
[CascadingParameter]
private ProcessorHubClient ProcessorHub { get; set; } = null!;
private string TableHeight = "0px";
private ElementReference divRef;
private ElementReference toolbarRef;
private HttpClient http = default!;
private List _items = [];
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 CallDisabled = true;
private void OnProcessorStateChanged(ProcessorState state)
{
if (state == ProcessorState.Ready)
{
_ = 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()
{
_items.Clear();
var vars = await http.GetFromJsonAsync>("/api/ScriptVariables") ?? [];
foreach (var item in vars.Select((value, index) => (value, index)))
{
_items.Add(new ScriptVariableModel(item.index + 1, item.value));
}
StateHasChanged();
}
private async Task ResetValue(ScriptVariableModel model)
{
if (CallDisabled) return;
var result = await http.PutFromJsonAsync($"/api/ScriptVariables/{model.Name}/Reset", new UpdateVariableModel(model.Name, ""));
if(result == null)
{
Snackbar.Add("Failed to reset variable value: server response null", Severity.Error);
}
else if (result.IsSuccess)
{
Snackbar.Add(result.Message, Severity.Success);
}
else
{
Snackbar.Add($"Failed to reset variable value: {result.Message}", Severity.Error);
}
}
private async Task ShowSetValueDialog(ScriptVariableModel model)
{
if (CallDisabled) return;
var parameters = new DialogParameters()
{
{x => x.Model, model},
};
var dialog = await DialogService.ShowAsync("Set Variable Value", parameters);
var resultDialog = await dialog.Result;
}
public void Dispose()
{
ProcessorHub.StateChanged -= OnProcessorStateChanged;
ProcessorHub.ConnectionStateChanged -= OnConnectionStateChanged;
}
}