242 lines
8.6 KiB
C#
242 lines
8.6 KiB
C#
using Microsoft.Extensions.Caching.Distributed;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
using MP.MONO.Core.DTO;
|
|
using MP.MONO.Data;
|
|
using MP.MONO.Data.Controllers;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using StackExchange.Redis;
|
|
|
|
namespace MP.MONO.UI.Data
|
|
{
|
|
public class CurrentDataService : IDisposable
|
|
{
|
|
#region Private Fields
|
|
|
|
private static IConfiguration _configuration;
|
|
private static ILogger<CurrentDataService> _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
|
|
|
|
/// <summary>
|
|
/// Controller dati da DB
|
|
/// </summary>
|
|
public static MpDbController dbController;
|
|
|
|
#endregion Public Fields
|
|
|
|
#region Public Constructors
|
|
|
|
public CurrentDataService(IConfiguration configuration, ILogger<CurrentDataService> logger, IConnectionMultiplexer redisConn)
|
|
{
|
|
_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");
|
|
productionPipe = new MessagePipe(redisConn, "production");
|
|
machStatsPipe = new MessagePipe(redisConn, "machstats");
|
|
maintPipe = new MessagePipe(redisConn, "maintenance");
|
|
toolsPipe = new MessagePipe(redisConn, "tools");
|
|
machEvPipe = new MessagePipe(redisConn, "events");
|
|
actLogPipe = new MessagePipe(redisConn, "activitylog");
|
|
|
|
// 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");
|
|
}
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Properties
|
|
|
|
public MessagePipe alarmPipe { get; set; } = null!;
|
|
public MessagePipe machStatsPipe { get; set; } = null!;
|
|
public MessagePipe maintPipe { get; set; } = null!;
|
|
public MessagePipe parametersPipe { get; set; } = null!;
|
|
public MessagePipe productionPipe { get; set; } = null!;
|
|
public MessagePipe statusPipe { get; set; } = null!;
|
|
public MessagePipe toolsPipe { get; set; } = null!;
|
|
public MessagePipe machEvPipe { get; set; } = null!;
|
|
public MessagePipe actLogPipe { get; set; } = null!;
|
|
|
|
#endregion Public Properties
|
|
|
|
#region Private Methods
|
|
|
|
/// <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 $"MAPO-MONO:{dataType}";
|
|
}
|
|
|
|
#endregion Private Methods
|
|
|
|
#region Public Methods
|
|
|
|
public void Dispose()
|
|
{
|
|
// Clear database controller
|
|
dbController.Dispose();
|
|
}
|
|
|
|
public async Task<List<DisplayDataDTO>> MachineStats()
|
|
{
|
|
List<DisplayDataDTO>? dbResult = new List<DisplayDataDTO>();
|
|
string cacheKey = mHash("Machine:MachStats:Current");
|
|
string rawData = await redisDb.StringGetAsync(cacheKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
dbResult = JsonConvert.DeserializeObject<List<DisplayDataDTO>>(rawData);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<DisplayDataDTO>();
|
|
}
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
public async Task<List<DisplayDataDTO>> MachineParameters()
|
|
{
|
|
List<DisplayDataDTO>? dbResult = new List<DisplayDataDTO>();
|
|
string cacheKey = mHash("Machine:Parameters:Current");
|
|
string rawData = await redisDb.StringGetAsync(cacheKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
dbResult = JsonConvert.DeserializeObject<List<DisplayDataDTO>>(rawData);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<DisplayDataDTO>();
|
|
}
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
public async Task<ProductionDTO> MachineProd()
|
|
{
|
|
ProductionDTO? dbResult = new ProductionDTO();
|
|
string cacheKey = mHash("Machine:Production:Current");
|
|
string rawData = await redisDb.StringGetAsync(cacheKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
dbResult = JsonConvert.DeserializeObject<ProductionDTO>(rawData);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new ProductionDTO();
|
|
}
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
public async Task<MachineDTO> MachineStatus()
|
|
{
|
|
MachineDTO? dbResult = new MachineDTO();
|
|
string cacheKey = mHash("Machine:Status:Current");
|
|
string rawData = await redisDb.StringGetAsync(cacheKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
dbResult = JsonConvert.DeserializeObject<MachineDTO>(rawData);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new MachineDTO();
|
|
}
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
|
|
public async Task<List<DisplayDataDTO>> MachineMaintenance()
|
|
{
|
|
List<DisplayDataDTO>? dbResult = new List<DisplayDataDTO>();
|
|
string cacheKey = mHash("Machine:Maintenance:Current");
|
|
string rawData = await redisDb.StringGetAsync(cacheKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
dbResult = JsonConvert.DeserializeObject<List<DisplayDataDTO>>(rawData);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<DisplayDataDTO>();
|
|
}
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
public async Task<List<DisplayDataDTO>> MachineTools()
|
|
{
|
|
List<DisplayDataDTO>? dbResult = new List<DisplayDataDTO>();
|
|
string cacheKey = mHash("Machine:Tools:Current");
|
|
string rawData = await redisDb.StringGetAsync(cacheKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
dbResult = JsonConvert.DeserializeObject<List<DisplayDataDTO>>(rawData);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<DisplayDataDTO>();
|
|
}
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
public async Task<List<DisplayDataDTO>> MachineActLog()
|
|
{
|
|
List<DisplayDataDTO>? dbResult = new List<DisplayDataDTO>();
|
|
string cacheKey = mHash("Machine:ActivityLog:Current");
|
|
string rawData = await redisDb.StringGetAsync(cacheKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
dbResult = JsonConvert.DeserializeObject<List<DisplayDataDTO>>(rawData);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<DisplayDataDTO>();
|
|
}
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
public async Task<List<DisplayDataDTO>> MachineEventHist()
|
|
{
|
|
List<DisplayDataDTO>? dbResult = new List<DisplayDataDTO>();
|
|
string cacheKey = mHash("Machine:Events:Current");
|
|
string rawData = await redisDb.StringGetAsync(cacheKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
dbResult = JsonConvert.DeserializeObject<List<DisplayDataDTO>>(rawData);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<DisplayDataDTO>();
|
|
}
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |