91 lines
3.5 KiB
C#
91 lines
3.5 KiB
C#
using Microsoft.AspNetCore.Identity.UI.Services;
|
|
using Microsoft.Extensions.Caching.Distributed;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
using MP.MONO.Data.Controllers;
|
|
using NLog;
|
|
|
|
namespace MP.MONO.UI.Data
|
|
{
|
|
public class CurrentDataService : IDisposable
|
|
{
|
|
|
|
private static IConfiguration _configuration;
|
|
private static ILogger<CurrentDataService> _logger;
|
|
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
|
private readonly IEmailSender _emailSender;
|
|
private readonly IDistributedCache distributedCache;
|
|
private readonly IMemoryCache memoryCache;
|
|
|
|
public static MpDbController dbController;
|
|
|
|
/// <summary>
|
|
/// Durata assoluta massima della cache IN SECONDI
|
|
/// </summary>
|
|
private int chAbsExp = 60 * 5;
|
|
|
|
/// <summary>
|
|
/// Durata della cache IN SECONDI in modalità inattiva (non acceduta) prima di venire rimossa
|
|
/// NON estende oltre il tempo massimo di validità della cache (chAbsExp)
|
|
/// </summary>
|
|
private int chSliExp = 60 * 1;
|
|
|
|
public CurrentDataService(IConfiguration configuration, ILogger<CurrentDataService> logger, IMemoryCache memoryCache, IDistributedCache distributedCache, IEmailSender emailSender)
|
|
{
|
|
_logger = logger;
|
|
_configuration = configuration;
|
|
_emailSender = emailSender;
|
|
// conf cache
|
|
this.memoryCache = memoryCache;
|
|
this.distributedCache = distributedCache;
|
|
// conf DB
|
|
string connStr = _configuration.GetConnectionString("MP.MONO.Data");
|
|
if (string.IsNullOrEmpty(connStr))
|
|
{
|
|
_logger.LogError("ConnString empty!");
|
|
}
|
|
else
|
|
{
|
|
dbController = new MpDbController(configuration);
|
|
_logger.LogInformation("DbController OK");
|
|
}
|
|
}
|
|
|
|
private DistributedCacheEntryOptions cacheOpt(bool fastCache)
|
|
{
|
|
var numSecAbsExp = fastCache ? chAbsExp : chAbsExp * 10;
|
|
var numSecSliExp = fastCache ? chSliExp : chSliExp * 10;
|
|
return new DistributedCacheEntryOptions().SetAbsoluteExpiration(DateTime.Now.AddSeconds(numSecAbsExp)).SetSlidingExpiration(TimeSpan.FromSeconds(numSecSliExp));
|
|
}
|
|
public void Dispose()
|
|
{
|
|
// Clear database controller
|
|
dbController.Dispose();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Hash Redis contenente i dati MP di una specifico TYPE (es StatusMacchina, StateMachineIngressi, ...)
|
|
/// </summary>
|
|
/// <param name="dataType"></param>
|
|
/// <returns></returns>
|
|
private static string mHash(string dataType)
|
|
{
|
|
return $"DATA:{dataType}";
|
|
}
|
|
|
|
protected Dictionary<string, string> ParametersList { get; set; } = new Dictionary<string, string>();
|
|
|
|
public async Task<Dictionary<string, string>> GetCurrentParameters()
|
|
{
|
|
//ParametersList = new Dictionary<string, string>();
|
|
//ParametersList.Add("Speed", $"{Random.Shared.Next(0, 5000)}");
|
|
//ParametersList.Add("Feed", $"{Random.Shared.Next(0, 10000)}");
|
|
//ParametersList.Add("Load", $"{Random.Shared.Next(0, 100)}");
|
|
//return ParametersList;
|
|
ParametersList = Enumerable.Range(1, 20)
|
|
.ToDictionary(x => $"Par{x:00}", x => $"{Random.Shared.Next(0, 1000)}");
|
|
await Task.Delay(1);
|
|
return ParametersList;
|
|
}
|
|
}
|
|
} |