Files
mapo-core/MP.Data/Services/MessageService.cs
T
Samuele Locatelli 4f8b46882c Aggiunta modelli ST
2023-10-04 08:55:06 +02:00

514 lines
13 KiB
C#

using Blazored.LocalStorage;
using Blazored.SessionStorage;
using MP.Data.DatabaseModels;
using NLog;
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Threading.Tasks;
namespace MP.Data.Services
{
public class MessageService
{
#region Public Fields
public int orarioDip = 0;
#endregion Public Fields
#region Public Constructors
public MessageService(ILocalStorageService genLocalStorage, ISessionStorageService sessStore)
{
localStorage = genLocalStorage;
sessionStore = sessStore;
}
#endregion Public Constructors
#region Public Events
public event Action EA_OperUpdated = null!;
public event Action EA_PageUpdated = null!;
#endregion Public Events
#if false
public event Action EA_DateChanged = null!;
public event Action EA_CalModeChanged = null!;
public event Action EA_TimbUpd = null!;
#endif
#if false
public RegAttivitaModel? clonedRA { get; set; } = null;
/// <summary>
/// Calcola HASH del codice impiego = payload utente
/// </summary>
/// <returns></returns>
public string HashDip
{
get
{
DataValidator cDV = new DataValidator(_rigaOper);
string hash = cDV.HashDip;
return hash;
}
}
#endif
#region Public Properties
/// <summary>
/// Dizionario macchine
/// </summary>
public Dictionary<string, string> DictMacchine
{
get
{
Dictionary<string, string> answ = new Dictionary<string, string>();
var pUpd = Task.Run(async () =>
{
answ = await localStorage.GetItemAsync<Dictionary<string, string>>(KeyMacDict);
});
pUpd.Wait();
return answ;
}
set
{
var pUpd = Task.Run(async () =>
{
await localStorage.SetItemAsync(KeyMacDict, value);
});
pUpd.Wait();
}
}
public int MatrOpr
{
get
{
int answ = 102;
//int answ = -1;
if (_rigaOper != null)
{
answ = _rigaOper.MatrOpr;
}
return answ;
}
}
public string UserAuthKey
{
get
{
string answ = "";
if (_rigaOper != null)
{
answ = _rigaOper.authKey;
}
return answ;
}
}
public AnagOperatoriModel? RigaOper
{
get => _rigaOper;
set
{
// salvo
_rigaOper = value;
if (EA_OperUpdated != null)
{
EA_OperUpdated?.Invoke();
}
}
}
#endregion Public Properties
#region Public Methods
public async Task ClearLocalStor()
{
await localStorage.ClearAsync();
}
public async Task ClearSessionStor()
{
await sessionStore.ClearAsync();
}
/// <summary>
/// Restituisce il valore Ipv4 del Device da localstorage
/// </summary>
/// <returns></returns>
public async Task<string> getDevIpAsync()
{
string answ = "";
var result = await localStorage.GetItemAsync<string>(KeyDevIp4);
if (result != null)
{
answ = result;
}
return answ;
}
/// <summary>
/// Restituisce il valore di DeviceSecret da localstorage
/// </summary>
/// <returns></returns>
public async Task<string> getDevSecretAsync()
{
string answ = "";
var result = await localStorage.GetItemAsync<string>(KeyDevSec);
if (result != null)
{
answ = result;
}
return answ;
}
/// <summary>
/// Recupero dati MSE x macchina
/// </summary>
/// <param name="idxMacchina"></param>
/// <returns></returns>
public async Task<MappaStatoExpl> GetMachineMse(string idxMacchina)
{
MappaStatoExpl answ = new MappaStatoExpl();
//answ = await localStorage.GetItemAsync<MappaStatoExpl>(machineMse(idxMacchina));
string tryString = await localStorage.GetItemAsync<string>(machineMse(idxMacchina));
if (tryString != "")
{
answ = JsonSerializer.Deserialize<MappaStatoExpl>(tryString);
}
return answ;
}
/// <summary>
/// Macchine attualmente selezionata
/// </summary>
public async Task<string> IdxMaccGet()
{
return await sessionStore.GetItemAsync<string>("CurrMach");
}
/// <summary>
/// Macchine attualmente selezionata
/// </summary>
public async Task IdxMaccSet(string machSel)
{
await sessionStore.SetItemAsync("CurrMach", machSel);
}
#if false
public bool IsActive
{
get
{
bool answ = false;
if (_rigaOper != null && _rigaOper.Attivo != null)
{
answ = (bool)_rigaOper.Attivo;
}
return answ;
}
}
public bool isRicTimb { get; set; } = false;
public CalendarModeEnum.modoControllo modoCal { get; set; } = CalendarModeEnum.modoControllo.showCalendar;
#endif
#if false
/// <summary>
/// Indica ultima azione compiuta
/// </summary>
public string LastAction
{
get => lastAction;
set
{
if (lastAction != value)
{
lastAction = value;
}
}
}
/// <summary>
/// Indica se il prossimo stato timbratura sia entrata
/// </summary>
public bool NextIsEntrata
{
get => nextIsEntrata;
set
{
if (nextIsEntrata != value)
{
nextIsEntrata = value;
}
}
}
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 bool PayloadOk { get; set; } = false;
public RegAttivitaModel? recordRA { get; set; } = null;
#endif
#if false
public string SearchVal
{
get => _searchVal;
set
{
if (_searchVal != value)
{
_searchVal = value;
if (EA_DipUpdated != null)
{
EA_DipUpdated?.Invoke();
}
}
}
}
public int selWeekNum { get; set; } = 0;
public int selYear { get; set; } = DateTime.Today.Year;
public bool ShowSearch
{
get => showSearch;
set
{
if (showSearch != value)
{
showSearch = value;
}
}
}
public DateTime targetDate { get; set; } = DateTime.Today;
public DateTime targetDateMancTimb { get; set; }
#endif
/// <summary>
/// Effettua salvataggio in localstorage dei dati MSE correnti
/// </summary>
/// <param name="currListMSE"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public async Task SaveMse(List<MappaStatoExpl> currListMSE)
{
foreach (var item in currListMSE)
{
string serVal = JsonSerializer.Serialize(item);
await localStorage.SetItemAsync(machineMse(item.IdxMacchina), serVal);
}
}
/// <summary>
/// Scrive il valore di IPV4 del device nel localstoragee
/// </summary>
/// <param name="newVal"></param>
/// <returns></returns>
public async Task<bool> setDevIpv4Async(string newVal)
{
bool answ = false;
try
{
await localStorage.SetItemAsync(KeyDevIp4, newVal);
answ = true;
}
catch (Exception ex)
{
Log.Error($"Eccezione in setDevIpv4Async{Environment.NewLine}{ex}");
}
return answ;
}
/// <summary>
/// Scrive il valore di DeviceSecret nel localstoragee
/// </summary>
/// <param name="newVal"></param>
/// <returns></returns>
public async Task<bool> setDevSecretAsync(string newVal)
{
bool answ = false;
try
{
await localStorage.SetItemAsync(KeyDevSec, newVal);
answ = true;
}
catch (Exception ex)
{
Log.Error($"Eccezione in setDevSecretAsync{Environment.NewLine}{ex}");
}
return answ;
}
#endregion Public Methods
#region Protected Fields
protected const string KeyDevIp4 = "DevIpv4";
protected const string KeyDevSec = "DevSec";
protected const string KeyMacDict = "MachineDict";
#endregion Protected Fields
#if false
public void ReportDateChange()
{
if (EA_DateChanged != null)
{
EA_DateChanged?.Invoke();
}
}
public void setModeCalendar()
{
modoCal = CalendarModeEnum.modoControllo.showCalendar;
if (EA_CalModeChanged != null)
{
EA_CalModeChanged?.Invoke();
}
}
public void setModeGauge()
{
modoCal = CalendarModeEnum.modoControllo.showGauge;
if (EA_CalModeChanged != null)
{
EA_CalModeChanged?.Invoke();
}
}
public void setModeProj()
{
modoCal = CalendarModeEnum.modoControllo.showProj;
if (EA_CalModeChanged != null)
{
EA_CalModeChanged?.Invoke();
}
}
public void setModeTempRil()
{
modoCal = CalendarModeEnum.modoControllo.showTempRil;
if (EA_CalModeChanged != null)
{
EA_CalModeChanged?.Invoke();
}
}
public void setModeRichTimb()
{
modoCal = CalendarModeEnum.modoControllo.showTimbr;
if (EA_CalModeChanged != null)
{
EA_CalModeChanged?.Invoke();
}
}
public void setModeShowWeek()
{
modoCal = CalendarModeEnum.modoControllo.showWeek;
if (EA_CalModeChanged != null)
{
EA_CalModeChanged?.Invoke();
}
}
public void ReportTimbratura()
{
if (EA_TimbUpd != null)
{
EA_TimbUpd?.Invoke();
}
}
#endif
#region Protected Properties
protected ILocalStorageService localStorage { get; set; } = null!;
protected ISessionStorageService sessionStore { get; set; } = null!;
#endregion Protected Properties
#region Private Fields
private AnagOperatoriModel? _rigaOper;
private Logger Log = LogManager.GetCurrentClassLogger();
#endregion Private Fields
#region Private Methods
private string machineMse(string idxMacc)
{
return $"MSE_{idxMacc}";
}
#endregion Private Methods
#if false
private string _pageIcon = "";
private string _pageName = "";
#endif
#if false
private string _searchVal = "";
private bool showSearch;
#endif
#if false
private string lastAction { get; set; } = "";
private bool nextIsEntrata { get; set; } = true;
#endif
#if false
private void ReportPageUpd()
{
if (EA_PageUpdated != null)
{
EA_PageUpdated?.Invoke();
}
}
private void ReportSearch()
{
if (EA_DipUpdated != null)
{
EA_DipUpdated?.Invoke();
}
}
#endif
}
}