OK display sim parameters in HOME
This commit is contained in:
@@ -8,6 +8,7 @@ using NLog;
|
||||
using StackExchange.Redis;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace MP.MONO.UI.Data
|
||||
{
|
||||
@@ -19,6 +20,15 @@ namespace MP.MONO.UI.Data
|
||||
private static ILogger<CurrentDataService> _logger;
|
||||
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
||||
private readonly IDistributedCache distributedCache;
|
||||
/// <summary>
|
||||
/// Oggetto per connessione a REDIS
|
||||
/// </summary>
|
||||
private ConnectionMultiplexer redisConn = null!;
|
||||
//ISubscriber sub = redis.GetSubscriber();
|
||||
/// <summary>
|
||||
/// Oggetto DB redis da impiegare x chiamate R/W
|
||||
/// </summary>
|
||||
private IDatabase redisDb = null!;
|
||||
|
||||
/// <summary>
|
||||
/// Durata assoluta massima della cache IN SECONDI
|
||||
@@ -42,12 +52,13 @@ namespace MP.MONO.UI.Data
|
||||
/// <summary>
|
||||
/// Controller dati simulati
|
||||
/// </summary>
|
||||
public static SimController simController;
|
||||
public static SimController simGen;
|
||||
|
||||
#endregion Public Fields
|
||||
|
||||
|
||||
public MessagePipe alarmPipe { get; set; } = null!;
|
||||
public MessagePipe parametersPipe { get; set; } = null!;
|
||||
public MessagePipe statusPipe { get; set; } = null!;
|
||||
|
||||
#region Public Constructors
|
||||
@@ -56,10 +67,18 @@ namespace MP.MONO.UI.Data
|
||||
{
|
||||
_logger = logger;
|
||||
_configuration = configuration;
|
||||
|
||||
// setup compoenti REDIS
|
||||
this.redisConn = ConnectionMultiplexer.Connect(_configuration.GetConnectionString("Redis"));
|
||||
this.redisDb = this.redisConn.GetDatabase();
|
||||
// setup canali pub/sub
|
||||
alarmPipe = new MessagePipe(redisConn, "allarms");
|
||||
parametersPipe = new MessagePipe(redisConn, "parameters");
|
||||
statusPipe = new MessagePipe(redisConn, "status");
|
||||
|
||||
// conf cache
|
||||
this.distributedCache = distributedCache;
|
||||
|
||||
// conf DB
|
||||
string connStr = _configuration.GetConnectionString("MP.MONO.Data");
|
||||
if (string.IsNullOrEmpty(connStr))
|
||||
@@ -71,7 +90,7 @@ namespace MP.MONO.UI.Data
|
||||
dbController = new MpDbController(configuration);
|
||||
_logger.LogInformation("DbController OK");
|
||||
}
|
||||
simController = new SimController();
|
||||
simGen = new SimController();
|
||||
}
|
||||
|
||||
#endregion Public Constructors
|
||||
@@ -114,25 +133,38 @@ namespace MP.MONO.UI.Data
|
||||
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;
|
||||
|
||||
var newParamList = Enumerable.Range(1, 10)
|
||||
.ToDictionary(x => $"Par{x:00}", x => $"{Random.Shared.Next(0, 1000)}");
|
||||
foreach (var item in newParamList)
|
||||
string cacheKey = mHash("Machine:Parameters:Current");
|
||||
string rawData = await redisDb.StringGetAsync(cacheKey);
|
||||
if (!string.IsNullOrEmpty(rawData))
|
||||
{
|
||||
ParametersList.Add(item.Key, item.Value);
|
||||
ParametersList = JsonConvert.DeserializeObject<Dictionary<string, string>>(rawData);
|
||||
}
|
||||
else
|
||||
{
|
||||
ParametersList = simGen.GetCurrentParameters();
|
||||
rawData = JsonConvert.SerializeObject(ParametersList);
|
||||
await redisDb.StringSetAsync(cacheKey, rawData);
|
||||
}
|
||||
await Task.Delay(1);
|
||||
return ParametersList;
|
||||
}
|
||||
|
||||
public async Task<List<DisplayDataDTO>> MachineDisplay()
|
||||
{
|
||||
List<DisplayDataDTO>? dbResult = new List<DisplayDataDTO>();
|
||||
string cacheKey = mHash("Machine:DisplayData");
|
||||
string cacheKey = mHash("Machine:Parameters:Current");
|
||||
string rawData = await redisDb.StringGetAsync(cacheKey);
|
||||
if (!string.IsNullOrEmpty(rawData))
|
||||
{
|
||||
dbResult = JsonConvert.DeserializeObject<List<DisplayDataDTO>>(rawData);
|
||||
}
|
||||
else
|
||||
{
|
||||
dbResult = simGen.MachineGetParameters();
|
||||
rawData = JsonConvert.SerializeObject(dbResult);
|
||||
await redisDb.StringSetAsync(cacheKey, rawData);
|
||||
}
|
||||
#if false
|
||||
|
||||
string rawData;
|
||||
var redisDataList = await distributedCache.GetAsync(cacheKey);
|
||||
if (redisDataList != null)
|
||||
@@ -156,7 +188,8 @@ namespace MP.MONO.UI.Data
|
||||
if (dbResult == null)
|
||||
{
|
||||
dbResult = new List<DisplayDataDTO>();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return await Task.FromResult(dbResult);
|
||||
}
|
||||
|
||||
@@ -195,19 +228,16 @@ namespace MP.MONO.UI.Data
|
||||
{
|
||||
MachineDTO? dbResult = new MachineDTO();
|
||||
string cacheKey = mHash("Machine:Status:Current");
|
||||
string rawData;
|
||||
var redisDataList = await distributedCache.GetAsync(cacheKey);
|
||||
if (redisDataList != null)
|
||||
string rawData = await redisDb.StringGetAsync(cacheKey);
|
||||
if (!string.IsNullOrEmpty(rawData))
|
||||
{
|
||||
rawData = Encoding.UTF8.GetString(redisDataList);
|
||||
dbResult = JsonConvert.DeserializeObject<MachineDTO>(rawData);
|
||||
}
|
||||
else
|
||||
{
|
||||
dbResult = simController.MachineGetStatus();
|
||||
dbResult = simGen.MachineGetStatus();
|
||||
rawData = JsonConvert.SerializeObject(dbResult);
|
||||
redisDataList = Encoding.UTF8.GetBytes(rawData);
|
||||
//await distributedCache.SetAsync(cacheKey, redisDataList, cacheOpt(true));
|
||||
redisDb.StringSetAsync(cacheKey, rawData);
|
||||
}
|
||||
if (dbResult == null)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user