377 lines
14 KiB
C#
377 lines
14 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 Public Fields
|
|
|
|
/// <summary>
|
|
/// Controller dati da DB
|
|
/// </summary>
|
|
public static MpDbController dbController = null!;
|
|
|
|
#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();
|
|
_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 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);
|
|
}
|
|
|
|
public async Task<bool> createMissingSchedTask(int machineId)
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
bool answ = await dbController.SchedMaintTaskCreateMissing(machineId);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata chiamata a createMissingSchedTask: {ts.TotalMilliseconds} ms");
|
|
return await Task.FromResult(answ);
|
|
}
|
|
|
|
public async Task<List<DataLogModel>> DataLogGetFilt(int machineId, string fluxType, DateTime inizio, DateTime fine)
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
List<DataLogModel>? dbResult = dbController.DataLogGetFilt(machineId, fluxType, inizio, fine);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB ParamLogGetFilt: {ts.TotalMilliseconds} ms");
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
// Clear database controller
|
|
dbController.Dispose();
|
|
}
|
|
|
|
public async Task<List<DisplayDataDTO>> getActLog()
|
|
{
|
|
List<DisplayDataDTO>? dbResult = new List<DisplayDataDTO>();
|
|
string rawData = await redisDb.StringGetAsync(Constants.ACT_LOG_CURR_KEY);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
dbResult = JsonConvert.DeserializeObject<List<DisplayDataDTO>>(rawData);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<DisplayDataDTO>();
|
|
}
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Conf allarmi (da ADAPTER) senza valori silenziati da app
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<BaseAlarmConf>> getAlarmConfig()
|
|
{
|
|
List<BaseAlarmConf>? dbResult = new List<BaseAlarmConf>();
|
|
string rawData = await redisDb.StringGetAsync(Constants.ALARMS_CONF_KEY);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
dbResult = JsonConvert.DeserializeObject<List<BaseAlarmConf>>(rawData);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<BaseAlarmConf>();
|
|
}
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
public async Task<List<string>> getAlarms()
|
|
{
|
|
List<string>? dbResult = new List<string>();
|
|
string rawData = await redisDb.StringGetAsync(Constants.ALARM_CURR_KEY);
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
dbResult = JsonConvert.DeserializeObject<List<string>>(rawData);
|
|
}
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<string>();
|
|
}
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Conf allarmi IMPOSTATA con valori silenziati
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<BaseAlarmConf>> getAlarmSetup()
|
|
{
|
|
List<BaseAlarmConf>? dbResult = new List<BaseAlarmConf>();
|
|
string rawData = await redisDb.StringGetAsync(Constants.ALARMS_SETT_KEY);
|
|
//se non avessi trovato valori cerco quelli di setup...
|
|
if (string.IsNullOrEmpty(rawData))
|
|
{
|
|
rawData = await redisDb.StringGetAsync(Constants.ALARMS_CONF_KEY);
|
|
}
|
|
// ora provo a deserializzare
|
|
if (!string.IsNullOrEmpty(rawData))
|
|
{
|
|
dbResult = JsonConvert.DeserializeObject<List<BaseAlarmConf>>(rawData);
|
|
}
|
|
// altrimenti imposto vuoto
|
|
if (dbResult == null)
|
|
{
|
|
dbResult = new List<BaseAlarmConf>();
|
|
}
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
public async Task<List<DisplayDataDTO>> getEventHist()
|
|
{
|
|
List<DisplayDataDTO>? dbResult = new List<DisplayDataDTO>();
|
|
string rawData = await redisDb.StringGetAsync(Constants.EVENT_LOG_CURR_KEY);
|
|
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 rawData = await redisDb.StringGetAsync(Constants.MAINT_STATS_CURR_KEY);
|
|
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 rawData = await redisDb.StringGetAsync(Constants.MODE_CONF_KEY);
|
|
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 rawData = await redisDb.StringGetAsync(Constants.PARAMS_CURR_KEY);
|
|
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 rawData = await redisDb.StringGetAsync(Constants.PROD_CURR_KEY);
|
|
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 rawData = await redisDb.StringGetAsync(Constants.MACH_STATS_CURR_KEY);
|
|
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 rawData = await redisDb.StringGetAsync(Constants.STATUS_CURR_KEY);
|
|
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 rawData = await redisDb.StringGetAsync(Constants.STATUS_CONF_KEY);
|
|
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 rawData = await redisDb.StringGetAsync(Constants.TOOLS_CURR_KEY);
|
|
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<PrevMaintTaskModel>> PrevMaintTaskGetFilt(int machineId, int skipRec, int numRec)
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
List<PrevMaintTaskModel>? dbResult = dbController.PrevMaintTaskGetFilt(machineId, skipRec, numRec);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB PrevMaintTaskGetFilt: {ts.TotalMilliseconds} ms");
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
public async Task<List<ScheduledMaintModel>> SchedMaintTaskGetFilt(int machineId, int skipRec, int numRec, bool showCompleted)
|
|
{
|
|
Stopwatch stopWatch = new Stopwatch();
|
|
stopWatch.Start();
|
|
List<ScheduledMaintModel>? dbResult = dbController.SchedMaintTaskGetFilt(machineId, skipRec, numRec, showCompleted);
|
|
stopWatch.Stop();
|
|
TimeSpan ts = stopWatch.Elapsed;
|
|
Log.Trace($"Effettuata lettura da DB SchedMaintTaskGetFilt: {ts.TotalMilliseconds} ms");
|
|
return await Task.FromResult(dbResult);
|
|
}
|
|
|
|
public async Task<bool> setAlarmSetup(List<BaseAlarmConf> alarmConf)
|
|
{
|
|
bool answ = false;
|
|
string rawData = JsonConvert.SerializeObject(alarmConf);
|
|
await redisDb.StringSetAsync(Constants.ALARMS_SETT_KEY, rawData);
|
|
return await Task.FromResult(answ);
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Private Fields
|
|
|
|
private static IConfiguration _configuration = null!;
|
|
private static ILogger<CurrentDataService> _logger = null!;
|
|
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
|
|
}
|
|
} |