157 lines
5.3 KiB
C#
157 lines
5.3 KiB
C#
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
|
|
{
|
|
public class MpDataService : IDisposable
|
|
{
|
|
#region Private Fields
|
|
|
|
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
|
|
|
|
public static MP.Data.Controllers.MpMonController dbController;
|
|
|
|
#endregion Public Fields
|
|
|
|
#region Public Constructors
|
|
|
|
public MpDataService(IConfiguration configuration, ILogger<MpDataService> logger)
|
|
{
|
|
_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))
|
|
{
|
|
_logger.LogError("ConnString empty!");
|
|
}
|
|
else
|
|
{
|
|
dbController = new MP.Data.Controllers.MpMonController(configuration);
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.AppendLine($"DbController OK");
|
|
//sb.AppendLine($"CST: {dbController.CustomersCount()} | CNT: {dbController.CountersCount()} | BSK: {dbController.BasketsCount()} | NGT: {dbController.NegotiationsCount()} | DOC: {dbController.DocsCount()} | ITM: {dbController.ItemsCount()} | RES: {dbController.ResourcesCount()}");
|
|
_logger.LogInformation(sb.ToString());
|
|
}
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
public Task<List<ConfigModel>> ConfigGetAll()
|
|
{
|
|
return Task.FromResult(dbController.ConfigGetAll().ToList());
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
// Clear database controller
|
|
dbController.Dispose();
|
|
}
|
|
|
|
public Task<List<Macchine>> MacchineGetAll()
|
|
{
|
|
return Task.FromResult(dbController.MacchineGetAll().ToList());
|
|
}
|
|
|
|
public Task<List<MappaStatoExpl>> MseGetAll()
|
|
{
|
|
var dbResult = dbController.MseGetAll();
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<MappaStatoExpl>();
|
|
}
|
|
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
|
|
}
|
|
} |