using global::Microsoft.AspNetCore.Components; using MP.Data; using MP.Data.DatabaseModels; using MP.Data.Services; using MP_TAB3.Components; using NLog; namespace MP_TAB3.Pages { public partial class ProdStop { #region Protected Fields protected int dltMinRealtime = 1; protected bool isProcessing = false; protected int minAnticipoRicalcolo = 1; protected bool rdm_ChkOnly = false; protected int rdm_nEvCheck = 1; protected int rdm_nEvStep = 1; #endregion Protected Fields #region Protected Properties protected string alertCss { get; set; } = "alert-danger"; protected MappaStatoExpl? CurrMSE { get; set; } = null; protected DateTime DtRif { get; set; } = DateTime.Now; protected List events2show { get; set; } = new List(); protected string IdxMacc { get; set; } = ""; /// /// Determina se insert sia Realtime o batch con DataOra (in base a diff tra DataOra /// selezionata e realtime, se superiore ad X minuti NON � realtime) /// protected bool insRealtime { get { bool answ = true; if (Math.Abs(DtRif.Subtract(DateTime.Now).TotalMinutes) > dltMinRealtime) { answ = false; } return answ; } } protected string lblOut { get; set; } = ""; [Inject] protected StatusData MDataService { get; set; } = null!; [Inject] protected MessageService MServ { get; set; } = null!; [Inject] protected NavigationManager NavMan { get; set; } = null!; protected NotesEditor? noteEdit { get; set; } [Inject] protected SharedMemService SMServ { get; set; } = null!; [Inject] protected TabDataService TabDServ { get; set; } = null!; #endregion Protected Properties #region Protected Methods /// /// Processo registrazione eventi /// /// /// protected async Task EventRecord(int IdxEv) { isProcessing = true; await Task.Delay(1); // salvo evento e processo... if (IdxEv > 0) { var rigaEvento = SMServ.GetEventRow(IdxEv); if (rigaEvento != null) { var rigaStato = TabDServ.StatoMacchina(IdxMacc); // processo evento... if (insRealtime) { DateTime adesso = DateTime.Now.Floor(TimeSpan.FromSeconds(1)); EventListModel newRec = new EventListModel() { IdxMacchina = IdxMacc, InizioStato = adesso, IdxTipo = IdxEv, CodArticolo = rigaStato.CodArticolo, Value = "DRT", MatrOpr = MatrOpr, pallet = rigaStato.pallet }; // se realtime await TabDServ.EvListInsert(newRec, MP.Data.Objects.Enums.tipoInputEvento.barcode); // resetta il microstato in modo da ricevere successive info HW await TabDServ.resetMicrostatoMacchina(IdxMacc); } else { // in primis disabilito insert... TabDServ.MacchinaSetInsEnab(IdxMacc, false); // calcolo evento string evento = $"{IdxEv}"; string commento = ""; try { evento = rigaEvento.Nome.Replace("Barcode - ", ""); } catch { } // genero stringa pseudo-univoca string codRich = $"{DateTime.Now:yyMMddHHmmss}-{IdxEv}"; // x prima cosa scrivo un evento tipo "1" x chiudere la durata appena prima // dell'evento successivo try { // cerco da 1 sec DOPO evento... var tabNext = TabDServ.DDB_getNext(IdxMacc, DtRif.AddSeconds(1)); DateTime nextEvDT = tabNext != null ? tabNext.InizioStato : DateTime.Now.AddMinutes(-1); // fix salvo la dichiarazione di chiusura commento = $"999 - M.Lav EndEvt: {evento} [{codRich}]"; // 1 hard-coded x resettare EventListModel newRec = new EventListModel() { IdxMacchina = IdxMacc, InizioStato = nextEvDT.AddSeconds(-1), IdxTipo = 1, CodArticolo = rigaStato.CodArticolo, Value = commento, MatrOpr = MatrOpr, pallet = rigaStato.pallet }; await TabDServ.EvListInsert(newRec, MP.Data.Objects.Enums.tipoInputEvento.barcode); // update commento apertura! commento = $"999 - Dich StartEvt: {evento} [{codRich}]"; newRec = new EventListModel() { IdxMacchina = IdxMacc, InizioStato = DtRif.AddSeconds(1), IdxTipo = IdxEv, CodArticolo = rigaStato.CodArticolo, Value = commento, MatrOpr = MatrOpr, pallet = rigaStato.pallet }; await TabDServ.EvListInsert(newRec, MP.Data.Objects.Enums.tipoInputEvento.barcode); // eseguo ricalcolo! DateTime startRicalcolo = DtRif.AddMinutes(minAnticipoRicalcolo); // eseguo ricalcolo periodo.. await TabDServ.DDB_DoRecalc(IdxMacc, startRicalcolo, 1, rdm_nEvStep, rdm_nEvCheck, rdm_ChkOnly); // chiamo registrazione commento... if (noteEdit != null) { await noteEdit.ForceSave(DtRif.AddSeconds(5)); DtRif = DateTime.Now; } } catch (Exception exc) { Log.Error($"Eccezione in fase di riqualifica fermi{Environment.NewLine}{exc}"); } // riabilito insert... anche se non dovrebbe servire x stored ricalcolo precedente... TabDServ.MacchinaSetInsEnab(IdxMacc, true); } // mostro esito alertCss = "alert-succes"; lblOut = $"Registrata dichiarazione fermata [{rigaEvento.Nome}] alle {DateTime.Now:HH:mm:ss}"; } else { alertCss = "alert-danger"; lblOut = $"Codice evento non valido! {IdxEv}"; } } // faccio refresh x singola macchina 2019.03.26 await TabDServ.RicalcMse(IdxMacc, 0); // rileggo e salvo.. var ListMSE = await MDataService.MseGetAll(true); if (ListMSE != null) { // salvo in LocalStorage... await MServ.SaveMse(ListMSE); // aggiorno MSE attuale CurrMSE = ListMSE.Find(x => x.IdxMacchina == IdxMacc); } isProcessing = false; } protected override async Task OnInitializedAsync() { dltMinRealtime = SMServ.GetConfInt("dltMinRealtime"); minAnticipoRicalcolo = SMServ.GetConfInt("minAnticipoRicalcolo"); rdm_nEvStep = SMServ.GetConfInt("rdm_nEvStep"); rdm_nEvCheck = SMServ.GetConfInt("rdm_nEvCheck"); rdm_ChkOnly = SMServ.GetConfBool("rdm_ChkOnly"); // leggo gli altri dati await ReloadData(); } protected async Task RefreshData(List newList) { var ListMSE = newList; if (!string.IsNullOrEmpty(IdxMacc)) { var rawData = ListMSE.Find(x => x.IdxMacchina == IdxMacc); if (rawData != null) { CurrMSE = rawData; } else { await ReloadData(); } } } protected void SetDate(DateTime newDate) { DtRif = newDate; } #endregion Protected Methods #region Private Fields private static Logger Log = LogManager.GetCurrentClassLogger(); #endregion Private Fields #region Private Properties private int MatrOpr { get => MServ.MatrOpr; } #endregion Private Properties #region Private Methods private async Task ReloadData() { if (string.IsNullOrEmpty(IdxMacc)) { try { IdxMacc = await MServ.IdxMaccGet(); // recupero MSE macchina.... if (!string.IsNullOrEmpty(IdxMacc)) { CurrMSE = await MServ.GetMachineMse(IdxMacc); } var eventsAll = await TabDServ.AnagEventiGetByMacch(IdxMacc); if (eventsAll != null) { events2show = eventsAll.Where(x => x.EventoTablet).OrderBy(x => x.label).ToList(); } } catch(Exception exc) { Log.Error($"ProdStop: Eccezione in reloadData {Environment.NewLine}{exc}"); await MServ.LastOpenedPageSet("/"); } } } #endregion Private Methods } }