Files
2024-07-01 19:00:55 +02:00

737 lines
23 KiB
C#

using Blazored.LocalStorage;
using Blazored.SessionStorage;
using Microsoft.AspNetCore.Components;
using Microsoft.VisualBasic;
using NLog;
using StackExchange.Redis;
using System.Diagnostics;
namespace MagMan.Core.Services
{
public class MessageService
{
#region Public Constructors
public MessageService(ILocalStorageService genLocalStorage, ISessionStorageService sessStore, IConnectionMultiplexer RedConn)
{
// gestione sessioni in browser
localStore = genLocalStorage;
sessionStore = sessStore;
// setup componenti REDIS
redisConn = RedConn;
redisDb = redisConn.GetDatabase();
// setup canali pub/sub
QueUpdGen = new MessagePipe(redisConn, Const.Q_MMAN_GEN);
QueUpdAliasMat = new MessagePipe(redisConn, Const.Q_MMAN_ALIAS_MAT);
QueUpdInve= new MessagePipe(redisConn, Const.Q_MMAN_LOG);
QueUpdLog = new MessagePipe(redisConn, Const.Q_MMAN_LOG);
QueUpdProj = new MessagePipe(redisConn, Const.Q_MMAN_PROJ);
QueUpdRes = new MessagePipe(redisConn, Const.Q_MMAN_RES);
}
#endregion Public Constructors
#region Public Events
public event Action EA_CustomerSel = null!;
public event Action EA_FilterUpdated = null!;
public event Action EA_HideSearch = null!;
public event Action EA_KeySel = null!;
public event Action EA_PageUpdated = null!;
public event Action EA_SearchUpdated = null!;
public event Action<bool> EA_ShowCustomers = null!;
public event Action EA_ShowSearch = null!;
#endregion Public Events
#region Public Properties
public int CustomerID
{
get => _customerID;
set
{
if (_customerID != value)
{
_customerID = value;
if (EA_CustomerSel != null)
{
EA_CustomerSel?.Invoke();
}
}
}
}
public int KeyNum
{
get => _keyNum;
set
{
if (_keyNum != value)
{
_keyNum = value;
if (EA_KeySel != null)
{
EA_KeySel?.Invoke();
}
}
}
}
public string PageIcon
{
get => _pageIcon;
set
{
if (_pageIcon != value)
{
_pageIcon = value;
ReportPageUpd();
}
}
}
public string PageName
{
get => _pageName;
set
{
if (_pageName != value)
{
_pageName = value;
ReportPageUpd();
}
}
}
public string SearchVal
{
get => _searchVal;
set
{
if (_searchVal != value)
{
_searchVal = value;
if (EA_SearchUpdated != null)
{
EA_SearchUpdated?.Invoke();
}
}
}
}
public string SelOrderCode { get; set; } = "";
public string SelPlantId { get; set; } = "0";
public bool ShowCustomers
{
get => _showCustomers;
set
{
_showCustomers = value;
if (EA_ShowCustomers != null)
{
EA_ShowCustomers?.Invoke(value);
}
}
}
public bool ShowSearch
{
get => _showSearch;
set
{
if (_showSearch != value)
{
_showSearch = value;
if (_showSearch)
{
if (EA_ShowSearch != null)
{
EA_ShowSearch?.Invoke();
}
}
else
{
if (EA_HideSearch != null)
{
EA_HideSearch?.Invoke();
}
}
}
}
}
/// <summary>
/// Message pipe ricezione dati da EgtBW relativi a info Alias/Materials
/// </summary>
public MessagePipe QueUpdAliasMat { get; set; } = null!;
/// <summary>
/// Message pipe ricezione dati da EgtBW (General/Generics)
/// </summary>
public MessagePipe QueUpdGen { get; set; } = null!;
/// <summary>
/// Message pipe ricezione dati da EgtBW relativi a info LogMachine
/// </summary>
public MessagePipe QueUpdLog { get; set; } = null!;
/// <summary>
/// Message pipe ricezione dati da EgtBW relativi a info Projects
/// </summary>
public MessagePipe QueUpdProj { get; set; } = null!;
/// <summary>
/// Message pipe ricezione dati da EgtBW relativi a info Resources
/// </summary>
public MessagePipe QueUpdRes { get; set; } = null!;
/// <summary>
/// Message pipe ricezione dati da EgtBW relativi a info Inventario (aggiunta/consumi)
/// </summary>
public MessagePipe QueUpdInve { get; set; } = null!;
public string UserName { get; set; } = "NA";
#endregion Public Properties
#region Public Methods
/// <summary>
/// Cliente selezionato (da browser data cache)
/// </summary>
public async Task<int> ClientIdGet()
{
var answ = await localStore.GetItemAsync<int>("ClientID");
return answ;
}
/// <summary>
/// Imposta Cliente selezionato (browser data cache)
/// </summary>
public async Task ClientIdSet(int newVal)
{
await localStore.SetItemAsync("ClientID", newVal);
}
/// <summary>
/// Num record x la pagina corrente
/// </summary>
public async Task<int> NumRowGridGet(string gridName)
{
var answ = await localStore.GetItemAsync<int>($"{gridName}_nRow");
answ = answ > 0 ? answ : 10;
return answ;
}
/// <summary>
/// Imposta num record x la pagina corrente
/// </summary>
public async Task NumRowGridSet(string gridName, int newVal)
{
await localStore.SetItemAsync($"{gridName}_nRow", newVal);
}
/// <summary>
/// Recupera CustomerID dal dizionario dei token noti o cercando sul DB
/// </summary>
/// <param name="RestToken"></param>
/// <returns></returns>
public async Task<int> CustomerIdByToken(string RestToken)
{
string currKey = $"{Const.rKeyConfig}:Mserv:Dict:Token2CustID";
int answ = await RedisHashGetInt(currKey, RestToken);
return answ;
}
/// <summary>
/// salvataggio in dizionario Redis associativo Token / Customer
/// </summary>
/// <param name="RestToken"></param>
/// <param name="CustId"></param>
/// <returns></returns>
public bool CustomerIdByTokenSet(string RestToken, int CustId)
{
string currKey = $"{Const.rKeyConfig}:Mserv:Dict:Token2CustID";
bool fatto = RedisHashSet(currKey, RestToken, $"{CustId}");
return fatto;
}
/// <summary>
/// KeyNum da cliente selezionato (da browser data cache)
/// </summary>
public async Task<int> KeyNumGet()
{
var answ = await localStore.GetItemAsync<int>("KeyNum");
return answ;
}
/// <summary>
/// Imposta KeyNum da cliente selezionato (browser data cache)
/// </summary>
public async Task KeyNumSet(int newVal)
{
await localStore.SetItemAsync("KeyNum", newVal);
}
/// <summary>
/// Recupera CustomerID dal dizionario dei token noti o cercando sul DB
/// </summary>
/// <param name="CustID"></param>
/// <returns></returns>
public async Task<int> MainKeyByCustomer(int CustID)
{
string currKey = $"{Const.rKeyConfig}:Mserv:Dict:Cust2MKey";
int answ = await RedisHashGetInt(currKey, $"{CustID}");
return answ;
}
/// <summary>
/// salvataggio in dizionario Redis associativo Token / Customer
/// </summary>
/// <param name="RestToken"></param>
/// <param name="CustId"></param>
/// <returns></returns>
public bool MainKeyByCustomerSet(int CustId, int MainKey)
{
string currKey = $"{Const.rKeyConfig}:Mserv:Dict:Cust2MKey";
bool fatto = RedisHashSet(currKey, $"{CustId}", $"{MainKey}");
return fatto;
}
/// <summary>
/// Recupera MainKey dal dizionario dei token noti o cercando sul DB
/// </summary>
/// <param name="RestToken"></param>
/// <returns></returns>
public async Task<int> MainKeyByToken(string RestToken)
{
string currKey = $"{Const.rKeyConfig}:Mserv:Dict:Token2MKey";
int answ = await RedisHashGetInt(currKey, RestToken);
return answ;
}
/// <summary>
/// salvataggio in dizionario Redis associativo Token / Customer
/// </summary>
/// <param name="RestToken"></param>
/// <param name="CustId"></param>
/// <returns></returns>
public bool MainKeyByTokenSet(string RestToken, int CustId)
{
string currKey = $"{Const.rKeyConfig}:Mserv:Dict:Token2MKey";
bool fatto = RedisHashSet(currKey, RestToken, $"{CustId}");
return fatto;
}
/// <summary>
/// Get single hash record
/// </summary>
/// <param name="currKey">Redis Key for Hashlist</param>
/// <param name="chiave">Requested key on list</param>
/// <returns>Value as Int</returns>
public async Task<int> RedisHashGetInt(RedisKey currKey, string chiave)
{
int result = 0;
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
var hasVal = await redisDb.HashExistsAsync(currKey, chiave);
if (hasVal)
{
var rawRes = await redisDb.HashGetAsync(currKey, chiave);
if (rawRes.HasValue)
{
int.TryParse($"{rawRes}", out result);
}
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Trace($"RedisHashGetInt | {currKey} | in: {ts.TotalMilliseconds} ms");
return result;
}
/// <summary>
/// Get single hash record
/// </summary>
/// <param name="currKey">Redis Key for Hashlist</param>
/// <param name="chiave">Requested key on list</param>
/// <returns>Value as string</returns>
public async Task<string> RedisHashGetString(RedisKey currKey, string chiave)
{
string result = "";
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
var hasVal = await redisDb.HashExistsAsync(currKey, chiave);
if (hasVal)
{
var rawRes = await redisDb.HashGetAsync(currKey, chiave);
if (rawRes.HasValue)
{
result = $"{rawRes}";
}
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Trace($"RedisHashGetString | {currKey} | in: {ts.TotalMilliseconds} ms");
return result;
}
/// <summary>
/// Remove for single hash record
/// </summary>
/// <param name="currKey">Chiave redis della Hashlist</param>
/// <param name="chiave">Chiave nella HashList</param>
/// <returns>Esito rimozione</returns>
public async Task<bool> RedisHashRemove(RedisKey currKey, string chiave)
{
bool fatto = false;
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
fatto = await redisDb.HashDeleteAsync(currKey, chiave);
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Trace($"RedisHashRemove | {currKey} | in: {ts.TotalMilliseconds} ms");
return fatto;
}
/// <summary>
/// Resetta 1:1 i dizionari Hash in Redis
/// </summary>
/// <returns></returns>
public bool ResetHashDict()
{
bool fatto = false;
string baseKey = $"{Const.rKeyConfig}:Mserv:Dict";
try
{
// salvo!
RedisHashDictSet($"{baseKey}:Token2CustID", new Dictionary<string, string>());
RedisHashDictSet($"{baseKey}:Token2MKey", new Dictionary<string, string>());
RedisHashDictSet($"{baseKey}:Cust2MKey", new Dictionary<string, string>());
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione in ResetHashDict{Environment.NewLine}{exc}");
}
return fatto;
}
/// <summary>
/// Svuota localstorage (clear)
/// </summary>
/// <returns></returns>
public async Task<bool> StoreLocalClear()
{
bool answ = false;
try
{
await localStore.ClearAsync();
answ = true;
}
catch (Exception ex)
{
Log.Error($"Eccezione in StoreLocalClear{Environment.NewLine}{ex}");
}
return answ;
}
/// <summary>
/// Restituisce il valore richiesto da localstorage
/// </summary>
/// <param name="sKey">Chiave</param>
/// <returns></returns>
public async Task<string> StoreLocalGet(string sKey)
{
string answ = "";
var result = await localStore.GetItemAsync<string>(sKey);
if (result != null)
{
answ = result;
}
return answ;
}
/// <summary>
/// Scrive il valore nel localstorage
/// </summary>
/// <param name="sKey">Chiave</param>
/// <param name="sVal">Valore associato</param>
/// <returns></returns>
public async Task<bool> StoreLocalSet(string sKey, string sVal)
{
bool answ = false;
try
{
await localStore.SetItemAsStringAsync(sKey, sVal);
answ = true;
}
catch (Exception ex)
{
Log.Error($"Eccezione in StoreLocalSet{Environment.NewLine}{ex}");
}
return answ;
}
/// <summary>
/// Svuota sessionstorage (clear)
/// </summary>
/// <returns></returns>
public async Task<bool> StoreSessClear()
{
bool answ = false;
try
{
await sessionStore.ClearAsync();
answ = true;
}
catch (Exception ex)
{
Log.Error($"Eccezione in StoreLocalClear{Environment.NewLine}{ex}");
}
return answ;
}
/// <summary>
/// Restituisce il valore richiesto da sessionstorage
/// </summary>
/// <param name="sKey">Chiave</param>
/// <returns></returns>
public async Task<string> StoreSessGet(string sKey)
{
string answ = "";
var result = await sessionStore.GetItemAsync<string>(sKey);
if (result != null)
{
answ = result;
}
return answ;
}
/// <summary>
/// Scrive il valore nel sessionstorage (tab)
/// </summary>
/// <param name="sKey">Chiave</param>
/// <param name="sVal">Valore associato</param>
/// <returns></returns>
public async Task<bool> StoreSessSet(string sKey, string sVal)
{
bool answ = false;
try
{
await sessionStore.SetItemAsStringAsync(sKey, sVal);
answ = true;
}
catch (Exception ex)
{
Log.Error($"Eccezione in StoreSessSet{Environment.NewLine}{ex}");
}
return answ;
}
#endregion Public Methods
#region Protected Fields
/// <summary>
/// Oggetto per connessione a REDIS
/// </summary>
protected IConnectionMultiplexer redisConn = null!;
/// <summary>
/// Oggetto DB redis da impiegare x chiamate R/W
/// </summary>
protected IDatabase redisDb = null!;
#endregion Protected Fields
#region Protected Properties
protected ILocalStorageService localStore { get; set; } = null!;
protected ISessionStorageService sessionStore { get; set; } = null!;
#endregion Protected Properties
#region Protected Methods
/// <summary>
/// Effettua upsert in HasList redis
/// </summary>
/// <param name="currKey">Chiave redis della Hashlist</param>
/// <param name="chiave">Chiave nella HashList</param>
/// <param name="valore">Valore da salvare</param>
/// <returns>Num record nella HashList</returns>
protected async Task<long> RedisHashUpsert(RedisKey currKey, string chiave, string valore)
{
long numReq = 0;
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
await redisDb.HashSetAsync(currKey, chiave, valore);
numReq = await redisDb.HashLengthAsync(currKey);
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Log.Trace($"RedisHashUpsert | {currKey} | in: {ts.TotalMilliseconds} ms");
return numReq;
}
#endregion Protected Methods
#if false
private SelectData _detailFilter = SelectData.Init(5, 15);
#endif
#region Private Fields
private int _customerID = -1;
private int _keyNum = -1;
private string _pageIcon = "";
private string _pageName = "";
private string _searchVal = "";
private bool _showCustomers = true;
private bool _showSearch = false;
private Logger Log = LogManager.GetCurrentClassLogger();
#endregion Private Fields
#region Private Methods
/// <summary>
/// Recupero HashSet redis come Dictionary
/// </summary>
/// <param name="currKey"></param>
/// <param name="dict"></param>
private Dictionary<string, string> RedisHashDictGet(RedisKey currKey)
{
Dictionary<string, string> answ = new Dictionary<string, string>();
try
{
answ = redisDb
.HashGetAll(currKey)
.ToDictionary(x => $"{x.Name}", x => $"{x.Value}");
}
catch (Exception exc)
{
Log.Info($"Errore RedisHashDictGet | currKey: {currKey}{Environment.NewLine}{exc}");
}
return answ;
}
/// <summary>
/// Salvataggio Dictionary come HashSet Redis
/// </summary>
/// <param name="currKey"></param>
/// <param name="dict"></param>
private bool RedisHashDictSet(RedisKey currKey, Dictionary<string, string> dict)
{
bool fatto = false;
try
{
HashEntry[] data2ins = new HashEntry[dict.Count];
int i = 0;
foreach (KeyValuePair<string, string> kvp in dict)
{
data2ins[i] = new HashEntry(kvp.Key, kvp.Value);
i++;
}
// salvo!
redisDb.HashSet(currKey, data2ins);
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione in RedisHashDictSet | currKey: {currKey}{Environment.NewLine}{exc}");
}
return fatto;
}
/// <summary>
/// Salvataggio Dictionary come HashSet Redis
/// </summary>
/// <param name="currKey"></param>
/// <param name="dict"></param>
/// <param name="ttl"></param>
private bool RedisHashDictSet(RedisKey currKey, Dictionary<string, string> dict, TimeSpan ttl)
{
bool fatto = false;
try
{
HashEntry[] data2ins = new HashEntry[dict.Count];
int i = 0;
foreach (KeyValuePair<string, string> kvp in dict)
{
data2ins[i] = new HashEntry(kvp.Key, kvp.Value);
i++;
}
// salvo!
redisDb.HashSet(currKey, data2ins);
redisDb.KeyExpire(currKey, ttl);
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione in RedisHashDictSet(+TTL) | currKey: {currKey} | ttl: {ttl}{Environment.NewLine}{exc}");
}
return fatto;
}
/// <summary>
/// Aggiunta KVP in HashSet Redis
/// </summary>
/// <param name="currKey"></param>
/// <param name="dict"></param>
private bool RedisHashSet(RedisKey currKey, string hashField, string value)
{
bool fatto = false;
try
{
// salvo!
redisDb.HashSet(currKey, hashField, value);
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione in RedisHashSet | currKey: {currKey}{Environment.NewLine}{exc}");
}
return fatto;
}
private void ReportPageUpd()
{
if (EA_PageUpdated != null)
{
EA_PageUpdated?.Invoke();
}
}
private void ReportSearch()
{
if (EA_SearchUpdated != null)
{
EA_SearchUpdated?.Invoke();
}
}
#endregion Private Methods
#if false
/// <summary>
/// Dizionario totale preferenze utente
/// </summary>
public Dictionary<string, string> UsersPrefDict
{
get => RedisHashDictGet((RedisKey)$"{redisBaseKey}:{MatrOpr}");
set => RedisHashDictSet((RedisKey)$"{redisBaseKey}:{MatrOpr}", value);
}
#endif
}
}