Inizio update x gestione override dati in MON
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
using MP.Data.DatabaseModels;
|
||||
using System.Text;
|
||||
using StackExchange.Redis;
|
||||
using NLog;
|
||||
using MP.Data.DTO;
|
||||
using Newtonsoft.Json;
|
||||
using MP.Data;
|
||||
|
||||
namespace MP.Mon.Data
|
||||
{
|
||||
@@ -10,6 +15,19 @@ namespace MP.Mon.Data
|
||||
private static IConfiguration _configuration;
|
||||
private static ILogger<MpDataService> _logger;
|
||||
|
||||
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
||||
|
||||
/// <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!;
|
||||
|
||||
#endregion Private Fields
|
||||
|
||||
#region Public Fields
|
||||
@@ -24,6 +42,13 @@ namespace MP.Mon.Data
|
||||
{
|
||||
_logger = logger;
|
||||
_configuration = configuration;
|
||||
|
||||
// setup compoenti REDIS
|
||||
this.redisConn = ConnectionMultiplexer.Connect(_configuration.GetConnectionString("Redis"));
|
||||
this.redisDb = this.redisConn.GetDatabase();
|
||||
//// setup canali pub/sub
|
||||
//actLogPipe = new MessagePipe(redisConn, Constants.ACT_LOG_M_QUEUE);
|
||||
|
||||
// conf DB
|
||||
string connStr = _configuration.GetConnectionString("Mp.Mon");
|
||||
if (string.IsNullOrEmpty(connStr))
|
||||
@@ -70,6 +95,63 @@ namespace MP.Mon.Data
|
||||
return Task.FromResult(dbResult);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configurazione (REDIS) dei tag in override
|
||||
/// </summary>
|
||||
/// <param name="CodIOB">IOB di cui cercare la conf</param>
|
||||
/// <returns></returns>
|
||||
public async Task<List<TagDataDTO>> getTagConf(string CodIOB)
|
||||
{
|
||||
List<TagDataDTO>? currResult = new List<TagDataDTO>();
|
||||
// cerco in REDIS la conf x l'IOB
|
||||
string rawData = await redisDb.StringGetAsync($"{Constants.CONF_MON_KEY}:{CodIOB}");
|
||||
if (!string.IsNullOrEmpty(rawData))
|
||||
{
|
||||
currResult = JsonConvert.DeserializeObject<List<TagDataDTO>>(rawData);
|
||||
}
|
||||
// altrimenti in conf file, se presente, e salvo
|
||||
if (currResult == null)
|
||||
{
|
||||
currResult = tryLoadConf(CodIOB);
|
||||
}
|
||||
// altrimenti vuoto!
|
||||
if (currResult == null)
|
||||
{
|
||||
currResult = new List<TagDataDTO>();
|
||||
}
|
||||
return await Task.FromResult(currResult);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Prova a caricare da file la conf dell'IOB richiesto salvando su REDIS
|
||||
/// </summary>
|
||||
/// <param name="codIOB"></param>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
private List<TagDataDTO> tryLoadConf(string codIOB)
|
||||
{
|
||||
List<TagDataDTO> currConf = new List<TagDataDTO>();
|
||||
string strExeFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
|
||||
string strWorkPath = Path.GetDirectoryName(strExeFilePath);
|
||||
|
||||
string filePath = $"{strWorkPath}/Conf/{codIOB}";
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
string rawData = File.ReadAllText(filePath);
|
||||
if (!string.IsNullOrEmpty(rawData))
|
||||
{
|
||||
currConf = JsonConvert.DeserializeObject<List<TagDataDTO>>(rawData);
|
||||
// salvo in redis!
|
||||
redisDb.StringSet($"{Constants.CONF_MON_KEY}:{codIOB}", rawData);
|
||||
}
|
||||
}
|
||||
if (currConf == null)
|
||||
{
|
||||
currConf = new List<TagDataDTO>();
|
||||
}
|
||||
// rendo!
|
||||
return currConf;
|
||||
}
|
||||
|
||||
#endregion Public Methods
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user