using Microsoft.Extensions.Configuration; using MP.Data.DbModels; using NLog; using StackExchange.Redis; using System.Collections.Generic; using System.Linq; namespace MP.Data.Services { public class SharedMemService : BaseServ { #region Public Constructors /// /// Init servizio TAB /// /// public SharedMemService(IConfiguration configuration, IConnectionMultiplexer redConn) : base(configuration, redConn) { } #endregion Public Constructors #region Public Properties /// /// URL di base applicativo /// public string baseUrl { get; set; } = ""; /// /// URL di base x immagini /// public string baseUrlImages { get; set; } = ""; /// /// List configurazione attiva da tab DB /// public List DbConfig { get; set; } = new List(); /// /// Dizionario configurazione attiva da tab DB /// public Dictionary DictConfig { get; set; } = new Dictionary(); /// /// Dizionario Eventi (codice, record completo) /// public Dictionary DictEventi { get; set; } = new Dictionary(); /// /// Dizionario macchine (shared) /// public Dictionary DictMacchine { get; set; } = new Dictionary(); /// /// Dizionario macchine con info manual(bool) /// public Dictionary DictMacchManual { get; set; } = new Dictionary(); /// /// Dizionario macchine con info multi(int) /// public Dictionary DictMacchMulti { get; set; } = new Dictionary(); /// /// Dizionario Menu /// public Dictionary> DictMenu { get; set; } = new Dictionary>(); /// /// Dizionario Stati (codice, record completo) /// public Dictionary DictStati { get; set; } = new Dictionary(); /// /// Dizionario globale (key: lingua_lemma, lowercase) /// public Dictionary DictVocab { get; set; } = new Dictionary(); /// /// Lista completa eventi /// public List ListEventi { get; set; } = new List(); /// /// Lista macchine master2slave stati /// public List ListM2S { get; set; } = new List(); /// /// List configurazione attiva da tab DB /// public List ListMSFD { get; set; } = new List(); /// /// Lista completa stati /// public List ListStati { get; set; } = new List(); /// /// Lista completa lemmi e traduzioni vocabolario /// public List ListVocab { get; set; } = new List(); public bool MenuOk { get => AllMenuData.Count > 0; } #endregion Public Properties #region Public Methods /// /// Reset cache memory /// public void ClearCache() { #if false DictMacchine = new Dictionary(); DictMacchMulti = new Dictionary(); DictMenu = new Dictionary>(); DbConfig = new List(); DictConfig = new Dictionary(); DictEventi = new Dictionary(); DictStati = new Dictionary(); DictVocab = new Dictionary(); #endif AllMenuData.Clear(); DbConfig.Clear(); DictConfig.Clear(); DictEventi.Clear(); DictMacchine.Clear(); DictMacchManual.Clear(); DictMacchMulti.Clear(); DictMenu.Clear(); DictStati.Clear(); DictVocab.Clear(); ListEventi.Clear(); ListM2S.Clear(); ListMSFD.Clear(); ListStati.Clear(); ListVocab.Clear(); Log.Info("SharedMemService | Cache resetted!"); } public string GetConf(string chiave) { string answ = ""; if (DictConfig.ContainsKey(chiave)) { answ = DictConfig[chiave]; } return answ; } public bool GetConfBool(string chiave) { bool answ = false; bool.TryParse(GetConf(chiave), out answ); return answ; } public int GetConfInt(string chiave) { int answ = 0; int.TryParse(GetConf(chiave), out answ); return answ; } /// /// Recupero riga evento da dizionario /// /// /// public AnagEventiModel GetEventRow(int IdxTipo) { AnagEventiModel answ = new AnagEventiModel(); if (DictEventi.ContainsKey(IdxTipo)) { answ = DictEventi[IdxTipo]; } return answ; } /// /// Recupero riga stato da dizionario /// /// /// public AnagStatiModel GetStateRow(int IdxStato) { AnagStatiModel answ = new AnagStatiModel(); if (DictStati.ContainsKey(IdxStato)) { answ = DictStati[IdxStato]; } return answ; } /// /// Restituisce il livello pagina dato URL /// - se contiene ?IdxMacc --> T2D (detail) /// - altrimenti --> T2H /// /// /// public string PageLevel(string fullUrl) { string answ = "T2H"; var urlPart = fullUrl.Split("/"); string pageUrl = urlPart.Last().ToLower(); // cerco nell'elenco... if (AllMenuData.Count > 0) { var sRec = AllMenuData .Where(x => x.NavigateUrl.ToLower() == pageUrl) .FirstOrDefault(); if (sRec != null) { answ = sRec.TipoLink; } } return answ; } public void SetConfig(List? allConf) { DbConfig = allConf ?? new List(); // salvo dizionario DictConfig = DbConfig.ToDictionary(x => x.Chiave, x => x.Valore); Log.Info("SharedMemService | SetConfig executed!"); } public void SetEventi(List allEvents) { ListEventi = allEvents ?? new List(); // salvo dizionario DictEventi = ListEventi.ToDictionary(x => x.IdxTipo, x => x); Log.Info("SharedMemService | SetEventi executed!"); } public void SetM2S(List newList) { ListM2S = newList ?? new List(); Log.Info("SharedMemService | SetM2S executed!"); } public void SetMsfd(List newList) { ListMSFD = newList ?? new List(); // salvo dizionario MULTI DictMacchMulti = ListMSFD.ToDictionary(x => x.IdxMacchina, x => x.Multi); DictMacchManual = ListMSFD.ToDictionary(x => x.IdxMacchina, x => x.IsManual == 1); Log.Info("SharedMemService | SetMsfd executed!"); } public void SetStati(List allStati) { ListStati = allStati ?? new List(); // salvo dizionario DictStati = ListStati.ToDictionary(x => x.IdxStato, x => x); Log.Info("SharedMemService | SetStati executed!"); } /// /// Effettua setup memorie menu recuperando dati dal DB + popolando dizionario x livelli /// public void SetupMenu(List AllData) { // svuoto i valori in essere DictMenu.Clear(); // salvo nuovo set AllMenuData = AllData; // ciclo x recuperare i distinti TIPI di menu... var tList = AllMenuData .Select(x => x.TipoLink) .Distinct() .ToList(); // per ogni tipo --> ciclo! foreach (var item in tList) { var currMenu = AllMenuData .Where(x => x.TipoLink == item) .ToList(); if (!DictMenu.ContainsKey(item)) { DictMenu.Add(item, currMenu); } } Log.Info("SharedMemService | SetupMenu executed!"); } /// /// Effettua setup vocabolario /// /// public void SetVocab(List allVoc) { ListVocab = allVoc ?? new List(); // salvo dizionario DictVocab = ListVocab.ToDictionary(x => $"{x.Lingua}_{x.Lemma.ToUpper()}", x => x.Traduzione); Log.Info("SharedMemService | SetVocab executed!"); } public string Traduci(string lingua, string lemma) { return Traduci($"{lingua}_{lemma}".ToUpper()); } /// /// Traduzione diretta vocabolario /// /// Formato {lingua}_{lemma}, lowercase /// public string Traduci(string lingua_lemma) { string answ = $"[{lingua_lemma}]"; if (DictVocab.ContainsKey(lingua_lemma)) { answ = DictVocab[lingua_lemma]; } return answ; } #endregion Public Methods #region Protected Methods protected override void Dispose(bool disposing) { if (!_disposed) { if (disposing) { // Free managed resources here ClearCache(); } // Free unmanaged resources here _disposed = true; } // Call base class implementation. base.Dispose(disposing); } #endregion Protected Methods #region Private Fields private static Logger Log = LogManager.GetCurrentClassLogger(); private bool _disposed = false; #endregion Private Fields #region Private Properties private List AllMenuData { get; set; } = new List(); #endregion Private Properties } }