364 lines
12 KiB
C#
364 lines
12 KiB
C#
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
|
|
|
|
/// <summary>
|
|
/// Init servizio TAB
|
|
/// </summary>
|
|
/// <param name="configuration"></param>
|
|
public SharedMemService(IConfiguration configuration, IConnectionMultiplexer redConn) : base(configuration, redConn)
|
|
{
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Properties
|
|
|
|
/// <summary>
|
|
/// URL di base applicativo
|
|
/// </summary>
|
|
public string baseUrl { get; set; } = "";
|
|
|
|
/// <summary>
|
|
/// URL di base x immagini
|
|
/// </summary>
|
|
public string baseUrlImages { get; set; } = "";
|
|
|
|
/// <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 manual(bool)
|
|
/// </summary>
|
|
public Dictionary<string, bool> DictMacchManual { get; set; } = new Dictionary<string, bool>();
|
|
|
|
/// <summary>
|
|
/// Dizionario macchine con info multi(int)
|
|
/// </summary>
|
|
public Dictionary<string, int> DictMacchMulti { get; set; } = new Dictionary<string, int>();
|
|
|
|
/// <summary>
|
|
/// Dizionario Menu
|
|
/// </summary>
|
|
public Dictionary<string, List<LinkMenuModel>> DictMenu { get; set; } = new Dictionary<string, List<LinkMenuModel>>();
|
|
|
|
/// <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>
|
|
/// Lista macchine master2slave stati
|
|
/// </summary>
|
|
public List<Macchine2SlaveModel> ListM2S { get; set; } = new List<Macchine2SlaveModel>();
|
|
|
|
/// <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 completa lemmi e traduzioni vocabolario
|
|
/// </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()
|
|
{
|
|
#if false
|
|
DictMacchine = new Dictionary<string, string>();
|
|
DictMacchMulti = new Dictionary<string, int>();
|
|
DictMenu = new Dictionary<string, List<LinkMenuModel>>();
|
|
DbConfig = new List<ConfigModel>();
|
|
DictConfig = new Dictionary<string, string>();
|
|
DictEventi = new Dictionary<int, AnagEventiModel>();
|
|
DictStati = new Dictionary<int, AnagStatiModel>();
|
|
DictVocab = new Dictionary<string, string>();
|
|
#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;
|
|
}
|
|
|
|
/// <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 --> T2D (detail)
|
|
/// - altrimenti --> 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 SetM2S(List<Macchine2SlaveModel> newList)
|
|
{
|
|
ListM2S = newList ?? new List<Macchine2SlaveModel>();
|
|
Log.Info("SharedMemService | SetM2S executed!");
|
|
}
|
|
|
|
public void SetMsfd(List<VMSFDModel> newList)
|
|
{
|
|
ListMSFD = newList ?? new List<VMSFDModel>();
|
|
// 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<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<LinkMenuModel> 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 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<LinkMenuModel> AllMenuData { get; set; } = new List<LinkMenuModel>();
|
|
|
|
#endregion Private Properties
|
|
}
|
|
} |