Files
mapo-core/MP.IOC/Controllers/BenchController.cs
T
Samuele Locatelli 2fffbf5bb8 Modifiche classi dati x IOC e base
- split controllers IOC / SPEC
- riog conf stringhe const
2023-02-15 19:37:29 +01:00

378 lines
12 KiB
C#

using Microsoft.AspNetCore.Mvc;
using MP.Data;
using MP.IOC.Data;
using NLog;
using System.Diagnostics;
namespace MP.IOC.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class BenchController : ControllerBase
{
#region Public Constructors
public BenchController(IConfiguration configuration, MpDataService DataService)
{
Log.Info("Starting BenchController");
_configuration = configuration;
DService = DataService;
Log.Info("Avviata BenchController");
}
#endregion Public Constructors
#region Public Methods
/// <summary>
/// Conteggio chiavi REDIS dato pattern api/Bench/CountKeys
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("CountKeys")]
public string CountKeys(string? id)
{
string answ = "ND";
if (id == null) id = "";
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// conto quanti oggetti ho in memoria REDIS...
string groupHash = ""; // DataLayer.mHash("");
if (id.Length > 0)
{
groupHash += id + ":";
}
groupHash += "*";
answ = $"Trovate {DService.RedisCountKey(groupHash)} Hash per {groupHash}";
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
// accodo tempo!
answ += $"{Environment.NewLine}Elapsed: {ts.TotalMilliseconds}ms";
// ritorno
return answ;
}
/// <summary>
/// Bench recupero dati macchina api/Bench/DtMac?id=SIMUL_01
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("DatiMacc")]
public string DatiMacc(string id)
{
string answ = "ND";
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// se id nullo --> KO!
if (id == null)
{
answ = "KO";
}
else
{
answ = "";
try
{
Dictionary<string, string> valori = DService.ResetDatiMacchina(id);
foreach (var item in valori)
{
answ += $"{item.Key}|{item.Value}{Environment.NewLine}";
}
}
catch
{ }
}
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
// accodo tempo!
answ += $"{Environment.NewLine}Elapsed: {ts.TotalMilliseconds}ms";
// ritorno
return answ;
}
/// <summary> Recupera riga traduzione microstato x processing da HASH table
/// api/Bench/FindSMI?id=60&idxMS=3&valore=1 </summary> <param name="id"></param> <param
/// name="idxMS"></param> <param name="valore"></param> <returns></returns>
[HttpGet("FindSMI")]
public string FindSMI(int? id, int? idxMS, int? valore)
{
string answ = "ND";
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// se id nullo --> KO!
if (id == null)
{
answ = "KO";
}
else
{
// recupero dati x sapere quale famiglia SMI è necessaria e quale stato / segnale si
// sia ricevuto
int idxFamIn = Convert.ToInt32(id);
int idxMicroStato = Convert.ToInt32(idxMS);
int valIOB = Convert.ToInt32(valore);
// recupero microstato macchina da chiave relativa
answ = "";
try
{
var fiHASH = Utils.hSMI(idxFamIn);
string outVal = "";
bool trovato = DService.RedisHashPresent(fiHASH);
if (!trovato)
{
// ricarico tabella!
KeyValuePair<string, string>[] valori = DService.StateMachInByKey(idxFamIn);
answ = string.Format("Ricaricata SMI per famiglia {0}<br/>", fiHASH);
}
// recupero singolo valore (stringa) x chiave
outVal = DService.ValoreSMI(idxFamIn, idxMicroStato, valIOB);
// mostro output
answ += $"idxFamIN: {idxFamIn} | idxMS: {idxMicroStato} | valIOB: {valIOB} | out: {outVal}";
}
catch
{ }
}
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
// accodo tempo!
answ += $"{Environment.NewLine}Total Time Elapsed: {ts.TotalMilliseconds}ms";
// ritorno
return answ;
}
/// <summary>
/// Test verifica insert enabled x macchina
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("InsEnab")]
public string InsEnab(string id)
{
string answ = "ND";
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// se id nullo --> KO!
if (id == null)
{
answ = "KO";
}
else
{
// recupero microstato macchina da chiave relativa
answ = "";
try
{
answ += $"Macchina {id}, insEnabled {DService.IobInsEnab(id)}{Environment.NewLine}";
}
catch
{ }
}
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
// accodo tempo!
answ += $"{Environment.NewLine}Total Time Elapsed: {ts.TotalMilliseconds}ms";
// ritorno
return answ;
}
/// <summary>
/// pulizia dati api/Bench/RClean?id=100
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("RClean")]
public string RClean(int? id)
{
string answ = "ND";
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// se id nullo --> KO!
if (id == null)
{
answ = "KO";
}
else
{
string chiave;
KeyValuePair<string, string>[] valori = new KeyValuePair<string, string>[2];
try
{
// svuoto i precedenti hash... CICLO!
for (int i = 0; i < id; i++)
{
chiave = string.Format("test:{0}", i);
DService.RedisDelKey(chiave);
}
answ = "OK";
}
catch
{ }
}
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
// accodo tempo!
answ += $"{Environment.NewLine}Elapsed: {ts.TotalMilliseconds}ms";
// ritorno
return answ;
}
//// GET: IOB (è un check alive)
//public string Index()
//{
// return "OK";
//}
/// <summary>
/// Setup dati di test hash api/Bench/RSetup?id=100
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("RSetup")]
public string RSetup(int? id)
{
string answ = "ND";
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// se id nullo --> KO!
if (id == null)
{
answ = "KO";
}
else
{
string chiave;
KeyValuePair<string, string>[] valori = new KeyValuePair<string, string>[2];
try
{
// salvo il datasetet come insieme di hash in redis...
for (int i = 0; i < id; i++)
{
chiave = string.Format("test:{0}", i);
valori[0] = new KeyValuePair<string, string>("numero", i.ToString());
valori[1] = new KeyValuePair<string, string>("doppio", (i * 2).ToString());
DService.RedisSetHash(chiave, valori);
}
answ = "OK";
}
catch
{ }
}
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
// accodo tempo!
answ += $"{Environment.NewLine}Elapsed: {ts.TotalMilliseconds}ms";
// ritorno
return answ;
}
/// <summary>
/// Recupero singolo valore hash api/Bench/RSingleHash?id=50
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("RSingleHash")]
public string RSingleHash(int? id)
{
string answ = "ND";
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// se id nullo --> KO!
if (id == null)
{
answ = "KO";
}
else
{
answ = "";
string chiave;
KeyValuePair<string, string>[] valori = new KeyValuePair<string, string>[2];
try
{
// ora restituisco record completo dell'hash con ID indicato
chiave = string.Format("test:{0}", id);
valori = DService.RedisGetHash(chiave);
foreach (var item in valori)
{
answ += string.Format("{0}|{1}<br/>", item.Key, item.Value);
}
}
catch
{ }
}
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
// accodo tempo!
answ += $"{Environment.NewLine}Elapsed: {ts.TotalMilliseconds}ms";
// ritorno
return answ;
}
/// <summary>
/// Lettura tab StateMachineIngressi
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("StateMachineIn")]
public string StateMachineIn(int? id)
{
string answ = "ND";
long splitTime = 0;
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
// se id nullo --> KO!
if (id == null)
{
answ = "KO";
}
else
{
int idxFamIn = Convert.ToInt32(id);
answ = "";
try
{
KeyValuePair<string, string>[] valori = DService.StateMachInByKey(idxFamIn);
splitTime = stopWatch.ElapsedMilliseconds;
foreach (var item in valori)
{
answ += $"{item.Key}|{item.Value}{Environment.NewLine}";
}
}
catch
{ }
}
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
// accodo tempo!
answ += $"{Environment.NewLine}ReadTime: {splitTime}ms<br>Total Time Elapsed: {ts.TotalMilliseconds}ms";
// ritorno
return answ;
}
#endregion Public Methods
#region Protected Properties
/// <summary>
/// Dataservice x accesso DB
/// </summary>
protected MpDataService DService { get; set; }
#endregion Protected Properties
#region Private Fields
private static IConfiguration _configuration = null!;
private static Logger Log = LogManager.GetCurrentClassLogger();
#endregion Private Fields
}
}