299 lines
10 KiB
C#
299 lines
10 KiB
C#
using MP.MONO.Core;
|
|
using MP.MONO.Core.CONF;
|
|
using MP.MONO.Core.DTO;
|
|
using MP.MONO.Data;
|
|
using MP.MONO.Data.Controllers;
|
|
using MP.MONO.Data.DbModels;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using StackExchange.Redis;
|
|
using System.Diagnostics;
|
|
|
|
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
|
|
actLogPipe = new MessagePipe(redisConn, Constants.ACT_LOG_M_QUEUE);
|
|
alarmPipe = new MessagePipe(redisConn, Constants.ALARM_M_QUEUE);
|
|
machEvPipe = new MessagePipe(redisConn, Constants.EVENT_LOG_M_QUEUE);
|
|
parametersPipe = new MessagePipe(redisConn, Constants.PARAMS_M_QUEUE);
|
|
productionPipe = new MessagePipe(redisConn, Constants.PROD_M_QUEUE);
|
|
machStatsPipe = new MessagePipe(redisConn, Constants.MACH_STATS_M_QUEUE);
|
|
maintPipe = new MessagePipe(redisConn, Constants.MAINT_STATS_M_QUEUE);
|
|
statusPipe = new MessagePipe(redisConn, Constants.STATUS_M_QUEUE);
|
|
toolsPipe = new MessagePipe(redisConn, Constants.TOOLS_M_QUEUE);
|
|
|
|
// 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 actLogPipe { get; set; } = null!;
|
|
|
|
public MessagePipe alarmPipe { get; set; } = null!;
|
|
|
|
public MessagePipe machEvPipe { 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!;
|
|
|
|
#endregion Public Properties
|
|
|
|
#region Public Methods
|
|
|
|
public void Dispose()
|
|
{
|
|
// Clear database controller
|
|
dbController.Dispose();
|
|
}
|
|
|
|
public async Task<List<DisplayDataDTO>> getActLog()
|
|
{
|
|
List<DisplayDataDTO>? dbResult = new List<DisplayDataDTO>();
|
|
string cacheKey = Constants.ACT_LOG_CURR_KEY;
|
|
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<string>> getAlarms()
|
|
{
|
|
List<string>? dbResult = new List<string>();
|
|
string cacheKey = Constants.ALARM_CURR_KEY;
|
|
string rawData = await redisDb.StringGetAsync(cacheKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
dbResult = JsonConvert.DeserializeObject<List<string>>(rawData);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<string>();
|
|
}
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
public async Task<List<DisplayDataDTO>> getEventHist()
|
|
{
|
|
List<DisplayDataDTO>? dbResult = new List<DisplayDataDTO>();
|
|
string cacheKey = Constants.EVENT_LOG_CURR_KEY;
|
|
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>> getMaintenance()
|
|
{
|
|
List<DisplayDataDTO>? dbResult = new List<DisplayDataDTO>();
|
|
string cacheKey = Constants.MAINT_STATS_CURR_KEY;
|
|
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<MachineMode>> getModesList()
|
|
{
|
|
List<MachineMode>? dbResult = new List<MachineMode>();
|
|
string cacheKey = Constants.MODE_CONF_KEY;
|
|
string rawData = await redisDb.StringGetAsync(cacheKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
dbResult = JsonConvert.DeserializeObject<List<MachineMode>>(rawData);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<MachineMode>();
|
|
}
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
public async Task<List<DisplayDataDTO>> getParameters()
|
|
{
|
|
List<DisplayDataDTO>? dbResult = new List<DisplayDataDTO>();
|
|
string cacheKey = Constants.PARAMS_CURR_KEY;
|
|
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> getProd()
|
|
{
|
|
ProductionDTO? dbResult = new ProductionDTO();
|
|
string cacheKey = Constants.PROD_CURR_KEY;
|
|
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<List<DisplayDataDTO>> getStats()
|
|
{
|
|
List<DisplayDataDTO>? dbResult = new List<DisplayDataDTO>();
|
|
string cacheKey = Constants.MACH_STATS_CURR_KEY;
|
|
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<MachineDTO> getStatus()
|
|
{
|
|
MachineDTO? dbResult = new MachineDTO();
|
|
string cacheKey = Constants.STATUS_CURR_KEY;
|
|
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<MachineStatus>> getStatusList()
|
|
{
|
|
List<MachineStatus>? dbResult = new List<MachineStatus>();
|
|
string cacheKey = Constants.STATUS_CONF_KEY;
|
|
string rawData = await redisDb.StringGetAsync(cacheKey);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
dbResult = JsonConvert.DeserializeObject<List<MachineStatus>>(rawData);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<MachineStatus>();
|
|
}
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
public async Task<List<DisplayDataDTO>> getTools()
|
|
{
|
|
List<DisplayDataDTO>? dbResult = new List<DisplayDataDTO>();
|
|
string cacheKey = Constants.TOOLS_CURR_KEY;
|
|
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<AlarmLogModel>> AlarmLogGetFilt(int machineId, int skipRec, int numRec)
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
List<AlarmLogModel>? dbResult = dbController.AlarmLogGetFilt(machineId, skipRec, numRec);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB AlarmLogGetFilt: {ts.TotalMilliseconds} ms");
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |