Files
mapo-core/MP.Data/Services/SharedMemService.cs
T
2023-11-06 15:49:02 +01:00

302 lines
9.8 KiB
C#

using MP.Data.DatabaseModels;
using NLog;
using StackExchange.Redis;
using System.Collections.Generic;
using System.Linq;
namespace MP.Data.Services
{
public class SharedMemService : BaseServ
{
#region Public Properties
/// <summary>
/// List configurazione attiva da tab DB
/// </summary>
public List<ConfigModel> DbConfig { get; set; } = new List<ConfigModel>();
/// <summary>
/// Dizionario configurazione attiva da tab DB
/// </summary>
public Dictionary<string, string> DictConfig { get; set; } = new Dictionary<string, string>();
/// <summary>
/// Dizionario Eventi (codice, record completo)
/// </summary>
public Dictionary<int, AnagEventiModel> DictEventi { get; set; } = new Dictionary<int, AnagEventiModel>();
/// <summary>
/// Dizionario macchine (shared)
/// </summary>
public Dictionary<string, string> DictMacchine { get; set; } = new Dictionary<string, string>();
/// <summary>
/// Dizionario macchine con info multi(bool)
/// </summary>
public Dictionary<string, int> DictMacchMulti { get; set; } = new Dictionary<string, int>();
/// <summary>
/// Dizionario Menu
/// </summary>
public Dictionary<string, List<LinkMenu>> DictMenu { get; set; } = new Dictionary<string, List<LinkMenu>>();
/// <summary>
/// Dizionario Stati (codice, record completo)
/// </summary>
public Dictionary<int, AnagStatiModel> DictStati { get; set; } = new Dictionary<int, AnagStatiModel>();
/// <summary>
/// Dizionario globale (key: lingua_lemma, lowercase)
/// </summary>
public Dictionary<string, string> DictVocab { get; set; } = new Dictionary<string, string>();
/// <summary>
/// Lista completa eventi
/// </summary>
public List<AnagEventiModel> ListEventi { get; set; } = new List<AnagEventiModel>();
/// <summary>
/// List configurazione attiva da tab DB
/// </summary>
public List<VMSFDModel> ListMSFD { get; set; } = new List<VMSFDModel>();
/// <summary>
/// Lista completa stati
/// </summary>
public List<AnagStatiModel> ListStati { get; set; } = new List<AnagStatiModel>();
/// <summary>
/// Lista macchine master2slave stati
/// </summary>
public List<Macchine2SlaveModel> ListM2S { get; set; } = new List<Macchine2SlaveModel>();
/// <summary>
/// Lista completa stati
/// </summary>
public List<VocabolarioModel> ListVocab { get; set; } = new List<VocabolarioModel>();
public bool MenuOk
{
get => AllMenuData.Count > 0;
}
#endregion Public Properties
#region Public Methods
/// <summary>
/// Reset cache memory
/// </summary>
public void ClearCache()
{
DictMacchine = new Dictionary<string, string>();
DictMacchMulti = new Dictionary<string, int>();
DictMenu = new Dictionary<string, List<LinkMenu>>();
DbConfig = new List<ConfigModel>();
DictConfig = new Dictionary<string, string>();
DictEventi = new Dictionary<int, AnagEventiModel>();
DictStati = new Dictionary<int, AnagStatiModel>();
DictVocab = new Dictionary<string, string>();
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;
}
/// <summary>
/// Recupero riga evento da dizionario
/// </summary>
/// <param name="IdxTipo"></param>
/// <returns></returns>
public AnagEventiModel GetEventRow(int IdxTipo)
{
AnagEventiModel answ = new AnagEventiModel();
if (DictEventi.ContainsKey(IdxTipo))
{
answ = DictEventi[IdxTipo];
}
return answ;
}
/// <summary>
/// Recupero riga stato da dizionario
/// </summary>
/// <param name="IdxStato"></param>
/// <returns></returns>
public AnagStatiModel GetStateRow(int IdxStato)
{
AnagStatiModel answ = new AnagStatiModel();
if (DictStati.ContainsKey(IdxStato))
{
answ = DictStati[IdxStato];
}
return answ;
}
/// <summary>
/// Restituisce il livello pagina dato URL
/// - se contiene ?IdxMacc --&gt; T2D (detail)
/// - altrimenti --&gt; T2H
/// </summary>
/// <param name="fullUrl"></param>
/// <returns></returns>
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<ConfigModel>? allConf)
{
DbConfig = allConf ?? new List<ConfigModel>();
// salvo dizionario
DictConfig = DbConfig.ToDictionary(x => x.Chiave, x => x.Valore);
Log.Info("SharedMemService | SetConfig executed!");
}
public void SetEventi(List<AnagEventiModel> allEvents)
{
ListEventi = allEvents ?? new List<AnagEventiModel>();
// salvo dizionario
DictEventi = ListEventi.ToDictionary(x => x.IdxTipo, x => x);
Log.Info("SharedMemService | SetEventi executed!");
}
public void SetMsfd(List<VMSFDModel> newList)
{
ListMSFD = newList ?? new List<VMSFDModel>();
// salvo dizionario MULTI
DictMacchMulti = ListMSFD.ToDictionary(x => x.IdxMacchina, x => x.Multi);
Log.Info("SharedMemService | SetMsfd executed!");
}
public void SetM2S(List<Macchine2SlaveModel> newList)
{
ListM2S = newList ?? new List<Macchine2SlaveModel>();
Log.Info("SharedMemService | SetM2S executed!");
}
public void SetStati(List<AnagStatiModel> allStati)
{
ListStati = allStati ?? new List<AnagStatiModel>();
// salvo dizionario
DictStati = ListStati.ToDictionary(x => x.IdxStato, x => x);
Log.Info("SharedMemService | SetStati executed!");
}
/// <summary>
/// Effettua setup memorie menu recuperando dati dal DB + popolando dizionario x livelli
/// </summary>
public void SetupMenu(List<LinkMenu> 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!");
}
/// <summary>
/// Effettua setup vocabolario
/// </summary>
/// <param name="allVoc"></param>
public void SetVocab(List<VocabolarioModel> allVoc)
{
ListVocab = allVoc ?? new List<VocabolarioModel>();
// 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());
}
/// <summary>
/// Traduzione diretta vocabolario
/// </summary>
/// <param name="lingua_lemma">Formato {lingua}_{lemma}, lowercase</param>
/// <returns></returns>
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 Private Fields
private static Logger Log = LogManager.GetCurrentClassLogger();
/// <summary>
/// Oggetto vocabolario x uso continuo traduzione
/// </summary>
private List<VocabolarioModel> ObjVocabolario = new List<VocabolarioModel>();
#endregion Private Fields
#region Private Properties
private List<LinkMenu> AllMenuData { get; set; } = new List<LinkMenu>();
#endregion Private Properties
}
}