@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 Результат запроса принтера Имя хоста @if (processingQueryToPrinter) { } else if (queryPrinterToAdd is not null) { @queryPrinterToAdd.hostname } else { } IP Адрес @if (processingQueryToPrinter) { } else if (queryPrinterToAdd is not null) { @queryPrinterToAdd.ipAddress } else { } Модель принтера @if (processingQueryToPrinter) { } else if (queryPrinterToAdd is not null) { @if (queryPrinterToAdd.model is null) {
} else { @queryPrinterToAdd.model } } else { }
Местоположение @if (processingQueryToPrinter) { } else if (queryPrinterToAdd is not null) { @if (queryPrinterToAdd.location is null) { } else { @queryPrinterToAdd.location } } else { }
@if (processingQueryOIDToPrinter) { } else if (ResultOIDQuery is not null) { @ResultOIDQuery } else { }
@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 printerModels = new(); private List printTypes = new(); private readonly IReadOnlyDictionary inputAttributes = new Dictionary { { "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>( "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>( "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(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("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 }); } }