Files
mapo-iob-man/IOB-MAN/FluxLogData.cs
T
2025-11-28 09:56:51 +01:00

112 lines
4.4 KiB
C#

/// <summary>
/// Dedicated Windows Forms form for viewing detailed flux log data of a specific IOB (Industrial Operation Block).
///
/// This component serves as a specialized viewer that loads a Blazor-based UI to display real-time or historical flux logs
/// for a given IOB (e.g., PLC, HMI) using a specific CodIOB.
///
/// Key Features:
/// - Takes a CodIOB (code identifier) and a FluxLogManService instance to initialize the view.
/// - Hosts a Blazor WebAssembly UI via Windows Forms BlazorWebView.
/// - Loads a route in the Blazor app (e.g., /FlogDetail/{codIob}) to display logs for the selected IOB.
/// - Provides a focused, isolated environment for log inspection without interfering with the main dashboard.
/// </summary>
using IOB_MAN.Core.Services;
using Microsoft.AspNetCore.Components.WebView.WindowsForms;
using Microsoft.Extensions.DependencyInjection;
using NLog;
using System.Diagnostics;
namespace IOB_MAN
{
public partial class FluxLogData : Form
{
#region Public Constructors
/// <summary>
/// Initializes a new instance of the FluxLogData form.
///
/// Parameters:
/// - mainFLMService: The FluxLogManService instance responsible for managing log data and access.
/// - CodIOB: The unique identifier (code) of the IOB for which logs are being viewed.
///
/// Initializes the form, sets the IOB code, and configures the Blazor view to display logs.
/// </summary>
/// <param name="mainFLMService">Service providing access to flux log data and operations.</param>
/// <param name="CodIOB">The IOB code (e.g., "PLC001") for which detailed logs are requested.</param>
public FluxLogData(FluxLogManService mainFLMService, string CodIOB)
{
InitializeComponent();
codIob = CodIOB;
FLMService = mainFLMService;
InitBlazorView();
}
#endregion Public Constructors
#region Private Fields
/// <summary>
/// Logger instance for structured logging within this form.
/// Used to capture errors during startup or UI rendering.
/// </summary>
private static Logger Log = LogManager.GetCurrentClassLogger();
/// <summary>
/// The IOB code (e.g., "PLC001") associated with the logs being displayed.
/// Used to route the correct data in the Blazor component.
/// </summary>
private string codIob = "";
#endregion Private Fields
#region Private Properties
/// <summary>
/// Injected service for managing flux log operations.
/// Responsible for retrieving, filtering, and presenting log data.
/// </summary>
private static FluxLogManService FLMService { get; set; } = null!;
#endregion Private Properties
#region Private Methods
/// <summary>
/// Initializes the Blazor WebView to display detailed flux log data for the specified IOB.
///
/// Steps:
/// 1. Creates a ServiceCollection and adds the BlazorWebView support.
/// 2. Registers the FluxLogManService as a singleton to provide log data access.
/// 3. Sets the host page to index.html.
/// 4. Configures the root component to render MainBlazor within the "#app" element.
/// 5. Sets the initial navigation path to /FlogDetail/{codIob} so the Blazor app loads the correct log view.
///
/// Exception Handling:
/// - Catches any startup errors and logs them for debugging.
/// </summary>
private void InitBlazorView()
{
Stopwatch sw = new Stopwatch();
sw.Start();
try
{
var services = new ServiceCollection();
services.AddWindowsFormsBlazorWebView();
services.AddSingleton<FluxLogManService>(FLMService);
blazorWebView1.HostPage = "wwwroot\\index.html";
blazorWebView1.Services = services.BuildServiceProvider();
blazorWebView1.RootComponents.Add<MainBlazor>("#app");
// Navigate to the detailed log page for the selected IOB
blazorWebView1.StartPath = $"/FlogDetail/{codIob}";
}
catch (Exception exc)
{
Log.Error($"Exception during startup{Environment.NewLine}{exc}");
}
}
#endregion Private Methods
}
}