using EgwCoreLib.Razor; using global::Microsoft.AspNetCore.Components; using Microsoft.JSInterop; using MP.Data.DbModels; using MP.Data.Services; using NLog; namespace MP_TAB3.Components { public partial class MachineBlock : IDisposable { #region Public Properties [Parameter] public bool doBlink { get; set; } = false; /// /// True quando in status map, false altrimenti /// [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 MappaStatoExplModel? RecMSE { get; set; } = null; [Parameter] public bool ShowCard { get; set; } [Parameter] public int Width { get; set; } = 0; #endregion Public Properties #region Public Methods /// /// formatta la durata in minuti in un modo "human readable" gg/ore/min /// /// minuti /// 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; } public void Dispose() { innerCircleVals.Clear(); outerCircleVals.Clear(); datiProdAct = null; LastRecMSE = null; TabDServ.DataInvalidated -= TabDServ_DataInvalidated; } /// /// url completo immagine /// /// /// public string ImgUrlMacc(string url) { if (string.IsNullOrEmpty(url.ToString())) { url = "Steamware.png"; } string fullPath = $"{imgBasePath}/{url}"; return fullPath; } #endregion Public Methods #region Protected Fields protected string baseCss = "sem"; protected int currMaxVal = 888; protected List innerCircleVals = new List(); protected int kaFactor = 60 / 2; protected int maxVal = 999; protected List outerCircleVals = new List(); #endregion Protected Fields #region Protected Properties [Inject] protected IConfiguration config { get; set; } = null!; /// /// CSS Class bordo da stato macchina /// 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; } } /// /// CSS Class x overlay (effetto spento x macchina spenta/ sGr) /// protected string cssClassOverlay { get { string answ = ""; if (RecMSE != null) { answ = RecMSE.Semaforo == "sGr" ? "bg-dark opacity-50" : ""; } return answ; } } /// /// CSS class x testo (se descr lunga scorre...) /// 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 (CRecMSE != null) { answ = new ProdAdvDispl.ProdCounter() { numPzConf = (int)CRecMSE.PezziConf, numPzOrd = (int)CRecMSE.NumPezzi, numPzProd = (int)CRecMSE.PezziProd }; } return answ; } } /// /// Dati produzione rilevati /// protected StatoProdModel? datiProdAct { get; set; } = null; [Inject] protected IJSRuntime JSRuntime { get; set; } = null!; [Inject] protected MessageService MServ { get; set; } = null!; [Inject] protected SharedMemService MStor { get; set; } = null!; [Inject] protected NavigationManager NavMan { get; set; } = null!; protected int NumScartiConf { get => datiProdAct != null ? datiProdAct.PzConfScarto - datiProdAct.PzConfRilav : 0; } [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 string isDisabled(bool testCond) { return testCond ? $"disabled" : ""; } protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) { await InvokeAsync(StateHasChanged); } isLoading = RecMSE == null; isMobile = await JSRuntime.InvokeAsync("isDevice"); await Task.Delay(1); } protected override async Task OnInitializedAsync() { TabDServ.DataInvalidated += TabDServ_DataInvalidated; isLoading = true; // salvo i parametri precedenti x calcolare variazioni... saveParams(); // abilitazione disegni... TabDServ.ConfigGetVal("enableDisegno", ref enableDisegno); TabDServ.ConfigGetVal("TAB_ShowQtaExtra", ref showQtaExtra); await ReloadXDL(true); await loadDetails(); detailLoaded = true; } protected override async Task OnParametersSetAsync() { // controllo variazione parametri... string lastIMS = LastIdxMacchSub; if (paramsChanged()) { saveParams(); isLoading = RecMSE == null; // controllo SE ho variazioni cos� rileggo if (!string.IsNullOrEmpty(IdxMacchSub) && RecMSE != null) { // solo se cambia macchina forzo da DB bool doForce = lastIMS != IdxMacchSub; RecMSESub = TabDServ.MseGetSub(RecMSE.IdxMacchina, IdxMacchSub, doForce); } else { RecMSESub = RecMSE; } // carico dettagli await loadDetails(); } setGaugeVals(); } protected void setGaugeVals() { // se � una doppio pallet uso dati MSE if (string.IsNullOrEmpty(IdxMacchSub) && isMulti) { if (CRecMSE != null) { innerCircleVals.Clear(); outerCircleVals.Clear(); currMaxVal = CRecMSE.NumPezzi; // int.Parse(CRecMSE.NumPezzi.ToString()!); innerCircleVals.Add(new CircleGaugeMulti.CircSegm() { Color = "#FFC107", Value = CRecMSE.PezziProd }); if (CRecMSE.PezziConf > 0) { innerCircleVals.Add(new CircleGaugeMulti.CircSegm() { Color = "#198754", Value = CRecMSE.PezziConf }); // se ho scarti aggiungo all'inizio if (datiProdAct != null && datiProdAct.PzConfScarto > 0) { innerCircleVals.Add(new CircleGaugeMulti.CircSegm() { Color = "#CD1916", Value = datiProdAct.PzConfScarto }); } } if (CRecMSE.extraVal > 0) { outerCircleVals.Add(new CircleGaugeMulti.CircSegm() { Color = "#1367FD", Value = CRecMSE.extraVal }); } } } // altrimenti da dati produzione else { if (datiProdAct != null) { innerCircleVals.Clear(); outerCircleVals.Clear(); currMaxVal = datiProdAct.PzRichODL; innerCircleVals.Add(new CircleGaugeMulti.CircSegm() { Color = "#FFC107", Value = datiProdAct.PzTotODL }); innerCircleVals.Add(new CircleGaugeMulti.CircSegm() { Color = "#198754", Value = datiProdAct.PzConfAll }); // se ho scarti aggiungo all'inizio if (datiProdAct.PzConfScarto > 0) { innerCircleVals.Add(new CircleGaugeMulti.CircSegm() { Color = "#CD1916", Value = datiProdAct.PzConfScarto }); } if (CRecMSE != null && CRecMSE.extraVal > 0) { outerCircleVals.Add(new CircleGaugeMulti.CircSegm() { Color = "#1367FD", Value = CRecMSE.extraVal }); } } } } protected async Task ShowDetail() { // salvo idxMacch await MServ.IdxMaccSet(RecMSE!.IdxMacchina); // salvo se sia manuale... 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 detailLoaded = false; private bool enableDisegno = false; private bool forceCloseOdl = true; private int IdxOdl = 0; private int IdxOdlAltra = 0; private bool inAttr = false; private bool isLoading = false; private bool isManual = false; private bool isMulti = false; private bool LastFullMode = true; private string LastIdxMacchSub = ""; private int LastKeepAliveMin = 5; private MappaStatoExplModel? LastRecMSE = null; private bool LastShowCard = true; private bool showOdlDetail = false; private bool showPodl = false; private bool showQtaExtra = false; #endregion Private Fields #region Private Properties /// /// Info correnti secondo tipo selezione /// private MappaStatoExplModel? CRecMSE { get { MappaStatoExplModel? answ = RecMSE; if (!string.IsNullOrEmpty(IdxMacchSub)) { answ = RecMSESub; } return answ; } } private ODLExpModel currOdl { get; set; } = new ODLExpModel(); private PODLExpModel currPodl { get; set; } = new PODLExpModel(); private Dictionary heightList { get; set; } = new Dictionary() { { "200", "200px" }, { "400", "400px" }, { "600", "600px" }, { "800", "800px" }, { "1000", "1000px" } }; /// /// Restituisce il codice IdxMacchina dell'altra tavola (se multi) altrimenti la stessa macchina... /// private string idxMaccAltraTav { get { string answ = ""; if (CRecMSE != null) { try { // verifico se SIA una tavola (ha char "#") int iSharp = IdxMaccMain.IndexOf('#'); if (iSharp > 0) { // ora verifico SE ALTRA TAVOLA ha ODL... string nomeTav = IdxMaccMain.Substring(iSharp); string altraTav = nomeTav.Substring(0, nomeTav.Length - 1); altraTav += nomeTav.EndsWith("1") ? "2" : "1"; // sistemo nome answ = IdxMaccMain.Replace(nomeTav, altraTav); } } catch { } } return answ; } } /// /// Macchina corrente (main oppure sub) /// private string IdxMaccCur { get => string.IsNullOrEmpty(IdxMacchSub) ? IdxMaccMain : IdxMacchSub; } /// /// Macchina selezionata MAIN /// private string IdxMaccMain { get => CRecMSE != null ? CRecMSE.IdxMacchina : ""; } private string imgBasePath { get => MStor.baseUrlImages; } /// /// Boolean Mobile vs Desktop /// private bool isMobile { get; set; } private MappaStatoExplModel? RecMSESub { get; set; } = null; private bool showDraw { get; set; } = false; #endregion Private Properties #region Private Methods /// /// Rilettura dettagli /// /// private async Task loadDetails() { // verifico di non essere in reloading e di avere dati... if (!MServ.IsReloading && CRecMSE != null) { bool doForce = false; if (SDService.MachNumPzGet(IdxMaccCur) != CRecMSE.PezziProd || SDService.MachProdStGet(IdxMaccCur).IdxOdl != CRecMSE.IdxOdl || (datiProdAct != null && datiProdAct.PzTotODL != CRecMSE.PezziProd)) { datiProdAct = await TabDServ.StatoProdMacchinaAsync(IdxMaccCur, DateTime.Now); SDService.MachProdStSet(IdxMaccCur, datiProdAct); SDService.MachNumPzSet(IdxMaccCur, CRecMSE.PezziProd); doForce = true; } else { datiProdAct = SDService.MachProdStGet(IdxMaccCur); } // fix multi if (SMServ.DictMacchMulti.ContainsKey(IdxMaccMain)) { isMulti = SMServ.DictMacchMulti[IdxMaccMain] == 1; } else { isMulti = false; } // fix manual if (SMServ.DictMacchManual.ContainsKey(IdxMaccMain)) { isManual = SMServ.DictMacchManual[IdxMaccMain]; } else { isManual = false; } // verificare if (!FullMode || !detailLoaded || doForce) { await ReloadXDL(true); } } } /// /// Verifica variazione parametri /// /// private bool paramsChanged() { if (LastFullMode != FullMode) return true; if (LastKeepAliveMin != KeepAliveMin) return true; if (LastShowCard != ShowCard) return true; if (LastIdxMacchSub != IdxMacchSub) return true; if (LastRecMSE == null && RecMSE != null) return true; if (LastRecMSE != null && RecMSE == null) return true; if (LastRecMSE != null && !LastRecMSE.MostlyEquals(RecMSE)) return true; return false; } private async Task ReloadXDL(bool reloadFromOdl) { if (CRecMSE != null) { int currIdxPOdl = CRecMSE.IdxPOdl ?? 0; if (CRecMSE != null) { if (reloadFromOdl) { currIdxPOdl = CRecMSE.IdxPOdl ?? 0; } // se ho PODL valido... if (currIdxPOdl > 0) { currPodl = await TabDServ.PODLExp_getByKey(currIdxPOdl); } else { currPodl = new PODLExpModel() { IdxOdl = CRecMSE.IdxOdl ?? 0, KeyRichiesta = "-", CodArticolo = CRecMSE.CodArticolo, DescArticolo = CRecMSE.CodArticolo, NumPezzi = CRecMSE.NumPezzi, Tcassegnato = CRecMSE.TCAssegnato }; } } // update a runtime dati ODL se assegnato if (currPodl.IdxOdl > 0) { currOdl = await TabDServ.OdlByIdx(currPodl.IdxOdl, false); } if (!string.IsNullOrEmpty(IdxMaccMain)) { await updateIdxOdl(); } } } private void saveParams() { LastIdxMacchSub = IdxMacchSub; LastRecMSE = RecMSE; LastFullMode = FullMode; LastShowCard = ShowCard; LastKeepAliveMin = KeepAliveMin; } private async void TabDServ_DataInvalidated(object? sender, EventArgs e) { ReloadEventArgs rea = (ReloadEventArgs)e; if (rea.ReloadMessage == IdxMaccCur) { // se riceve evento relativo a questo impianto --> ricarico dati prod... detailLoaded = false; Log.Info("TabDServ_DataInvalidated: Reload Data"); // carico dettagli await loadDetails(); setGaugeVals(); } } /// /// Toggle visibilit� button /// private void ToggleDraw() { showDraw = !showDraw; } /// /// Toggle visibilit� blocco PODL /// private void TogglePOdl() { showPodl = !showPodl; } private async Task updateIdxOdl() { if (CRecMSE != null) { // sistemo idxOdl... var tabOdl = await TabDServ.OdlCurrByMacc(IdxMaccMain, false); IdxOdl = tabOdl.IdxOdl; if (isMulti) { if (!string.IsNullOrEmpty(idxMaccAltraTav)) { var tabOdlAltra = await TabDServ.OdlCurrByMacc(idxMaccAltraTav, false); IdxOdlAltra = tabOdlAltra.IdxOdl; } } } } #endregion Private Methods } }