Files
CartriX/CartriX.Web/Pages/AddPrinterDialog.razor
T

372 lines
15 KiB
Plaintext

@using CartriX.Shared.DTOs.Printer
@using CartriX.Shared.DTOs.PrinterModel
@using System.Text.Json
@using CartriX.Shared.Models
@inject TooltipService tooltipService
@inject HttpClient Http
@inject NotificationService notificationService
<RadzenStack Gap="16px">
<RadzenStack Orientation="Orientation.Horizontal" AlignItems="AlignItems.Center" Wrap="FlexWrap.Wrap">
<RadzenLabel Text="IP Адрес принтера:" />
<RadzenTextBox Placeholder="123.255.255.255"
Style="flex: 1; min-width: 200px;"
@bind-Value="printerIpOrHostname" />
</RadzenStack>
<RadzenButton Text="отправить запрос"
ButtonStyle="ButtonStyle.Secondary"
Icon="content_paste_search"
BusyText="обработка запроса"
IsBusy="processingQueryToPrinter"
Click="@OnQueryPrinter" />
<RadzenTable>
<RadzenTableHeader>
<RadzenTableHeaderRow>
<RadzenTableHeaderCell ColSpan="2">
Результат запроса принтера
</RadzenTableHeaderCell>
</RadzenTableHeaderRow>
</RadzenTableHeader>
<RadzenTableBody>
<RadzenTableRow>
<RadzenTableCell>Имя хоста</RadzenTableCell>
<RadzenTableCell>
@if (processingQueryToPrinter)
{
<RadzenSkeleton Animation="SkeletonAnimation.Wave" Style="width: 100%;" />
}
else if (queryPrinterToAdd is not null)
{
@queryPrinterToAdd.hostname
}
else
{
<RadzenSkeleton Animation="SkeletonAnimation.None" Style="width: 100%;" />
}
</RadzenTableCell>
</RadzenTableRow>
<RadzenTableRow>
<RadzenTableCell>IP Адрес</RadzenTableCell>
<RadzenTableCell>
@if (processingQueryToPrinter)
{
<RadzenSkeleton Animation="SkeletonAnimation.Wave" Style="width: 100%;" />
}
else if (queryPrinterToAdd is not null)
{
@queryPrinterToAdd.ipAddress
}
else
{
<RadzenSkeleton Animation="SkeletonAnimation.None" Style="width: 100%;" />
}
</RadzenTableCell>
</RadzenTableRow>
<RadzenTableRow>
<RadzenTableCell>Модель принтера</RadzenTableCell>
<RadzenTableCell>
@if (processingQueryToPrinter)
{
<RadzenSkeleton Animation="SkeletonAnimation.Wave" Style="width: 100%;" />
}
else if (queryPrinterToAdd is not null)
{
@if (queryPrinterToAdd.model is null)
{
<div style="position: relative; display: flex; width: 100%; max-width: 400px;">
<RadzenAutoComplete Data="printerModels"
Placeholder="Модель XXX"
OpenOnFocus="true"
TextProperty="name"
@bind-Value="selectedPrinterModel"
MinLength="0"
FilterDelay="100"
FilterOperator="StringFilterOperator.Contains"
FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive"
Style="width: 100%;"
InputAttributes="@inputAttributes"
Name="AutoCompleteComboBox" />
<div class="rz-dropdown-trigger rz-corner-right" style="pointer-events: none;">
<span class="notranslate rz-dropdown-trigger-icon rzi rzi-chevron-down"></span>
</div>
</div>
}
else
{
@queryPrinterToAdd.model
}
}
else
{
<RadzenSkeleton Animation="SkeletonAnimation.None" Style="width: 100%;" />
}
</RadzenTableCell>
</RadzenTableRow>
<RadzenTableRow>
<RadzenTableCell>Местоположение</RadzenTableCell>
<RadzenTableCell>
@if (processingQueryToPrinter)
{
<RadzenSkeleton Animation="SkeletonAnimation.Wave" Style="width: 100%;" />
}
else if (queryPrinterToAdd is not null)
{
@if (queryPrinterToAdd.location is null)
{
<RadzenTextBox Style="width: 100%;"
@bind-Value="locationValue"
Placeholder="Кабинет XXX" />
}
else
{
@queryPrinterToAdd.location
}
}
else
{
<RadzenSkeleton Animation="SkeletonAnimation.None" Style="width: 100%;" />
}
</RadzenTableCell>
</RadzenTableRow>
</RadzenTableBody>
</RadzenTable>
<RadzenStack Orientation="Orientation.Horizontal" AlignItems="AlignItems.Center" Wrap="FlexWrap.Wrap">
<RadzenLabel Text="Имя принтера:" />
<RadzenTextBox Style="width: 100%; min-width: 200px; flex: 1;" Disabled="queryPrinterToAdd is null" @bind-Value="nameValue" Placeholder="Принтер XXX" />
</RadzenStack>
<RadzenStack Orientation="Orientation.Horizontal" AlignItems="AlignItems.Center" Wrap="FlexWrap.Wrap">
<RadzenLabel Text="Тип печати:" />
<RadzenSelectBar Data="printTypes" TValue="int" TextProperty="name" ValueProperty="id" Style="margin-left: auto;" Disabled="queryPrinterToAdd is null" @bind-Value="selectedTypePrintId" />
</RadzenStack>
<RadzenStack Orientation="Orientation.Horizontal" AlignItems="AlignItems.Center" Wrap="FlexWrap.Wrap">
<RadzenLabel Text="OID для опроса счетчика страниц:" />
<RadzenTextBox Disabled="queryPrinterToAdd is null" @bind-Value="OIDValue" Change="@(() => ResultOIDQuery = null!)" Style="flex: 1; min-width: 200px;" />
<RadzenButton Text="Опрос" Disabled="!PrinterDataIsFull()" Click="@(() => OnQueringPrinter(queryPrinterToAdd!.ipAddress, OIDValue))" IsBusy="processingQueryOIDToPrinter" BusyText="Опрос" Style="white-space: nowrap;" ButtonStyle="ButtonStyle.Secondary" />
<RadzenButton Icon="help" ButtonStyle="ButtonStyle.Light" Disabled="!PrinterDataIsFull()" />
</RadzenStack>
<RadzenStack Orientation="Orientation.Horizontal" AlignItems="AlignItems.Center" JustifyContent="JustifyContent.SpaceBetween">
<RadzenLabel Text="Результат опроса OID:" />
@if (processingQueryOIDToPrinter)
{
<RadzenSkeleton Animation="SkeletonAnimation.Wave" Style="width: 75px;" />
}
else if (ResultOIDQuery is not null)
{
@ResultOIDQuery
}
else
{
<RadzenSkeleton Animation="SkeletonAnimation.None" Style="width: 75px;" />
}
</RadzenStack>
<RadzenButton Text="Добавить"
Disabled="!CanAddPrinter()"
Click="@OnAddPrinter"
IsBusy="processingAddPrinter"
BusyText="добавление..."
ButtonStyle="ButtonStyle.Primary" />
</RadzenStack>
@code {
private string printerIpOrHostname = "";
private bool processingQueryToPrinter = false;
private bool processingQueryOIDToPrinter = false;
private bool processingAddPrinter = false;
private string? selectedPrinterModel;
private string? locationValue;
private string? nameValue;
private string OIDValue = "1.3.6.1.2.1.43.10.2.1.4.1.1";
private string ResultOIDQuery = null!;
private int selectedTypePrintId = 1;
private QueryPrinterToAdd? queryPrinterToAdd;
private List<PrinterModelDto> printerModels = new();
private List<PrintType> printTypes = new();
private readonly IReadOnlyDictionary<string, object> inputAttributes =
new Dictionary<string, object> { { "style", "padding-right: 2rem;" } };
protected override async Task OnInitializedAsync()
{
await LoadPrinterModels();
await LoadPrintTypes();
}
void ShowTooltip(ElementReference elementReference, TooltipOptions options = null!) => tooltipService.Open(elementReference, "OID для общего счётчика страниц.", options);
private async Task LoadPrintTypes()
{
try
{
printTypes = await Http.GetFromJsonAsync<List<PrintType>>(
"http://localhost:5197/api/Printer/PrintTypes") ?? new();
}
catch (Exception ex)
{
Console.WriteLine($"Ошибка загрузки типов печати: {ex.Message}");
ShowNotification("Ошибка", "Не удалось загрузить модели принтеров", NotificationSeverity.Error);
}
}
private async Task LoadPrinterModels()
{
try
{
printerModels = await Http.GetFromJsonAsync<List<PrinterModelDto>>(
"http://localhost:5197/api/Printer/Models") ?? new();
}
catch (Exception ex)
{
Console.WriteLine($"Ошибка загрузки моделей: {ex.Message}");
ShowNotification("Ошибка", "Не удалось загрузить модели принтеров", NotificationSeverity.Error);
}
}
private async Task OnQueryPrinter()
{
if (string.IsNullOrWhiteSpace(printerIpOrHostname))
{
ShowNotification("Ошибка", "Введите IP адрес или имя хоста", NotificationSeverity.Warning);
return;
}
processingQueryToPrinter = true;
selectedPrinterModel = null;
locationValue = null;
try
{
var response = await Http.GetAsync(
$"http://localhost:5197/api/Printer/GetSNMP/{printerIpOrHostname}");
if (response.IsSuccessStatusCode)
{
var jsonContent = await response.Content.ReadAsStringAsync();
queryPrinterToAdd = JsonConvert.DeserializeObject<QueryPrinterToAdd>(jsonContent);
}
else
{
ShowNotification("Ошибка", "Не удалось получить информацию о принтере", NotificationSeverity.Error);
}
nameValue = queryPrinterToAdd!.hostname!.Split(".")[0];
}
catch (Exception ex)
{
Console.WriteLine($"Ошибка: {ex.Message}");
ShowNotification("Ошибка", $"Ошибка подключения: {ex.Message}", NotificationSeverity.Error);
}
finally
{
processingQueryToPrinter = false;
}
}
private bool CanAddPrinter()
{
if (queryPrinterToAdd is null)
return false;
bool OIDValid = !string.IsNullOrWhiteSpace(ResultOIDQuery);
return PrinterDataIsFull() && OIDValid;
}
private bool PrinterDataIsFull()
{
if (queryPrinterToAdd is null)
return false;
bool modelValid = !string.IsNullOrWhiteSpace(queryPrinterToAdd.model) ||
!string.IsNullOrWhiteSpace(selectedPrinterModel);
bool locationValid = !string.IsNullOrWhiteSpace(queryPrinterToAdd.location) ||
!string.IsNullOrWhiteSpace(locationValue);
return modelValid && locationValid;
}
private async Task OnQueringPrinter(string ipAddress, string Oid)
{
ResultOIDQuery = null!;
processingQueryOIDToPrinter = true;
var response = await Http.GetAsync($"http://localhost:5197/api/Printer/GetSNMPOneData?{nameof(ipAddress)}={ipAddress}&{nameof(Oid)}={Oid}");
ResultOIDQuery = await response.Content.ReadAsStringAsync();
processingQueryOIDToPrinter = false;
}
private async Task OnAddPrinter()
{
if (!CanAddPrinter())
return;
processingAddPrinter = true;
try
{
CreatePrinterRequestDto request = new()
{
name = nameValue!,
hostname = queryPrinterToAdd!.hostname!,
ipAddress = queryPrinterToAdd!.ipAddress,
modelName = queryPrinterToAdd!.model! ?? selectedPrinterModel!,
locationValue = queryPrinterToAdd!.location ?? locationValue!,
typePrintId = selectedTypePrintId.ToString(),
oidForQueryCountPrintedAllTime = OIDValue,
countPrintedAllTime = ResultOIDQuery
};
var response = await Http.PostAsJsonAsync<CreatePrinterRequestDto>("http://localhost:5197/api/Printer/AddPrinter", request);
if (response.IsSuccessStatusCode)
{
ShowNotification("Успех", "Принтер добавлен", NotificationSeverity.Success);
ResetForm();
}
else
{
Console.WriteLine(JsonConvert.SerializeObject(request));
ShowNotification("Ошибка", "Не удалось добавить принтер", NotificationSeverity.Error);
}
}
catch (Exception ex)
{
Console.WriteLine($"Ошибка добавления: {ex.Message}");
ShowNotification("Ошибка", $"Ошибка: {ex.Message}", NotificationSeverity.Error);
}
finally
{
processingAddPrinter = false;
}
}
private void ResetForm()
{
printerIpOrHostname = "";
queryPrinterToAdd = null;
selectedPrinterModel = null;
locationValue = null;
ResultOIDQuery = null!;
}
private void ShowNotification(string title, string message, NotificationSeverity severity)
{
notificationService.Notify(new NotificationMessage
{
Severity = severity,
Summary = title,
Detail = message,
Duration = 3000
});
}
}