Files
mapo-iob-man/IOB-MAN/Components/Compo/ApplicationCheck.razor.cs
T
2025-07-24 18:59:00 +02:00

526 lines
14 KiB
C#

using IOB_MAN.Core;
using IOB_MAN.Core.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using NLog;
using System.Diagnostics;
using System.Runtime.InteropServices;
using static IOB_MAN.Core.CoreEnum;
namespace IOB_MAN.Components.Compo
{
public partial class ApplicationCheck : IDisposable
{
#region Public Methods
public void Dispose()
{
ACService.EA_StatusUpdated -= ACService_EA_StatusUpdated;
}
#endregion Public Methods
#region Protected Fields
protected string iobTypeSel = "";
protected List<int> PageSizeDispl = new List<int>() { 5, 10, 15, 20, 25, 50 };
#endregion Protected Fields
#region Protected Properties
[Inject]
protected AppControlService ACService { get; set; } = null!;
protected bool AutoRestart
{
get => ACService.AutoRestartEnabled;
set
{
if (ACService.AutoRestartEnabled != value)
{
ACService.AutoRestartEnabled = value;
// eseguo check/riavvio...
if (value)
{
ACService.DoReopenClosed();
}
else
{
countAutoRestart = "0000";
ACService.DelayRestart(!value);
}
currPage = 1;
ForceReload();
}
}
}
protected string contextMenuStyle
{
get => $"position: absolute; top: {menuYpx}; left: {menuXpx}; z-index:999;";
}
protected string cssCheckProc
{
get
{
string answ = "text-light";
if (NumProcStarted < NumProcConfig)
{
answ = "text-info";
}
else
{
double ratio = (double)NumProcRunning / NumProcStarted;
if (ratio < 0.5)
{
answ = "text-danger";
}
else if (ratio < 1)
{
answ = "text-warning";
}
}
return answ;
}
}
protected string cssProcAttivi
{
get
{
string answ = "text-success";
double ratio = (double)NumProcRunning / NumProcStarted;
if (ratio < 0.5 || NumProcStarted == 0)
{
answ = "text-danger";
}
else if (ratio < 1)
{
answ = "text-warning";
}
return answ;
}
}
protected string cssProcAvviati
{
get
{
string answ = "text-light";
double ratio = (double)NumProcStarted / NumProcConfig;
if (ratio < 0.5)
{
answ = "text-danger";
}
else if (ratio < 1)
{
answ = "text-warning";
}
return answ;
}
}
protected List<string> CurrIobType
{
get => ACService.CurrIobType;
}
[Inject]
protected FluxLogManService FLMService { get; set; } = null!;
protected string IobTypeSel
{
get => iobTypeSel;
set
{
if (iobTypeSel != value)
{
iobTypeSel = value;
ForceReload();
}
}
}
[Inject]
protected IJSRuntime JSRuntime { get; set; } = null!;
/// <summary>
/// Livello Log impostato
/// </summary>
protected LogLevelIob LogLevel
{
get => ACService.LogLevel;
set => ACService.LogLevel = value;
}
[Inject]
protected NavigationManager NavMan { get; set; } = null!;
protected int NumProcConfig
{
get => ACService.NumProcConfig;
}
protected int NumProcRunning
{
get => ACService.NumProcRunning;
}
protected int NumProcStarted
{
get => ACService.NumProcStarted;
}
protected int NumTypeConfig
{
get => ACService.NumTypeConfig;
}
#endregion Protected Properties
#region Protected Methods
protected string CssClassIob(IobAdapt currRec)
{
string css = !currRec.isRunning ? "bg-danger bg-opacity-25" : "";
return css;
}
protected void DelayRestart()
{
ACService.DelayRestart(false);
}
protected async Task DoCloseAll()
{
// chiude tutto
CloseAllChild(false);
await ACService.DoScan();
//ForceReload();
}
protected void DoCloseChild(IobAdapt currRec)
{
ACService.DoCloseChild(currRec);
dxMenuVisible = false;
}
/// <summary>
/// Nasconde gli IOB
/// </summary>
/// <returns></returns>
protected void DoHideAll()
{
foreach (var item in CurrRecords)
{
try
{
Process p = Process.GetProcessById(item.pID);
// cerco e chiudo quelli che mi interessano...
var windowsHandle = p.MainWindowHandle;
ShowWindowAsync(windowsHandle, CoreEnum.SW_SHOWMINIMIZED);
}
catch (Exception exc)
{
// errore era già chiuso..
Log.Error($"Errore in HIDE windows:{Environment.NewLine}{exc}");
}
}
}
protected async Task DoRestartAll(bool reloadConf)
{
// chiude tutto
CloseAllChild(true);
// se richiesto rilettura conf riesegue anche quello...
if (reloadConf)
{
await ACService.DoScan();
ACService.DoReloadConfig();
}
ACService.DoOpenAllChild();
await ACService.DoScan();
//ForceReload();
}
/// <summary>
/// Mostra in primo piano tutte le finestre IOB
/// </summary>
protected void DoShowAll()
{
foreach (var item in CurrRecords)
{
try
{
Process p = Process.GetProcessById(item.pID);
// cerco e chiudo quelli che mi interessano...
var windowsHandle = p.MainWindowHandle;
ShowWindowAsync(windowsHandle, CoreEnum.SW_SHOWNORMAL);
}
catch (Exception exc)
{
// errore era già chiuso..
Log.Error($"Errore in SHOW windows:{Environment.NewLine}{exc}");
}
}
}
/// <summary>
/// Mostra in primo piano tutte le finestre IOB
/// </summary>
protected void DoShowChild(IobAdapt item)
{
try
{
Process p = Process.GetProcessById(item.pID);
// cerco e chiudo quelli che mi interessano...
var windowsHandle = p.MainWindowHandle;
ShowWindowAsync(windowsHandle, CoreEnum.SW_SHOWNORMAL);
}
catch (Exception exc)
{
// errore era già chiuso..
Log.Error($"Errore in SHOW windows:{Environment.NewLine}{exc}");
}
}
protected void DoStartChild(IobAdapt currRec)
{
ACService.DoOpenChildSel(currRec);
dxMenuVisible = false;
}
protected async Task ForceCheck()
{
isLoading = true;
ListRecords = new List<IobAdapt>();
await ACService.DoScan();
await Task.Delay(1);
isLoading = false;
}
protected override void OnInitialized()
{
ACService.EA_StatusUpdated += ACService_EA_StatusUpdated;
}
protected override async Task OnParametersSetAsync()
{
await ACService.DoScan();
ForceReload();
}
protected void SetNumRec(int newNum)
{
numRecord = newNum;
currPage = 1;
ForceReload();
}
protected void SetPage(int newNum)
{
currPage = newNum;
ForceReload();
}
#endregion Protected Methods
#region Private Fields
/// <summary>
/// Classe logger
/// </summary>
private static Logger Log = LogManager.GetCurrentClassLogger();
private string countAutoRestart = "";
#if false
private int currVal = 100;
private int nextVal = 100;
#endif
private bool dxMenuVisible = false;
private bool enableKillTask = true;
private string menuXpx = "0px";
private string menuYpx = "0px";
private IobAdapt? selIOB = null;
#endregion Private Fields
#region Private Properties
private string ComputerName
{
get => Environment.MachineName;
}
/// <summary>
/// Css e abilitazione chiusura (solo se c'è qualcosa da chiudere)
/// </summary>
private string cssBtnCloseAll
{
get => NumProcStarted > 0 ? "" : "disabled opacity-50";
}
private int currPage { get; set; } = 1;
private List<IobAdapt> CurrRecords
{
get => ACService.ListIobAdapters;
}
private string DomainName
{
get => Environment.UserDomainName;
}
private bool isLoading { get; set; } = false;
private List<IobAdapt>? ListRecords { get; set; } = null;
private int numRecord { get; set; } = 20;
private List<IobAdapt>? SearchRecords { get; set; } = null;
private string selIobTgtPath
{
get
{
string answ = selIOB != null ? ACService.IobTgtPath(selIOB.CodIOB) : "";
return answ;
}
}
private int totalCount { get; set; } = 0;
private string UserName
{
get => Environment.UserName;
}
#endregion Private Properties
#region Private Methods
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
private void ACService_EA_StatusUpdated()
{
// verifico gestione atuorestart...
if (!AutoRestart)
{
var remSec = (int)ACService.VetoAutoCheck.Subtract(DateTime.Now).TotalSeconds;
countAutoRestart = remSec > 0 ? $"{remSec:N0}" : "!!!";
// se va a zero... abilito riavvio!
if (remSec <= 0)
{
AutoRestart = true;
}
}
ForceReload();
}
/// <summary>
/// Chiude tutti i child
/// </summary>
/// <param name="doReset">resetta elenco</param>
private void CloseAllChild(bool doReset)
{
enableKillTask = false;
ACService.DoCloseAll(doReset);
enableKillTask = true;
}
private async Task ForceReboot(Microsoft.AspNetCore.Components.Web.MouseEventArgs args)
{
if (!await JSRuntime.InvokeAsync<bool>("confirm", "Sicuro di voler riavviare il programma?"))
return;
// richiesta riavvio
ACService.RaiseRebootReq();
}
private void ForceReload()
{
isLoading = true;
SearchRecords = CurrRecords
.Where(x => string.IsNullOrEmpty(IobTypeSel) || x.TgtName == IobTypeSel)
.ToList();
totalCount = SearchRecords.Count;
ListRecords = SearchRecords
.OrderBy(x => x.CodIOB)
.Skip(numRecord * (currPage - 1))
.Take(numRecord)
.ToList();
isLoading = false;
StateHasChanged();
}
private void IobFolderOpenApp()
{
if (selIOB != null)
Process.Start("explorer.exe", selIobTgtPath);
// chiudo e resetto selezione
selIOB = null;
dxMenuVisible = false;
}
private void IobFolderOpenConf()
{
if (selIOB != null)
Process.Start("explorer.exe", Path.Combine(selIobTgtPath, "DATA", "CONF"));
// chiudo e resetto selezione
selIOB = null;
dxMenuVisible = false;
}
private void IobFolderOpenLog()
{
if (selIOB != null)
Process.Start("explorer.exe", Path.Combine(selIobTgtPath, "logs", selIOB.CodIOB));
// chiudo e resetto selezione
selIOB = null;
dxMenuVisible = false;
}
private void NavAbout()
{
NavMan.NavigateTo("About");
}
private void OpenFLForm()
{
if (selIOB != null)
FLMService.RequestLoadFLogData(selIOB.CodIOB);
// chiudo e resetto selezione
selIOB = null;
dxMenuVisible = false;
}
private void RightClick(Microsoft.AspNetCore.Components.Web.MouseEventArgs e, IobAdapt currRec)
{
selIOB = currRec;
if (e.Button == 2)
{
dxMenuVisible = true;
menuXpx = $"{e.ClientX}px";
menuYpx = $"{e.ClientY - 90}px";
}
}
#endregion Private Methods
}
}