1103 lines
43 KiB
C#
1103 lines
43 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using MP.Data.DatabaseModels;
|
|
using MP.Data.Objects;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using StackExchange.Redis;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using static MP.Data.Objects.Enums;
|
|
|
|
namespace MP.Data.Services
|
|
{
|
|
public class TabDataService : BaseServ, IDisposable
|
|
{
|
|
#region Public Constructors
|
|
|
|
/// <summary>
|
|
/// Init servizio TAB
|
|
/// </summary>
|
|
/// <param name="configuration"></param>
|
|
public TabDataService(IConfiguration configuration)
|
|
{
|
|
_configuration = configuration;
|
|
|
|
// setup compoenti REDIS
|
|
redisConn = ConnectionMultiplexer.Connect(_configuration.GetConnectionString("Redis"));
|
|
redisDb = redisConn.GetDatabase();
|
|
|
|
// conf DB
|
|
ConnStr = _configuration.GetConnectionString("Mp.All");
|
|
if (string.IsNullOrEmpty(ConnStr))
|
|
{
|
|
Log.Error("ConnString empty!");
|
|
}
|
|
else
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
dbTabController = new Controllers.MpTabController(configuration);
|
|
sb.AppendLine($"TabDataService | MpTabController OK");
|
|
dbIocController = new Controllers.MpIocController(configuration);
|
|
sb.AppendLine($"TabDataService | MpIocController OK");
|
|
Log.Info(sb.ToString());
|
|
// sistemo i parametri x redHas...
|
|
CodModulo = _configuration.GetValue<string>("OptConf:CodModulo");
|
|
var cstringArray = ConnStr.Split(";");
|
|
foreach (var item in cstringArray)
|
|
{
|
|
var cData = item.Trim().Split("=");
|
|
if (cData.Length == 2)
|
|
{
|
|
if (!connStrParams.ContainsKey(cData[0]))
|
|
{
|
|
connStrParams.Add(cData[0], cData[1]);
|
|
}
|
|
}
|
|
}
|
|
// sistemo
|
|
DataSource = connStrParams["Server"];
|
|
DataBase = connStrParams["Database"];
|
|
}
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Elenco completo EVENTI
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<AnagEventiModel>> AnagEventiGetAll()
|
|
{
|
|
// setup parametri costanti
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<AnagEventiModel>? result = new List<AnagEventiModel>();
|
|
// cerco in redis...
|
|
string currKey = $"{redisBaseKey}:AnagEventi:ALL";
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<AnagEventiModel>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbTabController.AnagEventiGetAll();
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<AnagEventiModel>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"AnagEventiGetAll | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco EVENTI validi x macchina
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<vSelEventiBCodeModel>> AnagEventiGetByMacch(string IdxMacch)
|
|
{
|
|
// setup parametri costanti
|
|
DateTime startDate = new DateTime(2000, 1, 1);
|
|
DateTime endDate = DateTime.Today.AddDays(1);
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<vSelEventiBCodeModel>? result = new List<vSelEventiBCodeModel>();
|
|
// cerco in redis...
|
|
string currKey = $"{redisBaseKey}:VSEB:{IdxMacch}";
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
//if (!string.IsNullOrEmpty($"{rawData}"))
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<vSelEventiBCodeModel>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbTabController.AnagEventiGetByMacc(IdxMacch);
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<vSelEventiBCodeModel>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"AnagEventiGetByMacch | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco completo STATI
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<AnagStatiModel>> AnaStatiGetAll()
|
|
{
|
|
// setup parametri costanti
|
|
DateTime startDate = new DateTime(2000, 1, 1);
|
|
DateTime endDate = DateTime.Today.AddDays(1);
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<AnagStatiModel>? result = new List<AnagStatiModel>();
|
|
// cerco in redis...
|
|
string currKey = $"{redisBaseKey}:AnagStati";
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
//if (!string.IsNullOrEmpty($"{rawData}"))
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<AnagStatiModel>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbTabController.AnagStatiGetAll();
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<AnagStatiModel>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"AnaStatiGetAll | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera elenco ultimi commenti
|
|
/// </summary>
|
|
/// <param name="idxMacchina">Idx macchina, "*" = tutte</param>
|
|
/// <param name="maxRec">Num massimo di record da recuperare</param>
|
|
/// <returns></returns>
|
|
public async Task<List<CommentiModel>> CommentiGetLastByMacc(string idxMacchina, int maxRec)
|
|
{
|
|
// setup parametri costanti
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<CommentiModel> result = new List<CommentiModel>();
|
|
// cerco in redis...
|
|
string currKey = $"{redisBaseKey}:Commenti:{idxMacchina}:{maxRec}";
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
//if (!string.IsNullOrEmpty($"{rawData}"))
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<CommentiModel>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbTabController.CommentiGetLastByMacc(idxMacchina, maxRec);
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<CommentiModel>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"CommentiGetLastByMacc | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua conferma prod macchina dell'intero periodo da confermare (ultima conferma
|
|
/// --> dtEvent)
|
|
/// </summary>
|
|
/// <param name="idxMacchina">idx macchina da confermare</param>
|
|
/// <param name="modoConfProd">0=periodo, 1 = giorno, 2 = turno</param>
|
|
/// <param name="numPzConfermati">qta pezzi BUONI da confermare</param>
|
|
/// <param name="numPzScarto">qta pezzi SCARTO da confermare</param>
|
|
/// <param name="DataOraApp">DataOra in cui registrare approvazione</param>
|
|
/// <param name="MatrOpr">Matricola operatore</param>
|
|
/// <returns></returns>
|
|
public bool ConfermaProdMacchina(string idxMacchina, int modoConfProd, int numPzConfermati, int numPzScarto, DateTime DataOraApp, int MatrOpr)
|
|
{
|
|
bool answ = false;
|
|
answ = dbTabController.ConfermaProdMacchina(idxMacchina, modoConfProd, numPzConfermati, numPzScarto, DataOraApp, MatrOpr);
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua conferma prod macchina dell'intero periodo da confermare (ultima conferma
|
|
/// --> dtEvent)
|
|
/// </summary>
|
|
/// <param name="idxMacchina">idx macchina da confermare</param>
|
|
/// <param name="modoConfProd">0=periodo, 1 = giorno, 2 = turno</param>
|
|
/// <param name="numPzConfermati">qta pezzi BUONI da confermare</param>
|
|
/// <param name="numPzLasciati">
|
|
/// qta pezzi LASCIATI alla macchina da confermare (2 eventi 121 rettifica neg/pos)
|
|
/// </param>
|
|
/// <param name="numPzScarto">qta pezzi SCARTO da confermare</param>
|
|
/// <param name="DataOraApp">DataOra in cui registrare approvazione</param>
|
|
/// <param name="MatrOpr">Matricola operatore</param>
|
|
/// <returns></returns>
|
|
public bool ConfermaProdMacchinaFull(string idxMacchina, int modoConfProd, int numPzConfermati, int numPzLasciati, int numPzScarto, DateTime DataOraApp, int MatrOpr)
|
|
{
|
|
bool answ = false;
|
|
answ = dbTabController.ConfermaProdMacchinaFull(idxMacchina, modoConfProd, numPzConfermati, numPzLasciati, numPzScarto, DataOraApp, MatrOpr);
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Config values attivi
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<ConfigModel>> ConfigGetAll()
|
|
{
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<ConfigModel>? result = new List<ConfigModel>();
|
|
// cerco in redis...
|
|
string currKey = $"{redisBaseKey}:Conf";
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
//if (!string.IsNullOrEmpty($"{rawData}"))
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<ConfigModel>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = await Task.FromResult(dbTabController.ConfigGetAll());
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<ConfigModel>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"ConfigGetAll | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Inserimento record in DDB
|
|
/// </summary>
|
|
/// <param name="idxMacchina"></param>
|
|
/// <param name="inizio"></param>
|
|
/// <param name="idxStatoStart"></param>
|
|
/// <param name="nStepEventi"></param>
|
|
/// <param name="nRecCheck"></param>
|
|
/// <param name="checkOnly"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> DDB_DoRecalc(string idxMacchina, DateTime inizio, int idxStatoStart, int nStepEventi, int nRecCheck, bool checkOnly)
|
|
{
|
|
bool answ = dbTabController.DDB_DoRecalc(idxMacchina, inizio, idxStatoStart, nStepEventi, nRecCheck, checkOnly);
|
|
await FlushCache();
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera record successivo da DDB
|
|
/// </summary>
|
|
/// <param name="idxMacchina"></param>
|
|
/// <param name="inizioStato"></param>
|
|
/// <returns></returns>
|
|
public DiarioDiBordoModel DDB_getNext(string idxMacchina, DateTime inizioStato)
|
|
{
|
|
return dbTabController.DDB_getNext(idxMacchina, inizioStato);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
// Clear database controller
|
|
dbTabController.Dispose();
|
|
dbIocController.Dispose();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elimina una riga in EventList se trovata
|
|
/// </summary>
|
|
/// <param name="idxMacchina"></param>
|
|
/// <param name="dtEvento"></param>
|
|
/// <param name="idxTipo"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> EvListDelete(string idxMacchina, DateTime dtEvento, int idxTipo)
|
|
{
|
|
bool fatto = false;
|
|
try
|
|
{
|
|
// inserisco evento
|
|
fatto = dbTabController.EvListDelete(idxMacchina, dtEvento, idxTipo);
|
|
// reset cache eventi/commenti
|
|
await FlushCache("EvList");
|
|
await FlushCache("Commenti");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string logMsg = $"Eccezione in EvListDelete | macchina: {idxMacchina} | DataEv: {dtEvento} | idxTipo: {idxTipo}{Environment.NewLine}{exc}";
|
|
Log.Error(logMsg);
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera record EventList date condizioni filtro
|
|
/// </summary>
|
|
/// <param name="idxMacchina">Idx macchina, "*" = tutte</param>
|
|
/// <param name="dtLimit">Data limite per recupero antecedenti</param>
|
|
/// <param name="idxTipo">Tipo evento cercato, 0 = tutti</param>
|
|
/// <param name="maxRec">Num massimo di record da recuperare</param>
|
|
/// <returns></returns>
|
|
public async Task<List<EventListModel>> EvListGetLastBySearch(string idxMacchina, DateTime dtLimit, int idxTipo, int maxRec)
|
|
{
|
|
// setup parametri costanti
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<EventListModel> result = new List<EventListModel>();
|
|
// cerco in redis...
|
|
string currKey = $"{redisBaseKey}:EvList:Last:{idxMacchina}:{idxTipo}:{dtLimit:yyyyMMdd-HHmm}:{maxRec}";
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
//if (!string.IsNullOrEmpty($"{rawData}"))
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<EventListModel>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = await dbTabController.EvListGetLastBySearch(idxMacchina, dtLimit, idxTipo, maxRec);
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<EventListModel>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"EvListGetLastBySearch | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Scrive una riga in EventList inviato nel db + check
|
|
/// </summary>
|
|
/// <param name="newRec">Record da registrare</param>
|
|
/// <returns></returns>
|
|
public async Task<inputComandoMapo> EvListInsert(EventListModel newRec, tipoInputEvento tipoEv)
|
|
{
|
|
bool inserito = false;
|
|
try
|
|
{
|
|
// inserisco evento
|
|
inserito = await dbTabController.EvListInsert(newRec);
|
|
// reset cache eventi/commenti
|
|
await FlushCache("EvList");
|
|
await FlushCache("Commenti");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string logMsg = $"Eccezione in fase di scrittura evento con i seguenti dati | macchina: {newRec.IdxMacchina} | IdxTipo: {newRec.IdxTipo} | CodArticolo: {newRec.CodArticolo} | Value {newRec.Value} | MatrOpr {newRec.MatrOpr} | Pallet {newRec.pallet}{Environment.NewLine}{exc}";
|
|
Log.Error(logMsg);
|
|
}
|
|
// se non è un commento...
|
|
if (tipoEv != tipoInputEvento.commento)
|
|
{
|
|
try
|
|
{
|
|
// faccio controllo per eventuale cambio stato da tab transizioni...
|
|
dbTabController.CheckCambiaStatoBatch(tipoEv, newRec.IdxMacchina, newRec.InizioStato ?? DateTime.Now, newRec.IdxTipo, newRec.CodArticolo, newRec.Value, newRec.MatrOpr, newRec.pallet);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
string logMsg = $"Eccezione in checkCambiaStatoBatch(6) | tipoInputEvento: {tipoEv} |macchina: {newRec.IdxMacchina} | IdxTipo: {newRec.IdxTipo} | CodArticolo: {newRec.CodArticolo} | Value {newRec.Value} | MatrOpr {newRec.MatrOpr} | Pallet {newRec.pallet}{Environment.NewLine}{exc}";
|
|
Log.Error(logMsg);
|
|
}
|
|
}
|
|
// formatto output
|
|
inputComandoMapo answ = new inputComandoMapo();
|
|
answ.outValue = inserito.ToString();
|
|
answ.needStatusRefresh = true;
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupera elenco fermi non qualificati da filtro
|
|
/// </summary>
|
|
/// <param name="idxMacchina">Idx macchina, "*" = tutte</param>
|
|
/// <param name="gg">Num massimo di giorni antecedenti</param>
|
|
/// <param name="durataMin">Durata minima (in minuti)</param>
|
|
/// <returns></returns>
|
|
public async Task<List<FermiNonQualModel>> FermiNonQualificatiFilt(string idxMacchina, int gg, double durataMin)
|
|
{
|
|
// setup parametri costanti
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<FermiNonQualModel> result = new List<FermiNonQualModel>();
|
|
// cerco in redis...
|
|
string currKey = $"{redisBaseKey}:FNQ:{idxMacchina}:{gg}:{durataMin:N0}";
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<FermiNonQualModel>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbTabController.FermiNonQualificatiFilt(idxMacchina, gg, durataMin);
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(currKey, rawData, FastCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<FermiNonQualModel>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"FermiNonQualificatiFilt | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Pulizia cache Redis (tutta)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<bool> FlushCache()
|
|
{
|
|
RedisValue pattern = new RedisValue($"{redisBaseKey}:*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Pulizia cache Redis per chiave specifica (da redisBaseKey...)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<bool> FlushCache(string KeyReq)
|
|
{
|
|
RedisValue pattern = new RedisValue($"{redisBaseKey}:{KeyReq}:*");
|
|
bool answ = await ExecFlushRedisPattern(pattern);
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupero info IOB x TAB (da info registrate IOB-WIN--> MP-IO)
|
|
/// </summary>
|
|
/// <param name="IdxMacchina"></param>
|
|
/// <returns></returns>
|
|
public async Task<IOB_data> IobInfo(string IdxMacchina)
|
|
{
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
IOB_data? result = new IOB_data();
|
|
// cerco in redis...
|
|
string currKey = redHash($"hM2IOB:{IdxMacchina}");
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
//if (!string.IsNullOrEmpty($"{rawData}"))
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<IOB_data>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new IOB_data();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"IobInfo | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua ricalcolo MSE x macchina indicata
|
|
/// </summary>
|
|
/// <param name="idxMacchina">idx macchina da confermare</param>
|
|
/// <param name="insEnabled">Abilitazione insert ev macchina</param>
|
|
/// <returns></returns>
|
|
public bool MacchinaSetInsEnab(string idxMacchina, bool insEnabled)
|
|
{
|
|
bool answ = dbTabController.MacchinaSetInsEnab(idxMacchina, insEnabled);
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Resetta (rileggendo) i dati della macchina
|
|
/// </summary>
|
|
/// <param name="idxMacchina"></param>
|
|
/// <returns></returns>
|
|
public Dictionary<string, string> resetDatiMacchina(string idxMacchina)
|
|
{
|
|
Dictionary<string, string> answ = new Dictionary<string, string>();
|
|
#if false
|
|
string currHash = dtMaccHash(idxMacchina);
|
|
// inizio con un bel reset...
|
|
memLayer.ML.redFlushKey(currHash);
|
|
DS_applicazione.MSFDDataTable tabMSFD = new DS_applicazione.MSFDDataTable();
|
|
// 2018.01.08 SEMPRE senza singleton: instanzio un nuovo oggetto MapoDb
|
|
MapoDb connDb = new MapoDb();
|
|
tabMSFD = connDb.taMSFD.getByIdxMacc(idxMacchina);
|
|
bool trovato = false;
|
|
// se ho righe...
|
|
DS_applicazione.MSFDDataTable tab = new DS_applicazione.MSFDDataTable();
|
|
DS_applicazione.MSFDRow rigaMSFD;
|
|
if (tabMSFD.Rows.Count > 0)
|
|
{
|
|
try
|
|
{
|
|
rigaMSFD = tabMSFD[0];
|
|
trovato = true;
|
|
}
|
|
catch
|
|
{
|
|
rigaMSFD = tab.NewMSFDRow();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
rigaMSFD = tab.NewMSFDRow();
|
|
}
|
|
if (trovato)
|
|
{
|
|
// ora provo a compilare...
|
|
try
|
|
{
|
|
// salvo 1:1 i valori... STATO
|
|
answ.Add("IdxMicroStato", rigaMSFD.IdxMicroStato.ToString());
|
|
answ.Add("IdxStato", rigaMSFD.IdxStato.ToString());
|
|
answ.Add("CodArticolo", rigaMSFD.CodArticolo);
|
|
answ.Add("insEnabled", rigaMSFD.insEnabled.ToString());
|
|
answ.Add("sLogEnabled", rigaMSFD.sLogEnabled.ToString());
|
|
answ.Add("pallet", rigaMSFD.pallet);
|
|
answ.Add("CodArticolo_A", rigaMSFD.CodArticolo_A);
|
|
answ.Add("CodArticolo_B", rigaMSFD.CodArticolo_B);
|
|
answ.Add("TempoCicloBase", rigaMSFD.TempoCicloBase.ToString());
|
|
answ.Add("PzPalletProd", rigaMSFD.PzPalletProd.ToString());
|
|
answ.Add("MatrOpr", rigaMSFD.MatrOpr.ToString());
|
|
answ.Add("lastVal", rigaMSFD.lastVal);
|
|
answ.Add("TCBase", rigaMSFD.TempoCicloBase.ToString());
|
|
|
|
//...e SETUP
|
|
answ.Add("CodMacc", rigaMSFD.codmacchina);
|
|
answ.Add("IdxFamIn", rigaMSFD.IdxFamigliaIngresso.ToString());
|
|
answ.Add("Multi", rigaMSFD.Multi.ToString());
|
|
answ.Add("BitFilt", rigaMSFD.BitFilt.ToString());
|
|
answ.Add("MaxVal", rigaMSFD.MaxVal.ToString());
|
|
answ.Add("BSR", rigaMSFD.BSR.ToString());
|
|
answ.Add("ExplodeBit", rigaMSFD.ExplodeBit.ToString());
|
|
answ.Add("NumBit", rigaMSFD.NumBit.ToString());
|
|
answ.Add("IdxFamMacc", rigaMSFD.IdxFamiglia.ToString());
|
|
answ.Add("simplePallet", rigaMSFD.simplePallet.ToString());
|
|
answ.Add("palletChange", rigaMSFD.palletChange.ToString());
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Info(string.Format("Errore in compilazione dati MSFD:{0}{1}", Environment.NewLine, exc), tipoLog.EXCEPTION);
|
|
}
|
|
// cerco dati master/slave...
|
|
string isMaster = connDb.taM2S.getByMaster(idxMacchina).Count > 0 ? "1" : "0";
|
|
string isSlave = connDb.taM2S.getBySlave(idxMacchina).Count > 0 ? "1" : "0";
|
|
answ.Add("Master", isMaster);
|
|
answ.Add("Slave", isSlave);
|
|
|
|
// verifico il timeout che cambia a seconda che sia vero o falso insEnabled...
|
|
int tOutShort = memLayer.ML.cdvi("TmOut.MS.S");
|
|
int tOutLong = memLayer.ML.cdvi("TmOut.MS.L");
|
|
int redDtMacTOut = tOutLong;
|
|
try
|
|
{
|
|
redDtMacTOut = (answ["insEnabled"].ToLower() == "true") ? tOutShort : tOutLong;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Info($"Eccezione in calcolo timeout dati macchina: idxMacchina{idxMacchina} | TShort: {tOutShort} | TLong {tOutLong}{Environment.NewLine}{exc}");
|
|
}
|
|
// salvo in redis!
|
|
memLayer.ML.redSaveHashDict(currHash, answ, redDtMacTOut);
|
|
}
|
|
else
|
|
{
|
|
Log.Info("Errore in chiamata stp_MSFD_getMacc (connDb.taMSFD.getByIdxMacc(idxMacchina))");
|
|
}
|
|
#endif
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua reset microstato macchina
|
|
/// </summary>
|
|
/// <param name="idxMacchina"></param>
|
|
public void resetMicrostatoMacchina(string idxMacchina)
|
|
{
|
|
// salvo microstato 0...
|
|
MicroStatoMacchinaModel newRecMS = new MicroStatoMacchinaModel()
|
|
{
|
|
IdxMacchina = idxMacchina,
|
|
InizioStato = DateTime.Now,
|
|
IdxMicroStato = 0,
|
|
Value = "FER"
|
|
};
|
|
var result = dbTabController.MicroStatoMacchinaUpsert(newRecMS);
|
|
// reset in redis
|
|
resetDatiMacchina(idxMacchina);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua ricalcolo MSE x macchina indicata
|
|
/// </summary>
|
|
/// <param name="idxMacchina">idx macchina da confermare</param>
|
|
/// <param name="maxAgeSec">Num massimo secondi di "vecchiaia" del dato</param>
|
|
/// <returns></returns>
|
|
public bool RicalcMse(string idxMacchina, int maxAgeSec)
|
|
{
|
|
bool answ = false;
|
|
answ = dbTabController.RicalcMse(idxMacchina, maxAgeSec);
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupero Righe (Actual) della scheda tecnica da GRUPPO + ODL
|
|
/// </summary>
|
|
/// <param name="codGruppo"></param>
|
|
/// <param name="idxODL"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<ST_ActRow>> STAR_byGrpOdl(string codGruppo, int idxODL)
|
|
{
|
|
// setup parametri costanti
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<ST_ActRow>? result = new List<ST_ActRow>();
|
|
// cerco in redis...
|
|
string currKey = $"{redisBaseKey}:START:{codGruppo}:{idxODL}";
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
//if (!string.IsNullOrEmpty($"{rawData}"))
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<ST_ActRow>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbTabController.STAR_byGrpOdl(codGruppo, idxODL);
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<ST_ActRow>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"STAR_byGrpOdl | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Recupero Righe (Actual) della scheda tecnica da GRUPPO + ODL
|
|
/// </summary>
|
|
/// <param name="codGruppo"></param>
|
|
/// <param name="label"></param>
|
|
/// <param name="idxODL"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<ST_ActRow>> STAR_byGrpOdlLbl(string codGruppo, string label, int idxODL)
|
|
{
|
|
// setup parametri costanti
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<ST_ActRow>? result = new List<ST_ActRow>();
|
|
// cerco in redis...
|
|
string currKey = $"{redisBaseKey}:START:{codGruppo}:{label}:{idxODL}";
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
//if (!string.IsNullOrEmpty($"{rawData}"))
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<ST_ActRow>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbTabController.STAR_byGrpOdlLbl(codGruppo, label, idxODL);
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<ST_ActRow>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"STAR_byGrpOdlLbl | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Stato macchina
|
|
/// </summary>
|
|
/// <param name="idxMacchina"></param>
|
|
/// <returns></returns>
|
|
public async Task<StatoMacchineModel> StatoMacchina(string idxMacchina)
|
|
{
|
|
// setup parametri costanti
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
StatoMacchineModel? result = new StatoMacchineModel();
|
|
// cerco in redis...
|
|
string currKey = $"{redisBaseKey}:StatoMacc:{idxMacchina}";
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
//if (!string.IsNullOrEmpty($"{rawData}"))
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<StatoMacchineModel>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbTabController.StatoMacchina(idxMacchina);
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(currKey, rawData, TimeSpan.FromSeconds(2));
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new StatoMacchineModel();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"StatoMacchina | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Stato prod macchina (completo)
|
|
/// </summary>
|
|
/// <param name="idxMacchina"></param>
|
|
/// <param name="dtReq"></param>
|
|
/// <returns></returns>
|
|
public async Task<StatoProdModel> StatoProdMacchina(string idxMacchina, DateTime dtReq)
|
|
{
|
|
// setup parametri costanti
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
StatoProdModel? result = new StatoProdModel();
|
|
// cerco in redis...
|
|
string currKey = $"{redisBaseKey}:StatoProd:{idxMacchina}:{dtReq:HHmmss}";
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
//if (!string.IsNullOrEmpty($"{rawData}"))
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<StatoProdModel>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbTabController.StatoProdMacchina(idxMacchina, dtReq);
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(currKey, rawData, UltraFastCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new StatoProdModel();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"StatoProdMacchina | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Turno macchina
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<TurniMaccModel> TurnoMacchinaGet(string idxMacchina)
|
|
{
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
TurniMaccModel? result = new TurniMaccModel();
|
|
// cerco in redis...
|
|
string currKey = $"{redisBaseKey}:TurniMacc:{idxMacchina}";
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
//if (!string.IsNullOrEmpty($"{rawData}"))
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<TurniMaccModel>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = await Task.FromResult(dbTabController.TurnoMacchinaGet(idxMacchina));
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new TurniMaccModel();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"TurnoMacchinaGet | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco turni macchina (all)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<TurniMaccModel>> TurnoMacchinaGetAll()
|
|
{
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<TurniMaccModel>? result = new List<TurniMaccModel>();
|
|
// cerco in redis...
|
|
string currKey = $"{redisBaseKey}:TurniMacc:All";
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<TurniMaccModel>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = await Task.FromResult(dbTabController.TurnoMacchinaGetAll());
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(currKey, rawData, LongCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<TurniMaccModel>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"TurnoMacchinaGetAll | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Toggle stato turno
|
|
/// </summary>
|
|
/// <param name="idxMacchina"></param>
|
|
/// <param name="numTurno"></param>
|
|
/// <returns></returns>
|
|
public bool TurnoMacchinaToggle(string idxMacchina, int numTurno)
|
|
{
|
|
bool answ=dbTabController.TurnoMacchinaToggle(idxMacchina, numTurno);
|
|
FlushCache("TurniMacc").ConfigureAwait(false);
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Intera vista v_MSFD
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<VMSFDModel>> VMSFDGetAll()
|
|
{
|
|
// setup parametri costanti
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<VMSFDModel> result = new List<VMSFDModel>();
|
|
// cerco in redis...
|
|
string currKey = $"{redisBaseKey}:MSFD:ALL";
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<VMSFDModel>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbIocController.VMSFDGetAll();
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<VMSFDModel>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"VMSFDGetAll | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Vista v_MSFD x singola macchina (da stored) - singolo record
|
|
/// </summary>
|
|
/// <param name="idxMacc"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<VMSFDModel>> VMSFDGetByMacc(string idxMacc)
|
|
{
|
|
// setup parametri costanti
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<VMSFDModel>? result = new List<VMSFDModel>();
|
|
// cerco in redis...
|
|
string currKey = $"{redisBaseKey}:MSFD:MACCH:{idxMacc}";
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
//if (!string.IsNullOrEmpty($"{rawData}"))
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<VMSFDModel>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbIocController.VMSFDGetByMacc(idxMacc);
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(currKey, rawData, UltraFastCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<VMSFDModel>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"VMSFDGetByMacc | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Vista v_MSFD delle machine MULTI filtrato x macchina (da stored)
|
|
/// </summary>
|
|
/// <param name="idxMacc"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<VMSFDModel>> VMSFDGetMultiByMacc(string idxMacc)
|
|
{
|
|
// setup parametri costanti
|
|
string source = "DB";
|
|
Stopwatch sw = new Stopwatch();
|
|
sw.Start();
|
|
List<VMSFDModel>? result = new List<VMSFDModel>();
|
|
// cerco in redis...
|
|
string currKey = $"{redisBaseKey}:MSFD:MULTI:{idxMacc}";
|
|
RedisValue rawData = await redisDb.StringGetAsync(currKey);
|
|
//if (!string.IsNullOrEmpty($"{rawData}"))
|
|
if (rawData.HasValue)
|
|
{
|
|
result = JsonConvert.DeserializeObject<List<VMSFDModel>>($"{rawData}");
|
|
source = "REDIS";
|
|
}
|
|
else
|
|
{
|
|
result = dbIocController.VMSFDGetByMacc(idxMacc);
|
|
// serializzp e salvo...
|
|
rawData = JsonConvert.SerializeObject(result);
|
|
await redisDb.StringSetAsync(currKey, rawData, UltraLongCache);
|
|
}
|
|
if (result == null)
|
|
{
|
|
result = new List<VMSFDModel>();
|
|
}
|
|
sw.Stop();
|
|
Log.Debug($"VMSFDGetMultiByMacc | {source} | {sw.Elapsed.TotalMilliseconds}ms");
|
|
return result;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Protected Fields
|
|
|
|
/// <summary>
|
|
/// Oggetto per connessione a REDIS
|
|
/// </summary>
|
|
protected ConnectionMultiplexer redisConn = null!;
|
|
|
|
/// <summary>
|
|
/// Oggetto DB redis da impiegare x chiamate R/W
|
|
/// </summary>
|
|
protected IDatabase redisDb = null!;
|
|
|
|
#endregion Protected Fields
|
|
|
|
#region Private Fields
|
|
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
private string CodModulo = "";
|
|
|
|
private string ConnStr = "";
|
|
|
|
private Dictionary<string, string> connStrParams = new Dictionary<string, string>();
|
|
|
|
private string DataBase = "";
|
|
|
|
private string DataSource = "";
|
|
|
|
private string redisBaseKey = "MP:TAB:Cache";
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Properties
|
|
|
|
private static Controllers.MpIocController dbIocController { get; set; } = null!;
|
|
|
|
private static Controllers.MpTabController dbTabController { get; set; } = null!;
|
|
|
|
#endregion Private Properties
|
|
|
|
#region Private Methods
|
|
|
|
/// <summary>
|
|
/// Esegue flush memoria redis dato pattern
|
|
/// </summary>
|
|
/// <param name="pattern"></param>
|
|
/// <returns></returns>
|
|
private async Task<bool> ExecFlushRedisPattern(RedisValue pattern)
|
|
{
|
|
bool answ = false;
|
|
var listEndpoints = redisConn.GetEndPoints();
|
|
foreach (var endPoint in listEndpoints)
|
|
{
|
|
//var server = redisConnAdmin.GetServer(listEndpoints[0]);
|
|
var server = redisConn.GetServer(endPoint);
|
|
if (server != null)
|
|
{
|
|
var keyList = server.Keys(redisDb.Database, pattern);
|
|
foreach (var item in keyList)
|
|
{
|
|
await redisDb.KeyDeleteAsync(item);
|
|
}
|
|
answ = true;
|
|
}
|
|
}
|
|
|
|
return answ;
|
|
}
|
|
|
|
private string redHash(string keyName)
|
|
{
|
|
string result = keyName;
|
|
try
|
|
{
|
|
result = $"{CodModulo}:{DataSource}:{DataBase}:{keyName}".Replace("\\", "_");
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Error($"Errore in redHash{Environment.NewLine}{exc}");
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |