Files
mapo-core/MP-TAB3/Components/MachineBlock.razor.cs
T
2025-02-24 18:33:20 +01:00

509 lines
16 KiB
C#

using EgwCoreLib.Razor;
using global::Microsoft.AspNetCore.Components;
using Microsoft.EntityFrameworkCore.SqlServer.Query.Internal;
using Microsoft.JSInterop;
using MP.Data.DatabaseModels;
using MP.Data.Services;
using NLog;
using System;
namespace MP_TAB3.Components
{
public partial class MachineBlock
{
#region Public Properties
public string currIpv4 { get; set; } = "";
[Parameter]
public bool doBlink { get; set; } = false;
[Parameter]
public bool FullMode { get; set; } = true;
[Parameter]
public int Height { get; set; } = 0;
[Parameter]
public string IdxMacchSub { get; set; } = "";
[Parameter]
public int keepAliveMin { get; set; } = 5;
[Parameter]
public MappaStatoExpl? RecMSE { get; set; } = null;
[Parameter]
public bool showCard { get; set; }
[Parameter]
public int Width { get; set; } = 0;
#endregion Public Properties
#region Public Methods
/// <summary>
/// formatta la durata in minuti in un modo "human readable" gg/ore/min
/// </summary>
/// <param name="minuti">minuti</param>
/// <returns></returns>
public static string FormatDurata(double? minuti)
{
string answ = "??";
TimeSpan tempo = TimeSpan.FromMinutes(minuti ?? 0);
if (tempo.TotalMinutes < 60)
{
answ = $"{tempo.Minutes:00}:{tempo.Seconds:00}";
}
else if (tempo.TotalHours < 24)
{
answ = $"{tempo.Hours}h {tempo.Minutes}m";
}
else
{
answ = $"{tempo.Days}g {tempo.Hours}h";
}
return answ;
}
/// <summary>
/// url completo immagine
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public string ImgUrlMacc(string url)
{
if (string.IsNullOrEmpty(url.ToString()))
{
url = "Steamware.png";
}
string fullPath = Path.Combine(imgBasePath, url);
return fullPath;
}
protected int NumScartiConf
{
get => datiProdAct != null ? datiProdAct.PzConfScarto - datiProdAct.PzConfRilav : 0;
}
#endregion Public Methods
#region Protected Fields
protected string baseCss = "sem";
protected int currMaxVal = 1000;
protected List<CircleGaugeMulti.CircSegm> innerCircleVals = new List<CircleGaugeMulti.CircSegm>();
protected int kaFactor = 60 / 2;
protected int maxVal = 1000;
protected List<CircleGaugeMulti.CircSegm> outerCircleVals = new List<CircleGaugeMulti.CircSegm>();
#endregion Protected Fields
#region Protected Properties
[Inject]
protected IConfiguration config { get; set; } = null!;
/// <summary>
/// CSS Class bordo da stato macchina
/// </summary>
protected string cssClassBorder
{
get
{
string answ = "border-3";
// se blink --> bianco!
if (RecMSE != null)
{
switch (RecMSE.Semaforo)
{
case "sGi":
answ += doBlink ? " border-secondary shadow shadow-primary" : " border-warning shadow shadow-warning";
break;
case "sRo":
answ += doBlink ? " border-secondary shadow shadow-primary" : " border-danger shadow shadow-danger";
break;
case "sGr":
answ += " border-dark";
break;
case "sBl":
answ += " border-primary";
break;
case "sVe":
answ += " border-success";
break;
default:
answ += " border-secondary";
break;
}
}
return answ;
}
}
/// <summary>
/// CSS Class x overlay (effetto spento x macchina spenta/ sGr)
/// </summary>
protected string cssClassOverlay
{
get
{
string answ = "";
if (RecMSE != null)
{
answ = RecMSE.Semaforo == "sGr" ? "bg-dark opacity-50" : "";
}
return answ;
}
}
/// <summary>
/// CSS class x testo (se descr lunga scorre...)
/// </summary>
protected string cssClassTextDescr
{
get
{
string answ = "text-nowrap";
if (RecMSE != null && RecMSE.DescrizioneStato.Length >= 17)
{
answ = " scroll-left";
}
return answ;
}
}
protected ProdAdvDispl.ProdCounter CurrCount
{
get
{
ProdAdvDispl.ProdCounter answ = new ProdAdvDispl.ProdCounter();
if (RecMSE != null)
{
answ = new ProdAdvDispl.ProdCounter()
{
numPzConf = (int)RecMSE.PezziConf,
numPzOrd = (int)RecMSE.NumPezzi,
numPzProd = (int)RecMSE.PezziProd
};
}
return answ;
}
}
/// <summary>
/// Dati produzione rilevati
/// </summary>
protected StatoProdModel? datiProdAct { get; set; } = null;
[Inject]
protected IJSRuntime JSRuntime { get; set; } = null!;
[Inject]
protected MessageService MServ { get; set; } = null!;
[Inject]
protected NavigationManager NavMan { get; set; } = null!;
[Inject]
protected StatusData SDService { get; set; } = null!;
[Inject]
protected SharedMemService SMServ { get; set; } = null!;
[Inject]
protected TabDataService TabDServ { get; set; } = null!;
#endregion Protected Properties
#region Protected Methods
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await InvokeAsync(StateHasChanged);
}
isLoading = RecMSE == null;
isMobile = await JSRuntime.InvokeAsync<bool>("isDevice");
await Task.Delay(1);
}
protected override async Task OnInitializedAsync()
{
isLoading = true;
// abilitazione disegni...
TabDServ.ConfigGetVal("enableDisegno", ref enableDisegno);
await ReloadXDL(true);
// se configurata uso cartella virtuale... altrimenti cartella processo
var sImgBasePath = config.GetValue<string>("ServerConf:ImgBasePath") ?? (config.GetValue<string>("OptConf:ImgBasePath") ?? "");
if (!string.IsNullOrEmpty(sImgBasePath))
{
imgBasePath = sImgBasePath;
}
else
{
imgBasePath = $"{Environment.CurrentDirectory}/images/";
}
await Task.Delay(1);
}
protected override async Task OnParametersSetAsync()
{
DateTime adesso = DateTime.Now;
isLoading = RecMSE == null;
// controllo SE ho variazioni così rileggo
if (RecMSE != null)
{
if (SDService.MachNumPzGet(RecMSE.IdxMacchina) != RecMSE.PezziProd || SDService.MachProdStGet(RecMSE.IdxMacchina).IdxOdl != RecMSE.IdxOdl || (datiProdAct != null && datiProdAct.PzTotODL != RecMSE.PezziProd))
{
datiProdAct = await TabDServ.StatoProdMacchina(RecMSE.IdxMacchina, adesso);
SDService.MachProdStSet(RecMSE.IdxMacchina, datiProdAct);
SDService.MachNumPzSet(RecMSE.IdxMacchina, RecMSE.PezziProd);
}
else
{
datiProdAct = SDService.MachProdStGet(RecMSE.IdxMacchina);
}
if (string.IsNullOrEmpty(IdxMaccSel))
{
IdxMaccSel = RecMSE.IdxMacchina;
}
isMulti = SMServ.DictMacchMulti[RecMSE.IdxMacchina] == 1;
}
if (!string.IsNullOrEmpty(IdxMacchSub) && RecMSE != null)
{
RecMSE = TabDServ.MseGetSub(RecMSE.IdxMacchina, IdxMacchSub, true);
}
setGaugeVals();
}
protected void setGaugeVals()
{
if (RecMSE != null)
{
innerCircleVals.Clear();
outerCircleVals.Clear();
currMaxVal = int.Parse(RecMSE.NumPezzi.ToString()!);
innerCircleVals.Add(new CircleGaugeMulti.CircSegm() { Color = "#FFC107", Value = RecMSE.PezziProd });
if (RecMSE.PezziConf > 0)
{
innerCircleVals.Add(new CircleGaugeMulti.CircSegm() { Color = "#198754", Value = RecMSE.PezziConf });
// se ho scarti aggiungo all'inizio
if (datiProdAct != null && datiProdAct.PzConfScarto > 0)
{
innerCircleVals.Add(new CircleGaugeMulti.CircSegm() { Color = "#CD1916", Value = datiProdAct.PzConfScarto });
}
}
if (RecMSE.extraVal > 0)
{
outerCircleVals.Add(new CircleGaugeMulti.CircSegm() { Color = "#1367FD", Value = RecMSE.extraVal });
}
//Log.Trace($"MBlock | {RecMSE.IdxMacchina} | 03");
}
else
{
Log.Info("MBlock NO DATA");
}
}
protected async Task ShowDetail()
{
// salvo idxMacch
await MServ.IdxMaccSet(RecMSE!.IdxMacchina);
await MServ.LastOpenedPageSet("machine-detail");
// navigo!
NavMan.NavigateTo($"machine-detail");
}
#endregion Protected Methods
#region Private Fields
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
private bool enableDisegno = false;
private bool forceCloseOdl = true;
private int IdxOdl = 0;
private int IdxOdlAltra = 0;
private string imgBasePath = "";
private bool inAttr = false;
private bool isLoading = false;
private bool isMulti = false;
private bool showOdlDetail = false;
private bool showPodl = false;
protected string isDisabled(bool testCond)
{
return testCond ? $"disabled" : "";
}
#endregion Private Fields
#region Private Properties
private ODLExpModel currOdl { get; set; } = new ODLExpModel();
private PODLExpModel currPodl { get; set; } = new PODLExpModel();
private Dictionary<string, string> heightList { get; set; } = new Dictionary<string, string>() { { "200", "200px" }, { "400", "400px" }, { "600", "600px" }, { "800", "800px" }, { "1000", "1000px" } };
/// <summary>
/// Restituisce il codice IdxMacchina dell'altra tavola (se multi) altrimenti la stessa macchina...
/// </summary>
private string idxMaccAltraTav
{
get
{
string answ = "";
if (RecMSE != null)
{
try
{
// verifico se SIA una tavola (ha char "#")
int iSharp = IdxMaccSel.IndexOf('#');
if (iSharp > 0)
{
// ora verifico SE ALTRA TAVOLA ha ODL...
string nomeTav = IdxMaccSel.Substring(iSharp);
string altraTav = nomeTav.Substring(0, nomeTav.Length - 1);
altraTav += nomeTav.EndsWith("1") ? "2" : "1";
// sistemo nome
answ = IdxMaccSel.Replace(nomeTav, altraTav);
}
}
catch
{ }
}
return answ;
}
}
private string IdxMaccSel { get; set; } = "";
/// <summary>
/// Boolean Mobile vs Desktop
/// </summary>
private bool isMobile { get; set; }
private bool showDraw { get; set; } = false;
#endregion Private Properties
#region Private Methods
private string cssComStatus(string semaforo, DateTime? lastUpdateN)
{
DateTime lastUpdate = lastUpdateN.HasValue ? (DateTime)lastUpdateN : DateTime.Now.AddHours(-1);
string answ = cssStatus(semaforo);
if (DateTime.Now.Subtract(lastUpdate).TotalSeconds > (keepAliveMin * kaFactor))
{
answ = $"{baseCss}Ro";
// blink se secondo pari...
DateTime adesso = DateTime.Now;
int resto = 0;
Math.DivRem(adesso.Second, 2, out resto);
if (resto == 0)
{
answ += "_b";
}
}
return answ;
}
private string cssStatus(string codSemaforo)
{
// se vuoto --> mostra nero!
if (string.IsNullOrEmpty(codSemaforo))
{
codSemaforo = "sNe";
}
string codColore = codSemaforo.Substring(1, 2);
string answ = $"{baseCss}{codColore}";
return answ;
}
private async Task ReloadXDL(bool reloadFromOdl)
{
if (RecMSE != null)
{
int currIdxPOdl = RecMSE.IdxPOdl ?? 0;
if (RecMSE != null)
{
if (reloadFromOdl)
{
currIdxPOdl = RecMSE.IdxPOdl ?? 0;
}
// se ho PODL valido...
if (currIdxPOdl > 0)
{
currPodl = await TabDServ.PODLExp_getByKey(currIdxPOdl);
}
else
{
currPodl = new PODLExpModel()
{
IdxOdl = RecMSE.IdxOdl ?? 0,
KeyRichiesta = "-",
CodArticolo = RecMSE.CodArticolo,
DescArticolo = RecMSE.CodArticolo,
NumPezzi = RecMSE.NumPezzi,
Tcassegnato = RecMSE.TCAssegnato
};
}
}
// update a runtime dati ODL se assegnato
if (currPodl.IdxOdl > 0)
{
currOdl = await TabDServ.OdlByIdx(currPodl.IdxOdl, false);
}
await updateIdxOdl();
}
}
/// <summary>
/// Toggle visibilità button
/// </summary>
private void ToggleDraw()
{
showDraw = !showDraw;
}
/// <summary>
/// Toggle visibilità blocco PODL
/// </summary>
private void TogglePOdl()
{
showPodl = !showPodl;
}
private async Task updateIdxOdl()
{
if (RecMSE != null)
{
// sistemo idxOdl...
var tabOdl = await TabDServ.OdlCurrByMacc(IdxMaccSel, false);
IdxOdl = tabOdl.IdxOdl;
if (isMulti)
{
var tabOdlAltra = await TabDServ.OdlCurrByMacc(idxMaccAltraTav, false);
IdxOdlAltra = tabOdlAltra.IdxOdl;
}
}
}
#endregion Private Methods
}
}