Files
mapo-iob-man/IOB-MAN/BlazorForm.cs
T
2025-06-17 09:06:10 +02:00

238 lines
7.9 KiB
C#

using IOB_MAN8.Core.Data;
using IOB_MAN8.Core.Services;
using Microsoft.AspNetCore.Components.WebView.WindowsForms;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Win32;
using NLog;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.DirectoryServices.ActiveDirectory;
using System.Drawing;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace IOB_MAN8.App
{
public partial class BlazorForm : Form
{
#region Public Constructors
public BlazorForm()
{
InitializeComponent();
ServiceInit();
InitBlazorView();
StartTimer();
}
#endregion Public Constructors
#region Protected Fields
/// <summary>
/// Dataora prossima scadenza riavvio automatico
/// </summary>
protected DateTime tOutAutocheck = DateTime.Now;
#endregion Protected Fields
#region Private Fields
/// <summary>
/// Classe logger
/// </summary>
private static Logger Log = LogManager.GetCurrentClassLogger();
#endregion Private Fields
#region Private Properties
private static AppControlService ACService { get; set; } = null!;
private static FluxLogManService FLMService { get; set; } = null!;
private Size lastSize { get; set; } = new Size(1200, 700);
#endregion Private Properties
#region Private Methods
/// <summary>
/// Esecuzione del processo di restart
/// </summary>
private static void DoRestart(bool fullRestart)
{
// full restart usa sw esterno, altrimenti solo app che riparte SENZA check update esterno
if (fullRestart)
{
// uso restarter esterno...
Assembly assembly = Assembly.GetExecutingAssembly();
string startDir = Path.GetDirectoryName(assembly.Location)!;
string extPath = Path.Combine(startDir, "libs", "EgwAccRestarter.exe");
// uso processstartInfo x nascondere finestra
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.CreateNoWindow = true;
startInfo.FileName = extPath;
Process.Start(startInfo);
}
else
{
Application.Restart();
Environment.Exit(0);
}
}
/// <summary>
/// Riavvio il timer...
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private void ACService_EA_ConfigUpdated()
{
timerCheck.Stop();
timerTask.Stop();
tOutAutocheck = ACService.VetoAutoCheck;
StartTimer();
}
private void ACService_EA_ReloadRequested()
{
// effettua chiamata reload
DoRestart(false);
}
/// <summary>
/// Gestione evento restart applicazione
/// </summary>
private void ACService_EA_RestartRequested()
{
// effettua chiamata restart
DoRestart(true);
}
private void BlazorForm_FormClosing(object sender, FormClosingEventArgs e)
{
ACService.EA_ConfigUpdated -= ACService_EA_ConfigUpdated;
ACService.EA_RestartRequested -= ACService_EA_RestartRequested;
ACService.EA_ReloadRequested -= ACService_EA_ReloadRequested;
FLMService.EA_FluxLogReq -= FLMService_EA_FluxLogReq;
timerCheck.Stop();
timerCheck.Dispose();
timerTask.Stop();
timerTask.Dispose();
ACService.DoCloseAll(false);
}
/// <summary>
/// Evento completamento caricamento app
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BlazorForm_Load(object sender, EventArgs e)
{
SetPosition();
}
private void InitBlazorView()
{
Stopwatch sw = new Stopwatch();
sw.Start();
try
{
Log.Trace($"TrayMenu OK | {sw.ElapsedMilliseconds}ms");
var services = new ServiceCollection();
Log.Trace($"Add ServiceCollection | {sw.ElapsedMilliseconds}ms");
services.AddWindowsFormsBlazorWebView();
Log.Trace($"Add AddWindowsFormsBlazorWebView | {sw.ElapsedMilliseconds}ms");
services.AddSingleton<AppControlService>(ACService);
services.AddSingleton<FluxLogManService>(FLMService);
Log.Trace($"Add Singleton Services | {sw.ElapsedMilliseconds}ms");
blazorWebView1.HostPage = "wwwroot\\index.html";
blazorWebView1.Services = services.BuildServiceProvider();
blazorWebView1.RootComponents.Add<MainBlazor>("#app");
Log.Trace($"Add MainBlazor | {sw.ElapsedMilliseconds}ms");
}
catch (Exception exc)
{
Log.Error($"Exception during setartup{Environment.NewLine}{exc}");
}
}
private void ServiceInit()
{
//init preliminare servizio controllo
ACService = new AppControlService(true);
ACService.EA_ConfigUpdated += ACService_EA_ConfigUpdated;
ACService.EA_RestartRequested += ACService_EA_RestartRequested;
ACService.EA_ReloadRequested += ACService_EA_ReloadRequested;
FLMService = new FluxLogManService();
FLMService.EA_FluxLogReq += FLMService_EA_FluxLogReq;
}
/// <summary>
/// Richiesta apertura nuova form x editing FluxLog: la implemento leggendo il codIOB e lo passo al controllo...
/// </summary>
/// <param name="codIOB"></param>
private void FLMService_EA_FluxLogReq(string codIOB)
{
// chiamo nuova form con parametro x CodIOB richiesto
FluxLogData FldForm = new FluxLogData(FLMService, codIOB);
FldForm.Show();
}
private void SetPosition()
{
// posiziono in basso a sx...
Rectangle workArea = Screen.GetWorkingArea(this);
this.Location = new Point((workArea.Right - lastSize.Width) / 2, (workArea.Bottom - lastSize.Height) / 2);
}
private void StartTimer()
{
// timer controlli da conf
timerCheck.Interval = (ACService.RefreshPeriod);
timerTask.Interval = (ACService.CheckRestartPeriod);
// avvio timer
timerCheck.Start();
timerTask.Start();
}
/// <summary>
/// Esecuzione task di verifica stato app
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void timerCheck_Tick(object sender, EventArgs e)
{
// fermo task...
timerCheck.Stop();
await ACService.DoScan();
// riavvio task x evitare sovrapposizioni in debug
timerCheck.Start();
}
/// <summary>
/// Timer task x eventuale riavvio processi fermi SE abilitato...
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timerTask_Tick(object sender, EventArgs e)
{
// fermo task...
timerTask.Stop();
// esegue controllo task x eventuale autorestart
ACService.DoAutoRestart(false);
// riavvio task x evitare sovrapposizioni in debug
timerTask.Start();
}
#endregion Private Methods
}
}